Skip to main content

Checkpoint: What the Compiler Emits

Challenge on lesson: Checkpoint: What the Compiler Emits

CHALLENGE
Difficulty: advanced+25 XP

Model what the optimizer emits โ€” as executable predicates, faithful to the transformations the module verified with -S output. 1. int emitted_ops_sum(int n) โ€” the compiler emits the closed form n*(n+1)/2 for the loop sum 1..n, not a multiply chain: return that value (use long long internally; n >= 0). 2. int strength_steps(int n) โ€” count the machine steps to compute n*13 by strength reduction on a positive int: while n_left > 1, halve it (integer divide by 2) and count one step; then add one for the final shift-add. Return 0 for n <= 1. 3. int would_elide(int pure, int calls, int observed) โ€” a pure computation whose result is never used may be removed entirely: return calls * pure when observed is nonzero, else 0.

Back to lesson: Checkpoint: What the Compiler Emits