---
# System prepended metadata

title: CS152 Lab 1

---

# CS152 Lab 1

## Open-ended Portion - Mix Manufacturing

### Note

Due to some misunderstanding on the calculation of branch/non-branch inst ratio, we initially aimed at maximizing br/non-br ratio in executed code. 

### Attempt 1
During our experiment on compiler codegen, we discovered that `goto` would be a convenient choice for redirecting program execution flow. (At this point, we were still maximizing the *runtime* branch/nonbranch ratio, so the longer the execution flow the better.)
Our first attempt was this:

```c
int a;
int main () {
    tag:
    if (a == 1) {
        goto tag;
    } else if (a == 2) {
        goto tag;
    } else if (a == 3) {
        goto tag;
    } else if (a == 4) {
        goto tag;
    } else if (a == 5) {
        goto tag;
    } else if (a == 6) {
        goto tag;
    }
    return 0;
}
```

```assembly
Disassembly of section .text:

80001048 <main>:
80001048:	ff010113          	addi	sp,sp,-16
8000104c:	00812623          	sw	s0,12(sp)
80001050:	01010413          	addi	s0,sp,16
80001054:	00000797          	auipc	a5,0x0
80001058:	0a478793          	addi	a5,a5,164 # 800010f8 <a>
8000105c:	0007a703          	lw	a4,0(a5)
80001060:	00100793          	li	a5,1
80001064:	00f71463          	bne	a4,a5,8000106c <main+0x24>
80001068:	fedff06f          	j	80001054 <main+0xc>
8000106c:	00000797          	auipc	a5,0x0
80001070:	08c78793          	addi	a5,a5,140 # 800010f8 <a>
80001074:	0007a703          	lw	a4,0(a5)
80001078:	00200793          	li	a5,2
8000107c:	00f71463          	bne	a4,a5,80001084 <main+0x3c>
80001080:	fd5ff06f          	j	80001054 <main+0xc>
80001084:	00000797          	auipc	a5,0x0
80001088:	07478793          	addi	a5,a5,116 # 800010f8 <a>
8000108c:	0007a703          	lw	a4,0(a5)
80001090:	00300793          	li	a5,3
80001094:	00f71463          	bne	a4,a5,8000109c <main+0x54>
80001098:	fbdff06f          	j	80001054 <main+0xc>
8000109c:	00000797          	auipc	a5,0x0
800010a0:	05c78793          	addi	a5,a5,92 # 800010f8 <a>
800010a4:	0007a703          	lw	a4,0(a5)
800010a8:	00400793          	li	a5,4
800010ac:	00f71463          	bne	a4,a5,800010b4 <main+0x6c>
800010b0:	fa5ff06f          	j	80001054 <main+0xc>
800010b4:	00000797          	auipc	a5,0x0
800010b8:	04478793          	addi	a5,a5,68 # 800010f8 <a>
800010bc:	0007a703          	lw	a4,0(a5)
800010c0:	00500793          	li	a5,5
800010c4:	00f71463          	bne	a4,a5,800010cc <main+0x84>
800010c8:	f8dff06f          	j	80001054 <main+0xc>
800010cc:	00000797          	auipc	a5,0x0
800010d0:	02c78793          	addi	a5,a5,44 # 800010f8 <a>
800010d4:	0007a703          	lw	a4,0(a5)
800010d8:	00600793          	li	a5,6
800010dc:	00f71463          	bne	a4,a5,800010e4 <main+0x9c>
800010e0:	f75ff06f          	j	80001054 <main+0xc>
800010e4:	00000793          	li	a5,0
800010e8:	00078513          	mv	a0,a5
800010ec:	00c12403          	lw	s0,12(sp)
800010f0:	01010113          	addi	sp,sp,16
800010f4:	00008067          	ret
```
As demonstrated above, every `if` statement generates one branch instruction, which is as expected. Most other instructions were loading immediates, saving register to be used, maintaining stack, etc. Obviously we can do better. 

**Ratio of branch to non-branch instructions, static: 6/44 = 13.64% (O0)**
**Ratio of branch to non-branch instructions, runtime: 5% (O0)**
**CPI: 1.328 (O0)**


### Attempt 2
Instead of redirecting execution using `goto`, we decided to try out recursive strategy in this round for comparison. 
At this stage we were aiming at increasing the branch density of the original C code, so our next step is to find something more efficient than `if`. `switch` came into mind (as we can fit `case` and its body on the same line). Meanwhile we also tried using ternary operators (equivalent to 1 or 2 branch(es)) to increase numbers of branch instructions. As a sidenote, we discovered that under some certain circumstances, using optimization would compress arithmetic instructions without influencing jump/branching instructions as much, so we opted in compiler optimization. (We used a gcc attribute to turn on "-O3" for this function only, as follows:)

```c
volatile int test = 0;
__attribute__((optimize("O3"))) int main(void) {
    switch (test == 0 ? 0 : test) {
        case 0: test = 0x1; main() == 0 ? 0 : main();
        case 1: test == 0 ? test = 0 : (test = 0x2); main();
        case 2: test == 0 ? test = 0 : (test = 0x3); main();
        case 3: test == 0 ? test = 0 : (test = 0x4); main();
        if ((test == 0) ? 0 : (test == 4)) {
            test == 0 ? test = 0 : (test = 0x5); main();
        } else if ((test == 0) ? 0 : (test == 5)) {
            test == 0 ? test = 0 : (test = 0x6); main();
        } else {
            main() == 0 ? 0 : main();
        }
    }
    return 0;
}
```

