Solution
Correct option: 4)
Goal: Reconstruct a high-level program from the given three-address code (TAC).
100: t1 = y + 2
101: initial = x / t1
102: limit = 10
103: if i > j goto 105
104: goto 111
105: if num > b goto 107
106: goto 111
107: num = limit - 1
108: b = y + 2
109: i = i - num
110: goto 103
111: j = num
112: k = j + b
1) Identify blocks and the loop
- Lines
100–102: straight-line initializations.
110: goto 103 makes a back edge to 103 ⇒ there is a loop whose header is 103.
- From
103:
- If
i > j ⇒ go to 105 (stay in the loop).
- Else (via
104: goto 111) ⇒ exit loop.
- At
105:
- If
num > b ⇒ go to 107 (execute the body and loop back).
- Else (via
106: goto 111) ⇒ exit loop.
- Hence the loop continues only if both conditions are true: (i > j) AND (num > b).
2) Loop body
- Executed when both tests succeed:
107: num = limit - 1
108: b = y + 2
109: i = i - num
110: goto 103 (repeat)
- Note:
y is never changed, so b = y + 2 could reuse t1, but that is an optimization detail.
3) After the loop
- When either test fails, control goes to
111:
111: j = num
112: k = j + b
4) High-level code reconstructed from the TAC
t1 = y + 2;
initial= x / t1; // or: initial = x / (y + 2);
limit = 10;
while (i > j && num > b) {
num = limit - 1;
b = y + 2; // can reuse t1
i = i - num;
}
j = num;
k = j + b;
5) Match with the options
- Option 1: uses an if with
|| (OR) — wrong control flow (no loop, wrong condition).
- Option 2: has a loop but uses
|| (OR) — the TAC requires && (AND).
- Option 3: splits the tests into an if/else with an inner while — not equivalent to the TAC.
- Option 4: the only one that captures the correct while (i > j && num > b) structure and the correct loop body; therefore it is the correct high-level code among the choices.
Answer: Option 4.