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

/* Variables and functions */
int addInstructionToMemory(int, int);
int /*<<< orphan*/ clearMemory();
int getInteger();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ system(char *);
int loadProgram() {
  clearMemory();
  int address = 0;
  int input;
  int isValidLoad = 1;
  printf("*** Type in the code or -9999999 to stop ***\n\n");
  printf("00 ? ");
  input = getInteger();
  while ((input != -9999999) && (address < 1000)) {
    if ((input > 999999) || (input < -999999 && input != -9999999)) {
      printf("\nWarning - Invalid instruction\nPlease try again!\n");
      printf("%02d ? ", address);
      input = getInteger();
    } else if (address >= 1000) {
      printf("\n*** Fatal Error - Memory Overflow ***\n");
      system("pause");
      isValidLoad = -1;
      input = -9999999;
    } else {
      if (addInstructionToMemory(address, input) == -1)
        return -1;
      address++;
      printf("%02d ? ", address);
      input = getInteger();
    }
  }
  printf("\n*** Program loading completed ***\n");
  return isValidLoad;
}


x86 assembly (clang, -O0):
<loadProgram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1070 <clearMemory@plt>
movl   $0x0,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xead(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getInteger@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0xff676981,-0xc(%rbp)
mov    %al,-0x11(%rbp)
je     11b2 <loadProgram+0x62>
cmpl   $0x3e8,-0x8(%rbp)
setl   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    11c2 <loadProgram+0x72>
jmp    129f <loadProgram+0x14f>
cmpl   $0xf423f,-0xc(%rbp)
jg     11e9 <loadProgram+0x99>
cmpl   $0xfff0bdc1,-0xc(%rbp)
jge    1217 <loadProgram+0xc7>
cmpl   $0xff676981,-0xc(%rbp)
je     1217 <loadProgram+0xc7>
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getInteger@plt>
mov    %eax,-0xc(%rbp)
jmp    129a <loadProgram+0x14a>
cmpl   $0x3e8,-0x8(%rbp)
jl     1251 <loadProgram+0x101>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe5e(%rip),%rdi
call   1040 <system@plt>
movl   $0xffffffff,-0x10(%rbp)
movl   $0xff676981,-0xc(%rbp)
jmp    1295 <loadProgram+0x145>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1060 <addInstructionToMemory@plt>
cmp    $0xffffffff,%eax
jne    1271 <loadProgram+0x121>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b3 <loadProgram+0x163>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getInteger@plt>
mov    %eax,-0xc(%rbp)
jmp    129a <loadProgram+0x14a>
jmp    1193 <loadProgram+0x43>
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadProgram>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1070 <clearMemory@plt>
lea    0xeac(%rip),%rdi
call   1030 <puts@plt>
lea    0xe92(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getInteger@plt>
cmp    $0xff676981,%eax
je     11f3 <loadProgram+0xa3>
lea    0xed5(%rip),%r14
lea    0xe75(%rip),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    -0xf4240(%rax),%ecx
cmp    $0xffe17b80,%ecx
ja     11c0 <loadProgram+0x70>
mov    %r14,%rdi
call   1030 <puts@plt>
jmp    11d1 <loadProgram+0x81>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %eax,%esi
call   1060 <addInstructionToMemory@plt>
cmp    $0xffffffff,%eax
je     1209 <loadProgram+0xb9>
add    $0x1,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getInteger@plt>
cmp    $0xff676981,%eax
je     11f3 <loadProgram+0xa3>
cmp    $0x3e8,%ebp
jl     11a0 <loadProgram+0x50>
lea    0xe42(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    1204 <loadProgram+0xb4>

LLVM IR (-O0):
@.str = [47 x i8] c"*** Type in the code or -9999999 to stop ***\0A\0A\00"
@.str.1 = [6 x i8] c"00 ? \00"
@.str.2 = [50 x i8] c"\0AWarning - Invalid instruction\0APlease try again!\0A\00"
@.str.3 = [8 x i8] c"%02d ? \00"
@.str.4 = [40 x i8] c"\0A*** Fatal Error - Memory Overflow ***\0A\00"
@.str.5 = [6 x i8] c"pause\00"
@.str.6 = [36 x i8] c"\0A*** Program loading completed ***\0A\00"
define i32 @loadProgram() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = call i32 (...) @clearMemory()
store i32 0, i32* %2
store i32 1, i32* %4
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 (...) @getInteger()
store i32 %8, i32* %3
br label %9
9: ; preds = %50, %0
%10 = load i32, i32* %3
%11 = icmp ne i32 %10, -9999999
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 1000
br label %15
15: ; preds = %12, %9
%16 = phi i1 [ false, %9 ], [ %14, %12 ]
br i1 %16, label %17, label %51
17: ; preds = %15
%18 = load i32, i32* %3
%19 = icmp sgt i32 %18, 999999
br i1 %19, label %26, label %20
20: ; preds = %17
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, -999999
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = load i32, i32* %3
%25 = icmp ne i32 %24, -9999999
br i1 %25, label %26, label %31
26: ; preds = %23, %17
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* %2
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = call i32 (...) @getInteger()
store i32 %30, i32* %3
br label %50
31: ; preds = %23, %20
%32 = load i32, i32* %2
%33 = icmp sge i32 %32, 1000
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @system(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %4
store i32 -9999999, i32* %3
br label %49
37: ; preds = %31
%38 = load i32, i32* %2
%39 = load i32, i32* %3
%40 = call i32 @addInstructionToMemory(i32 %38, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %37
store i32 -1, i32* %1
br label %54
43: ; preds = %37
%44 = load i32, i32* %2
%45 = add nsw i32 %44, 1
store i32 %45, i32* %2
%46 = load i32, i32* %2
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%48 = call i32 (...) @getInteger()
store i32 %48, i32* %3
br label %49
49: ; preds = %43, %34
br label %50
50: ; preds = %49, %26
br label %9
51: ; preds = %15
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%53 = load i32, i32* %4
store i32 %53, i32* %1
br label %54
54: ; preds = %51, %42
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
@.str.1 = [6 x i8] c"00 ? \00"
@.str.3 = [8 x i8] c"%02d ? \00"
@str = [46 x i8] c"*** Type in the code or -9999999 to stop ***\0A\00"
@str.7 = [35 x i8] c"\0A*** Program loading completed ***\00"
@str.8 = [49 x i8] c"\0AWarning - Invalid instruction\0APlease try again!\00"
define i32 @loadProgram() {
%1 = tail call i32 (...) @clearMemory()
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32 (...) @getInteger()
%5 = icmp eq i32 %4, -9999999
br i1 %5, label %25, label %6
6: ; preds = %0, %18
%7 = phi i32 [ %21, %18 ], [ %4, %0 ]
%8 = phi i32 [ %19, %18 ], [ 0, %0 ]
%9 = add i32 %7, -1000000
%10 = icmp ult i32 %9, -1999999
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.8, i64 0, i64 0))
br label %18
13: ; preds = %6
%14 = tail call i32 @addInstructionToMemory(i32 %8, i32 %7)
%15 = icmp eq i32 %14, -1
br i1 %15, label %27, label %16
16: ; preds = %13
%17 = add nsw i32 %8, 1
br label %18
18: ; preds = %16, %11
%19 = phi i32 [ %17, %16 ], [ %8, %11 ]
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = tail call i32 (...) @getInteger()
%22 = icmp ne i32 %21, -9999999
%23 = icmp slt i32 %19, 1000
%24 = and i1 %22, %23
br i1 %24, label %6, label %25
25: ; preds = %18, %0
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.7, i64 0, i64 0))
br label %27
27: ; preds = %13, %25
%28 = phi i32 [ 1, %25 ], [ -1, %13 ]
ret i32 %28
}