#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 */
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ array;

/* Variables and functions */
size_t backtrace(void **, int);
char **backtrace_symbols(void **, size_t);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
void log_dump_trace(char *buf, int buf_size) {
  int left = 0;
  void *array[64] = {NULL};
  size_t size = 0;
  char **strings = NULL;
  size_t i = 0;
  char line[512] = "";
  size = backtrace(array, sizeof(array) / sizeof(void *));
  strings = backtrace_symbols(array, size);
  buf[0] = '\0';
  for (i = 0; i < size; i++) {
    if ((left = buf_size - 1 - strlen(buf)) < 0)
      break;
    snprintf(line, sizeof(line), "\t%s\n", strings[i]);
    strncat(buf, line, left);
  }
  {
    free((strings));
    (strings) = NULL;
  };
}


x86 assembly (clang, -O0):
<log_dump_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1080 <memset@plt>
movq   $0x0,-0x218(%rbp)
movq   $0x0,-0x220(%rbp)
movq   $0x0,-0x228(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1080 <memset@plt>
lea    -0x210(%rbp),%rdi
mov    $0x40,%esi
call   1060 <backtrace@plt>
mov    %rax,-0x218(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x218(%rbp),%rsi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x220(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x228(%rbp)
mov    -0x228(%rbp),%rax
cmp    -0x218(%rbp),%rax
jae    12ac <log_dump_trace+0x13c>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x434(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x434(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1258 <log_dump_trace+0xe8>
jmp    12ac <log_dump_trace+0x13c>
lea    -0x430(%rbp),%rdi
mov    -0x220(%rbp),%rax
mov    -0x228(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
mov    $0x200,%esi
lea    0xd83(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x430(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1090 <strncat@plt>
mov    -0x228(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x228(%rbp)
jmp    1214 <log_dump_trace+0xa4>
mov    -0x220(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x220(%rbp)
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_dump_trace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %esi,0xc(%rsp)
mov    %rdi,%rbx
lea    0x210(%rsp),%rbp
mov    $0x200,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1080 <memset@plt>
lea    0x10(%rsp),%rdi
mov    $0x200,%edx
xor    %esi,%esi
call   1080 <memset@plt>
mov    %rbp,%rdi
mov    $0x40,%esi
call   1060 <backtrace@plt>
mov    %rax,%r13
mov    %rbp,%rdi
mov    %rax,%rsi
call   1040 <backtrace_symbols@plt>
mov    %rax,%r15
movb   $0x0,(%rbx)
test   %r13,%r13
je     1224 <log_dump_trace+0xb4>
xor    %r14d,%r14d
lea    0x10(%rsp),%rbp
xchg   %ax,%ax
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
not    %r12d
add    0xc(%rsp),%r12d
js     1224 <log_dump_trace+0xb4>
mov    (%r15,%r14,8),%rcx
mov    %rbp,%rdi
mov    $0x200,%esi
lea    0xdf8(%rip),%rdx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1090 <strncat@plt>
add    $0x1,%r14
cmp    %r14,%r13
jne    11e0 <log_dump_trace+0x70>
mov    %r15,%rdi
call   1030 <free@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"\09%s\0A\00"
define void @log_dump_trace(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [64 x i8*]
%7 = alloca i64
%8 = alloca i8**
%9 = alloca i64
%10 = alloca [512 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%11 = bitcast [64 x i8*]* %6 to i8*
store i64 0, i64* %7
store i8** null, i8*** %8
store i64 0, i64* %9
%12 = bitcast [512 x i8]* %10 to i8*
%13 = getelementptr inbounds [64 x i8*], [64 x i8*]* %6, i64 0, i64 0
%14 = call i64 @backtrace(i8** %13, i32 64)
store i64 %14, i64* %7
%15 = getelementptr inbounds [64 x i8*], [64 x i8*]* %6, i64 0, i64 0
%16 = load i64, i64* %7
%17 = call i8** @backtrace_symbols(i8** %15, i64 %16)
store i8** %17, i8*** %8
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i64 0
store i8 0, i8* %19
store i64 0, i64* %9
br label %20
20: ; preds = %43, %2
%21 = load i64, i64* %9
%22 = load i64, i64* %7
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %46
24: ; preds = %20
%25 = load i32, i32* %4
%26 = sub nsw i32 %25, 1
%27 = load i8*, i8** %3
%28 = call i32 @strlen(i8* %27)
%29 = sub nsw i32 %26, %28
store i32 %29, i32* %5
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %24
br label %46
32: ; preds = %24
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%34 = load i8**, i8*** %8
%35 = load i64, i64* %9
%36 = getelementptr inbounds i8*, i8** %34, i64 %35
%37 = load i8*, i8** %36
%38 = call i32 @snprintf(i8* %33, i32 512, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %37)
%39 = load i8*, i8** %3
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%41 = load i32, i32* %5
%42 = call i32 @strncat(i8* %39, i8* %40, i32 %41)
br label %43
43: ; preds = %32
%44 = load i64, i64* %9
%45 = add i64 %44, 1
store i64 %45, i64* %9
br label %20
46: ; preds = %31, %20
%47 = load i8**, i8*** %8
%48 = call i32 @free(i8** %47)
store i8** null, i8*** %8
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"\09%s\0A\00"
define void @log_dump_trace(i8* %0, i32 %1) {
%3 = alloca [64 x i8*]
%4 = alloca [512 x i8]
%5 = bitcast [64 x i8*]* %3 to i8*
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [64 x i8*], [64 x i8*]* %3, i64 0, i64 0
%8 = call i64 @backtrace(i8** %7, i32 64)
%9 = call i8** @backtrace_symbols(i8** %7, i64 %8)
store i8 0, i8* %0
%10 = icmp eq i64 %8, 0
br i1 %10, label %24, label %11
11: ; preds = %2, %17
%12 = phi i64 [ %22, %17 ], [ 0, %2 ]
%13 = call i32 @strlen(i8* %0)
%14 = xor i32 %13, -1
%15 = add i32 %14, %1
%16 = icmp slt i32 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %11
%18 = getelementptr inbounds i8*, i8** %9, i64 %12
%19 = load i8*, i8** %18
%20 = call i32 @snprintf(i8* %6, i32 512, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @strncat(i8* %0, i8* %6, i32 %15)
%22 = add nuw i64 %12, 1
%23 = icmp eq i64 %22, %8
br i1 %23, label %24, label %11
24: ; preds = %17, %11, %2
%25 = call i32 @free(i8** %9)
ret void
}