// File: /workspace/github_c_makefile_star2_O0/yutopp[P]cc/ir.c
// Function: ir_inst_fprint
void ir_inst_fprint(FILE* fp, IRInst* inst) {
    switch(inst->kind) {
    case IR_INST_KIND_LET:
        fprintf(fp, "%%%ld = ", inst->value.let.id);
        switch(inst->value.let.rhs.kind) {
        case IR_INST_VALUE_KIND_SYMBOL:
        {
            fprintf(fp, "SYMBOL %s", inst->value.let.rhs.value.symbol);
            break;
        }

        case IR_INST_VALUE_KIND_STRING:
        {
            fprintf(fp, "\"%s\"", inst->value.let.rhs.value.string);
            break;
        }

        case IR_INST_VALUE_KIND_REF:
        {
            if (inst->value.let.rhs.value.ref.is_global) {
                fprintf(fp, "g@");
            }
            fprintf(fp, "%ld", inst->value.let.rhs.value.ref.sym);
            break;
        }

        case IR_INST_VALUE_KIND_ADDR_OF:
        {
            fprintf(fp, "&%ld", inst->value.let.rhs.value.addr_of.sym);
            break;
        }

        case IR_INST_VALUE_KIND_IMM_INT:
        {
            fprintf(fp, "%d", inst->value.let.rhs.value.imm_int);
            break;
        }

        case IR_INST_VALUE_KIND_OP_BIN:
        {
            char const* op_buf = token_to_string(inst->value.let.rhs.value.op_bin.op);
            fprintf(fp, "%s %%%ld %%%ld",
                    op_buf,
                    inst->value.let.rhs.value.op_bin.lhs,
                    inst->value.let.rhs.value.op_bin.rhs);
            free((char*)op_buf);
            break;
        }

        case IR_INST_VALUE_KIND_CALL:
        {
            fprintf(fp, "call %%%ld", inst->value.let.rhs.value.call.lhs);
            Vector* args = inst->value.let.rhs.value.call.args;
            for(size_t i=0; i<vector_len(args); ++i) {
                IRSymbolID* sym = (IRSymbolID*)vector_at(args, i);
                fprintf(fp, " %%%ld", *sym);
            }
            break;
        }

        default:
            assert(0); // TODO: error handling
        }
        fprintf(fp, "\n");
        break;

    case IR_INST_KIND_RET:
        fprintf(fp, "RET %%%ld\n", inst->value.ret.id);
        break;

    case IR_INST_KIND_BRANCH:
        fprintf(fp, "BR %%%ld: ", inst->value.branch.cond);
        fprintf(fp, "then -> %ld, ", inst->value.branch.then_bb->id);
        fprintf(fp, "else -> %ld\n", inst->value.branch.else_bb->id);

        break;

    case IR_INST_KIND_JUMP:
        fprintf(fp, "JUMP -> %ld\n", inst->value.jump.next_bb->id);
        break;

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x38(%rbp)
subq $0x3, %rax
ja 0x5585
movq -0x38(%rbp), %rax
leaq 0x62b1(%rip), %rcx      # 0xb5a0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x6342(%rip), %rsi      # 0xb64d
xorl %eax, %eax
callq 0x10e0
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0x40(%rbp)
subq $0x6, %rax
ja 0x54bb
movq -0x40(%rbp), %rax
leaq 0x627e(%rip), %rcx      # 0xb5b0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x6308(%rip), %rsi      # 0xb656
movb $0x0, %al
callq 0x10e0
jmp 0x54da
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x621a(%rip), %rsi      # 0xb587
movb $0x0, %al
callq 0x10e0
jmp 0x54da
movq -0x10(%rbp), %rax
cmpl $0x0, 0x18(%rax)
je 0x5395
movq -0x8(%rbp), %rdi
leaq 0x62d2(%rip), %rsi      # 0xb660
movb $0x0, %al
callq 0x10e0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0x62cb(%rip), %rsi      # 0xb673
movb $0x0, %al
callq 0x10e0
jmp 0x54da
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x629c(%rip), %rsi      # 0xb663
movb $0x0, %al
callq 0x10e0
jmp 0x54da
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x18(%rax), %edx
leaq 0x66af(%rip), %rsi      # 0xba94
movb $0x0, %al
callq 0x10e0
jmp 0x54da
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x1bd0
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %r8
leaq 0x6247(%rip), %rsi      # 0xb668
movb $0x0, %al
callq 0x10e0
movq -0x18(%rbp), %rdi
callq 0x1040
jmp 0x54da
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x622e(%rip), %rsi      # 0xb677
movb $0x0, %al
callq 0x10e0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rdi
callq 0x4580
movq %rax, %rcx
movq -0x48(%rbp), %rax
cmpq %rcx, %rax
jae 0x54b9
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x4530
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rdx
leaq 0x61cc(%rip), %rsi      # 0xb670
movb $0x0, %al
callq 0x10e0
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x5464
jmp 0x54da
leaq 0x64a4(%rip), %rdi      # 0xb966
leaq 0x6127(%rip), %rsi      # 0xb5f0
movl $0x28d, %edx            # imm = 0x28D
leaq 0x61ad(%rip), %rcx      # 0xb682
callq 0x10b0
movq -0x8(%rbp), %rdi
leaq 0x6b8b(%rip), %rsi      # 0xc070
movb $0x0, %al
callq 0x10e0
jmp 0x55a4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x61a4(%rip), %rsi      # 0xb6a8
movb $0x0, %al
callq 0x10e0
jmp 0x55a4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x6190(%rip), %rsi      # 0xb6b3
movb $0x0, %al
callq 0x10e0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rdx
leaq 0x617e(%rip), %rsi      # 0xb6be
movb $0x0, %al
callq 0x10e0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdx
leaq 0x616f(%rip), %rsi      # 0xb6cc
movb $0x0, %al
callq 0x10e0
jmp 0x55a4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdx
leaq 0x615d(%rip), %rsi      # 0xb6d9
movb $0x0, %al
callq 0x10e0
jmp 0x55a4
leaq 0x63da(%rip), %rdi      # 0xb966
leaq 0x605d(%rip), %rsi      # 0xb5f0
movl $0x2a2, %edx            # imm = 0x2A2
leaq 0x60e3(%rip), %rcx      # 0xb682
callq 0x10b0
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)