#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*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ get_command(int /*<<< orphan*/ *);
int /*<<< orphan*/ initialize(char *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  FILE *dumpsim_file;
  if (argc < 3) {
    printf("Error: usage: %s <micro_code_file> <program_file_1> "
           "<program_file_2> ...\n",
           argv[0]);
    exit(1);
  }
  printf("LC-3b Simulator\n\n");
  initialize(argv[1], argv[2], argc - 2);
  if ((dumpsim_file = fopen("dumpsim", "w")) == NULL) {
    printf("Error: Can't open dumpsim file\n");
    exit(-1);
  }
  while (1)
    get_command(dumpsim_file);
}


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   $0x3,-0x8(%rbp)
jge    118f <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xeb4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x8(%rbp),%edx
sub    $0x2,%edx
call   1040 <initialize@plt>
lea    0xe9d(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11f1 <main+0xa1>
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    11f6 <main+0xa6>
mov    -0x18(%rbp),%rdi
call   1050 <get_command@plt>
jmp    11f6 <main+0xa6>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x2,%edi
jle    11ba <main+0x5a>
mov    %edi,%ebx
lea    0xedf(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%r14),%rdi
mov    0x10(%r14),%rsi
add    $0xfffffffe,%ebx
mov    %ebx,%edx
call   1050 <initialize@plt>
lea    0xeb7(%rip),%rdi
lea    0xeb8(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d5 <main+0x75>
mov    %rax,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <get_command@plt>
jmp    11b0 <main+0x50>
mov    (%r14),%rsi
lea    0xe3c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [74 x i8] c"Error: usage: %s <micro_code_file> <program_file_1> <program_file_2> ...\0A\00"
@.str.1 = [18 x i8] c"LC-3b Simulator\0A\0A\00"
@.str.2 = [8 x i8] c"dumpsim\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [32 x i8] c"Error: Can't open dumpsim file\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 3
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = load i32, i32* %4
%24 = sub nsw i32 %23, 2
%25 = call i32 @initialize(i8* %19, i8* %22, i32 %24)
%26 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %26, i32** %6
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %15
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %15
br label %32
32: ; preds = %31, %32
%33 = load i32*, i32** %6
%34 = call i32 @get_command(i32* %33)
br label %32
}

LLVM IR (-O2):
@.str = [74 x i8] c"Error: usage: %s <micro_code_file> <program_file_1> <program_file_2> ...\0A\00"
@.str.2 = [8 x i8] c"dumpsim\00"
@.str.3 = [2 x i8] c"w\00"
@str = [17 x i8] c"LC-3b Simulator\0A\00"
@str.5 = [31 x i8] c"Error: Can't open dumpsim file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = add nsw i32 %0, -2
%15 = tail call i32 @initialize(i8* %11, i8* %13, i32 %14)
%16 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %8
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.5, i64 0, i64 0))
%20 = tail call i32 @exit(i32 -1)
unreachable
21: ; preds = %8, %21
%22 = tail call i32 @get_command(i32* %16)
br label %21
}