#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 backtrace(void **, int);
char **backtrace_symbols(void **, int);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ printf(char *, ...);
void IOSIG_backtrace() {
  int i, nptrs;
  void *bt_buffer[512];
  char **strings;
  nptrs = backtrace(bt_buffer, 512);
  printf("IOSIG backtraced %d addresses.\n", nptrs);
  strings = backtrace_symbols(bt_buffer, nptrs);
  if (strings == NULL) {
    return;
  }
  for (i = 0; i < nptrs; i++) {
    printf("%s\n", strings[i]);
  }
  free(strings);
}


x86 assembly (clang, -O0):
<IOSIG_backtrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
lea    -0x1010(%rbp),%rdi
mov    $0x200,%esi
call   1050 <backtrace@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
jne    1199 <IOSIG_backtrace+0x59>
jmp    11e3 <IOSIG_backtrace+0xa3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11d7 <IOSIG_backtrace+0x97>
mov    -0x1018(%rbp),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11a0 <IOSIG_backtrace+0x60>
mov    -0x1018(%rbp),%rdi
call   1030 <free@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<IOSIG_backtrace>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1060 <backtrace@plt>
mov    %eax,%ebp
lea    0xe8c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <backtrace_symbols@plt>
test   %rax,%rax
je     11ba <IOSIG_backtrace+0x6a>
mov    %rax,%r14
test   %ebp,%ebp
jle    11b2 <IOSIG_backtrace+0x62>
mov    %ebp,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    (%r14,%rbx,8),%rdi
call   1040 <puts@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11a0 <IOSIG_backtrace+0x50>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"IOSIG backtraced %d addresses.\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define void @IOSIG_backtrace() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [512 x i8*]
%4 = alloca i8**
%5 = getelementptr inbounds [512 x i8*], [512 x i8*]* %3, i64 0, i64 0
%6 = call i32 @backtrace(i8** %5, i32 512)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [512 x i8*], [512 x i8*]* %3, i64 0, i64 0
%10 = load i32, i32* %2
%11 = call i8** @backtrace_symbols(i8** %9, i32 %10)
store i8** %11, i8*** %4
%12 = load i8**, i8*** %4
%13 = icmp eq i8** %12, null
br i1 %13, label %14, label %15
14: ; preds = %0
br label %33
15: ; preds = %0
store i32 0, i32* %1
br label %16
16: ; preds = %27, %15
%17 = load i32, i32* %1
%18 = load i32, i32* %2
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = load i8**, i8*** %4
%22 = load i32, i32* %1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8*, i8** %21, i64 %23
%25 = load i8*, i8** %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %20
%28 = load i32, i32* %1
%29 = add nsw i32 %28, 1
store i32 %29, i32* %1
br label %16
30: ; preds = %16
%31 = load i8**, i8*** %4
%32 = call i32 @free(i8** %31)
br label %33
33: ; preds = %30, %14
ret void
}

LLVM IR (-O2):
@.str = [32 x i8] c"IOSIG backtraced %d addresses.\0A\00"
define void @IOSIG_backtrace() {
%1 = alloca [512 x i8*]
%2 = bitcast [512 x i8*]* %1 to i8*
%3 = getelementptr inbounds [512 x i8*], [512 x i8*]* %1, i64 0, i64 0
%4 = call i32 @backtrace(i8** %3, i32 512)
%5 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i8** @backtrace_symbols(i8** %3, i32 %4)
%7 = icmp eq i8** %6, null
br i1 %7, label %21, label %8
8: ; preds = %0
%9 = icmp sgt i32 %4, 0
br i1 %9, label %10, label %19
10: ; preds = %8
%11 = zext i32 %4 to i64
br label %12
12: ; preds = %10, %12
%13 = phi i64 [ 0, %10 ], [ %17, %12 ]
%14 = getelementptr inbounds i8*, i8** %6, i64 %13
%15 = load i8*, i8** %14
%16 = call i32 @puts(i8* dereferenceable(1) %15)
%17 = add nuw nsw i64 %13, 1
%18 = icmp eq i64 %17, %11
br i1 %18, label %19, label %12
19: ; preds = %12, %8
%20 = call i32 @free(i8** %6)
br label %21
21: ; preds = %0, %19
ret void
}