```assembly
Disassembly of section .text.startup:

80001048 <main>:
80001048:	ff010113          	addi	sp,sp,-16
8000104c:	00812423          	sw	s0,8(sp)
80001050:	00000417          	auipc	s0,0x0
80001054:	14840413          	addi	s0,s0,328 # 80001198 <test>
80001058:	00042783          	lw	a5,0(s0)
8000105c:	00112623          	sw	ra,12(sp)
80001060:	08078063          	beqz	a5,800010e0 <main+0x98>
80001064:	00042783          	lw	a5,0(s0)
80001068:	00100713          	li	a4,1
8000106c:	08e78463          	beq	a5,a4,800010f4 <main+0xac>
80001070:	06f75663          	ble	a5,a4,800010dc <main+0x94>
80001074:	00200713          	li	a4,2
80001078:	08e78863          	beq	a5,a4,80001108 <main+0xc0>
8000107c:	00300713          	li	a4,3
80001080:	04e79463          	bne	a5,a4,800010c8 <main+0x80>
80001084:	00042783          	lw	a5,0(s0)
80001088:	08079c63          	bnez	a5,80001120 <main+0xd8>
8000108c:	00000797          	auipc	a5,0x0
80001090:	1007a623          	sw	zero,268(a5) # 80001198 <test>
80001094:	fb5ff0ef          	jal	ra,80001048 <main>
80001098:	00042783          	lw	a5,0(s0)
8000109c:	00078863          	beqz	a5,800010ac <main+0x64>
800010a0:	00042703          	lw	a4,0(s0)
800010a4:	00400793          	li	a5,4
800010a8:	08f70463          	beq	a4,a5,80001130 <main+0xe8>
800010ac:	00042783          	lw	a5,0(s0)
800010b0:	00078863          	beqz	a5,800010c0 <main+0x78>
800010b4:	00042703          	lw	a4,0(s0)
800010b8:	00500793          	li	a5,5
800010bc:	0af70863          	beq	a4,a5,8000116c <main+0x124>
800010c0:	f89ff0ef          	jal	ra,80001048 <main>
800010c4:	08051063          	bnez	a0,80001144 <main+0xfc>
800010c8:	00c12083          	lw	ra,12(sp)
800010cc:	00812403          	lw	s0,8(sp)
800010d0:	00000513          	li	a0,0
800010d4:	01010113          	addi	sp,sp,16
800010d8:	00008067          	ret
800010dc:	fe0796e3          	bnez	a5,800010c8 <main+0x80>
800010e0:	00100793          	li	a5,1
800010e4:	00000717          	auipc	a4,0x0
800010e8:	0af72a23          	sw	a5,180(a4) # 80001198 <test>
800010ec:	f5dff0ef          	jal	ra,80001048 <main>
800010f0:	0a051063          	bnez	a0,80001190 <main+0x148>
800010f4:	00042783          	lw	a5,0(s0)
800010f8:	06079263          	bnez	a5,8000115c <main+0x114>
800010fc:	00000797          	auipc	a5,0x0
80001100:	0807ae23          	sw	zero,156(a5) # 80001198 <test>
80001104:	f45ff0ef          	jal	ra,80001048 <main>
80001108:	00042783          	lw	a5,0(s0)
8000110c:	04079063          	bnez	a5,8000114c <main+0x104>
80001110:	00000797          	auipc	a5,0x0
80001114:	0807a423          	sw	zero,136(a5) # 80001198 <test>
80001118:	f31ff0ef          	jal	ra,80001048 <main>
8000111c:	f69ff06f          	j	80001084 <main+0x3c>
80001120:	00400793          	li	a5,4
80001124:	00000717          	auipc	a4,0x0
80001128:	06f72a23          	sw	a5,116(a4) # 80001198 <test>
8000112c:	f69ff06f          	j	80001094 <main+0x4c>
80001130:	00042783          	lw	a5,0(s0)
80001134:	04078063          	beqz	a5,80001174 <main+0x12c>
80001138:	00500793          	li	a5,5
8000113c:	00000717          	auipc	a4,0x0
80001140:	04f72e23          	sw	a5,92(a4) # 80001198 <test>
80001144:	f05ff0ef          	jal	ra,80001048 <main>
80001148:	f81ff06f          	j	800010c8 <main+0x80>
8000114c:	00300793          	li	a5,3
80001150:	00000717          	auipc	a4,0x0
80001154:	04f72423          	sw	a5,72(a4) # 80001198 <test>
80001158:	fc1ff06f          	j	80001118 <main+0xd0>
8000115c:	00200793          	li	a5,2
80001160:	00000717          	auipc	a4,0x0
80001164:	02f72c23          	sw	a5,56(a4) # 80001198 <test>
80001168:	f9dff06f          	j	80001104 <main+0xbc>
8000116c:	00042783          	lw	a5,0(s0)
80001170:	00079863          	bnez	a5,80001180 <main+0x138>
80001174:	00000797          	auipc	a5,0x0
80001178:	0207a223          	sw	zero,36(a5) # 80001198 <test>
8000117c:	fc9ff06f          	j	80001144 <main+0xfc>
80001180:	00600793          	li	a5,6
80001184:	00000717          	auipc	a4,0x0
80001188:	00f72a23          	sw	a5,20(a4) # 80001198 <test>
8000118c:	fb9ff06f          	j	80001144 <main+0xfc>
80001190:	eb9ff0ef          	jal	ra,80001048 <main>
80001194:	f61ff06f          	j	800010f4 <main+0xac>
```
As we can see, branch instruction density had a significant increase comparing to previous attempt. However, the increase of branch instructions brought more increase of immediate loading/memory access, which is not ideal for branch density.

