// File: /workspace/github_cpp_makefile_star2_O3/Davidc2525[P]Leaf/compiler/backend/x86_64_linux/generator.cpp
// Function: x86_64_linux_generator_visitor::visit(LEAF::AST::FuncDeclStatementNode*)
void x86_64_linux_generator_visitor::visit(FuncDeclStatementNode *func)
{
    UPDATE_STATICS
    Section *st = assembly->get_program()->section(Sections::text);

    //label of name function
    st->add(new Label((char *)func->name->c_str()));

    //preamble
    DInstruccion *push_rbp = new DInstruccion("push");
    push_rbp->add_operand(new RegisterOperand("rbp"));
    st->add(push_rbp);
    DInstruccion *mov_to_rbp = new DInstruccion("mov");
    mov_to_rbp->add_operand(new RegisterOperand("rbp"));
    mov_to_rbp->add_operand(new RegisterOperand("rsp"));
    st->add(mov_to_rbp);
    DInstruccion *sub_rsp = new DInstruccion("sub");
    sub_rsp->add_operand(new RegisterOperand("rsp"));
    sub_rsp->add_operand(new InmediateIntOperand(func->obj->type->size));
    st->add(sub_rsp);

    //body function
    vector<ASTU>::iterator x = func->nodos.begin();
    for (; x != func->nodos.end(); x++)
    {
        (*x)->accept(this);
    }

    Label *ret_label = new Label((char *)rand_label::newLabel(10)->c_str());
    st->add(ret_label);

    //epilogo
    DInstruccion *restore_rbp = new DInstruccion("mov");
    restore_rbp->add_operand(new RegisterOperand("rsp"));
    restore_rbp->add_operand(new RegisterOperand("rbp"));
    st->add(restore_rbp);

    DInstruccion *pop_rbp = new DInstruccion("pop");
    pop_rbp->add_operand(new RegisterOperand("rbp"));
    st->add(pop_rbp);

    DInstruccion *ret = new DInstruccion("ret");
    st->add(ret);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, %r14
incl 0x8(%rdi)
movq %rsi, %r15
movq 0x10(%rdi), %rax
movq (%rax), %rdi
movl $0x2, %esi
callq 0x10346
movq %rax, %rbx
movl $0x10, %edi
callq 0x5290
movq %rax, %r12
movq 0x40(%r15), %rax
movq (%rax), %rsi
movq %r12, %rdi
callq 0xf844
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf294
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x5184(%rip), %rsi      # 0x1b3df
movq %rax, %rdi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %r13
leaq 0x4d03(%rip), %rsi      # 0x1af7a
movq %rax, %rdi
callq 0xfe82
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf294
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x5139(%rip), %rsi      # 0x1b3e4
movq %rax, %rdi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %r13
leaq 0x4cb3(%rip), %rsi      # 0x1af7a
movq %rax, %rdi
callq 0xfe82
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x20(%rax)
movl $0x10, %edi
callq 0x5290
movq %rax, %r13
leaq 0x4e5f(%rip), %rsi      # 0x1b14f
movq %rax, %rdi
callq 0xfe82
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf294
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x50c4(%rip), %rsi      # 0x1b3e8
movq %rax, %rdi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %r13
leaq 0x4e0f(%rip), %rsi      # 0x1b14f
movq %rax, %rdi
callq 0xfe82
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x20(%rax)
movl $0x10, %edi
callq 0x5290
movq %rax, %r13
movq 0x8(%r15), %rax
movq 0x18(%rax), %rax
movl 0xc(%rax), %esi
movq %r13, %rdi
callq 0xfe48
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf294
movq 0x28(%r15), %r12
cmpq 0x30(%r15), %r12
je 0x163a9
movq (%r12), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *(%rax)
addq $0x8, %r12
jmp 0x16391
movl $0x10, %edi
callq 0x5290
movq %rax, %r14
movl $0xa, %edi
callq 0x836e
movq (%rax), %rsi
movq %r14, %rdi
callq 0xf844
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf294
movl $0x28, %edi
callq 0x5290
movq %rax, %r14
leaq 0x4ffa(%rip), %rsi      # 0x1b3e4
movq %rax, %rdi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
leaq 0x4d49(%rip), %rsi      # 0x1b14f
movq %rax, %rdi
callq 0xfe82
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
leaq 0x4b4c(%rip), %rsi      # 0x1af7a
movq %rax, %rdi
callq 0xfe82
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf294
movl $0x28, %edi
callq 0x5290
movq %rax, %r14
leaq 0x4f8b(%rip), %rsi      # 0x1b3ec
movq %rax, %rdi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
leaq 0x4afd(%rip), %rsi      # 0x1af7a
movq %rax, %rdi
callq 0xfe82
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf294
movl $0x28, %edi
callq 0x5290
movq %rax, %r14
leaq 0x4f40(%rip), %rsi      # 0x1b3f0
movq %rax, %rdi
callq 0xfc44
movq %rbx, %rdi
movq %r14, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xf294
jmp 0x164e1
jmp 0x164d4
jmp 0x164e1
jmp 0x164d4
movq %rax, %rbx
movl $0x10, %esi
movq %r15, %rdi
jmp 0x16526
movq %rax, %rbx
movl $0x28, %esi
jmp 0x16523
jmp 0x164f7
jmp 0x164f7
jmp 0x16504
jmp 0x164f7
jmp 0x164f7
jmp 0x16504
movq %rax, %rbx
movl $0x10, %esi
movq %r13, %rdi
jmp 0x16526
movq %rax, %rbx
movl $0x28, %esi
jmp 0x16516
movq %rax, %rbx
movl $0x10, %esi
movq %r12, %rdi
jmp 0x16526
movq %rax, %rbx
movl $0x10, %esi
movq %r14, %rdi
callq 0x52a0
movq %rbx, %rdi
callq 0x5470
nop