// File: /workspace/github_c_makefile_star2_O0/yutopp[P]cc/asm_x86_64.c
// Function: built_from_ir_global_inst
void built_from_ir_global_inst(ASM_X86_64 *a, IRInst* inst) {
    assert(inst->kind == IR_INST_KIND_LET);
    IRSymbolID var_id = inst->value.let.id;
    IRInstValue* let_rhs = &inst->value.let.rhs;

    switch(let_rhs->kind) {
    case IR_INST_VALUE_KIND_SYMBOL:
    {
        // Save a symbol
        ASM_X86_64_Value value = {
            .kind = ASM_X86_64_VALUE_KIND_SYMBOL,
            .value = {
                .symbol = let_rhs->value.symbol,
            },
        };
        asm_x86_64_set_val(a, var_id, value, 1);

        fprintf(DEBUGOUT, "SYMBOL %ld = ", var_id);
        fprint_value(DEBUGOUT, &value);
        fprintf(DEBUGOUT, "\n");

        break;
    }

    case IR_INST_VALUE_KIND_STRING:
    {
        char* label_id = (char*)malloc(sizeof(char) * 10);
        snprintf(label_id, 10, ".S%07ld", a->string_label_count);
        a->string_label_count++;

        {
            // .section .rodata
            ASM_X86_64_Inst* inst = (ASM_X86_64_Inst*)vector_append(a->insts);
            inst->kind = ASM_X86_64_INST_KIND_SECTION;
            inst->value.section.name = ".rodata";
        }

        {
            // .label 'label_id'
            ASM_X86_64_Inst* inst = (ASM_X86_64_Inst*)vector_append(a->insts);
            inst->kind = ASM_X86_64_INST_KIND_LABEL;
            inst->value.label.name = label_id;
            inst->value.label.generated = 1;
        }

        {
            // .string 'string'
            ASM_X86_64_Inst* inst = (ASM_X86_64_Inst*)vector_append(a->insts);
            inst->kind = ASM_X86_64_INST_KIND_STRING;
            inst->value.string.s = let_rhs->value.string;
        }

        ASM_X86_64_Value str = {
            .kind = ASM_X86_64_VALUE_KIND_STRING,
            .value = {
                .string = {
                    .label = label_id,
                },
            },
        };

        asm_x86_64_set_val(a, var_id, str, 1);

        fprintf(DEBUGOUT, "STRING %ld = ", var_id);
        fprint_value(DEBUGOUT, &str);
        fprintf(DEBUGOUT, "\n");

        break;
    }

    default:
        fprintf(stderr, "Unexpected kind: %d\n", let_rhs->kind);
        assert(0); // TODO: error handling
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x7ade
jmp 0x7afd
leaq 0x41e0(%rip), %rdi      # 0xbcc5
leaq 0x40f9(%rip), %rsi      # 0xbbe5
movl $0x373, %edx            # imm = 0x373
leaq 0x41ec(%rip), %rcx      # 0xbce4
callq 0x10b0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x74(%rbp)
testl %eax, %eax
je 0x7b35
jmp 0x7b24
movl -0x74(%rbp), %eax
subl $0x1, %eax
je 0x7bc3
jmp 0x7d0b
movl $0x0, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x38(%rbp), %rax
movl $0x1, %edx
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x7fe0
movq 0x745a(%rip), %rax      # 0xefd8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0x418f(%rip), %rsi      # 0xbd1b
movb $0x0, %al
callq 0x10e0
movq 0x743e(%rip), %rax      # 0xefd8
movq (%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x7830
movq 0x742b(%rip), %rax      # 0xefd8
movq (%rax), %rdi
leaq 0x44b9(%rip), %rsi      # 0xc070
movb $0x0, %al
callq 0x10e0
jmp 0x7d48
movl $0xa, %edi
callq 0x1120
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rcx
movl $0xa, %esi
leaq 0x4140(%rip), %rdx      # 0xbd29
movb $0x0, %al
callq 0x10a0
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x4480
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl $0x0, (%rax)
movq -0x48(%rbp), %rax
leaq 0x410c(%rip), %rcx      # 0xbd31
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x4480
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl $0x5, (%rax)
movq -0x40(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x4480
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movl $0x1, (%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x2, -0x70(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x70(%rbp), %rax
movl $0x1, %edx
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x7fe0
movq 0x730f(%rip), %rax      # 0xefd8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0x4062(%rip), %rsi      # 0xbd39
movb $0x0, %al
callq 0x10e0
movq 0x72f3(%rip), %rax      # 0xefd8
movq (%rax), %rdi
leaq -0x70(%rbp), %rsi
callq 0x7830
movq 0x72e0(%rip), %rax      # 0xefd8
movq (%rax), %rdi
leaq 0x436e(%rip), %rsi      # 0xc070
movb $0x0, %al
callq 0x10e0
jmp 0x7d48
movq 0x72e6(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x20(%rbp), %rax
movl (%rax), %edx
leaq 0x3f61(%rip), %rsi      # 0xbc83
movb $0x0, %al
callq 0x10e0
leaq 0x3c36(%rip), %rdi      # 0xb966
leaq 0x3eae(%rip), %rsi      # 0xbbe5
movl $0x3ba, %edx            # imm = 0x3BA
leaq 0x3fa1(%rip), %rcx      # 0xbce4
callq 0x10b0
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)