#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 backtrace(void **buffer, int size) {
  int i;
  for (i = 1; getframeaddr(i + 1) != NULL && i != size + 1; i++) {
    buffer[i - 1] = getreturnaddr(i);
    if (buffer[i - 1] == NULL)
      break;
  }
  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     1164 <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    1174 <backtrace+0x54>
jmp    11c0 <backtrace+0xa0>
mov    -0x10(%rbp),%edi
call   1040 <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    11ad <backtrace+0x8d>
jmp    11c0 <backtrace+0xa0>
jmp    11b2 <backtrace+0x92>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1136 <backtrace+0x16>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<backtrace>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
add    $0x1,%r15d
add    $0xffffffffffffffff,%r15
xor    %r12d,%r12d
nopl   0x0(%rax)
mov    %r12,%rbx
lea    0x2(%r12),%edi
call   1030 <getframeaddr@plt>
test   %rax,%rax
je     116c <backtrace+0x4c>
cmp    %rbx,%r15
je     116c <backtrace+0x4c>
lea    0x1(%rbx),%r12
mov    %r12d,%edi
call   1040 <getreturnaddr@plt>
mov    %rax,(%r14,%rbx,8)
test   %rax,%rax
jne    1140 <backtrace+0x20>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
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 = %35, %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 %38
18: ; preds = %16
%19 = load i32, i32* %5
%20 = call i8* @getreturnaddr(i32 %19)
%21 = load i8**, i8*** %3
%22 = load i32, i32* %5
%23 = sub nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %21, i64 %24
store i8* %20, i8** %25
%26 = load i8**, i8*** %3
%27 = load i32, i32* %5
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %26, i64 %29
%31 = load i8*, i8** %30
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %34
33: ; preds = %18
br label %38
34: ; preds = %18
br label %35
35: ; preds = %34
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
br label %6
38: ; preds = %33, %16
%39 = load i32, i32* %5
%40 = sub nsw i32 %39, 1
ret i32 %40
}

LLVM IR (-O2):
define i32 @backtrace(i8** nocapture writeonly %0, i32 %1) {
%3 = add nsw i32 %1, 1
%4 = zext i32 %3 to i64
br label %5
5: ; preds = %13, %2
%6 = phi i64 [ %7, %13 ], [ 1, %2 ]
%7 = add nuw nsw i64 %6, 1
%8 = trunc i64 %7 to i32
%9 = tail call i32* @getframeaddr(i32 %8)
%10 = icmp ne i32* %9, null
%11 = icmp ne i64 %6, %4
%12 = select i1 %10, i1 %11, i1 false
br i1 %12, label %13, label %19
13: ; preds = %5
%14 = trunc i64 %6 to i32
%15 = tail call i8* @getreturnaddr(i32 %14)
%16 = add nsw i64 %6, -1
%17 = getelementptr inbounds i8*, i8** %0, i64 %16
store i8* %15, i8** %17
%18 = icmp eq i8* %15, null
br i1 %18, label %19, label %5
19: ; preds = %13, %5
%20 = trunc i64 %6 to i32
%21 = add nsw i32 %20, -1
ret i32 %21
}