int main(int argc, char **argv) {
  register char *const basepointer __asm("rbp");
  register char *const stackpointer __asm("rsp");
  int ret1;
  int x = 3;
  int y = 5;
  char *newsp, *oldsp;
  char *newbp, *oldbp;
  printf("main frame address (0) %p\n", __builtin_frame_address(0));
  printf("main frame address (1) %p\n", __builtin_frame_address(1));
  printf("main rbp %p\n", basepointer);
  printf("main rsp %p\n", stackpointer);
  printf("main stack frame size = 0x%x\n", basepointer - stackpointer);
  char *mystack = malloc((1024 * 1024));
  printf("Allocated heap space starts at %p\n", mystack);
  oldbp = basepointer;
  oldsp = stackpointer;
  newbp = (mystack + (1024 * 1024));
  newsp = newbp - (oldbp - oldsp);
  memcpy(newsp, stackpointer, basepointer - stackpointer);
  printf("Stack frame dumped. Switching frame pointer and stack pointer\n");
  __asm volatile("movq %0, %%rbp;"
                 "movq %1, %%rsp;"
                 :
                 : "r"(newbp), "r"(newsp));
  printf("Stack frame switched to heap memory\n");
  printf("Calling func1\n");
  ret1 = func1(x, y);
  printf("Returned from func1, restoring stack frame\n");
  __asm volatile("movq %0, %%rbp;"
                 "movq %1, %%rsp;"
                 :
                 : "r"(oldbp), "r"(oldsp));
  memcpy(oldsp, newsp, basepointer - stackpointer);
  printf("ret1 = %d\n", ret1);
  free(mystack);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x3,-0x28(%rbp)
movl   $0x5,-0x2c(%rbp)
mov    %rbp,%rsi
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    %rbp,%rax
mov    (%rax),%rsi
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xe8d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
sub    %rax,%rsi
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x100000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rsi
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x58(%rbp),%rax
add    $0x100000,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x50(%rbp),%rdx
mov    -0x40(%rbp),%rcx
sub    %rcx,%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rax
sub    %rax,%rdx
call   1060 <memcpy@plt>
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x48(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %rax,%rbp
mov    %rcx,%rsp
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    $0x0,%al
call   1040 <func1@plt>
mov    %eax,-0x24(%rbp)
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x50(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    %rax,%rbp
mov    %rcx,%rsp
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rax
sub    %rax,%rdx
call   1060 <memcpy@plt>
mov    -0x24(%rbp),%esi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
lea    0xe92(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x0(%rbp),%rsi
lea    0xe98(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xea5(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xea4(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xea3(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x100000,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
lea    0xea6(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x100000(%r14),%rbx
call   1070 <memcpy@plt>
lea    0xeb7(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rbp
mov    %rax,%rsp
lea    0xee3(%rip),%rdi
call   1050 <puts@plt>
lea    0xefb(%rip),%rdi
call   1050 <puts@plt>
mov    $0x3,%edi
mov    $0x5,%esi
xor    %eax,%eax
call   1040 <func1@plt>
mov    %eax,%ebx
lea    0xeea(%rip),%rdi
call   1050 <puts@plt>
mov    %rax,%rbp
mov    %rax,%rsp
call   1070 <memcpy@plt>
lea    0xe58(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"main frame address (0) %p\0A\00"
@.str.1 = [27 x i8] c"main frame address (1) %p\0A\00"
@.str.2 = [13 x i8] c"main rbp %p\0A\00"
@.str.3 = [13 x i8] c"main rsp %p\0A\00"
@.str.4 = [30 x i8] c"main stack frame size = 0x%x\0A\00"
@.str.5 = [35 x i8] c"Allocated heap space starts at %p\0A\00"
@.str.6 = [63 x i8] c"Stack frame dumped. Switching frame pointer and stack pointer\0A\00"
@.str.7 = [37 x i8] c"Stack frame switched to heap memory\0A\00"
@.str.8 = [15 x i8] c"Calling func1\0A\00"
@.str.9 = [44 x i8] c"Returned from func1, restoring stack frame\0A\00"
@.str.10 = [11 x i8] c"ret1 = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 3, i32* %9
store i32 5, i32* %10
%16 = call i8* @llvm.frameaddress.p0i8(i32 0)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i8* @llvm.frameaddress.p0i8(i32 1)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i8*, i8** %6
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = load i8*, i8** %7
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = load i8*, i8** %6
%25 = load i8*, i8** %7
%26 = ptrtoint i8* %24 to i64
%27 = ptrtoint i8* %25 to i64
%28 = sub i64 %26, %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i64 %28)
%30 = call i8* @malloc(i64 1048576)
store i8* %30, i8** %15
%31 = load i8*, i8** %15
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i8* %31)
%33 = load i8*, i8** %6
store i8* %33, i8** %14
%34 = load i8*, i8** %7
store i8* %34, i8** %12
%35 = load i8*, i8** %15
%36 = getelementptr inbounds i8, i8* %35, i64 1048576
store i8* %36, i8** %13
%37 = load i8*, i8** %13
%38 = load i8*, i8** %14
%39 = load i8*, i8** %12
%40 = ptrtoint i8* %38 to i64
%41 = ptrtoint i8* %39 to i64
%42 = sub i64 %40, %41
%43 = sub i64 0, %42
%44 = getelementptr inbounds i8, i8* %37, i64 %43
store i8* %44, i8** %11
%45 = load i8*, i8** %11
%46 = load i8*, i8** %7
%47 = load i8*, i8** %6
%48 = load i8*, i8** %7
%49 = ptrtoint i8* %47 to i64
%50 = ptrtoint i8* %48 to i64
%51 = sub i64 %49, %50
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.6, i64 0, i64 0))
%53 = load i8*, i8** %13
%54 = load i8*, i8** %11
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0))
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
%57 = load i32, i32* %9
%58 = load i32, i32* %10
%59 = call i32 (i32, i32, ...) bitcast (i32 (...)* @func1 to i32 (i32, i32, ...)*)(i32 %57, i32 %58)
store i32 %59, i32* %8
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.9, i64 0, i64 0))
%61 = load i8*, i8** %14
%62 = load i8*, i8** %12
%63 = load i8*, i8** %12
%64 = load i8*, i8** %11
%65 = load i8*, i8** %6
%66 = load i8*, i8** %7
%67 = ptrtoint i8* %65 to i64
%68 = ptrtoint i8* %66 to i64
%69 = sub i64 %67, %68
%70 = load i32, i32* %8
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.10, i64 0, i64 0), i32 %70)
%72 = load i8*, i8** %15
ret i32 0
}

