#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned int);
int /*<<< orphan*/ free(unsigned int *);
scalar_t__ malloc(unsigned int);
int trace_geteachoff(int);
int trace_geteachsize(int);
int /*<<< orphan*/ trace_gettotalcon(unsigned int *);
int trace_gettotalsize();
void dump_trace(int index, char *TRACE_FILE) {
  FILE *trace_f = fopen(TRACE_FILE, "a");
  fprintf(trace_f, "%d\n", index);
  int i;
  unsigned int curr_trace_num = trace_gettotalsize() / sizeof(unsigned int);
  unsigned int *trace_ram =
      (unsigned int *)malloc(curr_trace_num * sizeof(unsigned int));
  trace_gettotalcon(trace_ram);
  int cur_size = trace_geteachsize(index) / sizeof(unsigned int);
  int cur_off = trace_geteachoff(index) / sizeof(unsigned int);
  while (trace_ram[cur_size + cur_off - 1] == 0) {
    trace_gettotalcon(trace_ram);
  }
  for (i = cur_off; i < cur_off + cur_size; i++) {
    fprintf(trace_f, "%08X\n", trace_ram[i]);
  }
  free(trace_ram);
  fclose(trace_f);
}


x86 assembly (clang, -O0):
<dump_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe41(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x0,%al
call   10b0 <trace_gettotalsize@plt>
cltq
shr    $0x2,%rax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <trace_gettotalcon@plt>
mov    -0x4(%rbp),%edi
call   1050 <trace_geteachsize@plt>
cltq
shr    $0x2,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
call   10a0 <trace_geteachoff@plt>
cltq
shr    $0x2,%rax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    -0x30(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmpl   $0x0,(%rax,%rcx,4)
jne    123b <dump_trace+0xab>
mov    -0x28(%rbp),%rdi
call   1080 <trace_gettotalcon@plt>
jmp    1213 <dump_trace+0x83>
mov    -0x30(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x30(%rbp),%ecx
add    -0x2c(%rbp),%ecx
cmp    %ecx,%eax
jge    127b <dump_trace+0xeb>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xd9e(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1241 <dump_trace+0xb1>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_trace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rax
mov    %edi,%r12d
lea    0xe58(%rip),%rsi
mov    %rax,%rdi
call   1090 <fopen@plt>
mov    %rax,%r14
lea    0xe48(%rip),%rsi
mov    %rax,%rdi
mov    %r12d,%edx
call   1060 <fprintf@plt>
xor    %eax,%eax
call   10b0 <trace_gettotalsize@plt>
and    $0xfffffffc,%eax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <trace_gettotalcon@plt>
mov    %r12d,%edi
call   1050 <trace_geteachsize@plt>
mov    %eax,%r15d
mov    %eax,%ebp
sar    $0x2,%ebp
mov    %r12d,%edi
call   10a0 <trace_geteachoff@plt>
mov    %eax,%r12d
sar    $0x2,%r12d
add    %r12d,%ebp
movslq %ebp,%r13
cmpl   $0x0,-0x4(%rbx,%r13,4)
jne    122e <dump_trace+0x9e>
lea    -0x1(%r13),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1080 <trace_gettotalcon@plt>
cmpl   $0x0,(%rbx,%rbp,4)
je     1220 <dump_trace+0x90>
cmp    $0x4,%r15d
jl     1257 <dump_trace+0xc7>
movslq %r12d,%rbp
lea    0xdc8(%rip),%r15
xchg   %ax,%ax
mov    (%rbx,%rbp,4),%edx
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <fprintf@plt>
add    $0x1,%rbp
cmp    %r13,%rbp
jl     1240 <dump_trace+0xb0>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"a\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [6 x i8] c"%08X\0A\00"
define void @dump_trace(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %5
%13 = load i32*, i32** %5
%14 = load i32, i32* %3
%15 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i32 (...) @trace_gettotalsize()
%17 = sext i32 %16 to i64
%18 = udiv i64 %17, 4
%19 = trunc i64 %18 to i32
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = zext i32 %20 to i64
%22 = mul i64 %21, 4
%23 = trunc i64 %22 to i32
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i32*
store i32* %25, i32** %8
%26 = load i32*, i32** %8
%27 = call i32 @trace_gettotalcon(i32* %26)
%28 = load i32, i32* %3
%29 = call i32 @trace_geteachsize(i32 %28)
%30 = sext i32 %29 to i64
%31 = udiv i64 %30, 4
%32 = trunc i64 %31 to i32
store i32 %32, i32* %9
%33 = load i32, i32* %3
%34 = call i32 @trace_geteachoff(i32 %33)
%35 = sext i32 %34 to i64
%36 = udiv i64 %35, 4
%37 = trunc i64 %36 to i32
store i32 %37, i32* %10
br label %38
38: ; preds = %48, %2
%39 = load i32*, i32** %8
%40 = load i32, i32* %9
%41 = load i32, i32* %10
%42 = add nsw i32 %40, %41
%43 = sub nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %39, i64 %44
%46 = load i32, i32* %45
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %38
%49 = load i32*, i32** %8
%50 = call i32 @trace_gettotalcon(i32* %49)
br label %38
51: ; preds = %38
%52 = load i32, i32* %10
store i32 %52, i32* %6
br label %53
53: ; preds = %67, %51
%54 = load i32, i32* %6
%55 = load i32, i32* %10
%56 = load i32, i32* %9
%57 = add nsw i32 %55, %56
%58 = icmp slt i32 %54, %57
br i1 %58, label %59, label %70
59: ; preds = %53
%60 = load i32*, i32** %5
%61 = load i32*, i32** %8
%62 = load i32, i32* %6
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i32, i32* %61, i64 %63
%65 = load i32, i32* %64
%66 = call i32 @fprintf(i32* %60, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %59
%68 = load i32, i32* %6
%69 = add nsw i32 %68, 1
store i32 %69, i32* %6
br label %53
70: ; preds = %53
%71 = load i32*, i32** %8
%72 = call i32 @free(i32* %71)
%73 = load i32*, i32** %5
%74 = call i32 @fclose(i32* %73)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"a\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [6 x i8] c"%08X\0A\00"
define void @dump_trace(i32 %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%5 = tail call i32 (...) @trace_gettotalsize()
%6 = and i32 %5, -4
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i32*
%9 = tail call i32 @trace_gettotalcon(i32* %8)
%10 = tail call i32 @trace_geteachsize(i32 %0)
%11 = ashr i32 %10, 2
%12 = tail call i32 @trace_geteachoff(i32 %0)
%13 = ashr i32 %12, 2
%14 = add nsw i32 %13, %11
%15 = add nsw i32 %14, -1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i32, i32* %8, i64 %16
%18 = load i32, i32* %17
%19 = icmp eq i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %25, %2
%21 = icmp sgt i32 %10, 3
br i1 %21, label %22, label %36
22: ; preds = %20
%23 = sext i32 %13 to i64
%24 = sext i32 %14 to i64
br label %29
25: ; preds = %2, %25
%26 = tail call i32 @trace_gettotalcon(i32* %8)
%27 = load i32, i32* %17
%28 = icmp eq i32 %27, 0
br i1 %28, label %25, label %20
29: ; preds = %22, %29
%30 = phi i64 [ %23, %22 ], [ %34, %29 ]
%31 = getelementptr inbounds i32, i32* %8, i64 %30
%32 = load i32, i32* %31
%33 = tail call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = add nsw i64 %30, 1
%35 = icmp slt i64 %34, %24
br i1 %35, label %29, label %36
36: ; preds = %29, %20
%37 = tail call i32 @free(i32* %8)
%38 = tail call i32 @fclose(i32* %3)
ret void
}