// File: /workspace/github_c_makefile_star2_O3/yutopp[P]cc/ir.c
// Function: build_statement
void build_statement(IRBuilder* builder, Node* node, IRFunction* f) {
    switch(node->kind) {
    case NODE_STMT_COMPOUND:
    {
        printf("LOG: statement compound\n");

        Vector* stmts = node->value.stmt_compound.stmts;
        for(size_t i=0; i<vector_len(stmts); ++i) {
            Node** n = (Node**)vector_at(stmts, i);
            build_statement(builder, *n, f);
        }
        break;
    }

    case NODE_STMT_EXPR:
        printf("LOG: statement expr\n");

        if (node->value.stmt_expr.expr) {
            build_expression(builder, node->value.stmt_expr.expr, f);
        }
        break;

    case NODE_STMT_IF:
        fprintf(DEBUGOUT, "LOG: statement if\n");

        IRSymbolID cond = build_expression(builder, node->value.stmt_if.cond, f);

        IRBB* then_bb = ir_builder_build_bb(builder);
        IRBB* else_bb = NULL;
        if (node->value.stmt_if.else_b) {
            else_bb = ir_builder_build_bb(builder);
        }
        IRBB* final_bb = ir_builder_build_bb(builder);

        // Terminate current bb
        IRInst inst = {
            .kind = IR_INST_KIND_BRANCH,
            .value = {
                .branch = {
                    .cond = cond,
                    .then_bb = then_bb,
                    .else_bb = else_bb != NULL ? else_bb : final_bb,
                },
            },
        };
        ir_bb_terminate(builder->current_bb, inst);

        // then block
        ir_builder_set_current_bb(builder, then_bb);
        build_statement(builder, node->value.stmt_if.then_b, f);
        if (builder->current_bb->term == NULL) {
            IRInst then_inst = {
                .kind = IR_INST_KIND_JUMP,
                .value = {
                    .jump = {
                        .next_bb = final_bb,
                    },
                },
            };
            ir_bb_terminate(builder->current_bb, then_inst);
        }

        // then block
        if (node->value.stmt_if.else_b) {
            ir_builder_set_current_bb(builder, else_bb);
            build_statement(builder, node->value.stmt_if.else_b, f);
            if (builder->current_bb->term == NULL) {
                IRInst else_inst = {
                    .kind = IR_INST_KIND_JUMP,
                    .value = {
                        .jump = {
                            .next_bb = final_bb,
                        },
                    },
                };
                ir_bb_terminate(builder->current_bb, else_inst);
            }
        }

        ir_builder_set_current_bb(builder, final_bb);

        break;

    case NODE_STMT_JUMP:
        printf("LOG: statement jump\n");

        switch(node->value.stmt_jump.kind) {
        case TOK_KIND_RETURN:
        {
            IRSymbolID expr_ref = build_expression(builder, node->value.stmt_jump.expr, f);

            IRInst inst = {
                .kind = IR_INST_KIND_RET,
                .value = {
                    .ret = {
                        .id = expr_ref,
                    },
                },
            };
            ir_bb_terminate(builder->current_bb, inst);

            break;
        }

        default:
            assert(0); // TODO: error handling...
            break;
        }
        break;

    default:
        assert(0); // TODO: error handling...
        break;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl (%rsi), %eax
addl $-0x2, %eax
cmpl $0x3, %eax
ja 0x3f1b
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x3839(%rip), %rcx      # 0x74ac
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x3b04(%rip), %rdi      # 0x7787
callq 0x1070
movq 0x8(%r15), %r15
movq %r15, %rdi
callq 0x2f0b
testq %rax, %rax
je 0x3eed
xorl %r12d, %r12d
movq %r15, %rdi
movq %r12, %rsi
callq 0x2ef6
movq (%rax), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3c47
incq %r12
movq %r15, %rdi
callq 0x2f0b
cmpq %rax, %r12
jb 0x3ca0
jmp 0x3eed
movq 0x7303(%rip), %rax      # 0xafd8
movq (%rax), %rcx
leaq 0x3966(%rip), %rdi      # 0x7645
movl $0x12, %esi
movl $0x1, %edx
callq 0x11c0
movq 0x8(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3f59
movq %rax, 0x10(%rsp)
movq (%rbx), %r12
movq 0x10(%r12), %rdi
callq 0x5d3e
movq 0x20(%r12), %rsi
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x5af8
incq 0x20(%r12)
cmpq $0x0, 0x18(%r15)
je 0x3dec
movq (%rbx), %r12
movq 0x10(%r12), %rdi
callq 0x5d3e
movq 0x20(%r12), %rsi
movq %rax, (%rsp)
movq %rax, %rdi
callq 0x5af8
incq 0x20(%r12)
jmp 0x3df4
leaq 0x39ff(%rip), %rdi      # 0x775f
callq 0x1070
cmpl $0x19, 0x8(%r15)
jne 0x3f3a
movq 0x10(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3f59
movq 0x8(%rbx), %rbx
cmpq $0x0, 0x18(%rbx)
jne 0x3efc
movq %rax, %r14
movl $0x30, %edi
callq 0x1160
movq %rax, 0x18(%rbx)
movq $0x1, (%rax)
movq %r14, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
jmp 0x3eed
leaq 0x39b2(%rip), %rdi      # 0x7773
callq 0x1070
movq 0x8(%r15), %rsi
testq %rsi, %rsi
je 0x3eed
movq %rbx, %rdi
movq %r14, %rdx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3f59
movq $0x0, (%rsp)
movq (%rbx), %r13
movq 0x10(%r13), %rdi
callq 0x5d3e
movq %rax, %rbp
movq 0x20(%r13), %rsi
movq %rax, %rdi
callq 0x5af8
incq 0x20(%r13)
movq 0x8(%rbx), %r13
cmpq $0x0, 0x18(%r13)
jne 0x3efc
movq (%rsp), %rax
testq %rax, %rax
movq %rbp, %r12
cmovneq %rax, %r12
movl $0x30, %edi
callq 0x1160
movq %rax, 0x18(%r13)
movl $0x2, (%rax)
movq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x8(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %r12, 0x18(%rax)
movq %rcx, 0x8(%rbx)
movq 0x10(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3c47
movq 0x8(%rbx), %r13
cmpq $0x0, 0x18(%r13)
jne 0x3e9c
movl $0x30, %edi
callq 0x1160
movq %rax, 0x18(%r13)
movq $0x3, (%rax)
movq %rbp, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
cmpq $0x0, 0x18(%r15)
je 0x3ee9
movq (%rsp), %rax
movq %rax, 0x8(%rbx)
movq 0x18(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3c47
movq 0x8(%rbx), %r14
cmpq $0x0, 0x18(%r14)
jne 0x3ee9
movl $0x30, %edi
callq 0x1160
movq %rax, 0x18(%r14)
movq $0x3, (%rax)
movq %rbp, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movq %rbp, 0x8(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x37ff(%rip), %rdi      # 0x7702
leaq 0x35c6(%rip), %rsi      # 0x74d0
leaq 0x3802(%rip), %rcx      # 0x7713
movl $0xf, %edx
callq 0x10d0
leaq 0x391c(%rip), %rdi      # 0x783e
leaq 0x35a7(%rip), %rsi      # 0x74d0
leaq 0x3728(%rip), %rcx      # 0x7658
movl $0x145, %edx            # imm = 0x145
callq 0x10d0
leaq 0x38fd(%rip), %rdi      # 0x783e
leaq 0x3588(%rip), %rsi      # 0x74d0
leaq 0x3709(%rip), %rcx      # 0x7658
movl $0x13f, %edx            # imm = 0x13F
callq 0x10d0