#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*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ mem_write_32(int, int);
int /*<<< orphan*/ printf(char *, ...);
void load_program(char *program_filename, char *pc_filename) {
  FILE *prog;
  FILE *pc_file;
  int ii, word, pc_word;
  prog = fopen(program_filename, "r");
  pc_file = fopen(pc_filename, "r");
  if (prog == NULL) {
    printf("Error: Can't open program file %s\n", program_filename);
    exit(-1);
  }
  ii = 0;
  while (fscanf(prog, "%x\n", &word) != EOF) {
    if (fscanf(pc_file, "%x\n", &pc_word) != EOF) {
      mem_write_32(pc_word, word);
      ii += 4;
    } else {
      printf("Incomplete PC File\n");
    }
  }
  printf("Read %d words from program into memory.\n\n", ii / 4);
}


x86 assembly (clang, -O0):
<load_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11af <load_program+0x5f>
mov    -0x8(%rbp),%rsi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe64(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <fscanf@plt>
mov    0x2e17(%rip),%rcx
cmp    (%rcx),%rax
je     122a <load_program+0xda>
mov    -0x20(%rbp),%rdi
lea    0xe40(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1040 <fscanf@plt>
mov    0x2df3(%rip),%rcx
cmp    (%rcx),%rax
je     1217 <load_program+0xc7>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1050 <mem_write_32@plt>
mov    -0x24(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x24(%rbp)
jmp    1225 <load_program+0xd5>
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11b6 <load_program+0x66>
mov    -0x24(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_program>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %rdi,%r12
lea    0xe88(%rip),%rbx
mov    %rbx,%rsi
call   1070 <fopen@plt>
mov    %rax,%r15
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1070 <fopen@plt>
test   %r15,%r15
je     1248 <load_program+0xe8>
mov    %rax,%r14
lea    0xe84(%rip),%rsi
mov    %rsp,%rdx
mov    %r15,%rdi
call   1050 <fscanf@plt>
mov    0x2e35(%rip),%r12
xor    %r13d,%r13d
cmp    (%r12),%rax
jne    11e7 <load_program+0x87>
lea    0x3(%r13),%esi
test   %r13d,%r13d
cmovns %r13d,%esi
sar    $0x2,%esi
lea    0xe58(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r13d,%r13d
lea    0xe34(%rip),%rbx
mov    %rsp,%rbp
jmp    1220 <load_program+0xc0>
cs nopw 0x0(%rax,%rax,1)
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1050 <fscanf@plt>
cmp    (%r12),%rax
je     11bc <load_program+0x5c>
mov    %r14,%rdi
mov    %rbx,%rsi
lea    0x4(%rsp),%rdx
call   1050 <fscanf@plt>
cmp    (%r12),%rax
je     1200 <load_program+0xa0>
mov    0x4(%rsp),%edi
mov    (%rsp),%esi
call   1060 <mem_write_32@plt>
add    $0x4,%r13d
jmp    120c <load_program+0xac>
lea    0xdb3(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [35 x i8] c"Error: Can't open program file %s\0A\00"
@.str.2 = [4 x i8] c"%x\0A\00"
@EOF = global i64 0
@.str.3 = [20 x i8] c"Incomplete PC File\0A\00"
@.str.4 = [42 x i8] c"Read %d words from program into memory.\0A\0A\00"
define void @load_program(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %6
%14 = load i32*, i32** %5
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i8*, i8** %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %2
store i32 0, i32* %7
br label %21
21: ; preds = %39, %20
%22 = load i32*, i32** %5
%23 = call i64 @fscanf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %8)
%24 = load i64, i64* @EOF
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %40
26: ; preds = %21
%27 = load i32*, i32** %6
%28 = call i64 @fscanf(i32* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %9)
%29 = load i64, i64* @EOF
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %37
31: ; preds = %26
%32 = load i32, i32* %9
%33 = load i32, i32* %8
%34 = call i32 @mem_write_32(i32 %32, i32 %33)
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 4
store i32 %36, i32* %7
br label %39
37: ; preds = %26
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %31
br label %21
40: ; preds = %21
%41 = load i32, i32* %7
%42 = sdiv i32 %41, 4
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i32 %42)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [35 x i8] c"Error: Can't open program file %s\0A\00"
@.str.2 = [4 x i8] c"%x\0A\00"
@EOF = global i64 0
@.str.4 = [42 x i8] c"Read %d words from program into memory.\0A\0A\00"
@str = [19 x i8] c"Incomplete PC File\00"
define void @load_program(i8* %0, i8* nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %7, null
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = call i64 @fscanf(i32* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%12 = load i64, i64* @EOF
%13 = icmp eq i64 %11, %12
br i1 %13, label %34, label %17
14: ; preds = %2
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %10, %29
%18 = phi i32 [ %30, %29 ], [ 0, %10 ]
%19 = call i64 @fscanf(i32* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%20 = load i64, i64* @EOF
%21 = icmp eq i64 %19, %20
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = load i32, i32* %4
%24 = load i32, i32* %3
%25 = call i32 @mem_write_32(i32 %23, i32 %24)
%26 = add nsw i32 %18, 4
br label %29
27: ; preds = %17
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %27, %22
%30 = phi i32 [ %26, %22 ], [ %18, %27 ]
%31 = call i64 @fscanf(i32* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%32 = load i64, i64* @EOF
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %17
34: ; preds = %29, %10
%35 = phi i32 [ 0, %10 ], [ %30, %29 ]
%36 = sdiv i32 %35, 4
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i32 %36)
ret void
}