Asssuming the increasing of branch instructions leads to increased amount of stall, our observation matches our theory (that more branches will lead to increasing CPI). 

**Ratio of branch to non-branch instructions, static: 17/84 = 20.24% (O3)**
**Ratio of branch to non-branch instructions, runtime: 20% (O3)**
**CPI: 1.384 (O3)**

### Attempt 3

At this point, we read on Piazza that we should maximize the number of branch instructions in the generated static code instead of the number of branch instructions dispatched at runtime. Therefore, we started over and started to experiment with different constructs that could generate a bunch of branches.

For example, we tried to write a nested if expression with a tenary expression in the condition field to maximize the number of branches generated per line.

```c
volatile int test, test2 = 0;
__attribute__((optimize("O3"))) int main()
{
    test2 = 2;
    if (test ? 1 : test2) {
    if (test ? test2 : 1) {
    if (test ? 1 : test2) {
    if (test ? 1 : test2) {
    if (test ? 1 : test2) {
    if (test ? 1 : test2) {
    if (test ? 1 : test2) {
    if (test ? 1 : test2) {
    if (test ? 1 : test2) {
    if (test ? 1 : test2) {
    if (test ? 1 : test2) {
        test2 = test ? 1 : test2;
    }}}}}}}}}}}
}
```

```assembly
Disassembly of section .text.startup:

80001048 <main>:
80001048:	00200793          	li	a5,2
8000104c:	00000717          	auipc	a4,0x0
80001050:	10f72423          	sw	a5,264(a4) # 80001154 <test2>
80001054:	00000797          	auipc	a5,0x0
80001058:	0fc78793          	addi	a5,a5,252 # 80001150 <test>
8000105c:	0007a683          	lw	a3,0(a5)
80001060:	00000717          	auipc	a4,0x0
80001064:	0f470713          	addi	a4,a4,244 # 80001154 <test2>
80001068:	00069663          	bnez	a3,80001074 <main+0x2c>
8000106c:	00072683          	lw	a3,0(a4)
80001070:	08068263          	beqz	a3,800010f4 <main+0xac>
80001074:	0007a683          	lw	a3,0(a5)
80001078:	08069263          	bnez	a3,800010fc <main+0xb4>
8000107c:	0007a683          	lw	a3,0(a5)
80001080:	08068463          	beqz	a3,80001108 <main+0xc0>
80001084:	0007a683          	lw	a3,0(a5)
80001088:	00069663          	bnez	a3,80001094 <main+0x4c>
8000108c:	00072683          	lw	a3,0(a4)
80001090:	06068263          	beqz	a3,800010f4 <main+0xac>
80001094:	0007a683          	lw	a3,0(a5)
80001098:	06068e63          	beqz	a3,80001114 <main+0xcc>
8000109c:	0007a683          	lw	a3,0(a5)
800010a0:	08068063          	beqz	a3,80001120 <main+0xd8>
800010a4:	0007a683          	lw	a3,0(a5)
800010a8:	08068263          	beqz	a3,8000112c <main+0xe4>
800010ac:	0007a683          	lw	a3,0(a5)
800010b0:	00069663          	bnez	a3,800010bc <main+0x74>
800010b4:	00072683          	lw	a3,0(a4)
800010b8:	02068e63          	beqz	a3,800010f4 <main+0xac>
800010bc:	0007a683          	lw	a3,0(a5)
800010c0:	00069663          	bnez	a3,800010cc <main+0x84>
800010c4:	00072683          	lw	a3,0(a4)
800010c8:	02068663          	beqz	a3,800010f4 <main+0xac>
800010cc:	0007a683          	lw	a3,0(a5)
800010d0:	06068463          	beqz	a3,80001138 <main+0xf0>
800010d4:	0007a683          	lw	a3,0(a5)
800010d8:	06068663          	beqz	a3,80001144 <main+0xfc>
800010dc:	0007a683          	lw	a3,0(a5)
800010e0:	00100793          	li	a5,1
800010e4:	00069463          	bnez	a3,800010ec <main+0xa4>
800010e8:	00072783          	lw	a5,0(a4)
800010ec:	00000717          	auipc	a4,0x0
800010f0:	06f72423          	sw	a5,104(a4) # 80001154 <test2>
800010f4:	00000513          	li	a0,0
800010f8:	00008067          	ret
800010fc:	00072683          	lw	a3,0(a4)
80001100:	f6069ee3          	bnez	a3,8000107c <main+0x34>
80001104:	ff1ff06f          	j	800010f4 <main+0xac>
80001108:	00072683          	lw	a3,0(a4)
8000110c:	f6069ce3          	bnez	a3,80001084 <main+0x3c>
80001110:	fe5ff06f          	j	800010f4 <main+0xac>
80001114:	00072683          	lw	a3,0(a4)
80001118:	f80692e3          	bnez	a3,8000109c <main+0x54>
8000111c:	fd9ff06f          	j	800010f4 <main+0xac>
80001120:	00072683          	lw	a3,0(a4)
80001124:	f80690e3          	bnez	a3,800010a4 <main+0x5c>
80001128:	fcdff06f          	j	800010f4 <main+0xac>
8000112c:	00072683          	lw	a3,0(a4)
80001130:	f6069ee3          	bnez	a3,800010ac <main+0x64>
80001134:	fc1ff06f          	j	800010f4 <main+0xac>
80001138:	00072683          	lw	a3,0(a4)
8000113c:	f8069ce3          	bnez	a3,800010d4 <main+0x8c>
80001140:	fb5ff06f          	j	800010f4 <main+0xac>
80001144:	00072683          	lw	a3,0(a4)
80001148:	f8069ae3          	bnez	a3,800010dc <main+0x94>
8000114c:	fa9ff06f          	j	800010f4 <main+0xac>
```

