#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 /*<<< orphan*/ *getframeaddr(int);
void *getreturnaddr(int);
int /*<<< orphan*/ printf(char *, ...);
int backtrace(void **buffer, int size) {
  int i;
  for (i = 1; getframeaddr(i + 1) != NULL && i != size + 1; i++) {
    printf("back %d\n", i);
    buffer[i - 1] = getreturnaddr(i);
    if (buffer[i - 1] == NULL)
      break;
    printf("end for\n");
  }
  return (i - 1);
}


x86 assembly (clang, -O0):
<backtrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1030 <getframeaddr@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x11(%rbp)
je     1174 <backtrace+0x44>
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1184 <backtrace+0x54>
jmp    11ea <backtrace+0xba>
mov    -0x10(%rbp),%esi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1050 <getreturnaddr@plt>
mov    %rax,%rdx
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    11ce <backtrace+0x9e>
jmp    11ea <backtrace+0xba>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1146 <backtrace+0x16>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<backtrace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
mov    $0x2,%edi
call   1040 <getframeaddr@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11c8 <backtrace+0x88>
test   %r15d,%r15d
je     11c8 <backtrace+0x88>
add    $0x1,%r15d
mov    $0x1,%ebp
lea    0xe89(%rip),%r12
lea    0xe8b(%rip),%r13
xchg   %ax,%ax
mov    %r12,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <getreturnaddr@plt>
mov    %rax,-0x8(%r14,%rbp,8)
test   %rax,%rax
je     11c2 <backtrace+0x82>
lea    0x1(%rbp),%rbx
mov    %r13,%rdi
call   1030 <puts@plt>
add    $0x2,%ebp
mov    %ebp,%edi
call   1040 <getframeaddr@plt>
test   %rax,%rax
je     11c5 <backtrace+0x85>
mov    %rbx,%rbp
cmp    %r15,%rbx
jne    1180 <backtrace+0x40>
jmp    11c5 <backtrace+0x85>
mov    %rbp,%rbx
add    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"back %d\0A\00"
@.str.1 = [9 x i8] c"end for\0A\00"
define i32 @backtrace(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
store i8** %0, i8*** %3
store i32 %1, i32* %4
store i32 1, i32* %5
br label %6
6: ; preds = %38, %2
%7 = load i32, i32* %5
%8 = add nsw i32 %7, 1
%9 = call i32* @getframeaddr(i32 %8)
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = load i32, i32* %5
%13 = load i32, i32* %4
%14 = add nsw i32 %13, 1
%15 = icmp ne i32 %12, %14
br label %16
16: ; preds = %11, %6
%17 = phi i1 [ false, %6 ], [ %15, %11 ]
br i1 %17, label %18, label %41
18: ; preds = %16
%19 = load i32, i32* %5
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %5
%22 = call i8* @getreturnaddr(i32 %21)
%23 = load i8**, i8*** %3
%24 = load i32, i32* %5
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %23, i64 %26
store i8* %22, i8** %27
%28 = load i8**, i8*** %3
%29 = load i32, i32* %5
%30 = sub nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8*, i8** %28, i64 %31
%33 = load i8*, i8** %32
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %36
35: ; preds = %18
br label %41
36: ; preds = %18
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %36
%39 = load i32, i32* %5
%40 = add nsw i32 %39, 1
store i32 %40, i32* %5
br label %6
41: ; preds = %35, %16
%42 = load i32, i32* %5
%43 = sub nsw i32 %42, 1
ret i32 %43
}

LLVM IR (-O2):
@.str = [9 x i8] c"back %d\0A\00"
@str = [8 x i8] c"end for\00"
define i32 @backtrace(i8** nocapture writeonly %0, i32 %1) {
%3 = tail call i32* @getframeaddr(i32 2)
%4 = icmp ne i32* %3, null
%5 = icmp ne i32 %1, 0
%6 = and i1 %4, %5
br i1 %6, label %7, label %32
7: ; preds = %2
%8 = add nsw i32 %1, 1
%9 = zext i32 %8 to i64
br label %10
10: ; preds = %7, %19
%11 = phi i64 [ 1, %7 ], [ %20, %19 ]
%12 = phi i64 [ 2, %7 ], [ %22, %19 ]
%13 = trunc i64 %11 to i32
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = tail call i8* @getreturnaddr(i32 %13)
%16 = add nsw i64 %11, -1
%17 = getelementptr inbounds i8*, i8** %0, i64 %16
store i8* %15, i8** %17
%18 = icmp eq i8* %15, null
br i1 %18, label %28, label %19
19: ; preds = %10
%20 = add nuw nsw i64 %11, 1
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%22 = add nuw nsw i64 %12, 1
%23 = trunc i64 %22 to i32
%24 = tail call i32* @getframeaddr(i32 %23)
%25 = icmp ne i32* %24, null
%26 = icmp ne i64 %12, %9
%27 = select i1 %25, i1 %26, i1 false
br i1 %27, label %10, label %28
28: ; preds = %19, %10
%29 = phi i64 [ %11, %10 ], [ %12, %19 ]
%30 = trunc i64 %29 to i32
%31 = add i32 %30, -1
br label %32
32: ; preds = %28, %2
%33 = phi i32 [ 0, %2 ], [ %31, %28 ]
ret i32 %33
}