// File: /workspace/github_c_makefile_star2_O2/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 $0x98, %rsp
movl (%rsi), %eax
addl $-0x2, %eax
cmpl $0x3, %eax
ja 0x38ab
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x2e78(%rip), %rcx      # 0x64ac
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x3143(%rip), %rdi      # 0x6787
callq 0x1070
movq 0x8(%r15), %r15
xorl %r12d, %r12d
movq %r15, %rdi
callq 0x29db
cmpq %rax, %r12
jae 0x3899
movq %r15, %rdi
movq %r12, %rsi
callq 0x29c6
movq (%rax), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3605
incq %r12
jmp 0x3650
movq 0x7952(%rip), %rax      # 0xafd8
movq (%rax), %rcx
leaq 0x2fb5(%rip), %rdi      # 0x6645
pushq $0x12
popq %rsi
pushq $0x1
popq %rdx
callq 0x11c0
movq 0x8(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x38e9
movq %rax, %r12
movq %rbx, %rdi
callq 0x3c54
movq %rax, %rbp
cmpq $0x0, 0x18(%r15)
je 0x3778
movq %rbx, %rdi
callq 0x3c54
movq %rax, %r13
jmp 0x377b
leaq 0x3085(%rip), %rdi      # 0x675f
callq 0x1070
cmpl $0x19, 0x8(%r15)
jne 0x38ca
movq 0x10(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x38e9
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movl $0x1, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq 0x8(%rbx), %rdi
movaps 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
movaps 0x50(%rsp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x3c82
jmp 0x3899
leaq 0x3029(%rip), %rdi      # 0x6773
callq 0x1070
movq 0x8(%r15), %rsi
testq %rsi, %rsi
je 0x3899
movq %rbx, %rdi
movq %r14, %rdx
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x38e9
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x3c54
movl $0x2, 0x68(%rsp)
movq %r12, 0x70(%rsp)
movq %rax, %r12
movq %rbp, 0x78(%rsp)
testq %r13, %r13
cmovneq %r13, %rax
movq %rax, 0x80(%rsp)
movq 0x8(%rbx), %rdi
movups 0x68(%rsp), %xmm0
movups 0x78(%rsp), %xmm1
movups 0x88(%rsp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x3c82
movq %rbp, 0x8(%rbx)
movq 0x10(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3605
movq 0x8(%rbx), %rdi
cmpq $0x0, 0x18(%rdi)
jne 0x382f
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movl $0x3, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movaps 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
movaps 0x50(%rsp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x3c82
cmpq $0x0, 0x18(%r15)
je 0x3895
movq %r13, 0x8(%rbx)
movq 0x18(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3605
movq 0x8(%rbx), %rdi
cmpq $0x0, 0x18(%rdi)
jne 0x3895
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movl $0x3, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movaps 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
movaps 0x50(%rsp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x3c82
movq %r12, 0x8(%rbx)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2f8c(%rip), %rdi      # 0x683e
leaq 0x2c17(%rip), %rsi      # 0x64d0
leaq 0x2d98(%rip), %rcx      # 0x6658
movl $0x145, %edx            # imm = 0x145
callq 0x10d0
leaq 0x2f6d(%rip), %rdi      # 0x683e
leaq 0x2bf8(%rip), %rsi      # 0x64d0
leaq 0x2d79(%rip), %rcx      # 0x6658
movl $0x13f, %edx            # imm = 0x13F
callq 0x10d0