#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*/ debugProgram();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ initializeVars();
char initializeVarsForTick(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
scalar_t__ readProgram(char *);
int tick();
int /*<<< orphan*/ writeSignals(int, int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  if (argc < 4) {
    printf("usage: desk_vm input.bal input.tv output.simout\n");
    return 1;
  }
  initializeVars();
  if (readProgram(argv[1]) != 0)
    return 1;
  debugProgram();
  printf("checkpoint 1\n");
  FILE *testFile;
  testFile = fopen(argv[2], "r");
  if (testFile == NULL) {
    printf("Test Vectors File open failure!\n");
    return 3;
  }
  FILE *pFile;
  pFile = fopen(argv[3], "w+");
  if (pFile == NULL) {
    printf("File open failue\n");
    return 3;
  }
  int tickct = 1;
  char val;
  int tickerror = 0;
  char signalInitError;
  do {
    signalInitError = initializeVarsForTick(testFile);
    if (signalInitError) {
      printf("\nNo more signals found to simulate\n");
      return signalInitError;
    }
    tickerror = tick();
    writeSignals(tickct, pFile);
    if (!tickerror) {
      tickct++;
      val = 'n';
    }
  } while (val == 'y' && !tickerror);
  fclose(pFile);
  printf("checkpoint 2\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11c8 <main+0x38>
lea    0xe49(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    132e <main+0x19e>
mov    $0x0,%al
call   1080 <initializeVars@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <readProgram@plt>
cmp    $0x0,%rax
je     11f2 <main+0x62>
movl   $0x1,-0x4(%rbp)
jmp    132e <main+0x19e>
mov    $0x0,%al
call   1060 <debugProgram@plt>
lea    0xe31(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe2b(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1240 <main+0xb0>
lea    0xe12(%rip),%rdi
call   1050 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    132e <main+0x19e>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xe13(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    127b <main+0xeb>
lea    0xdfb(%rip),%rdi
call   1050 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    132e <main+0x19e>
movl   $0x1,-0x24(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <initializeVarsForTick@plt>
mov    %al,-0x2d(%rbp)
cmpb   $0x0,-0x2d(%rbp)
je     12b7 <main+0x127>
lea    0xdd1(%rip),%rdi
call   1050 <printf@plt>
movsbl -0x2d(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132e <main+0x19e>
mov    $0x0,%al
call   1030 <tick@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1090 <writeSignals@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    12e4 <main+0x154>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
movb   $0x6e,-0x25(%rbp)
jmp    12e9 <main+0x159>
movsbl -0x25(%rbp),%ecx
xor    %eax,%eax
cmp    $0x79,%ecx
mov    %al,-0x2e(%rbp)
jne    1307 <main+0x177>
cmpl   $0x0,-0x2c(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x2e(%rbp)
mov    -0x2e(%rbp),%al
test   $0x1,%al
jne    1289 <main+0xf9>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd79(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x3,%edi
jg     11af <main+0x1f>
lea    0xed3(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebp
jmp    1248 <main+0xb8>
mov    %rsi,%rbx
xor    %eax,%eax
call   1080 <initializeVars@plt>
mov    0x8(%rbx),%rdi
call   1070 <readProgram@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    1248 <main+0xb8>
xor    %eax,%eax
call   1060 <debugProgram@plt>
lea    0xe2b(%rip),%rdi
call   1040 <puts@plt>
mov    0x10(%rbx),%rdi
lea    0xe16(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     122e <main+0x9e>
mov    %rax,%rbp
mov    0x18(%rbx),%rdi
lea    0xe00(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1237 <main+0xa7>
mov    %rax,%r14
mov    %rbp,%rdi
call   10a0 <initializeVarsForTick@plt>
test   %al,%al
je     124f <main+0xbf>
mov    %eax,%ebx
lea    0xdfb(%rip),%rdi
call   1040 <puts@plt>
movsbl %bl,%ebp
jmp    1248 <main+0xb8>
lea    0xe1e(%rip),%rdi
jmp    123e <main+0xae>
lea    0xe04(%rip),%rdi
call   1040 <puts@plt>
mov    $0x3,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <tick@plt>
mov    $0x1,%edi
mov    %r14,%rsi
call   1090 <writeSignals@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
lea    0xd9e(%rip),%rdi
call   1040 <puts@plt>
jmp    1248 <main+0xb8>

LLVM IR (-O0):
@.str = [49 x i8] c"usage: desk_vm input.bal input.tv output.simout\0A\00"
@.str.1 = [14 x i8] c"checkpoint 1\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [33 x i8] c"Test Vectors File open failure!\0A\00"
@.str.4 = [3 x i8] c"w+\00"
@.str.5 = [18 x i8] c"File open failue\0A\00"
@.str.6 = [36 x i8] c"\0ANo more signals found to simulate\0A\00"
@.str.7 = [14 x i8] c"checkpoint 2\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i8
%10 = alloca i32
%11 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 4
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %79
16: ; preds = %2
%17 = call i32 (...) @initializeVars()
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i64 @readProgram(i8* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %16
store i32 1, i32* %3
br label %79
24: ; preds = %16
%25 = call i32 (...) @debugProgram()
%26 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %30, i32** %6
%31 = load i32*, i32** %6
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %35
33: ; preds = %24
%34 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
store i32 3, i32* %3
br label %79
35: ; preds = %24
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 3
%38 = load i8*, i8** %37
%39 = call i32* @fopen(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %39, i32** %7
%40 = load i32*, i32** %7
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %44
42: ; preds = %35
%43 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
store i32 3, i32* %3
br label %79
44: ; preds = %35
store i32 1, i32* %8
store i32 0, i32* %10
br label %45
45: ; preds = %73, %44
%46 = load i32*, i32** %6
%47 = call signext i8 @initializeVarsForTick(i32* %46)
store i8 %47, i8* %11
%48 = load i8, i8* %11
%49 = icmp ne i8 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %45
%51 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%52 = load i8, i8* %11
%53 = sext i8 %52 to i32
store i32 %53, i32* %3
br label %79
54: ; preds = %45
%55 = call i32 (...) @tick()
store i32 %55, i32* %10
%56 = load i32, i32* %8
%57 = load i32*, i32** %7
%58 = call i32 @writeSignals(i32 %56, i32* %57)
%59 = load i32, i32* %10
%60 = icmp ne i32 %59, 0
br i1 %60, label %64, label %61
61: ; preds = %54
%62 = load i32, i32* %8
%63 = add nsw i32 %62, 1
store i32 %63, i32* %8
store i8 110, i8* %9
br label %64
64: ; preds = %61, %54
br label %65
65: ; preds = %64
%66 = load i8, i8* %9
%67 = sext i8 %66 to i32
%68 = icmp eq i32 %67, 121
br i1 %68, label %69, label %73
69: ; preds = %65
%70 = load i32, i32* %10
%71 = icmp ne i32 %70, 0
%72 = xor i1 %71, true
br label %73
73: ; preds = %69, %65
%74 = phi i1 [ false, %65 ], [ %72, %69 ]
br i1 %74, label %45, label %75
75: ; preds = %73
%76 = load i32*, i32** %7
%77 = call i32 @fclose(i32* %76)
%78 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %3
br label %79
79: ; preds = %75, %50, %42, %33, %23, %14
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
@.str.2 = [2 x i8] c"r\00"
@.str.4 = [3 x i8] c"w+\00"
@str = [13 x i8] c"checkpoint 1\00"
@str.8 = [13 x i8] c"checkpoint 2\00"
@str.9 = [35 x i8] c"\0ANo more signals found to simulate\00"
@str.10 = [17 x i8] c"File open failue\00"
@str.11 = [32 x i8] c"Test Vectors File open failure!\00"
@str.12 = [48 x i8] c"usage: desk_vm input.bal input.tv output.simout\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 4
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.12, i64 0, i64 0))
br label %39
6: ; preds = %2
%7 = tail call i32 (...) @initializeVars()
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i64 @readProgram(i8* %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %39
12: ; preds = %6
%13 = tail call i32 (...) @debugProgram()
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.11, i64 0, i64 0))
br label %39
21: ; preds = %12
%22 = getelementptr inbounds i8*, i8** %1, i64 3
%23 = load i8*, i8** %22
%24 = tail call i32* @fopen(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.10, i64 0, i64 0))
br label %39
28: ; preds = %21
%29 = tail call signext i8 @initializeVarsForTick(i32* %17)
%30 = icmp eq i8 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.9, i64 0, i64 0))
%33 = sext i8 %29 to i32
br label %39
34: ; preds = %28
%35 = tail call i32 (...) @tick()
%36 = tail call i32 @writeSignals(i32 1, i32* %24)
%37 = tail call i32 @fclose(i32* %24)
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
br label %39
39: ; preds = %19, %31, %34, %26, %6, %4
%40 = phi i32 [ 1, %4 ], [ 1, %6 ], [ 3, %19 ], [ 3, %26 ], [ %33, %31 ], [ 0, %34 ]
ret i32 %40
}