// File: 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 %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x98(%rbp)
movl 0x8(%rax), %ecx
incl %ecx
movl %ecx, 0x8(%rax)
movq 0x10(%rax), %rdi
callq 0xb010
movq %rax, %rdi
movl $0x2, %esi
callq 0x1c3c0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x90(%rbp)
movl $0x10, %edi
callq 0x62e0
movq %rax, -0x80(%rbp)
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x6120
movq -0x80(%rbp), %rdi
movq %rax, %rsi
callq 0x19b10
jmp 0x2e5ab
movq -0x88(%rbp), %rsi
movq -0x90(%rbp), %rdi
callq 0x18670
movl $0x28, %edi
callq 0x62e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xa0(%rbp)
leaq 0x7ba8(%rip), %rsi      # 0x36184
callq 0x1a4e0
jmp 0x2e5e3
movq -0xa0(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl $0x10, %edi
callq 0x62e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xa8(%rbp)
leaq 0x7723(%rip), %rsi      # 0x35d3a
callq 0x1b1e0
jmp 0x2e61e
movq -0xa8(%rbp), %rsi
movq -0xb0(%rbp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x18670
movl $0x28, %edi
callq 0x62e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xb8(%rbp)
leaq 0x7b29(%rip), %rsi      # 0x36189
callq 0x1a4e0
jmp 0x2e667
movq -0xb8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xc8(%rbp)
movl $0x10, %edi
callq 0x62e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xc0(%rbp)
leaq 0x769f(%rip), %rsi      # 0x35d3a
callq 0x1b1e0
jmp 0x2e6a2
movq -0xc0(%rbp), %rsi
movq -0xc8(%rbp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
movq -0x38(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl $0x10, %edi
callq 0x62e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xd0(%rbp)
leaq 0x782d(%rip), %rsi      # 0x35f0f
callq 0x1b1e0
jmp 0x2e6e9
movq -0xd0(%rbp), %rsi
movq -0xd8(%rbp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x18670
movl $0x28, %edi
callq 0x62e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xe0(%rbp)
leaq 0x7a62(%rip), %rsi      # 0x3618d
callq 0x1a4e0
jmp 0x2e732
movq -0xe0(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xf0(%rbp)
movl $0x10, %edi
callq 0x62e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xe8(%rbp)
leaq 0x77a9(%rip), %rsi      # 0x35f0f
callq 0x1b1e0
jmp 0x2e76d
movq -0xe8(%rbp), %rsi
movq -0xf0(%rbp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
movq -0x40(%rbp), %rax
movq %rax, -0x100(%rbp)
movl $0x10, %edi
callq 0x62e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xf8(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movl 0xc(%rax), %esi
callq 0x1b0f0
jmp 0x2e7bc
movq -0xf8(%rbp), %rsi
movq -0x100(%rbp), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x18670
movq -0x10(%rbp), %rdi
addq $0x28, %rdi
callq 0xdd70
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
addq $0x28, %rdi
callq 0xdc00
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x31da0
testb $0x1, %al
jne 0x2e815
jmp 0x2e970
leaq -0x48(%rbp), %rdi
callq 0x31de0
movq -0x98(%rbp), %rsi
movq (%rax), %rdi
movq (%rdi), %rax
callq *(%rax)
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x31e00
movq %rax, -0x58(%rbp)
jmp 0x2e7ee
movq -0x88(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x10, %esi
callq 0x62f0
jmp 0x2ec3b
movq -0xa0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x28, %esi
callq 0x62f0
jmp 0x2ec3b
movq -0xa8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x10, %esi
callq 0x62f0
jmp 0x2ec3b
movq -0xb8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x28, %esi
callq 0x62f0
jmp 0x2ec3b
movq -0xc0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x10, %esi
callq 0x62f0
jmp 0x2ec3b
movq -0xd0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x10, %esi
callq 0x62f0
jmp 0x2ec3b
movq -0xe0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x28, %esi
callq 0x62f0
jmp 0x2ec3b
movq -0xe8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x10, %esi
callq 0x62f0
jmp 0x2ec3b
movq -0xf8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x10, %esi
callq 0x62f0
jmp 0x2ec3b
movl $0x10, %edi
callq 0x62e0
movq %rax, -0x110(%rbp)
movl $0xa, %edi
callq 0xc8e0
movq %rax, -0x108(%rbp)
jmp 0x2e994
movq -0x108(%rbp), %rdi
callq 0x6120
movq -0x110(%rbp), %rdi
movq %rax, %rsi
callq 0x19b10
jmp 0x2e9b1
movq -0x110(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x18670
movl $0x28, %edi
callq 0x62e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x118(%rbp)
leaq 0x77a2(%rip), %rsi      # 0x36189
callq 0x1a4e0
jmp 0x2e9ee
movq -0x118(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x128(%rbp)
movl $0x10, %edi
callq 0x62e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x120(%rbp)
leaq 0x74ed(%rip), %rsi      # 0x35f0f
callq 0x1b1e0
jmp 0x2ea29
movq -0x120(%rbp), %rsi
movq -0x128(%rbp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
movq -0x68(%rbp), %rax
movq %rax, -0x138(%rbp)
movl $0x10, %edi
callq 0x62e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x130(%rbp)
leaq 0x72d1(%rip), %rsi      # 0x35d3a
callq 0x1b1e0
jmp 0x2ea70
movq -0x130(%rbp), %rsi
movq -0x138(%rbp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
movq -0x18(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x18670
movl $0x28, %edi
callq 0x62e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x140(%rbp)
leaq 0x76df(%rip), %rsi      # 0x36191
callq 0x1a4e0
jmp 0x2eab9
movq -0x140(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x150(%rbp)
movl $0x10, %edi
callq 0x62e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x148(%rbp)
leaq 0x724d(%rip), %rsi      # 0x35d3a
callq 0x1b1e0
jmp 0x2eaf4
movq -0x148(%rbp), %rsi
movq -0x150(%rbp), %rdi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
movq -0x18(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x18670
movl $0x28, %edi
callq 0x62e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x158(%rbp)
leaq 0x765f(%rip), %rsi      # 0x36195
callq 0x1a4e0
jmp 0x2eb3d
movq -0x158(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x18670
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq -0x110(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x10, %esi
callq 0x62f0
jmp 0x2ec3b
movq -0x118(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x28, %esi
callq 0x62f0
jmp 0x2ec3b
movq -0x120(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x10, %esi
callq 0x62f0
jmp 0x2ec3b
movq -0x130(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x10, %esi
callq 0x62f0
jmp 0x2ec3b
movq -0x140(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x28, %esi
callq 0x62f0
jmp 0x2ec3b
movq -0x148(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x10, %esi
callq 0x62f0
jmp 0x2ec3b
movq -0x158(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x28, %esi
callq 0x62f0
movq -0x20(%rbp), %rdi
callq 0x64f0
nopw %cs:(%rax,%rax)