// File: zerolfx[P]P-Machine/retf.cpp
// Function: Retf::execute(StackMachine*)
void Retf::execute(StackMachine *stack)
{	
	StackAddress p;
	if((typeid(p) != typeid(*(stack->fStore[stack->fMP + 2]))
		|| (typeid(p) != typeid(*(stack->fStore[stack->fMP + 3])))
		|| (typeid(p) != typeid(*(stack->fStore[stack->fMP + 4])))))
	{
		throw ExecutionError("instruction retf: stackframe has been compromised.");
	}
	
	int oldSP = stack->fSP;
	
	// function result in the local stack
	stack->fSP = stack->fMP;
	
	// return branch
	stack->fPC = stack->fStore[stack->fMP + 4]->getValue();
	
	// restore EP
	stack->fEP = stack->fStore[stack->fMP + 3]->getValue();
	
	// restore MP
	stack->fMP = stack->fStore[stack->fMP + 2]->getValue();
	
	for(int i = oldSP; i > stack->fSP; --i)
	{
		delete stack->fStore[i];
		stack->fStore.pop_back();
	}
	
	// adding cost of this instruction
	stack->fTime.count("retf");

	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x20(%rbp), %rdi
xorl %esi, %esi
callq 0x26800
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movl 0xc0(%rax), %eax
addl $0x2, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, %rax
jne 0x1f2d2
callq 0x7300
jmp 0x1f2d2
movq -0x90(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x2b671(%rip), %rdi     # 0x4a958
callq 0x9370
testb $0x1, %al
jne 0x1f39d
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movl 0xc0(%rax), %eax
addl $0x3, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
jne 0x1f328
callq 0x7300
jmp 0x1f328
movq -0x98(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x2b61b(%rip), %rdi     # 0x4a958
callq 0x9370
testb $0x1, %al
jne 0x1f39d
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movl 0xc0(%rax), %eax
addl $0x4, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
cmpq $0x0, %rax
jne 0x1f37a
callq 0x7300
jmp 0x1f37a
movq -0xa0(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x2b5c9(%rip), %rdi     # 0x4a958
callq 0x9370
testb $0x1, %al
jne 0x1f39d
jmp 0x1f466
movb $0x1, -0x52(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0xb0(%rbp)
leaq -0x51(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x74f0
movq -0xa8(%rbp), %rdx
leaq 0x1a359(%rip), %rsi     # 0x39729
leaq -0x50(%rbp), %rdi
callq 0x8940
jmp 0x1f3db
movq -0xb0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x283d0
jmp 0x1f3ed
movq -0xb0(%rbp), %rdi
movb $0x0, -0x52(%rbp)
leaq 0x2a681(%rip), %rsi     # 0x49a80
leaq 0x8ffa(%rip), %rdx      # 0x28400
callq 0x74a0
jmp 0x1f663
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x1f651
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x1f444
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x7170
leaq -0x51(%rbp), %rdi
callq 0x72e0
testb $0x1, -0x52(%rbp)
jne 0x1f455
jmp 0x1f461
movq -0xb0(%rbp), %rdi
callq 0x71c0
jmp 0x1f651
movq -0x10(%rbp), %rax
movl 0xb4(%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movl 0xc0(%rax), %ecx
movl %ecx, 0xb4(%rax)
movq -0x10(%rbp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movl 0xc0(%rax), %eax
addl $0x4, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x68(%rax), %rax
callq *%rax
movl %eax, -0xb4(%rbp)
jmp 0x1f4b3
movl -0xb4(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xb0(%rax)
movq -0x10(%rbp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movl 0xc0(%rax), %eax
addl $0x3, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x68(%rax), %rax
callq *%rax
movl %eax, -0xb8(%rbp)
jmp 0x1f4f3
movl -0xb8(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xbc(%rax)
movq -0x10(%rbp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movl 0xc0(%rax), %eax
addl $0x2, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x68(%rax), %rax
callq *%rax
movl %eax, -0xbc(%rbp)
jmp 0x1f533
movl -0xbc(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc0(%rax)
movl -0x58(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xb4(%rcx), %eax
jle 0x1f5ae
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movslq -0x5c(%rbp), %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, %rax
je 0x1f596
movq -0xc8(%rbp), %rdi
callq 0x26ca0
movq -0xc8(%rbp), %rdi
movl $0x8, %esi
callq 0x7280
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x8e90
movl -0x5c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x1f549
movq -0x10(%rbp), %rax
addq $0x78, %rax
movq %rax, -0xd8(%rbp)
leaq -0x81(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x74f0
movq -0xd0(%rbp), %rdx
leaq 0x1a17e(%rip), %rsi     # 0x3975c
leaq -0x80(%rbp), %rdi
callq 0x8940
jmp 0x1f5e9
movq -0xd8(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x2c4d0
jmp 0x1f5fb
leaq -0x80(%rbp), %rdi
callq 0x7170
leaq -0x81(%rbp), %rdi
callq 0x72e0
leaq -0x20(%rbp), %rdi
callq 0x26840
addq $0xe0, %rsp
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x1f645
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x7170
leaq -0x81(%rbp), %rdi
callq 0x72e0
leaq -0x20(%rbp), %rdi
callq 0x26840
movq -0x28(%rbp), %rdi
callq 0x74e0
nopw %cs:(%rax,%rax)