#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 */
struct FILE_T {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ fprintf(struct FILE_T *, char *, ...);
char *get_func_name(unsigned int);
void stacktrace2(unsigned int max_frames, struct FILE_T *f, unsigned int *ebp) {
  fprintf(f, "stack trace:\n");
  for (unsigned int frame = 0; frame < max_frames; ++frame) {
    unsigned int eip = ebp[1];
    if (eip == 0)
      break;
    ebp = (unsigned int *)ebp[0];
    char *name = get_func_name(eip);
    if (name) {
      fprintf(f, "  %p: %.32s\n", eip, name);
    } else {
      fprintf(f, "  %p: %.32s\n", eip, "unknown func");
      return;
    }
  }
}


x86 assembly (clang, -O0):
<stacktrace2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xec2(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    11e4 <stacktrace2+0xc4>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1171 <stacktrace2+0x51>
jmp    11e4 <stacktrace2+0xc4>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <get_func_name@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11b0 <stacktrace2+0x90>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x28(%rbp),%rcx
lea    0xe6a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    11d1 <stacktrace2+0xb1>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xe50(%rip),%rsi
lea    0xe56(%rip),%rcx
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    11e4 <stacktrace2+0xc4>
jmp    11d6 <stacktrace2+0xb6>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    114c <stacktrace2+0x2c>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stacktrace2>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r12d
lea    0xeb8(%rip),%rdi
mov    $0xd,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <fwrite@plt>
test   %r12d,%r12d
je     119d <stacktrace2+0x6d>
lea    0xea8(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    0x4(%rbx),%ebp
test   %ebp,%ebp
je     119d <stacktrace2+0x6d>
mov    (%rbx),%ebx
mov    %ebp,%edi
call   1030 <get_func_name@plt>
test   %rax,%rax
je     11a6 <stacktrace2+0x76>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0xffffffff,%r12d
jne    1170 <stacktrace2+0x40>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe61(%rip),%rsi
lea    0xe67(%rip),%rcx
mov    %r14,%rdi
mov    %ebp,%edx
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fprintf@plt>

LLVM IR (-O0):
%struct.FILE_T = type { i32 }
@.str = [14 x i8] c"stack trace:\0A\00"
@.str.1 = [13 x i8] c" %p: %.32s\0A\00"
@.str.2 = [13 x i8] c"unknown func\00"
define void @stacktrace2(i32 %0, %struct.FILE_T* %1, i32* %2) {
%4 = alloca i32
%5 = alloca %struct.FILE_T*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %4
store %struct.FILE_T* %1, %struct.FILE_T** %5
store i32* %2, i32** %6
%10 = load %struct.FILE_T*, %struct.FILE_T** %5
%11 = call i32 (%struct.FILE_T*, i8*, ...) @fprintf(%struct.FILE_T* %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %7
br label %12
12: ; preds = %43, %3
%13 = load i32, i32* %7
%14 = load i32, i32* %4
%15 = icmp ult i32 %13, %14
br i1 %15, label %16, label %46
16: ; preds = %12
%17 = load i32*, i32** %6
%18 = getelementptr inbounds i32, i32* %17, i64 1
%19 = load i32, i32* %18
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
br label %46
23: ; preds = %16
%24 = load i32*, i32** %6
%25 = getelementptr inbounds i32, i32* %24, i64 0
%26 = load i32, i32* %25
%27 = zext i32 %26 to i64
%28 = inttoptr i64 %27 to i32*
store i32* %28, i32** %6
%29 = load i32, i32* %8
%30 = call i8* @get_func_name(i32 %29)
store i8* %30, i8** %9
%31 = load i8*, i8** %9
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %38
33: ; preds = %23
%34 = load %struct.FILE_T*, %struct.FILE_T** %5
%35 = load i32, i32* %8
%36 = load i8*, i8** %9
%37 = call i32 (%struct.FILE_T*, i8*, ...) @fprintf(%struct.FILE_T* %34, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %35, i8* %36)
br label %42
38: ; preds = %23
%39 = load %struct.FILE_T*, %struct.FILE_T** %5
%40 = load i32, i32* %8
%41 = call i32 (%struct.FILE_T*, i8*, ...) @fprintf(%struct.FILE_T* %39, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %40, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %46
42: ; preds = %33
br label %43
43: ; preds = %42
%44 = load i32, i32* %7
%45 = add i32 %44, 1
store i32 %45, i32* %7
br label %12
46: ; preds = %38, %22, %12
ret void
}

LLVM IR (-O2):
%struct.FILE_T = type { i32 }
@.str = [14 x i8] c"stack trace:\0A\00"
@.str.1 = [13 x i8] c" %p: %.32s\0A\00"
@.str.2 = [13 x i8] c"unknown func\00"
define void @stacktrace2(i32 %0, %struct.FILE_T* nocapture %1, i32* nocapture %2) {
%4 = tail call i64 @fwrite(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 13, i64 1, %struct.FILE_T* %1)
%5 = icmp eq i32 %0, 0
br i1 %5, label %24, label %6
6: ; preds = %3, %18
%7 = phi i32* [ %20, %18 ], [ %2, %3 ]
%8 = phi i32 [ %22, %18 ], [ 0, %3 ]
%9 = getelementptr inbounds i32, i32* %7, i64 1
%10 = load i32, i32* %9
%11 = icmp eq i32 %10, 0
br i1 %11, label %24, label %12
12: ; preds = %6
%13 = load i32, i32* %7
%14 = tail call i8* @get_func_name(i32 %10)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 (%struct.FILE_T*, i8*, ...) @fprintf(%struct.FILE_T* %1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %10, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %24
18: ; preds = %12
%19 = zext i32 %13 to i64
%20 = inttoptr i64 %19 to i32*
%21 = tail call i32 (%struct.FILE_T*, i8*, ...) @fprintf(%struct.FILE_T* %1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %10, i8* %14)
%22 = add nuw i32 %8, 1
%23 = icmp eq i32 %22, %0
br i1 %23, label %24, label %6
24: ; preds = %18, %6, %3, %16
ret void
}