#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 */
scalar_t__ EOF;
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ run(int *, char);
int main(int argc, char **argv) {
  if (argc == 3 && *argv[1] == '-' &&
      (*(argv[1] + 1) == 's' || *(argv[1] + 1) == 'r')) {
    FILE *file;
    int mem[1000];
    int *tmp = mem;
    file = fopen(argv[2], "r");
    if (file) {
      while (fscanf(file, "%X", tmp++) != EOF)
        ;
      printf("Program memory loaded\n");
      run(mem, *(argv[1] + 1));
    }
  } else
    printf("Usage: ./spim <-step or -run> <file>\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xfd0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    123c <main+0xfc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    123c <main+0xfc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x73,%eax
je     11a1 <main+0x61>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x72,%eax
jne    123c <main+0xfc>
lea    -0xfc0(%rbp),%rax
mov    %rax,-0xfc8(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe42(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1237 <main+0xf7>
jmp    11d7 <main+0x97>
mov    -0x18(%rbp),%rdi
mov    -0xfc8(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0xfc8(%rbp)
mov    %eax,%edx
lea    0xe09(%rip),%rsi
call   1050 <fscanf@plt>
mov    0x2de3(%rip),%rcx
cmp    (%rcx),%rax
je     1213 <main+0xd3>
jmp    11d7 <main+0x97>
lea    0xdeb(%rip),%rdi
call   1040 <printf@plt>
lea    -0xfc0(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl 0x1(%rax),%esi
call   1030 <run@plt>
jmp    1248 <main+0x108>
lea    0xdd9(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0xfd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xfa0,%rsp
cmp    $0x3,%edi
jne    11d4 <main+0x94>
mov    %rsi,%r14
mov    0x8(%rsi),%rax
cmpb   $0x2d,(%rax)
jne    11d4 <main+0x94>
mov    0x1(%rax),%al
and    $0xfe,%al
cmp    $0x72,%al
jne    11d4 <main+0x94>
mov    0x10(%r14),%rdi
lea    0xe8b(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e0 <main+0xa0>
mov    %rax,%r15
mov    %rsp,%rbx
lea    0xe76(%rip),%r12
mov    0x2e55(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1050 <fscanf@plt>
add    $0x4,%ebx
cmp    0x0(%r13),%rax
jne    11a0 <main+0x60>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%r14),%rax
movsbl 0x1(%rax),%esi
mov    %rsp,%rdi
call   1040 <run@plt>
jmp    11e0 <main+0xa0>
lea    0xe2a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0xfa0,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%X\00"
@EOF = global i64 0
@.str.2 = [23 x i8] c"Program memory loaded\0A\00"
@.str.3 = [38 x i8] c"Usage: ./spim <-step or -run> <file>\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [1000 x i32]
%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 eq i32 %9, 3
br i1 %10, label %11, label %62
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 45
br i1 %17, label %18, label %62
18: ; preds = %11
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = getelementptr inbounds i8, i8* %21, i64 1
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 115
br i1 %25, label %34, label %26
26: ; preds = %18
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = getelementptr inbounds i8, i8* %29, i64 1
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 114
br i1 %33, label %34, label %62
34: ; preds = %26, %18
%35 = getelementptr inbounds [1000 x i32], [1000 x i32]* %7, i64 0, i64 0
store i32* %35, i32** %8
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
%39 = call i32* @fopen(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %39, i32** %6
%40 = load i32*, i32** %6
%41 = icmp ne i32* %40, null
br i1 %41, label %42, label %61
42: ; preds = %34
br label %43
43: ; preds = %51, %42
%44 = load i32*, i32** %6
%45 = load i32*, i32** %8
%46 = getelementptr inbounds i32, i32* %45, i32 1
store i32* %46, i32** %8
%47 = ptrtoint i32* %45 to i32
%48 = call i64 @fscanf(i32* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %47)
%49 = load i64, i64* @EOF
%50 = icmp ne i64 %48, %49
br i1 %50, label %51, label %52
51: ; preds = %43
br label %43
52: ; preds = %43
%53 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%54 = getelementptr inbounds [1000 x i32], [1000 x i32]* %7, i64 0, i64 0
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 1
%57 = load i8*, i8** %56
%58 = getelementptr inbounds i8, i8* %57, i64 1
%59 = load i8, i8* %58
%60 = call i32 @run(i32* %54, i8 signext %59)
br label %61
61: ; preds = %52, %34
br label %64
62: ; preds = %26, %11, %2
%63 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %64
64: ; preds = %62, %61
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%X\00"
@EOF = global i64 0
@str = [37 x i8] c"Usage: ./spim <-step or -run> <file>\00"
@str.4 = [22 x i8] c"Program memory loaded\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1000 x i32]
%4 = icmp eq i32 %0, 3
br i1 %4, label %5, label %37
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 45
br i1 %9, label %10, label %37
10: ; preds = %5
%11 = getelementptr inbounds i8, i8* %7, i64 1
%12 = load i8, i8* %11
%13 = and i8 %12, -2
%14 = icmp eq i8 %13, 114
br i1 %14, label %15, label %37
15: ; preds = %10
%16 = bitcast [1000 x i32]* %3 to i8*
%17 = getelementptr inbounds [1000 x i32], [1000 x i32]* %3, i64 0, i64 0
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %36, label %22
22: ; preds = %15, %22
%23 = phi i32* [ %24, %22 ], [ %17, %15 ]
%24 = getelementptr inbounds i32, i32* %23, i64 1
%25 = ptrtoint i32* %23 to i64
%26 = trunc i64 %25 to i32
%27 = call i64 @fscanf(i32* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i64, i64* @EOF
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %22
30: ; preds = %22
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.4, i64 0, i64 0))
%32 = load i8*, i8** %6
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = load i8, i8* %33
%35 = call i32 @run(i32* %17, i8 signext %34)
br label %36
36: ; preds = %30, %15
br label %39
37: ; preds = %10, %5, %2
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
br label %39
39: ; preds = %37, %36
ret i32 0
}