#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int backtrace(void **, int);
char **backtrace_symbols(void **, int);
int /*<<< orphan*/ printf(char *, ...);
void trace() {
  int size = 16;
  void *array[16];
  int stack_num = backtrace(array, size);
  char **stacktrace = backtrace_symbols(array, stack_num);
  for (int i = 0; i < stack_num; ++i) {
    printf("  %s\n", stacktrace[i]);
  }
  printf("%p\n", &size);
}


x86 assembly (clang, -O0):
<trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x10,-0x4(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1040 <backtrace@plt>
mov    %eax,-0x94(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x94(%rbp),%esi
call   1030 <backtrace_symbols@plt>
mov    %rax,-0xa0(%rbp)
movl   $0x0,-0xa4(%rbp)
mov    -0xa4(%rbp),%eax
cmp    -0x94(%rbp),%eax
jge    11c0 <trace+0x90>
mov    -0xa0(%rbp),%rax
movslq -0xa4(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    117a <trace+0x4a>
lea    0xe3f(%rip),%rdi
lea    -0x4(%rbp),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trace>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x98,%rsp
movl   $0x10,0xc(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x10,%esi
call   1040 <backtrace@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <backtrace_symbols@plt>
test   %ebp,%ebp
jle    1197 <trace+0x67>
mov    %rax,%r15
mov    %ebp,%ebp
lea    0xe8d(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15,%rbx,8),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1180 <trace+0x50>
lea    0xe68(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x98,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c" %s\0A\00"
@.str.1 = [4 x i8] c"%p\0A\00"
define void @trace() {
%1 = alloca i32
%2 = alloca [16 x i8*]
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
store i32 16, i32* %1
%6 = getelementptr inbounds [16 x i8*], [16 x i8*]* %2, i64 0, i64 0
%7 = load i32, i32* %1
%8 = call i32 @backtrace(i8** %6, i32 %7)
store i32 %8, i32* %3
%9 = getelementptr inbounds [16 x i8*], [16 x i8*]* %2, i64 0, i64 0
%10 = load i32, i32* %3
%11 = call i8** @backtrace_symbols(i8** %9, i32 %10)
store i8** %11, i8*** %4
store i32 0, i32* %5
br label %12
12: ; preds = %23, %0
%13 = load i32, i32* %5
%14 = load i32, i32* %3
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %26
16: ; preds = %12
%17 = load i8**, i8*** %4
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8*, i8** %17, i64 %19
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %16
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %12
26: ; preds = %12
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %1)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c" %s\0A\00"
@.str.1 = [4 x i8] c"%p\0A\00"
define void @trace() {
%1 = alloca i32
%2 = alloca [16 x i8*]
%3 = bitcast i32* %1 to i8*
store i32 16, i32* %1
%4 = bitcast [16 x i8*]* %2 to i8*
%5 = getelementptr inbounds [16 x i8*], [16 x i8*]* %2, i64 0, i64 0
%6 = call i32 @backtrace(i8** %5, i32 16)
%7 = call i8** @backtrace_symbols(i8** %5, i32 %6)
%8 = icmp sgt i32 %6, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = zext i32 %6 to i64
br label %13
11: ; preds = %13, %0
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* %1)
ret void
13: ; preds = %9, %13
%14 = phi i64 [ 0, %9 ], [ %18, %13 ]
%15 = getelementptr inbounds i8*, i8** %7, i64 %14
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = add nuw nsw i64 %14, 1
%19 = icmp eq i64 %18, %10
br i1 %19, label %11, label %13
}