dewolf
dewolf copied to clipboard
[Restructuring] Transform Guarded do-while loops
Proposal
Depending on the optimization level, easy while loops are sometimes transformed into an if-condition that has only a true branch consisting of a do-while loop with the same condition, i.e., while(cond){...}
is transformed into if(cond){do{...}while(cond)}
.
An example of this problem is function main
in a.zip.
The decompiled code is
int main(int arg1, char ** arg2, char ** arg3) {
int var_0;
int var_1;
unsigned long var_2;
int * var_3;
var_3 = &var_0;
__isoc99_scanf(/* format */ "%d", var_3);
var_3 = &var_1;
__isoc99_scanf(/* format */ "%d", var_3);
printf(/* format */ "b = %d\n", (unsigned int)var_1);
var_2 = (unsigned int)var_0;
if ((unsigned int)var_0 < var_1) { // <---------------- guarded condition
do {
printf(/* format */ "a = %d\n", (unsigned int)var_0, var_2);
var_0++;
var_2 = (unsigned int)var_0;
}
while ((unsigned int)var_0 < var_1); // <---------------- same condition as guarded if-condition
}
puts(/* str */ "done");
return 0;
}
Approach
The task could be done by the loop structure. It has to be done before the for-loop restructuring such that we can transform these loops to for-loops if we needed.