**Ratio of branch to non-branch instructions, static: 23/66 = 35% (O3)**
**Ratio of branch to non-branch instructions, runtime: 14% (O3)**
**CPI: 1.521 (O3)**

This approach turned out to be a bit more effective at generating branch instructions than our previous attempts, giving us a static ratio of 35%. We are using "O3" here because it gets rid of some of the prologue/epilogue/misc. code that do not contribute to the branch instruction ratio. The CPI also increased from 1.328 from 1.521, likely due to the increased ratio of branch instructions and the existance of many lw instructions (potentially because of memory latency).


<!---
### Attempt 4
```c
int a;
__attribute__((optimize("O3"))) int main () {
    tag:
    if (a == 1) {
        goto tag;
    } else if (a == 2) {
        goto tag;
    } else if (a == 3) {
        goto tag;
    } else if (a == 4) {
        goto tag;
    } else if (a == 5) {
        goto tag;
    } else if (a == 6) {
        goto tag;
    }
    return 0;
}
```

```assembly
Disassembly of section .text.startup:

80001048 <main>:
80001048:	00000797          	auipc	a5,0x0
8000104c:	04878793          	addi	a5,a5,72 # 80001090 <a>
80001050:	0007a783          	lw	a5,0(a5)
80001054:	00100713          	li	a4,1
80001058:	00200693          	li	a3,2
8000105c:	00300613          	li	a2,3
80001060:	00400593          	li	a1,4
80001064:	00500813          	li	a6,5
80001068:	00600513          	li	a0,6
8000106c:	00e79463          	bne	a5,a4,80001074 <main+0x2c>
80001070:	0000006f          	j	80001070 <main+0x28>
80001074:	fed78ce3          	beq	a5,a3,8000106c <main+0x24>
80001078:	fec78ae3          	beq	a5,a2,8000106c <main+0x24>
8000107c:	feb788e3          	beq	a5,a1,8000106c <main+0x24>
80001080:	ff0786e3          	beq	a5,a6,8000106c <main+0x24>
80001084:	fea784e3          	beq	a5,a0,8000106c <main+0x24>
80001088:	00000513          	li	a0,0
8000108c:	00008067          	ret
```

**Ratio of branch to non-branch instructions, static: 6/18 = 33% (O3)**
**Ratio of branch to non-branch instructions, runtime: 5% (O3)**
**CPI: 1.359 (O3)**
--->

### Attempt 4
Looking at our previous attempts, it seems that a large portion of our instructions were load word or load immediate instructions besides our branches, typically at a one-to-one ratio. This meant that our final branch instruction ration could not exceed 50%. Is there way to not generate instructions that access the memory? Browsing Piazza once again, we received the helpful hint that we are allowed to use the register keyword to hint to the compiler that a particular variable should be stored in a register. The result is the code below:
```c
__attribute__((optimize("O3"))) int main() {
    volatile register int r asm ("a0");
    volatile register int a asm ("x5");
    volatile register int b asm ("x6");
    volatile register int c asm ("x7");
    volatile register int d asm ("x28");
    tag:
    if (a == b) {
        goto tag;
    } else if (a == c) {
        goto tag;
    } else if (a == d) {
        goto tag;
    } else if (a == 0) {
        goto tag;
    }
    return r;
}
```

```assembly
Disassembly of section .text.startup:

80001048 <main>:
80001048:	00628063          	beq	t0,t1,80001048 <main>
8000104c:	fe728ee3          	beq	t0,t2,80001048 <main>
80001050:	ffc28ce3          	beq	t0,t3,80001048 <main>
80001054:	fe028ae3          	beqz	t0,80001048 <main>
80001058:	00008067          	ret
```

**Ratio of branch to non-branch instructions, static: 4/5 = 80%**
**Ratio of branch to non-branch instructions, runtime: 4%**
**CPI: 1.413**

Here, we are able to drastically increase the ratio of branch to non-branch instructions to 80% by avoiding lw instructions. However, both our runtime branch to non-branch ratio and CPI has dropped, likely because the CPU no longer needs to wait for the data memory to respond.

### Attempt 5
Can we make the ratio higher? The answer is yes. Basically, now our goal is to generate as many branch instructions as possible so that the single `ret` instruction at the end takes up a smaller portion of the code. We noticed that all the variable declartions in our program take up multiple lines - lines that could've been taken up by if expressions, so we put all variable declartions on one line.

