#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 */
size_t backtrace(void **, int);
char **backtrace_symbols(void **, size_t);
int /*<<< orphan*/ free(char **);
int snprintf(char *, int, char *, ...);
char *__TraceLinuxStack(char szStackInfoString[], int iMaxStackInfoStringLen) {
  int iStackInfoStringSize = iMaxStackInfoStringLen;
  char *pCurBuff = szStackInfoString;
  void *bt[20] = {NULL};
  int iLen = snprintf(pCurBuff, iStackInfoStringSize, "TrackStack Begin\n");
  iStackInfoStringSize -= iLen;
  pCurBuff += iLen;
  size_t sz = backtrace(bt, 20);
  char **ppstrs = backtrace_symbols(bt, sz);
  for (size_t i = 2; i < sz; ++i) {
    iLen = snprintf(pCurBuff, iStackInfoStringSize, "%s \n", ppstrs[i]);
    iStackInfoStringSize -= iLen;
    pCurBuff += iLen;
  }
  free(ppstrs);
  iLen = snprintf(pCurBuff, iStackInfoStringSize, "TrackStack End\n");
  iStackInfoStringSize -= iLen;
  pCurBuff += iLen;
  return szStackInfoString;
}


x86 assembly (clang, -O0):
<__TraceLinuxStack>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0xc0(%rbp),%rdi
xor    %esi,%esi
mov    $0xa0,%edx
call   1070 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
lea    0xe6f(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %eax,-0xc4(%rbp)
mov    -0xc4(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc4(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
lea    -0xc0(%rbp),%rdi
mov    $0x14,%esi
call   1050 <backtrace@plt>
mov    %rax,-0xd0(%rbp)
lea    -0xc0(%rbp),%rdi
mov    -0xd0(%rbp),%rsi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0xd8(%rbp)
movq   $0x2,-0xe0(%rbp)
mov    -0xe0(%rbp),%rax
cmp    -0xd0(%rbp),%rax
jae    1277 <__TraceLinuxStack+0x127>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xd8(%rbp),%rax
mov    -0xe0(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xde1(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %eax,-0xc4(%rbp)
mov    -0xc4(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc4(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0xe0(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xe0(%rbp)
jmp    11fd <__TraceLinuxStack+0xad>
mov    -0xd8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
lea    0xd86(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %eax,-0xc4(%rbp)
mov    -0xc4(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc4(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__TraceLinuxStack>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
xorps  %xmm0,%xmm0
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0xe65(%rip),%rdx
xor    %eax,%eax
call   1060 <snprintf@plt>
sub    %eax,%ebp
movslq %eax,%r14
mov    %rbx,0x8(%rsp)
add    %rbx,%r14
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x14,%esi
call   1050 <backtrace@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <backtrace_symbols@plt>
mov    %rax,%r15
cmp    $0x3,%r12
jb     120d <__TraceLinuxStack+0xcd>
mov    $0x2,%r13d
xchg   %ax,%ax
mov    (%r15,%r13,8),%rcx
mov    %r14,%rdi
mov    %ebp,%esi
lea    0xe22(%rip),%rdx
xor    %eax,%eax
call   1060 <snprintf@plt>
sub    %eax,%ebp
movslq %eax,%rbx
add    %r14,%rbx
add    $0x1,%r13
mov    %rbx,%r14
cmp    %r13,%r12
jne    11e0 <__TraceLinuxStack+0xa0>
jmp    1210 <__TraceLinuxStack+0xd0>
mov    %r14,%rbx
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xdf8(%rip),%rdx
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    0x8(%rsp),%rax
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"TrackStack Begin\0A\00"
@.str.1 = [5 x i8] c"%s \0A\00"
@.str.2 = [16 x i8] c"TrackStack End\0A\00"
define i8* @__TraceLinuxStack(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [20 x i8*]
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8**
%11 = alloca i64
store i8* %0, i8** %3
store i32 %1, i32* %4
%12 = load i32, i32* %4
store i32 %12, i32* %5
%13 = load i8*, i8** %3
store i8* %13, i8** %6
%14 = bitcast [20 x i8*]* %7 to i8*
%15 = load i8*, i8** %6
%16 = load i32, i32* %5
%17 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %15, i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = load i32, i32* %5
%20 = sub nsw i32 %19, %18
store i32 %20, i32* %5
%21 = load i32, i32* %8
%22 = load i8*, i8** %6
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8* %24, i8** %6
%25 = getelementptr inbounds [20 x i8*], [20 x i8*]* %7, i64 0, i64 0
%26 = call i64 @backtrace(i8** %25, i32 20)
store i64 %26, i64* %9
%27 = getelementptr inbounds [20 x i8*], [20 x i8*]* %7, i64 0, i64 0
%28 = load i64, i64* %9
%29 = call i8** @backtrace_symbols(i8** %27, i64 %28)
store i8** %29, i8*** %10
store i64 2, i64* %11
br label %30
30: ; preds = %49, %2
%31 = load i64, i64* %11
%32 = load i64, i64* %9
%33 = icmp ult i64 %31, %32
br i1 %33, label %34, label %52
34: ; preds = %30
%35 = load i8*, i8** %6
%36 = load i32, i32* %5
%37 = load i8**, i8*** %10
%38 = load i64, i64* %11
%39 = getelementptr inbounds i8*, i8** %37, i64 %38
%40 = load i8*, i8** %39
%41 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %35, i32 %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %40)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = load i32, i32* %5
%44 = sub nsw i32 %43, %42
store i32 %44, i32* %5
%45 = load i32, i32* %8
%46 = load i8*, i8** %6
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8* %48, i8** %6
br label %49
49: ; preds = %34
%50 = load i64, i64* %11
%51 = add i64 %50, 1
store i64 %51, i64* %11
br label %30
52: ; preds = %30
%53 = load i8**, i8*** %10
%54 = call i32 @free(i8** %53)
%55 = load i8*, i8** %6
%56 = load i32, i32* %5
%57 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %55, i32 %56, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 %57, i32* %8
%58 = load i32, i32* %8
%59 = load i32, i32* %5
%60 = sub nsw i32 %59, %58
store i32 %60, i32* %5
%61 = load i32, i32* %8
%62 = load i8*, i8** %6
%63 = sext i32 %61 to i64
%64 = getelementptr inbounds i8, i8* %62, i64 %63
store i8* %64, i8** %6
%65 = load i8*, i8** %3
ret i8* %65
}

LLVM IR (-O2):
@.str = [18 x i8] c"TrackStack Begin\0A\00"
@.str.1 = [5 x i8] c"%s \0A\00"
@.str.2 = [16 x i8] c"TrackStack End\0A\00"
define i8* @__TraceLinuxStack(i8* returned %0, i32 %1) {
%3 = alloca [20 x i8*]
%4 = bitcast [20 x i8*]* %3 to i8*
%5 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%6 = sub nsw i32 %1, %5
%7 = sext i32 %5 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = getelementptr inbounds [20 x i8*], [20 x i8*]* %3, i64 0, i64 0
%10 = call i64 @backtrace(i8** %9, i32 20)
%11 = call i8** @backtrace_symbols(i8** %9, i64 %10)
%12 = icmp ugt i64 %10, 2
br i1 %12, label %18, label %13
13: ; preds = %18, %2
%14 = phi i8* [ %8, %2 ], [ %27, %18 ]
%15 = phi i32 [ %6, %2 ], [ %25, %18 ]
%16 = call i32 @free(i8** %11)
%17 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %14, i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
ret i8* %0
18: ; preds = %2, %18
%19 = phi i64 [ %28, %18 ], [ 2, %2 ]
%20 = phi i32 [ %25, %18 ], [ %6, %2 ]
%21 = phi i8* [ %27, %18 ], [ %8, %2 ]
%22 = getelementptr inbounds i8*, i8** %11, i64 %19
%23 = load i8*, i8** %22
%24 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %21, i32 %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = sub nsw i32 %20, %24
%26 = sext i32 %24 to i64
%27 = getelementptr inbounds i8, i8* %21, i64 %26
%28 = add nuw i64 %19, 1
%29 = icmp eq i64 %28, %10
br i1 %29, label %13, label %18
}