#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 stat {
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
int /*<<< orphan*/ lookup_sections(void *, int, void **);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *);
int bjh_prt_call_stack(const char *elf_nm, int addrs_sz, void **stack_addrs) {
  int fd;
  struct stat st;
  void *file;
  fd = open(elf_nm, O_RDONLY);
  if (fd == -1) {
    printf("ERROR: Can't open elf file \"%s\".\n", elf_nm);
    return 1;
  }
  if (fstat(fd, &st) == -1) {
    printf("ERROR: Can't stat elf file \"%s\".\n", elf_nm);
    close(fd);
    return 1;
  }
  file = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (file == MAP_FAILED) {
    printf("ERROR: Can't mmap elf file \"%s\".\n", elf_nm);
    close(fd);
    return 1;
  }
  lookup_sections(file, addrs_sz, stack_addrs);
  munmap(file, st.st_size);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<bjh_prt_call_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11be <bjh_prt_call_stack+0x4e>
mov    -0x10(%rbp),%rsi
lea    0xe53(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1283 <bjh_prt_call_stack+0x113>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11f7 <bjh_prt_call_stack+0x87>
mov    -0x10(%rbp),%rsi
lea    0xe44(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1283 <bjh_prt_call_stack+0x113>
mov    -0x28(%rbp),%esi
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
mov    0x2dc6(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d9d(%rip),%rcx
cmp    (%rcx),%rax
jne    1258 <bjh_prt_call_stack+0xe8>
mov    -0x10(%rbp),%rsi
lea    0xe05(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1283 <bjh_prt_call_stack+0x113>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1050 <lookup_sections@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1070 <munmap@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bjh_prt_call_stack>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %esi,%r12d
mov    %rdi,%r14
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     120e <bjh_prt_call_stack+0x9e>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
je     121f <bjh_prt_call_stack+0xaf>
mov    %rbp,0x10(%rsp)
mov    0x8(%rsp),%r13d
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    0x2e09(%rip),%rax
mov    (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    %r13d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r15
mov    0x2de3(%rip),%rax
cmp    (%rax),%r15
je     1228 <bjh_prt_call_stack+0xb8>
mov    %r15,%rdi
mov    %r12d,%esi
mov    0x10(%rsp),%rdx
call   1050 <lookup_sections@plt>
mov    %r15,%rdi
mov    %r13d,%esi
call   1070 <munmap@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1243 <bjh_prt_call_stack+0xd3>
lea    0xdeb(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
jmp    123e <bjh_prt_call_stack+0xce>
lea    0xdfc(%rip),%rdi
jmp    122f <bjh_prt_call_stack+0xbf>
lea    0xe15(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [34 x i8] c"ERROR: Can't open elf file \22%s\22.\0A\00"
@.str.1 = [34 x i8] c"ERROR: Can't stat elf file \22%s\22.\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [34 x i8] c"ERROR: Can't mmap elf file \22%s\22.\0A\00"
define i32 @bjh_prt_call_stack(i8* %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca %struct.stat
%10 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %4
br label %54
19: ; preds = %3
%20 = load i32, i32* %8
%21 = call i32 @fstat(i32 %20, %struct.stat* %9)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i8*, i8** %5
%25 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i32, i32* %8
%27 = call i32 @close(i32 %26)
store i32 1, i32* %4
br label %54
28: ; preds = %19
%29 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @MAP_PRIVATE
%33 = load i32, i32* %8
%34 = call i8* @mmap(i32* null, i32 %30, i32 %31, i32 %32, i32 %33, i32 0)
store i8* %34, i8** %10
%35 = load i8*, i8** %10
%36 = load i8*, i8** @MAP_FAILED
%37 = icmp eq i8* %35, %36
br i1 %37, label %38, label %43
38: ; preds = %28
%39 = load i8*, i8** %5
%40 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %39)
%41 = load i32, i32* %8
%42 = call i32 @close(i32 %41)
store i32 1, i32* %4
br label %54
43: ; preds = %28
%44 = load i8*, i8** %10
%45 = load i32, i32* %6
%46 = load i8**, i8*** %7
%47 = call i32 @lookup_sections(i8* %44, i32 %45, i8** %46)
%48 = load i8*, i8** %10
%49 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @munmap(i8* %48, i32 %50)
%52 = load i32, i32* %8
%53 = call i32 @close(i32 %52)
store i32 0, i32* %4
br label %54
54: ; preds = %43, %38, %23, %16
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [34 x i8] c"ERROR: Can't open elf file \22%s\22.\0A\00"
@.str.1 = [34 x i8] c"ERROR: Can't stat elf file \22%s\22.\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [34 x i8] c"ERROR: Can't mmap elf file \22%s\22.\0A\00"
define i32 @bjh_prt_call_stack(i8* %0, i32 %1, i8** %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %32
11: ; preds = %3
%12 = call i32 @fstat(i32 %7, %struct.stat* %4)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%16 = tail call i32 @close(i32 %7)
br label %32
17: ; preds = %11
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%19 = load i32, i32* %18
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @MAP_PRIVATE
%22 = tail call i8* @mmap(i32* null, i32 %19, i32 %20, i32 %21, i32 %7, i32 0)
%23 = load i8*, i8** @MAP_FAILED
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%27 = tail call i32 @close(i32 %7)
br label %32
28: ; preds = %17
%29 = tail call i32 @lookup_sections(i8* %22, i32 %1, i8** %2)
%30 = tail call i32 @munmap(i8* %22, i32 %19)
%31 = tail call i32 @close(i32 %7)
br label %32
32: ; preds = %28, %25, %14, %9
%33 = phi i32 [ 1, %9 ], [ 1, %14 ], [ 1, %25 ], [ 0, %28 ]
ret i32 %33
}