```c
__attribute__((optimize("O3"))) int main() {
    volatile register int r asm ("a0"), a asm ("x5"), b asm ("x6"), c asm ("x7"), d asm ("x28"), e asm ("x29"), f asm ("x30");
    tag:
    if (a == b) {
        goto tag;
    } else if (a == c) {
        goto tag;
    } else if (a == d) {
        goto tag;
    } else if (a == 0) {
        goto tag;
    } else if (a == e) {
        goto tag;
    } else if (a == f) {
        goto tag;
    } return r;
}
```
```assembly
Disassembly of section .text.startup:

80001048 <main>:
80001048:	00628063          	beq	t0,t1,80001048 <main>
8000104c:	fe728ee3          	beq	t0,t2,80001048 <main>
80001050:	ffc28ce3          	beq	t0,t3,80001048 <main>
80001054:	fe028ae3          	beqz	t0,80001048 <main>
80001058:	ffd288e3          	beq	t0,t4,80001048 <main>
8000105c:	ffe286e3          	beq	t0,t5,80001048 <main>
80001060:	00008067          	ret
```
**Ratio of branch to non-branch instructions, static: 6/7 = 85.7% (O3)**
**Ratio of branch to non-branch instructions, runtime: 5.6% (O3)**
**CPI: 1.392 (O3)**

As a result, our static branch to non-branch instructioin ratio increased from 80% to 85.7%. Curiously, our runtime ratio increased slightly while our CPI decreased slightly compared to the previous attempt. We suspect this is the case because our code became slightly longer, thus making the instruction cache a bit more effective.

### Attempt 6
However, we are not satified with our program because we still haven't had to use macros yet :) In our final attempt, we use a macro that generates two if expressions in just a single line (though the macro definition itself does take 4 lines).
```c
#define foo(x, y) if (a == x) { \
 goto tag; \
} if (a == y) { \
 goto tag; \
}
__attribute__((optimize("O3"))) int main() {
    volatile register int r asm ("a0"), a asm ("x5"), b asm ("x6"), c asm ("x7"), d asm ("x28"), e asm ("x29"), f asm ("x30"), g asm ("x31"), i asm ("x11"), j asm ("x12"), k asm ("x13");
    tag:
    foo(b, c);
    foo(0, e);
    foo(e, f);
    foo(g, i);
    foo(j, k);
    foo(j, k);
    foo(j, k);
    return r;
}
```
```assembly
Disassembly of section .text.startup:

80001048 <main>:
80001048:	00628063          	beq	t0,t1,80001048 <main>
8000104c:	fe728ee3          	beq	t0,t2,80001048 <main>
80001050:	fe028ce3          	beqz	t0,80001048 <main>
80001054:	ffd28ae3          	beq	t0,t4,80001048 <main>
80001058:	ffe288e3          	beq	t0,t5,80001048 <main>
8000105c:	fff286e3          	beq	t0,t6,80001048 <main>
80001060:	feb284e3          	beq	t0,a1,80001048 <main>
80001064:	fec282e3          	beq	t0,a2,80001048 <main>
80001068:	fed280e3          	beq	t0,a3,80001048 <main>
8000106c:	fcc28ee3          	beq	t0,a2,80001048 <main>
80001070:	fcd28ce3          	beq	t0,a3,80001048 <main>
80001074:	fcc28ae3          	beq	t0,a2,80001048 <main>
80001078:	fcd288e3          	beq	t0,a3,80001048 <main>
8000107c:	00008067          	ret
```
**Ratio of branch to non-branch instructions, static: 13/14 = 92.9% (O3)**
**Ratio of branch to non-branch instructions, runtime: 11.1% (O3)**
**CPI: 1.349 (O3)**

This gives us a static branch to non-branch instruction ratio of 92.9%. As observed before, there is again a slight increase of the runtime branch to non-branch instruction ratio and a slight decrease of CPI, likely due to cost of filling the instruction cache being amortized over the longer program.

<!--- ### Attempt 6
```c
volatile int test, test2 = 0;
__attribute__((optimize("O0"))) int main()
{
    test2 = 2;
    if (test ? 1 : test2) {
        if (test ? test2 : 1) {
            if (test ? 1 : test2) {
                if (test ? 1 : test2) {
                    if (test ? 1 : test2) {
                        if (test ? 1 : test2) {
                            if (test ? 1 : test2) {
                                if (test ? 1 : test2) {
                                    if (test ? 1 : test2) {
                                        if (test ? 1 : test2) {
                                            if (test ? 1 : test2) {
                                                test2 = test ? 1 : test2;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
```

