• Reverse all the printf and scanf statements in a basic block. If there are k such statements in a basic block, the first statement will be switched with the last statement, the second statement will be switched with the (k-1)th statement, and so on. Test your code with all possible cases.
• If the statement is a conditional branch in the form of `if cond goto L1; else goto L2`, interchange the two branches to `if cond goto L2; else goto L1`. Alternatively, you can change it to `if (not cond) goto L1; else goto L2`.
• If the statement is a self increment/decrement by 1, change it to opposite. For example, change `i=i+1` to `i=i-1` and vice versa.

When you are working on this transformation, use the following two tests and results to check your code. The counting instructions should be inserted after the control flow changes.

Program 1: ab.c

```int main()
{
int a, b;
printf("Input two integers for comparison\n");
scanf("%d", &a);
scanf("%d", &b);
printf("a is %d\n", a);
printf("b is %d\n", b);
if (a > b)
printf("a is greater than b\n");
else
printf("a is no greater than b\n");
}
```

Gimple tree before cs255 pass

```main ()
{
int b;
int a;

<bb 2>:
__builtin_puts (&"Input two integers for comparison"[0]);
scanf (&"%d"[0], &a);
scanf (&"%d"[0], &b);
printf (&"a is %d\n"[0], a);
printf (&"b is %d\n"[0], b);
if (a > b) goto <L0>; else goto <L1>;

<L0>:;
__builtin_puts (&"a is greater than b"[0]);
goto <bb 5> (<L2>);

<L1>:;
__builtin_puts (&"a is no greater than b"[0]);

<L2>:;
return;

}
```

Gimple tree after cs255 pass

```main ()
{
int b;
int a;

<bb 2>:
__builtin_puts (&"Input two integers for comparison"[0]);
printf (&"b is %d\n"[0], b);
printf (&"a is %d\n"[0], a);
scanf (&"%d"[0], &b);
scanf (&"%d"[0], &a);
if (a <= b) goto <L1>; else goto <L0>;

<L0>:;
__builtin_puts (&"a is greater than b"[0]);
goto <bb 5> (<L2>);

<L1>:;
__builtin_puts (&"a is no greater than b"[0]);

<L2>:;
return;

}
```

Program 2: loop.c

```int main()
{
int i, s, n;
printf("Input an integer for sum\n");
scanf("%d", &n);
for (i=0, s=0; i<=n; i++)
s += i;
printf("The sum from 1 to %d is %d\n", n, s);
}
```

Gimple tree before cs255 pass

```main ()
{
int n;
int s;
int i;
int n.0;

<bb 2>:
__builtin_puts (&"Input an integer for sum"[0]);
scanf (&"%d"[0], &n);
n.0 = n;
if (n.0 >= 0) goto <L13>; else goto <L14>;

<L14>:;
s = 0;
goto <bb 4> (<L2>);

<L13>:;
i = 0;
s = 0;

<L0>:;
s = s + i;
i = i + 1;
if (i != (int) ((unsigned int) n.0 + 1)) goto <L0>; else goto <L2>;

<L2>:;
printf (&"The sum from 1 to %d is %d\n"[0], n.0, s);
return;

}
```

Gimple tree after cs255 pass

```main ()
{
int n;
int s;
int i;
int n.0;

<bb 2>:
__builtin_puts (&"Input an integer for sum"[0]);
scanf (&"%d"[0], &n);
n.0 = n;
if (n.0 < 0) goto <L14>; else goto <L13>;

<L14>:;
s = 0;
goto <bb 4> (<L2>);

<L13>:;
i = 0;
s = 0;

<L0>:;
s = s + i;
i = i - 1;
if (i == (int) ((unsigned int) n.0 + 1)) goto <L2>; else goto <L0>;

<L2>:;
printf (&"The sum from 1 to %d is %d\n"[0], n.0, s);
return;

}
```

-- XiaomingGu - 02 Feb 2008

Edit | Attach | Watch | Print version |  | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r5 - 2008-02-06 - ChenDing

 Home Main Web P P P P View Edit Account
 Edit Attach
Copyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding URCS? Send feedback