LLVM IR (-O2):
@.str = [27 x i8] c"main frame address (0) %p\0A\00"
@.str.1 = [27 x i8] c"main frame address (1) %p\0A\00"
@.str.2 = [13 x i8] c"main rbp %p\0A\00"
@.str.3 = [13 x i8] c"main rsp %p\0A\00"
@.str.4 = [30 x i8] c"main stack frame size = 0x%x\0A\00"
@.str.5 = [35 x i8] c"Allocated heap space starts at %p\0A\00"
@.str.10 = [11 x i8] c"ret1 = %d\0A\00"
@str = [62 x i8] c"Stack frame dumped. Switching frame pointer and stack pointer\00"
@str.11 = [36 x i8] c"Stack frame switched to heap memory\00"
@str.12 = [14 x i8] c"Calling func1\00"
@str.13 = [43 x i8] c"Returned from func1, restoring stack frame\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i8* @llvm.frameaddress.p0i8(i32 0)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %3)
%5 = tail call i8* @llvm.frameaddress.p0i8(i32 1)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* undef)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* undef)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i64 undef)
%10 = tail call dereferenceable_or_null(1048576) i8* @malloc(i64 1048576)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i8* %10)
%12 = getelementptr inbounds i8, i8* %10, i64 1048576
%13 = getelementptr inbounds i8, i8* %10, i64 undef
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %13, i8* align 1 undef, i64 undef, i1 false)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @str, i64 0, i64 0))
tail call void asm sideeffect "movq $0, %rbp;movq $1, %rsp;", "r,r,~{dirflag},~{fpsr},~{flags}"(i8* %12, i8* %13)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.11, i64 0, i64 0))
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.12, i64 0, i64 0))
%17 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @func1 to i32 (i32, i32, ...)*)(i32 3, i32 5)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.13, i64 0, i64 0))
tail call void asm sideeffect "movq $0, %rbp;movq $1, %rsp;", "r,r,~{dirflag},~{fpsr},~{flags}"(i8* undef, i8* undef)
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 undef, i8* align 1 %13, i64 undef, i1 false)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.10, i64 0, i64 0), i32 %17)
tail call void @free(i8* %10)
ret i32 0
}