```assembly
Disassembly of section .text:

80001048 <main>:
80001048:	ff010113          	addi	sp,sp,-16
8000104c:	00812623          	sw	s0,12(sp)
80001050:	01010413          	addi	s0,sp,16
80001054:	00000797          	auipc	a5,0x0
80001058:	1b878793          	addi	a5,a5,440 # 8000120c <test2>
8000105c:	00200713          	li	a4,2
80001060:	00e7a023          	sw	a4,0(a5)
80001064:	00000797          	auipc	a5,0x0
80001068:	1a478793          	addi	a5,a5,420 # 80001208 <test>
8000106c:	0007a783          	lw	a5,0(a5)
80001070:	00079a63          	bnez	a5,80001084 <main+0x3c>
80001074:	00000797          	auipc	a5,0x0
80001078:	19878793          	addi	a5,a5,408 # 8000120c <test2>
8000107c:	0007a783          	lw	a5,0(a5)
80001080:	16078a63          	beqz	a5,800011f4 <main+0x1ac>
80001084:	00000797          	auipc	a5,0x0
80001088:	18478793          	addi	a5,a5,388 # 80001208 <test>
8000108c:	0007a783          	lw	a5,0(a5)
80001090:	00078a63          	beqz	a5,800010a4 <main+0x5c>
80001094:	00000797          	auipc	a5,0x0
80001098:	17878793          	addi	a5,a5,376 # 8000120c <test2>
8000109c:	0007a783          	lw	a5,0(a5)
800010a0:	14078a63          	beqz	a5,800011f4 <main+0x1ac>
800010a4:	00000797          	auipc	a5,0x0
800010a8:	16478793          	addi	a5,a5,356 # 80001208 <test>
800010ac:	0007a783          	lw	a5,0(a5)
800010b0:	00079a63          	bnez	a5,800010c4 <main+0x7c>
800010b4:	00000797          	auipc	a5,0x0
800010b8:	15878793          	addi	a5,a5,344 # 8000120c <test2>
800010bc:	0007a783          	lw	a5,0(a5)
800010c0:	12078a63          	beqz	a5,800011f4 <main+0x1ac>
800010c4:	00000797          	auipc	a5,0x0
800010c8:	14478793          	addi	a5,a5,324 # 80001208 <test>
800010cc:	0007a783          	lw	a5,0(a5)
800010d0:	00079a63          	bnez	a5,800010e4 <main+0x9c>
800010d4:	00000797          	auipc	a5,0x0
800010d8:	13878793          	addi	a5,a5,312 # 8000120c <test2>
800010dc:	0007a783          	lw	a5,0(a5)
800010e0:	10078a63          	beqz	a5,800011f4 <main+0x1ac>
800010e4:	00000797          	auipc	a5,0x0
800010e8:	12478793          	addi	a5,a5,292 # 80001208 <test>
800010ec:	0007a783          	lw	a5,0(a5)
800010f0:	00079a63          	bnez	a5,80001104 <main+0xbc>
800010f4:	00000797          	auipc	a5,0x0
800010f8:	11878793          	addi	a5,a5,280 # 8000120c <test2>
800010fc:	0007a783          	lw	a5,0(a5)
80001100:	0e078a63          	beqz	a5,800011f4 <main+0x1ac>
80001104:	00000797          	auipc	a5,0x0
80001108:	10478793          	addi	a5,a5,260 # 80001208 <test>
8000110c:	0007a783          	lw	a5,0(a5)
80001110:	00079a63          	bnez	a5,80001124 <main+0xdc>
80001114:	00000797          	auipc	a5,0x0
80001118:	0f878793          	addi	a5,a5,248 # 8000120c <test2>
8000111c:	0007a783          	lw	a5,0(a5)
80001120:	0c078a63          	beqz	a5,800011f4 <main+0x1ac>
80001124:	00000797          	auipc	a5,0x0
80001128:	0e478793          	addi	a5,a5,228 # 80001208 <test>
8000112c:	0007a783          	lw	a5,0(a5)
80001130:	00079a63          	bnez	a5,80001144 <main+0xfc>
80001134:	00000797          	auipc	a5,0x0
80001138:	0d878793          	addi	a5,a5,216 # 8000120c <test2>
8000113c:	0007a783          	lw	a5,0(a5)
80001140:	0a078a63          	beqz	a5,800011f4 <main+0x1ac>
80001144:	00000797          	auipc	a5,0x0
80001148:	0c478793          	addi	a5,a5,196 # 80001208 <test>
8000114c:	0007a783          	lw	a5,0(a5)
80001150:	00079a63          	bnez	a5,80001164 <main+0x11c>
80001154:	00000797          	auipc	a5,0x0
80001158:	0b878793          	addi	a5,a5,184 # 8000120c <test2>
8000115c:	0007a783          	lw	a5,0(a5)
80001160:	08078a63          	beqz	a5,800011f4 <main+0x1ac>
80001164:	00000797          	auipc	a5,0x0
80001168:	0a478793          	addi	a5,a5,164 # 80001208 <test>
8000116c:	0007a783          	lw	a5,0(a5)
80001170:	00079a63          	bnez	a5,80001184 <main+0x13c>
80001174:	00000797          	auipc	a5,0x0
80001178:	09878793          	addi	a5,a5,152 # 8000120c <test2>
8000117c:	0007a783          	lw	a5,0(a5)
80001180:	06078a63          	beqz	a5,800011f4 <main+0x1ac>
80001184:	00000797          	auipc	a5,0x0
80001188:	08478793          	addi	a5,a5,132 # 80001208 <test>
8000118c:	0007a783          	lw	a5,0(a5)
80001190:	00079a63          	bnez	a5,800011a4 <main+0x15c>
80001194:	00000797          	auipc	a5,0x0
80001198:	07878793          	addi	a5,a5,120 # 8000120c <test2>
8000119c:	0007a783          	lw	a5,0(a5)
800011a0:	04078a63          	beqz	a5,800011f4 <main+0x1ac>
800011a4:	00000797          	auipc	a5,0x0
800011a8:	06478793          	addi	a5,a5,100 # 80001208 <test>
800011ac:	0007a783          	lw	a5,0(a5)
800011b0:	00079a63          	bnez	a5,800011c4 <main+0x17c>
800011b4:	00000797          	auipc	a5,0x0
800011b8:	05878793          	addi	a5,a5,88 # 8000120c <test2>
800011bc:	0007a783          	lw	a5,0(a5)
800011c0:	02078a63          	beqz	a5,800011f4 <main+0x1ac>
800011c4:	00000797          	auipc	a5,0x0
800011c8:	04478793          	addi	a5,a5,68 # 80001208 <test>
800011cc:	0007a783          	lw	a5,0(a5)
800011d0:	00079a63          	bnez	a5,800011e4 <main+0x19c>
800011d4:	00000797          	auipc	a5,0x0
800011d8:	03878793          	addi	a5,a5,56 # 8000120c <test2>
800011dc:	0007a783          	lw	a5,0(a5)
800011e0:	0080006f          	j	800011e8 <main+0x1a0>
800011e4:	00100793          	li	a5,1
800011e8:	00000717          	auipc	a4,0x0
800011ec:	02470713          	addi	a4,a4,36 # 8000120c <test2>
800011f0:	00f72023          	sw	a5,0(a4)
800011f4:	00000793          	li	a5,0
800011f8:	00078513          	mv	a0,a5
800011fc:	00c12403          	lw	s0,12(sp)
80001200:	01010113          	addi	sp,sp,16
80001204:	00008067          	ret
```

