#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 mode_t;

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_RDWR;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int backtrace(void **, int);
int /*<<< orphan*/ backtrace_symbols_fd(void **, int, int);
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ write(int, char *, int);
void DumpTrace(void) {
  void *buffer[(100)] = {NULL};
  char **trace = NULL;
  int size = backtrace(buffer, (100));
  int flags = O_CREAT | O_RDWR | O_APPEND;
  mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
  int fd = open(("./memory_count"), flags, mode);
  if (-1 == fd) {
    return;
  }
  int sz = size > (10) ? (10) : size;
  backtrace_symbols_fd(buffer, sz, fd);
  write(fd, "--------------------\n\n", 22);
  close(fd);
}


x86 assembly (clang, -O0):
<DumpTrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x340,%rsp
lea    -0x320(%rbp),%rdi
xor    %esi,%esi
mov    $0x320,%edx
call   1060 <memset@plt>
movq   $0x0,-0x328(%rbp)
lea    -0x320(%rbp),%rdi
mov    $0x64,%esi
call   1050 <backtrace@plt>
mov    %eax,-0x32c(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    0x2e20(%rip),%rcx
or     (%rcx),%eax
mov    0x2df7(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x330(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    0x2df7(%rip),%rcx
or     (%rcx),%eax
mov    0x2e0e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x334(%rbp)
mov    -0x330(%rbp),%esi
mov    -0x334(%rbp),%edx
lea    0xe0b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x338(%rbp)
mov    $0xffffffff,%eax
cmp    -0x338(%rbp),%eax
jne    1216 <DumpTrace+0xb6>
jmp    1285 <DumpTrace+0x125>
cmpl   $0xa,-0x32c(%rbp)
jle    1233 <DumpTrace+0xd3>
mov    $0xa,%eax
mov    %eax,-0x340(%rbp)
jmp    123f <DumpTrace+0xdf>
mov    -0x32c(%rbp),%eax
mov    %eax,-0x340(%rbp)
mov    -0x340(%rbp),%eax
mov    %eax,-0x33c(%rbp)
lea    -0x320(%rbp),%rdi
mov    -0x33c(%rbp),%esi
mov    -0x338(%rbp),%edx
call   1030 <backtrace_symbols_fd@plt>
mov    -0x338(%rbp),%edi
lea    0xd9f(%rip),%rsi
mov    $0x16,%edx
call   1040 <write@plt>
mov    -0x338(%rbp),%edi
call   1070 <close@plt>
add    $0x340,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<DumpTrace>:
push   %rbp
push   %rbx
sub    $0x328,%rsp
mov    %rsp,%rbx
mov    $0x320,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    $0x64,%esi
call   1050 <backtrace@plt>
mov    0x2e69(%rip),%rcx
mov    0x2e3a(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2e0f(%rip),%rcx
or     (%rcx),%esi
mov    0x2e2e(%rip),%rcx
mov    0x2e17(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %eax,%ebp
mov    0x2e2a(%rip),%rax
or     (%rax),%edx
lea    0xe39(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1202 <DumpTrace+0xa2>
mov    %eax,%ebx
cmp    $0xa,%ebp
mov    $0xa,%esi
cmovl  %ebp,%esi
mov    %rsp,%rdi
mov    %eax,%edx
call   1030 <backtrace_symbols_fd@plt>
lea    0xe20(%rip),%rsi
mov    %ebx,%edi
mov    $0x16,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x328,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str = [15 x i8] c"./memory_count\00"
@.str.1 = [23 x i8] c"--------------------\0A\0A\00"
define void @DumpTrace() {
%1 = alloca [100 x i8*]
%2 = alloca i8**
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = bitcast [100 x i8*]* %1 to i8*
store i8** null, i8*** %2
%9 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 0
%10 = call i32 @backtrace(i8** %9, i32 100)
store i32 %10, i32* %3
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_RDWR
%13 = or i32 %11, %12
%14 = load i32, i32* @O_APPEND
%15 = or i32 %13, %14
store i32 %15, i32* %4
%16 = load i32, i32* @S_IRWXU
%17 = load i32, i32* @S_IRWXG
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRWXO
%20 = or i32 %18, %19
store i32 %20, i32* %5
%21 = load i32, i32* %4
%22 = load i32, i32* %5
%23 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp eq i32 -1, %24
br i1 %25, label %26, label %27
26: ; preds = %0
br label %43
27: ; preds = %0
%28 = load i32, i32* %3
%29 = icmp sgt i32 %28, 10
br i1 %29, label %30, label %31
30: ; preds = %27
br label %33
31: ; preds = %27
%32 = load i32, i32* %3
br label %33
33: ; preds = %31, %30
%34 = phi i32 [ 10, %30 ], [ %32, %31 ]
store i32 %34, i32* %7
%35 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 0
%36 = load i32, i32* %7
%37 = load i32, i32* %6
%38 = call i32 @backtrace_symbols_fd(i8** %35, i32 %36, i32 %37)
%39 = load i32, i32* %6
%40 = call i32 @write(i32 %39, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 22)
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
br label %43
43: ; preds = %33, %26
ret void
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@.str = [15 x i8] c"./memory_count\00"
@.str.1 = [23 x i8] c"--------------------\0A\0A\00"
define void @DumpTrace() {
%1 = alloca [100 x i8*]
%2 = bitcast [100 x i8*]* %1 to i8*
%3 = getelementptr inbounds [100 x i8*], [100 x i8*]* %1, i64 0, i64 0
%4 = call i32 @backtrace(i8** %3, i32 100)
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %6, %5
%8 = load i32, i32* @O_APPEND
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRWXU
%11 = load i32, i32* @S_IRWXG
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IRWXO
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %23, label %17
17: ; preds = %0
%18 = icmp slt i32 %4, 10
%19 = select i1 %18, i32 %4, i32 10
%20 = call i32 @backtrace_symbols_fd(i8** %3, i32 %19, i32 %15)
%21 = call i32 @write(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 22)
%22 = call i32 @close(i32 %15)
br label %23
23: ; preds = %0, %17
ret void
}