// File: zerolfx[P]P-Machine/retp.cpp
// Function: Retp::execute(StackMachine*)
void Retp::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 retp: stackframe has been compromised.");
	}
	
	int oldSP = stack->fSP;
	
	// proper procedure with no results
	stack->fSP = stack->fMP - 1;
	
	// return branch
	stack->fPC = stack->fStore[stack->fMP + 4]->getValue();
	
	// restore EP
	stack->fEP = stack->fStore[stack->fMP + 3]->getValue();

	// dynamic link
	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("retp");

}

// 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 0x1f782
callq 0x7300
jmp 0x1f782
movq -0x90(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x2b1c1(%rip), %rdi     # 0x4a958
callq 0x9370
testb $0x1, %al
jne 0x1f84d
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 0x1f7d8
callq 0x7300
jmp 0x1f7d8
movq -0x98(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x2b16b(%rip), %rdi     # 0x4a958
callq 0x9370
testb $0x1, %al
jne 0x1f84d
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 0x1f82a
callq 0x7300
jmp 0x1f82a
movq -0xa0(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x2b119(%rip), %rdi     # 0x4a958
callq 0x9370
testb $0x1, %al
jne 0x1f84d
jmp 0x1f916
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 0x19ee7(%rip), %rsi     # 0x39767
leaq -0x50(%rbp), %rdi
callq 0x8940
jmp 0x1f88b
movq -0xb0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x283d0
jmp 0x1f89d
movq -0xb0(%rbp), %rdi
movb $0x0, -0x52(%rbp)
leaq 0x2a1d1(%rip), %rsi     # 0x49a80
leaq 0x8b4a(%rip), %rdx      # 0x28400
callq 0x74a0
jmp 0x1fb15
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x1fb03
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x1f8f4
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 0x1f905
jmp 0x1f911
movq -0xb0(%rbp), %rdi
callq 0x71c0
jmp 0x1fb03
movq -0x10(%rbp), %rax
movl 0xb4(%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movl 0xc0(%rax), %ecx
decl %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 0x1f965
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 0x1f9a5
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 0x1f9e5
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 0x1fa60
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movslq -0x5c(%rbp), %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, %rax
je 0x1fa48
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 0x1f9fb
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 0x19d0a(%rip), %rsi     # 0x3979a
leaq -0x80(%rbp), %rdi
callq 0x8940
jmp 0x1fa9b
movq -0xd8(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x2c4d0
jmp 0x1faad
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 0x1faf7
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)