void test_push(void *mem) {
  unsigned long tmp;
  unsigned long *stack_top = mem + 4096;
  unsigned long *new_stack_top;
  unsigned long memw = 0x123456789abcdeful;
  __builtin_memset(mem, 0x55, (void *)stack_top - mem);
  asm volatile("mov %%rsp, %[tmp] \n\t"
               "mov %[stack_top], %%rsp \n\t"
               "pushq $-7 \n\t"
               "pushq %[reg] \n\t"
               "pushq (%[mem]) \n\t"
               "pushq $-7070707 \n\t"
               "mov %%rsp, %[new_stack_top] \n\t"
               "mov %[tmp], %%rsp"
               : [tmp] "=&r"(tmp), [new_stack_top] "=r"(new_stack_top)
               : [stack_top] "r"(stack_top), [reg] "r"(-17l), [mem] "r"(&memw)
               : "memory");
  report("push $imm8", stack_top[-1] == -7ul);
  report("push %reg", stack_top[-2] == -17ul);
  report("push mem", stack_top[-3] == 0x123456789abcdeful);
  report("push $imm", stack_top[-4] == -7070707);
}


x86 assembly (clang, -O0):
<test_push>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x18(%rbp)
movabs $0x123456789abcdef,%rax
45 23 01
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x8(%rbp),%rax
sub    %rax,%rdx
mov    $0x55,%esi
call   1030 <memset@plt>
mov    -0x18(%rbp),%rax
mov    $0xffffffffffffffef,%rdx
lea    -0x28(%rbp),%rsi
mov    %rsp,%rcx
mov    %rax,%rsp
push   $0xfffffffffffffff9
push   %rdx
push   (%rsi)
push   $0xffffffffff941c0d
mov    %rsp,%rax
mov    %rcx,%rsp
mov    %rcx,-0x10(%rbp)
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0xfffffffffffffff9,-0x8(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <report@plt>
mov    -0x18(%rbp),%rax
cmpq   $0xffffffffffffffef,-0x10(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%esi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <report@plt>
mov    -0x18(%rbp),%rax
movabs $0x123456789abcdef,%rcx
45 23 01
cmp    %rcx,-0x18(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%esi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <report@plt>
mov    -0x18(%rbp),%rax
cmpq   $0xffffffffff941c0d,-0x20(%rax)
ff
sete   %al
and    $0x1,%al
movzbl %al,%esi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <report@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_push>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x1000(%rdi),%r15
movabs $0x123456789abcdef,%r14
45 23 01
mov    %r14,0x8(%rsp)
mov    $0x1000,%edx
mov    $0x55,%esi
call   1030 <memset@plt>
mov    $0xffffffffffffffef,%rax
lea    0x8(%rsp),%rcx
mov    %rsp,%rdx
mov    %r15,%rsp
push   $0xfffffffffffffff9
push   %rax
push   (%rcx)
push   $0xffffffffff941c0d
mov    %rsp,%rax
mov    %rdx,%rsp
xor    %esi,%esi
cmpq   $0xfffffffffffffff9,0xff8(%rbx)
f9
sete   %sil
lea    0xe78(%rip),%rdi
xor    %eax,%eax
call   1040 <report@plt>
xor    %esi,%esi
cmpq   $0xffffffffffffffef,0xff0(%rbx)
ef
sete   %sil
lea    0xe67(%rip),%rdi
xor    %eax,%eax
call   1040 <report@plt>
xor    %esi,%esi
cmp    %r14,0xfe8(%rbx)
sete   %sil
lea    0xe56(%rip),%rdi
xor    %eax,%eax
call   1040 <report@plt>
xor    %esi,%esi
cmpq   $0xffffffffff941c0d,0xfe0(%rbx)
0d 1c 94 ff
sete   %sil
lea    0xe40(%rip),%rdi
xor    %eax,%eax
call   1040 <report@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"push $imm8\00"
@.str.1 = [10 x i8] c"push %reg\00"
@.str.2 = [9 x i8] c"push mem\00"
@.str.3 = [10 x i8] c"push $imm\00"
define void @test_push(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64*
%5 = alloca i64*
%6 = alloca i64
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = getelementptr i8, i8* %7, i64 4096
%9 = bitcast i8* %8 to i64*
store i64* %9, i64** %4
store i64 81985529216486895, i64* %6
%10 = load i8*, i8** %2
%11 = load i64*, i64** %4
%12 = bitcast i64* %11 to i8*
%13 = load i8*, i8** %2
%14 = ptrtoint i8* %12 to i64
%15 = ptrtoint i8* %13 to i64
%16 = sub i64 %14, %15
%17 = load i64*, i64** %4
%18 = call { i64, i64* } asm sideeffect "mov %rsp, $0 \0A\09mov $2, %rsp \0A\09pushq $$-7 \0A\09pushq $3 \0A\09pushq ($4) \0A\09pushq $$-7070707 \0A\09mov %rsp, $1 \0A\09mov $0, %rsp", "=&r,=r,r,r,r,~{memory},~{dirflag},~{fpsr},~{flags}"(i64* %17, i64 -17, i64* %6)
%19 = extractvalue { i64, i64* } %18, 0
%20 = extractvalue { i64, i64* } %18, 1
store i64 %19, i64* %3
store i64* %20, i64** %5
%21 = load i64*, i64** %4
%22 = getelementptr inbounds i64, i64* %21, i64 -1
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, -7
%25 = zext i1 %24 to i32
%26 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @report to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = load i64*, i64** %4
%28 = getelementptr inbounds i64, i64* %27, i64 -2
%29 = load i64, i64* %28
%30 = icmp eq i64 %29, -17
%31 = zext i1 %30 to i32
%32 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @report to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i64*, i64** %4
%34 = getelementptr inbounds i64, i64* %33, i64 -3
%35 = load i64, i64* %34
%36 = icmp eq i64 %35, 81985529216486895
%37 = zext i1 %36 to i32
%38 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @report to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = load i64*, i64** %4
%40 = getelementptr inbounds i64, i64* %39, i64 -4
%41 = load i64, i64* %40
%42 = icmp eq i64 %41, -7070707
%43 = zext i1 %42 to i32
%44 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @report to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %43)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"push $imm8\00"
@.str.1 = [10 x i8] c"push %reg\00"
@.str.2 = [9 x i8] c"push mem\00"
@.str.3 = [10 x i8] c"push $imm\00"
define void @test_push(i8* %0) {
%2 = alloca i64
%3 = getelementptr i8, i8* %0, i64 4096
%4 = bitcast i8* %3 to i64*
%5 = bitcast i64* %2 to i8*
store i64 81985529216486895, i64* %2
tail call void @llvm.memset.p0i8.i64(i8* align 1 dereferenceable(4096) %0, i8 85, i64 4096, i1 false)
%6 = call { i64, i64* } asm sideeffect "mov %rsp, $0 \0A\09mov $2, %rsp \0A\09pushq $$-7 \0A\09pushq $3 \0A\09pushq ($4) \0A\09pushq $$-7070707 \0A\09mov %rsp, $1 \0A\09mov $0, %rsp", "=&r,=r,r,r,r,~{memory},~{dirflag},~{fpsr},~{flags}"(i64* %4, i64 -17, i64* %2)
%7 = getelementptr i8, i8* %0, i64 4088
%8 = bitcast i8* %7 to i64*
%9 = load i64, i64* %8
%10 = icmp eq i64 %9, -7
%11 = zext i1 %10 to i32
%12 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @report to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr i8, i8* %0, i64 4080
%14 = bitcast i8* %13 to i64*
%15 = load i64, i64* %14
%16 = icmp eq i64 %15, -17
%17 = zext i1 %16 to i32
%18 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @report to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = getelementptr i8, i8* %0, i64 4072
%20 = bitcast i8* %19 to i64*
%21 = load i64, i64* %20
%22 = icmp eq i64 %21, 81985529216486895
%23 = zext i1 %22 to i32
%24 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @report to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = getelementptr i8, i8* %0, i64 4064
%26 = bitcast i8* %25 to i64*
%27 = load i64, i64* %26
%28 = icmp eq i64 %27, -7070707
%29 = zext i1 %28 to i32
%30 = call i32 (i8*, i32, ...) bitcast (i32 (...)* @report to i32 (i8*, i32, ...)*)(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %29)
ret void
}