#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*/ _stacktrace(void *, void **, int, int *, int);
int backtrace_np(void *ebp, void **buffer, int size) {
  if (!ebp)
    return 0;
  int frames;
  _stacktrace(ebp, buffer, size, &frames, 1);
  while (frames && !buffer[frames - 1])
    frames--;
  return frames;
}


x86 assembly (clang, -O0):
<backtrace_np>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    113a <backtrace_np+0x2a>
movl   $0x0,-0x4(%rbp)
jmp    11a1 <backtrace_np+0x91>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    -0x20(%rbp),%rcx
mov    $0x1,%r8d
call   1030 <_stacktrace@plt>
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x21(%rbp)
je     117d <backtrace_np+0x6d>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmpq   $0x0,(%rax,%rcx,8)
setne  %al
xor    $0xff,%al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    118d <backtrace_np+0x7d>
jmp    119b <backtrace_np+0x8b>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    1154 <backtrace_np+0x44>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<backtrace_np>:
test   %rdi,%rdi
je     1161 <backtrace_np+0x51>
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
lea    0xc(%rsp),%rcx
mov    $0x1,%r8d
call   1030 <_stacktrace@plt>
movslq 0xc(%rsp),%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     1166 <backtrace_np+0x56>
lea    (%rbx,%rcx,8),%rdx
add    $0xfffffffffffffff8,%rdx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpq   $0x0,(%rdx)
jne    1164 <backtrace_np+0x54>
add    $0xfffffffffffffff8,%rdx
add    $0xffffffff,%ecx
jne    1150 <backtrace_np+0x40>
jmp    1166 <backtrace_np+0x56>
xor    %eax,%eax
ret
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @backtrace_np(i8* %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %3
store i32 0, i32* %4
br label %36
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = load i8**, i8*** %6
%15 = load i32, i32* %7
%16 = call i32 @_stacktrace(i8* %13, i8** %14, i32 %15, i32* %8, i32 1)
br label %17
17: ; preds = %31, %12
%18 = load i32, i32* %8
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %17
%21 = load i8**, i8*** %6
%22 = load i32, i32* %8
%23 = sub nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %21, i64 %24
%26 = load i8*, i8** %25
%27 = icmp ne i8* %26, null
%28 = xor i1 %27, true
br label %29
29: ; preds = %20, %17
%30 = phi i1 [ false, %17 ], [ %28, %20 ]
br i1 %30, label %31, label %34
31: ; preds = %29
%32 = load i32, i32* %8
%33 = add nsw i32 %32, -1
store i32 %33, i32* %8
br label %17
34: ; preds = %29
%35 = load i32, i32* %8
store i32 %35, i32* %4
br label %36
36: ; preds = %34, %11
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
define i32 @backtrace_np(i8* %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = icmp eq i8* %0, null
br i1 %5, label %26, label %6
6: ; preds = %3
%7 = bitcast i32* %4 to i8*
%8 = call i32 @_stacktrace(i8* %0, i8** %1, i32 %2, i32* %4, i32 1)
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 0
br i1 %10, label %24, label %11
11: ; preds = %6
%12 = sext i32 %9 to i64
br label %13
13: ; preds = %11, %19
%14 = phi i64 [ %12, %11 ], [ %15, %19 ]
%15 = add nsw i64 %14, -1
%16 = getelementptr inbounds i8*, i8** %1, i64 %15
%17 = load i8*, i8** %16
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = trunc i64 %15 to i32
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %13
22: ; preds = %13
%23 = trunc i64 %14 to i32
br label %24
24: ; preds = %19, %22, %6
%25 = phi i32 [ 0, %6 ], [ %23, %22 ], [ 0, %19 ]
br label %26
26: ; preds = %3, %24
%27 = phi i32 [ %25, %24 ], [ 0, %3 ]
ret i32 %27
}