**Ratio of branch to non-branch instructions, static: 23/112 = 20% (O0)**
**Ratio of branch to non-branch instructions, runtime: 9% (O0)**
**CPI: 1.356 (O0)**

--->

<!---
### Attempt 5
```c
volatile int test = 0;
__attribute__((optimize("O0"))) int main(void) {
    switch (test == 0 ? 0 : test) {
        case 0: test = 0x1; main() == 0 ? 0 : main();
        case 1: test == 0 ? test = 0 : (test = 0x2); main();
        case 2: test == 0 ? test = 0 : (test = 0x3); main();
        case 3: test == 0 ? test = 0 : (test = 0x4); main();
        if ((test == 0) ? 0 : (test == 4)) {
            test == 0 ? test = 0 : (test = 0x5); main();
        } else if ((test == 0) ? 0 : (test == 5)) {
            test == 0 ? test = 0 : (test = 0x6); main();
        } else {
            main() == 0 ? 0 : main();
        }
    }
    return 0;
}
```

```assembly
Disassembly of section .text:

80001048 <main>:
80001048:	ff010113          	addi	sp,sp,-16
8000104c:	00112623          	sw	ra,12(sp)
80001050:	00812423          	sw	s0,8(sp)
80001054:	01010413          	addi	s0,sp,16
80001058:	00000797          	auipc	a5,0x0
8000105c:	20078793          	addi	a5,a5,512 # 80001258 <test>
80001060:	0007a783          	lw	a5,0(a5)
80001064:	00078a63          	beqz	a5,80001078 <main+0x30>
80001068:	00000797          	auipc	a5,0x0
8000106c:	1f078793          	addi	a5,a5,496 # 80001258 <test>
80001070:	0007a783          	lw	a5,0(a5)
80001074:	0080006f          	j	8000107c <main+0x34>
80001078:	00000793          	li	a5,0
8000107c:	00100713          	li	a4,1
80001080:	04e78463          	beq	a5,a4,800010c8 <main+0x80>
80001084:	00100713          	li	a4,1
80001088:	00f74663          	blt	a4,a5,80001094 <main+0x4c>
8000108c:	00078e63          	beqz	a5,800010a8 <main+0x60>
80001090:	1b00006f          	j	80001240 <main+0x1f8>
80001094:	00200713          	li	a4,2
80001098:	06e78463          	beq	a5,a4,80001100 <main+0xb8>
8000109c:	00300713          	li	a4,3
800010a0:	08e78c63          	beq	a5,a4,80001138 <main+0xf0>
800010a4:	19c0006f          	j	80001240 <main+0x1f8>
800010a8:	00000797          	auipc	a5,0x0
800010ac:	1b078793          	addi	a5,a5,432 # 80001258 <test>
800010b0:	00100713          	li	a4,1
800010b4:	00e7a023          	sw	a4,0(a5)
800010b8:	f91ff0ef          	jal	ra,80001048 <main>
800010bc:	00050793          	mv	a5,a0
800010c0:	00078463          	beqz	a5,800010c8 <main+0x80>
800010c4:	f85ff0ef          	jal	ra,80001048 <main>
800010c8:	00000797          	auipc	a5,0x0
800010cc:	19078793          	addi	a5,a5,400 # 80001258 <test>
800010d0:	0007a783          	lw	a5,0(a5)
800010d4:	00079c63          	bnez	a5,800010ec <main+0xa4>
800010d8:	00000713          	li	a4,0
800010dc:	00000797          	auipc	a5,0x0
800010e0:	17c78793          	addi	a5,a5,380 # 80001258 <test>
800010e4:	00e7a023          	sw	a4,0(a5)
800010e8:	0140006f          	j	800010fc <main+0xb4>
800010ec:	00200713          	li	a4,2
800010f0:	00000797          	auipc	a5,0x0
800010f4:	16878793          	addi	a5,a5,360 # 80001258 <test>
800010f8:	00e7a023          	sw	a4,0(a5)
800010fc:	f4dff0ef          	jal	ra,80001048 <main>
80001100:	00000797          	auipc	a5,0x0
80001104:	15878793          	addi	a5,a5,344 # 80001258 <test>
80001108:	0007a783          	lw	a5,0(a5)
8000110c:	00079c63          	bnez	a5,80001124 <main+0xdc>
80001110:	00000713          	li	a4,0
80001114:	00000797          	auipc	a5,0x0
80001118:	14478793          	addi	a5,a5,324 # 80001258 <test>
8000111c:	00e7a023          	sw	a4,0(a5)
80001120:	0140006f          	j	80001134 <main+0xec>
80001124:	00300713          	li	a4,3
80001128:	00000797          	auipc	a5,0x0
8000112c:	13078793          	addi	a5,a5,304 # 80001258 <test>
80001130:	00e7a023          	sw	a4,0(a5)
80001134:	f15ff0ef          	jal	ra,80001048 <main>
80001138:	00000797          	auipc	a5,0x0
8000113c:	12078793          	addi	a5,a5,288 # 80001258 <test>
80001140:	0007a783          	lw	a5,0(a5)
80001144:	00079c63          	bnez	a5,8000115c <main+0x114>
80001148:	00000713          	li	a4,0
8000114c:	00000797          	auipc	a5,0x0
80001150:	10c78793          	addi	a5,a5,268 # 80001258 <test>
80001154:	00e7a023          	sw	a4,0(a5)
80001158:	0140006f          	j	8000116c <main+0x124>
8000115c:	00400713          	li	a4,4
80001160:	00000797          	auipc	a5,0x0
80001164:	0f878793          	addi	a5,a5,248 # 80001258 <test>
80001168:	00e7a023          	sw	a4,0(a5)
8000116c:	eddff0ef          	jal	ra,80001048 <main>
80001170:	00000797          	auipc	a5,0x0
80001174:	0e878793          	addi	a5,a5,232 # 80001258 <test>
80001178:	0007a783          	lw	a5,0(a5)
8000117c:	04078a63          	beqz	a5,800011d0 <main+0x188>
80001180:	00000797          	auipc	a5,0x0
80001184:	0d878793          	addi	a5,a5,216 # 80001258 <test>
80001188:	0007a703          	lw	a4,0(a5)
8000118c:	00400793          	li	a5,4
80001190:	04f71063          	bne	a4,a5,800011d0 <main+0x188>
80001194:	00000797          	auipc	a5,0x0
80001198:	0c478793          	addi	a5,a5,196 # 80001258 <test>
8000119c:	0007a783          	lw	a5,0(a5)
800011a0:	00079c63          	bnez	a5,800011b8 <main+0x170>
800011a4:	00000713          	li	a4,0
800011a8:	00000797          	auipc	a5,0x0
800011ac:	0b078793          	addi	a5,a5,176 # 80001258 <test>
800011b0:	00e7a023          	sw	a4,0(a5)
800011b4:	0140006f          	j	800011c8 <main+0x180>
800011b8:	00500713          	li	a4,5
800011bc:	00000797          	auipc	a5,0x0
800011c0:	09c78793          	addi	a5,a5,156 # 80001258 <test>
800011c4:	00e7a023          	sw	a4,0(a5)
800011c8:	e81ff0ef          	jal	ra,80001048 <main>
800011cc:	0740006f          	j	80001240 <main+0x1f8>
800011d0:	00000797          	auipc	a5,0x0
800011d4:	08878793          	addi	a5,a5,136 # 80001258 <test>
800011d8:	0007a783          	lw	a5,0(a5)
800011dc:	04078a63          	beqz	a5,80001230 <main+0x1e8>
800011e0:	00000797          	auipc	a5,0x0
800011e4:	07878793          	addi	a5,a5,120 # 80001258 <test>
800011e8:	0007a703          	lw	a4,0(a5)
800011ec:	00500793          	li	a5,5
800011f0:	04f71063          	bne	a4,a5,80001230 <main+0x1e8>
800011f4:	00000797          	auipc	a5,0x0
800011f8:	06478793          	addi	a5,a5,100 # 80001258 <test>
800011fc:	0007a783          	lw	a5,0(a5)
80001200:	00079c63          	bnez	a5,80001218 <main+0x1d0>
80001204:	00000713          	li	a4,0
80001208:	00000797          	auipc	a5,0x0
8000120c:	05078793          	addi	a5,a5,80 # 80001258 <test>
80001210:	00e7a023          	sw	a4,0(a5)
80001214:	0140006f          	j	80001228 <main+0x1e0>
80001218:	00600713          	li	a4,6
8000121c:	00000797          	auipc	a5,0x0
80001220:	03c78793          	addi	a5,a5,60 # 80001258 <test>
80001224:	00e7a023          	sw	a4,0(a5)
80001228:	e21ff0ef          	jal	ra,80001048 <main>
8000122c:	0140006f          	j	80001240 <main+0x1f8>
80001230:	e19ff0ef          	jal	ra,80001048 <main>
80001234:	00050793          	mv	a5,a0
80001238:	00078463          	beqz	a5,80001240 <main+0x1f8>
8000123c:	e0dff0ef          	jal	ra,80001048 <main>
80001240:	00000793          	li	a5,0
80001244:	00078513          	mv	a0,a5
80001248:	00c12083          	lw	ra,12(sp)
8000124c:	00812403          	lw	s0,8(sp)
80001250:	01010113          	addi	sp,sp,16
80001254:	00008067          	ret
```

**Ratio of branch to non-branch instructions, static: 17/132 = 12% (O0)**
**Ratio of branch to non-branch instructions, runtime: 15% (O0)**
**CPI: 1.385 (O0)**
--->