// File: /workspace/github_c_makefile_star2_O1/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 0x424b
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x34f9(%rip), %rcx      # 0x749c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x37c4(%rip), %rdi      # 0x7777
callq 0x1070
movq 0x8(%r15), %r15
movq %r15, %rdi
callq 0x322b
testq %rax, %rax
je 0x421d
xorl %r12d, %r12d
movq %r15, %rdi
movq %r12, %rsi
callq 0x3216
movq (%rax), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3f77
incq %r12
movq %r15, %rdi
callq 0x322b
cmpq %rax, %r12
jb 0x3fd0
jmp 0x421d
movq 0x6fd3(%rip), %rax      # 0xafd8
movq (%rax), %rcx
leaq 0x3626(%rip), %rdi      # 0x7635
movl $0x12, %esi
movl $0x1, %edx
callq 0x11b0
movq 0x8(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x4289
movq %rax, 0x10(%rsp)
movq (%rbx), %r12
movq 0x10(%r12), %rdi
callq 0x603e
movq 0x20(%r12), %rsi
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x5df8
incq 0x20(%r12)
cmpq $0x0, 0x18(%r15)
je 0x411c
movq (%rbx), %r12
movq 0x10(%r12), %rdi
callq 0x603e
movq 0x20(%r12), %rsi
movq %rax, (%rsp)
movq %rax, %rdi
callq 0x5df8
incq 0x20(%r12)
jmp 0x4124
leaq 0x36bf(%rip), %rdi      # 0x774f
callq 0x1070
cmpl $0x19, 0x8(%r15)
jne 0x426a
movq 0x10(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x4289
movq 0x8(%rbx), %rbx
cmpq $0x0, 0x18(%rbx)
jne 0x422c
movq %rax, %r14
movl $0x30, %edi
callq 0x1150
movq %rax, 0x18(%rbx)
movq $0x1, (%rax)
movq %r14, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
jmp 0x421d
leaq 0x3672(%rip), %rdi      # 0x7763
callq 0x1070
movq 0x8(%r15), %rsi
testq %rsi, %rsi
je 0x421d
movq %rbx, %rdi
movq %r14, %rdx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4289
movq $0x0, (%rsp)
movq (%rbx), %r13
movq 0x10(%r13), %rdi
callq 0x603e
movq %rax, %rbp
movq 0x20(%r13), %rsi
movq %rax, %rdi
callq 0x5df8
incq 0x20(%r13)
movq 0x8(%rbx), %r13
cmpq $0x0, 0x18(%r13)
jne 0x422c
movq (%rsp), %rax
testq %rax, %rax
movq %rbp, %r12
cmovneq %rax, %r12
movl $0x30, %edi
callq 0x1150
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 0x3f77
movq 0x8(%rbx), %r13
cmpq $0x0, 0x18(%r13)
jne 0x41cc
movl $0x30, %edi
callq 0x1150
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 0x4219
movq (%rsp), %rax
movq %rax, 0x8(%rbx)
movq 0x18(%r15), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x3f77
movq 0x8(%rbx), %r14
cmpq $0x0, 0x18(%r14)
jne 0x4219
movl $0x30, %edi
callq 0x1150
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 0x34bf(%rip), %rdi      # 0x76f2
leaq 0x3286(%rip), %rsi      # 0x74c0
leaq 0x34c2(%rip), %rcx      # 0x7703
movl $0xf, %edx
callq 0x10d0
leaq 0x35dc(%rip), %rdi      # 0x782e
leaq 0x3267(%rip), %rsi      # 0x74c0
leaq 0x33e8(%rip), %rcx      # 0x7648
movl $0x145, %edx            # imm = 0x145
callq 0x10d0
leaq 0x35bd(%rip), %rdi      # 0x782e
leaq 0x3248(%rip), %rsi      # 0x74c0
leaq 0x33c9(%rip), %rcx      # 0x7648
movl $0x13f, %edx            # imm = 0x13F
callq 0x10d0