// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x2
popq %rsi
callq 0xe61c
movq %rax, %rbx
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %r12
movq 0x40(%r15), %rax
movq (%rax), %rsi
movq %r12, %rdi
callq 0xdc40
movq %rbx, %rdi
movq %r12, %rsi
callq 0xd750
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r12
leaq 0x4a48(%rip), %rsi      # 0x183ff
movq %rax, %rdi
callq 0xdf60
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %r13
leaq 0x45c9(%rip), %rsi      # 0x17f9a
movq %rax, %rdi
callq 0xe1b2
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xd750
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r12
leaq 0x4a01(%rip), %rsi      # 0x18404
movq %rax, %rdi
callq 0xdf60
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %r13
leaq 0x457d(%rip), %rsi      # 0x17f9a
movq %rax, %rdi
callq 0xe1b2
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x20(%rax)
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %r13
leaq 0x472b(%rip), %rsi      # 0x1816f
movq %rax, %rdi
callq 0xe1b2
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xd750
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r12
leaq 0x4992(%rip), %rsi      # 0x18408
movq %rax, %rdi
callq 0xdf60
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %r13
leaq 0x46df(%rip), %rsi      # 0x1816f
movq %rax, %rdi
callq 0xe1b2
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x20(%rax)
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %r13
movq 0x8(%r15), %rax
movq 0x18(%rax), %rax
movl 0xc(%rax), %esi
movq %r13, %rdi
callq 0xe17c
movq (%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xd750
movq 0x28(%r15), %r12
cmpq 0x30(%r15), %r12
je 0x13af7
movq (%r12), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *(%rax)
addq $0x8, %r12
jmp 0x13adf
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %r14
pushq $0xa
popq %rdi
callq 0x7864
movq (%rax), %rsi
movq %r14, %rdi
callq 0xdc40
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd750
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r14
leaq 0x48d2(%rip), %rsi      # 0x18404
movq %rax, %rdi
callq 0xdf60
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %r15
leaq 0x4623(%rip), %rsi      # 0x1816f
movq %rax, %rdi
callq 0xe1b2
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x20(%rax)
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %r15
leaq 0x4428(%rip), %rsi      # 0x17f9a
movq %rax, %rdi
callq 0xe1b2
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd750
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r14
leaq 0x4869(%rip), %rsi      # 0x1840c
movq %rax, %rdi
callq 0xdf60
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %r15
leaq 0x43dd(%rip), %rsi      # 0x17f9a
movq %rax, %rdi
callq 0xe1b2
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xd750
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r14
leaq 0x4822(%rip), %rsi      # 0x18410
movq %rax, %rdi
callq 0xdf60
movq %rbx, %rdi
movq %r14, %rsi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xd750
jmp 0x13c1d
jmp 0x13c12
jmp 0x13c1d
jmp 0x13c12
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r15, %rdi
jmp 0x13c56
movq %rax, %rbx
pushq $0x28
jmp 0x13c52
jmp 0x13c30
jmp 0x13c30
jmp 0x13c3b
jmp 0x13c30
jmp 0x13c30
jmp 0x13c3b
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r13, %rdi
jmp 0x13c56
movq %rax, %rbx
pushq $0x28
jmp 0x13c47
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r12, %rdi
jmp 0x13c56
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r14, %rdi
callq 0x5270
movq %rbx, %rdi
callq 0x5420
nop