#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*/ addCallTrace(unsigned int);
int /*<<< orphan*/ addSymbol(unsigned int);
int /*<<< orphan*/ emitSymbols();
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *, unsigned int *);
int /*<<< orphan*/ initSymbol(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stackInit();
int /*<<< orphan*/ stackPop();
int /*<<< orphan*/ stackPush(unsigned int);
int main(int argc, char *argv[]) {
  FILE *tracef;
  char type;
  unsigned int address;
  if (argc != 2) {
    printf("Usage: pvtrace <image>\n\n");
    exit(-1);
  }
  initSymbol(argv[1]);
  stackInit();
  tracef = fopen("trace.txt", "r");
  if (tracef == NULL) {
    printf("Can't open trace.txt\n");
    exit(-1);
  }
  while (!feof(tracef)) {
    fscanf(tracef, "%c0x%x\n", &type, &address);
    if (type == 'E') {
      addSymbol(address);
      addCallTrace(address);
      stackPush(address);
    } else if (type == 'X') {
      (void)stackPop();
    }
  }
  emitSymbols();
  fclose(tracef);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1206 <main+0x36>
lea    0xe09(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <initSymbol@plt>
mov    $0x0,%al
call   1090 <stackInit@plt>
lea    0xdf8(%rip),%rdi
lea    0xdfb(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1252 <main+0x82>
lea    0xde2(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
jmp    1257 <main+0x87>
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1275 <main+0xa5>
jmp    12d5 <main+0x105>
mov    -0x18(%rbp),%rdi
lea    0xdbb(%rip),%rsi
lea    -0x19(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1050 <fscanf@plt>
movsbl -0x19(%rbp),%eax
cmp    $0x45,%eax
jne    12b7 <main+0xe7>
mov    -0x20(%rbp),%edi
call   1070 <addSymbol@plt>
mov    -0x20(%rbp),%edi
call   10e0 <addCallTrace@plt>
mov    -0x20(%rbp),%edi
call   10f0 <stackPush@plt>
jmp    12d0 <main+0x100>
movsbl -0x19(%rbp),%eax
cmp    $0x58,%eax
jne    12cb <main+0xfb>
mov    $0x0,%al
call   10c0 <stackPop@plt>
jmp    12d0 <main+0x100>
jmp    1257 <main+0x87>
mov    $0x0,%al
call   10a0 <emitSymbols@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    129e <main+0xce>
mov    0x8(%rsi),%rdi
call   1080 <initSymbol@plt>
xor    %eax,%eax
call   1090 <stackInit@plt>
lea    0xe1d(%rip),%rdi
lea    0xe01(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12a7 <main+0xd7>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     1239 <main+0x69>
xor    %eax,%eax
call   10a0 <emitSymbols@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdc2(%rip),%r14
lea    0x3(%rsp),%r15
lea    0x4(%rsp),%r12
jmp    1263 <main+0x93>
nopl   0x0(%rax)
xor    %eax,%eax
call   10c0 <stackPop@plt>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    121c <main+0x4c>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
call   1050 <fscanf@plt>
movzbl 0x3(%rsp),%eax
cmp    $0x58,%al
je     1250 <main+0x80>
cmp    $0x45,%al
jne    1257 <main+0x87>
mov    0x4(%rsp),%edi
call   1070 <addSymbol@plt>
mov    0x4(%rsp),%edi
call   10e0 <addCallTrace@plt>
mov    0x4(%rsp),%edi
call   10f0 <stackPush@plt>
jmp    1257 <main+0x87>
lea    0xd7a(%rip),%rdi
jmp    12ae <main+0xde>
lea    0xd5c(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Usage: pvtrace <image>\0A\0A\00"
@.str.1 = [10 x i8] c"trace.txt\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [22 x i8] c"Can't open trace.txt\0A\00"
@.str.4 = [8 x i8] c"%c0x%x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @initSymbol(i8* %17)
%19 = call i32 (...) @stackInit()
%20 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %14
br label %27
27: ; preds = %52, %26
%28 = load i32*, i32** %6
%29 = call i32 @feof(i32* %28)
%30 = icmp ne i32 %29, 0
%31 = xor i1 %30, true
br i1 %31, label %32, label %53
32: ; preds = %27
%33 = load i32*, i32** %6
%34 = call i32 @fscanf(i32* %33, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %7, i32* %8)
%35 = load i8, i8* %7
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 69
br i1 %37, label %38, label %45
38: ; preds = %32
%39 = load i32, i32* %8
%40 = call i32 @addSymbol(i32 %39)
%41 = load i32, i32* %8
%42 = call i32 @addCallTrace(i32 %41)
%43 = load i32, i32* %8
%44 = call i32 @stackPush(i32 %43)
br label %52
45: ; preds = %32
%46 = load i8, i8* %7
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 88
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 (...) @stackPop()
br label %51
51: ; preds = %49, %45
br label %52
52: ; preds = %51, %38
br label %27
53: ; preds = %27
%54 = call i32 (...) @emitSymbols()
%55 = load i32*, i32** %6
%56 = call i32 @fclose(i32* %55)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [10 x i8] c"trace.txt\00"
@.str.2 = [2 x i8] c"r\00"
@.str.4 = [8 x i8] c"%c0x%x\0A\00"
@str = [21 x i8] c"Can't open trace.txt\00"
@str.5 = [24 x i8] c"Usage: pvtrace <image>\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp eq i32 %0, 2
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.5, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @initSymbol(i8* %12)
%14 = tail call i32 (...) @stackInit()
%15 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %10
%18 = call i32 @feof(i32* %15)
%19 = icmp eq i32 %18, 0
br i1 %19, label %23, label %38
20: ; preds = %10
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %17, %35
%24 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %3, i32* %4)
%25 = load i8, i8* %3
switch i8 %25, label %35 [
i8 69, label %26
i8 88, label %33
]
26: ; preds = %23
%27 = load i32, i32* %4
%28 = call i32 @addSymbol(i32 %27)
%29 = load i32, i32* %4
%30 = call i32 @addCallTrace(i32 %29)
%31 = load i32, i32* %4
%32 = call i32 @stackPush(i32 %31)
br label %35
33: ; preds = %23
%34 = call i32 (...) @stackPop()
br label %35
35: ; preds = %23, %33, %26
%36 = call i32 @feof(i32* %15)
%37 = icmp eq i32 %36, 0
br i1 %37, label %23, label %38
38: ; preds = %35, %17
%39 = call i32 (...) @emitSymbols()
%40 = call i32 @fclose(i32* %15)
ret i32 0
}