// File: /workspace/github_c_makefile_star2_O0/yutopp[P]cc/ir.c
// Function: build_expression
IRSymbolID build_expression(IRBuilder* builder, Node* node, IRFunction* f) {
    switch(node->kind) {
    case NODE_EXPR_BIN:
    {
        printf("LOG: expr bin = ");
        token_fprint_buf(stdout, node->value.expr_bin.op);
        printf("\n");

        IRSymbolID lhs_sym = build_expression(builder, node->value.expr_bin.lhs, f);
        IRSymbolID rhs_sym = build_expression(builder, node->value.expr_bin.rhs, f);

        IRSymbolID sym_id = ir_builder_build_local(builder);

        IRInstValue bin = {
            .kind = IR_INST_VALUE_KIND_OP_BIN,
            .value = {
                .op_bin = {
                    .op = node->value.expr_bin.op, // TODO: Change to builtin enums...
                    .lhs = lhs_sym,
                    .rhs = rhs_sym,
                },
            },
        };
        IRInst inst = {
            .kind = IR_INST_KIND_LET,
            .value = {
                .let = {
                    .id = sym_id,
                    .rhs = bin,
                },
            },
        };
        ir_bb_append_inst(builder->current_bb, &inst);
        ir_function_set_local(f, sym_id, 8); // TODO: fix

        return sym_id;
    }

    case NODE_EXPR_POSTFIX:
    {
        printf("LOG: expr post = \n");

        IRSymbolID lhs_sym = build_expression(builder, node->value.expr_postfix.lhs, f);

        switch(node->value.expr_postfix.kind) {
        case NODE_EXPR_POSTFIX_KIND_FUNC_CALL:
        {
            Vector* args = vector_new(sizeof(IRSymbolID)); // Vector<IRSymbolID>
            Node* args_list = node->value.expr_postfix.rhs;
            if (args_list) {
                assert(args_list->kind == NODE_ARGS_LIST);
                for(size_t i=0; i<vector_len(args_list->value.args_list.args); ++i) {
                    Node** a = (Node**)vector_at(args_list->value.args_list.args, i);
                    IRSymbolID a_sym = build_expression(builder, *a, f);

                    IRSymbolID* args_e = (IRSymbolID*)vector_append(args);
                    *args_e = a_sym;
                }
            }

            IRSymbolID sym_id = ir_builder_build_local(builder);

            IRInstValue call = {
                .kind = IR_INST_VALUE_KIND_CALL,
                .value = {
                    .call = {
                        .lhs = lhs_sym,
                        .args = args,
                    },
                },
            };
            IRInst inst = {
                .kind = IR_INST_KIND_LET,
                .value = {
                    .let = {
                        .id = sym_id,
                        .rhs = call,
                    },
                },
            };
            ir_bb_append_inst(builder->current_bb, &inst);
            ir_function_set_local(f, sym_id, 8); // TODO: fix

            return sym_id;
        }

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

    case NODE_LIT_INT:
    {
        printf("LOG: lit_int\n");
        IRSymbolID sym_id = ir_builder_build_local(builder);

        IRInstValue imm = {
            .kind = IR_INST_VALUE_KIND_IMM_INT,
            .value = {
                .imm_int = node->value.lit_int.v,
            },
        };
        IRInst inst = {
            .kind = IR_INST_KIND_LET,
            .value = {
                .let = {
                    .id = sym_id,
                    .rhs = imm,
                },
            },
        };
        ir_bb_append_inst(builder->current_bb, &inst);
        ir_function_set_local(f, sym_id, 8); // TODO: fix

        return sym_id;
    }

    case NODE_LIT_STRING:
    {
        printf("LOG: lit_string\n");

        IRInstValue sval = {
            .kind = IR_INST_VALUE_KIND_STRING,
            .value = {
                .string = node->value.lit_string.v,
            },
        };
        IRSymbolID str_sym_id = insert_definition(f->mod, sval);

        //
        IRSymbolID ref_sym_id = ir_builder_build_local(builder);

        {
            IRInstValue ref = {
                .kind = IR_INST_VALUE_KIND_REF,
                .value = {
                    .ref = {
                        .is_global = 1,
                        .sym = str_sym_id,
                    },
                },
            };
            IRInst inst = {
                .kind = IR_INST_KIND_LET,
                .value = {
                    .let = {
                        .id = ref_sym_id,
                        .rhs = ref,
                    },
                },
            };
            ir_bb_append_inst(builder->current_bb, &inst);
            ir_function_set_local(f, ref_sym_id, 8); // TODO: fix
        }

        IRSymbolID sym_id = ir_builder_build_local(builder);

        IRInstValue addr_of = {
            .kind = IR_INST_VALUE_KIND_ADDR_OF,
            .value = {
                .addr_of = {
                    .sym = ref_sym_id,
                },
            },
        };
        IRInst inst = {
            .kind = IR_INST_KIND_LET,
            .value = {
                .let = {
                    .id = sym_id,
                    .rhs = addr_of,
                },
            },
        };
        ir_bb_append_inst(builder->current_bb, &inst);
        ir_function_set_local(f, sym_id, 8); // TODO: fix

        return sym_id;
    }

    case NODE_ID:
    {
        // TODO: implement
        IRInstValue val = {
            .kind = IR_INST_VALUE_KIND_SYMBOL,
            .value = {
                .symbol = token_to_string(node->value.id.tok),
            },
        };
        IRSymbolID tmp_sym_id = insert_definition(f->mod, val);

        IRSymbolID sym_id = ir_builder_build_local(builder);

        IRInstValue ref = {
            .kind = IR_INST_VALUE_KIND_REF,
            .value = {
                .ref = {
                    .is_global = 1,
                    .sym = tmp_sym_id,
                },
            },
        };
        IRInst inst = {
            .kind = IR_INST_KIND_LET,
            .value = {
                .let = {
                    .id = sym_id,
                    .rhs = ref,
                },
            },
        };
        ir_bb_append_inst(builder->current_bb, &inst);
        ir_function_set_local(f, sym_id, 8); // TODO: fix

        return sym_id;
    }

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x300, %rsp            # imm = 0x300
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
addl $-0x6, %eax
movl %eax, %ecx
movq %rcx, -0x2d0(%rbp)
subl $0x4, %eax
ja 0x657c
movq -0x2d0(%rbp), %rax
leaq 0x572c(%rip), %rcx      # 0xb5dc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x595c(%rip), %rdi      # 0xb81c
movb $0x0, %al
callq 0x1090
movq 0x910a(%rip), %rax      # 0xefd8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x1d60
leaq 0x618b(%rip), %rdi      # 0xc070
movb $0x0, %al
callq 0x1090
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x5e70
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x5e70
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
callq 0x66b0
movq %rax, -0x38(%rbp)
movl $0x5, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x88(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x88(%rbp), %rsi
callq 0x66f0
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl $0x8, %edx
callq 0x4e20
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x659b
leaq 0x5873(%rip), %rdi      # 0xb82d
xorl %eax, %eax
callq 0x1090
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x5e70
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
testl %eax, %eax
jne 0x61b9
jmp 0x5fee
movl $0x8, %edi
callq 0x42c0
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xa0(%rbp)
je 0x60fa
movq -0xa0(%rbp), %rax
cmpl $0xb, (%rax)
jne 0x602a
jmp 0x6049
leaq 0x580f(%rip), %rdi      # 0xb840
leaq 0x55b8(%rip), %rsi      # 0xb5f0
movl $0x17c, %edx            # imm = 0x17C
leaq 0x581e(%rip), %rcx      # 0xb862
callq 0x10b0
movq $0x0, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x2d8(%rbp)
movq -0xa0(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x4580
movq %rax, %rcx
movq -0x2d8(%rbp), %rax
cmpq %rcx, %rax
jae 0x60f8
movq -0xa0(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0xa8(%rbp), %rsi
callq 0x4530
movq %rax, -0xb0(%rbp)
movq -0x10(%rbp), %rdi
movq -0xb0(%rbp), %rax
movq (%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x5e70
movq %rax, -0xb8(%rbp)
movq -0x98(%rbp), %rdi
callq 0x4480
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rcx
movq -0xc0(%rbp), %rax
movq %rcx, (%rax)
movq -0xa8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0x6054
jmp 0x60fa
movq -0x10(%rbp), %rdi
callq 0x66b0
movq %rax, -0xc8(%rbp)
movl $0x6, -0xe8(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl $0x0, -0x118(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x118(%rbp), %rsi
callq 0x66f0
movq -0x20(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movl $0x8, %edx
callq 0x4e20
movq -0xc8(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x659b
leaq 0x57a6(%rip), %rdi      # 0xb966
leaq 0x5429(%rip), %rsi      # 0xb5f0
movl $0x1a1, %edx            # imm = 0x1A1
leaq 0x568f(%rip), %rcx      # 0xb862
callq 0x10b0
leaq 0x56c2(%rip), %rdi      # 0xb8a1
movb $0x0, %al
callq 0x1090
movq -0x10(%rbp), %rdi
callq 0x66b0
movq %rax, -0x120(%rbp)
leaq -0x140(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x10c0
movl $0x4, -0x140(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x138(%rbp)
movl $0x0, -0x170(%rbp)
movq -0x120(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x140(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x138(%rbp), %rax
movq %rax, -0x158(%rbp)
movq -0x130(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x128(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x170(%rbp), %rsi
callq 0x66f0
movq -0x20(%rbp), %rdi
movq -0x120(%rbp), %rsi
movl $0x8, %edx
callq 0x4e20
movq -0x120(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x659b
leaq 0x55ff(%rip), %rdi      # 0xb8af
movb $0x0, %al
callq 0x1090
movl $0x1, -0x190(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x188(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x190(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x58c0
movq %rax, -0x198(%rbp)
movq -0x10(%rbp), %rdi
callq 0x66b0
movq %rax, -0x1a0(%rbp)
movl $0x2, -0x1c0(%rbp)
movl $0x1, -0x1b8(%rbp)
movq -0x198(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movl $0x0, -0x1f0(%rbp)
movq -0x1a0(%rbp), %rax
movq %rax, -0x1e8(%rbp)
movq -0x1c0(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movq -0x1b8(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movq -0x1b0(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movq -0x1a8(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x66f0
movq -0x20(%rbp), %rdi
movq -0x1a0(%rbp), %rsi
movl $0x8, %edx
callq 0x4e20
movq -0x10(%rbp), %rdi
callq 0x66b0
movq %rax, -0x1f8(%rbp)
movl $0x3, -0x218(%rbp)
movq -0x1a0(%rbp), %rax
movq %rax, -0x210(%rbp)
movl $0x0, -0x248(%rbp)
movq -0x1f8(%rbp), %rax
movq %rax, -0x240(%rbp)
movq -0x218(%rbp), %rax
movq %rax, -0x238(%rbp)
movq -0x210(%rbp), %rax
movq %rax, -0x230(%rbp)
movq -0x208(%rbp), %rax
movq %rax, -0x228(%rbp)
movq -0x200(%rbp), %rax
movq %rax, -0x220(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x248(%rbp), %rsi
callq 0x66f0
movq -0x20(%rbp), %rdi
movq -0x1f8(%rbp), %rsi
movl $0x8, %edx
callq 0x4e20
movq -0x1f8(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x659b
movl $0x0, -0x268(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1bd0
movq %rax, -0x260(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x268(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x58c0
movq %rax, -0x270(%rbp)
movq -0x10(%rbp), %rdi
callq 0x66b0
movq %rax, -0x278(%rbp)
movl $0x2, -0x298(%rbp)
movl $0x1, -0x290(%rbp)
movq -0x270(%rbp), %rax
movq %rax, -0x288(%rbp)
movl $0x0, -0x2c8(%rbp)
movq -0x278(%rbp), %rax
movq %rax, -0x2c0(%rbp)
movq -0x298(%rbp), %rax
movq %rax, -0x2b8(%rbp)
movq -0x290(%rbp), %rax
movq %rax, -0x2b0(%rbp)
movq -0x288(%rbp), %rax
movq %rax, -0x2a8(%rbp)
movq -0x280(%rbp), %rax
movq %rax, -0x2a0(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x2c8(%rbp), %rsi
callq 0x66f0
movq -0x20(%rbp), %rdi
movq -0x278(%rbp), %rsi
movl $0x8, %edx
callq 0x4e20
movq -0x278(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x659b
leaq 0x53e3(%rip), %rdi      # 0xb966
leaq 0x5066(%rip), %rsi      # 0xb5f0
movl $0x225, %edx            # imm = 0x225
leaq 0x52cc(%rip), %rcx      # 0xb862
callq 0x10b0
movq -0x8(%rbp), %rax
addq $0x300, %rsp            # imm = 0x300
popq %rbp
retq
nopl (%rax,%rax)