// File: /workspace/github_cpp_makefile_star2_O1/marxn[P]pupp/src/parser/function.cpp
// Function: FunctionNode::Provision()
bool FunctionNode::Provision()
{
        list<FuncArgDef*>::iterator i;
        for(i = this->ArgList->begin(); i!=this->ArgList->end(); i++)
        {
                string argname = (*i)->GetName();
                DataType argtype = (*i)->GetType();
                VariableDef * localvar = new VariableDef(argname);
                localvar->SetVarType(argtype);
                this->AddVariable(localvar);
                
                unsigned long varindex = localvar->GetVarIndex();
                (*i)->SetArgIndex(varindex);
        }

        Node * parent = this->GetParentNode();
        if(parent==NULL)
        {
                cerr<<"pupp provision error: A function cannot be defined without a context."<<endl;
                return false;
        }

        list<ClosureVarDesc*>::iterator j;

        if(this->CopyList!=NULL)
        {
                int pos = 0;
                
                for(j = this->CopyList->begin(); j!= this->CopyList->end(); j++)
                {
                        //Because its parent is NOT a Node but a lambda exp, the layer has to be set to 0 to skip the exp.
                        //This is extremely unusual.
                        unsigned long layer = 0; 
                        
                        string varname = (*j)->GetVarName();
                        VariableDef * vardef = parent->FindVariable(varname, &layer);
                        if(vardef==NULL)
                        {
                                cerr<<"pupp provision error: Variable "<<varname<<" has not been defined"<<endl;
                                return false;
                        }
                        
                        VariableDef * newvardef = new VariableDef(varname);
                        newvardef->SetVarType(vardef->GetVarType());
                        newvardef->SetNeedInstance(false);

                        this->AddVariable(newvardef);
                        
                        (*j)->SetSrcLayer(layer);
                        (*j)->SetSrcDef(vardef);
                        (*j)->SetDstDef(newvardef);
                        
                        if(pos++==0)
                        {
                                this->ClosureVarStartIndex = newvardef->GetVarIndex();
                        }
                        
                }
        }
        
        return ContainerNode::Provision();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
movq 0x98(%rdi), %rax
movq (%rax), %r14
cmpq %rax, %r14
je 0xbf72
leaq 0x10(%rsp), %r12
leaq 0x70(%rsp), %r15
movq 0x10(%r14), %rax
movq %r12, %r13
movq %r12, (%rsp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0x99e8
movq 0x10(%r14), %rax
movl 0x20(%rax), %ebp
movl $0x40, %edi
callq 0x6260
movq %rax, %r12
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x99e8
movq %r12, %rdi
movq %r15, %rsi
callq 0x18ba2
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0xbf24
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x6270
movq %r12, %rdi
movl %ebp, %esi
callq 0x18cb8
movq %rbx, %rdi
movq %r12, %rsi
callq 0xc3f8
movq %r12, %rdi
callq 0x18cf4
movq 0x10(%r14), %rcx
movq %rax, 0x30(%rcx)
movq (%rsp), %rdi
movq %r13, %r12
cmpq %r13, %rdi
je 0xbf62
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x6270
movq (%r14), %r14
cmpq 0x98(%rbx), %r14
jne 0xbea5
movq %rbx, %rdi
callq 0xc3e8
movq %rax, 0x90(%rsp)
testq %rax, %rax
je 0xc19d
movq 0xa0(%rbx), %rbp
testq %rbp, %rbp
je 0xc193
leaq 0x10(%rsp), %r14
movq $0x0, 0x20(%rsp)
leaq 0x50(%rsp), %r12
movq (%rbp), %rbp
cmpq 0xa0(%rbx), %rbp
je 0xc193
movq $0x0, 0x28(%rsp)
movq 0x10(%rbp), %rax
movq %r14, (%rsp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0x99e8
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
leaq 0x30(%rsp), %rdi
callq 0x99e8
movq 0x90(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0xc57c
movq %rax, %r15
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0xc038
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x6270
testq %r15, %r15
je 0xc101
movl $0x40, %edi
callq 0x6260
movq %rax, %r13
leaq 0x60(%rsp), %rax
movq %rax, 0x50(%rsp)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
movq %r12, %r14
movq %r12, %rdi
callq 0x99e8
movq %r13, %rdi
movq %r14, %rsi
callq 0x18ba2
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0xc096
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x6270
movq %r15, %rdi
callq 0x18cbc
movq %r13, %rdi
movl %eax, %esi
callq 0x18cb8
movq %r13, %rdi
xorl %esi, %esi
callq 0x18cfe
movq %rbx, %rdi
movq %r13, %rsi
callq 0xc3f8
movq 0x10(%rbp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x38(%rax)
movq %r15, 0x28(%rax)
movq 0x10(%rbp), %rax
movq %r13, 0x30(%rax)
movq 0x20(%rsp), %rax
leal 0x1(%rax), %r14d
testl %eax, %eax
jne 0xc0f2
movq %r13, %rdi
callq 0x18cf4
movq %rax, 0xa8(%rbx)
movl %r14d, %eax
movq %rax, 0x20(%rsp)
leaq 0x10(%rsp), %r14
jmp 0xc172
movl $0x1f, %edx
movq 0x3bee3(%rip), %rdi     # 0x47ff0
leaq 0x2a0df(%rip), %rsi     # 0x361f3
leaq 0x10(%rsp), %r14
callq 0x62f0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x3bec2(%rip), %rdi     # 0x47ff0
callq 0x62f0
movq %rax, %r13
movl $0x15, %edx
movq %rax, %rdi
leaq 0x2a0ce(%rip), %rsi     # 0x36213
callq 0x62f0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x6220
movsbl %al, %esi
movq %r13, %rdi
callq 0x6040
movq %rax, %rdi
callq 0x61b0
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0xc188
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x6270
testq %r15, %r15
jne 0xbfae
jmp 0xc1df
movq %rbx, %rdi
callq 0x9f4a
jmp 0xc1e1
movq 0x3be4c(%rip), %rbx     # 0x47ff0
leaq 0x2a435(%rip), %rsi     # 0x365e0
movl $0x45, %edx
movq %rbx, %rdi
callq 0x62f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x6220
movsbl %al, %esi
movq %rbx, %rdi
callq 0x6040
movq %rax, %rdi
callq 0x61b0
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0xc217
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x6270
jmp 0xc217
movq %rax, %r14
movl $0x40, %esi
movq %r13, %rdi
jmp 0xc23b
movq %rax, %r14
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0xc282
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x6270
jmp 0xc282
jmp 0xc27f
movq %rax, %r14
jmp 0xc26e
movq %rax, %r14
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0xc26e
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x6270
movl $0x40, %esi
movq %r12, %rdi
callq 0x6270
jmp 0xc295
jmp 0xc292
movq %rax, %r14
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
jne 0xc29e
jmp 0xc2ab
movq %rax, %r14
movq (%rsp), %rdi
cmpq %r13, %rdi
je 0xc2ab
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x6270
movq %r14, %rdi
callq 0x6470
nop