// File: /workspace/github_cpp_makefile_star2_O1/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 0x102f0
movq %rax, %rbx
movl $0x10, %edi
callq 0x5290
movq %rax, %r12
movq 0x40(%r15), %rax
movq (%rax), %rsi
movq %r12, %rdi
callq 0xf81c
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf258
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x4f0c(%rip), %rsi      # 0x1b37f
movq %rax, %rdi
callq 0xfc18
movl $0x10, %edi
callq 0x5290
movq %rax, %r13
leaq 0x4a8b(%rip), %rsi      # 0x1af1a
movq %rax, %rdi
callq 0xfe4e
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf258
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x4ec1(%rip), %rsi      # 0x1b384
movq %rax, %rdi
callq 0xfc18
movl $0x10, %edi
callq 0x5290
movq %rax, %r13
leaq 0x4a3b(%rip), %rsi      # 0x1af1a
movq %rax, %rdi
callq 0xfe4e
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x20(%rax)
movl $0x10, %edi
callq 0x5290
movq %rax, %r13
leaq 0x4be7(%rip), %rsi      # 0x1b0ef
movq %rax, %rdi
callq 0xfe4e
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf258
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x4e4c(%rip), %rsi      # 0x1b388
movq %rax, %rdi
callq 0xfc18
movl $0x10, %edi
callq 0x5290
movq %rax, %r13
leaq 0x4b97(%rip), %rsi      # 0x1b0ef
movq %rax, %rdi
callq 0xfe4e
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 0xfe14
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf258
movq 0x28(%r15), %r12
cmpq 0x30(%r15), %r12
je 0x165c1
movq (%r12), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *(%rax)
addq $0x8, %r12
jmp 0x165a9
movl $0x10, %edi
callq 0x5290
movq %rax, %r14
movl $0xa, %edi
callq 0x8392
movq (%rax), %rsi
movq %r14, %rdi
callq 0xf81c
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf258
movl $0x28, %edi
callq 0x5290
movq %rax, %r14
leaq 0x4d82(%rip), %rsi      # 0x1b384
movq %rax, %rdi
callq 0xfc18
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
leaq 0x4ad1(%rip), %rsi      # 0x1b0ef
movq %rax, %rdi
callq 0xfe4e
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
leaq 0x48d4(%rip), %rsi      # 0x1af1a
movq %rax, %rdi
callq 0xfe4e
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf258
movl $0x28, %edi
callq 0x5290
movq %rax, %r14
leaq 0x4d13(%rip), %rsi      # 0x1b38c
movq %rax, %rdi
callq 0xfc18
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
leaq 0x4885(%rip), %rsi      # 0x1af1a
movq %rax, %rdi
callq 0xfe4e
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf258
movl $0x28, %edi
callq 0x5290
movq %rax, %r14
leaq 0x4cc8(%rip), %rsi      # 0x1b390
movq %rax, %rdi
callq 0xfc18
movq %rbx, %rdi
movq %r14, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xf258
jmp 0x166f9
jmp 0x166ec
jmp 0x166f9
jmp 0x166ec
movq %rax, %rbx
movl $0x10, %esi
movq %r15, %rdi
jmp 0x1673e
movq %rax, %rbx
movl $0x28, %esi
jmp 0x1673b
jmp 0x1670f
jmp 0x1670f
jmp 0x1671c
jmp 0x1670f
jmp 0x1670f
jmp 0x1671c
movq %rax, %rbx
movl $0x10, %esi
movq %r13, %rdi
jmp 0x1673e
movq %rax, %rbx
movl $0x28, %esi
jmp 0x1672e
movq %rax, %rbx
movl $0x10, %esi
movq %r12, %rdi
jmp 0x1673e
movq %rax, %rbx
movl $0x10, %esi
movq %r14, %rdi
callq 0x52a0
movq %rbx, %rdi
callq 0x5470
nop