#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char getc(int /*<<< orphan*/ *);
load_program(char *p, char *fname) {
  FILE *fp;
  int i = 0;
  if ((fp = fopen(fname, "rb")) == NULL)
    return 0;
  i = 0;
  do {
    *p = getc(fp);
    p++;
    i++;
  } while (!feof(fp) && i < 10000);
  *(p - 2) = '\0';
  fclose(fp);
  return 1;
}


x86 assembly (clang, -O0):
<load_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe9e(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1181 <load_program+0x41>
movl   $0x0,-0x4(%rbp)
jmp    11f7 <load_program+0xb7>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <getc@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x25(%rbp)
jne    11d4 <load_program+0x94>
cmpl   $0x2710,-0x24(%rbp)
setl   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1188 <load_program+0x48>
mov    -0x10(%rbp),%rax
movb   $0x0,-0x2(%rax)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_program>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xeae(%rip),%rsi
mov    %rax,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11ac <load_program+0x6c>
mov    %rax,%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
call   1060 <getc@plt>
mov    %al,(%r14,%rbx,1)
mov    %r15,%rdi
call   1040 <feof@plt>
lea    0x1(%rbx),%rcx
test   %eax,%eax
jne    1197 <load_program+0x57>
cmp    $0x270f,%ebx
mov    %rcx,%rbx
jb     1170 <load_program+0x30>
movb   $0x0,-0x2(%r14,%rcx,1)
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    $0x1,%eax
jmp    11ae <load_program+0x6e>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
define i32 @load_program(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %7
%8 = load i8*, i8** %5
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 0, i32* %3
br label %35
12: ; preds = %2
store i32 0, i32* %7
br label %13
13: ; preds = %28, %12
%14 = load i32*, i32** %6
%15 = call signext i8 @getc(i32* %14)
%16 = load i8*, i8** %4
store i8 %15, i8* %16
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i32 1
store i8* %18, i8** %4
%19 = load i32, i32* %7
%20 = add nsw i32 %19, 1
store i32 %20, i32* %7
br label %21
21: ; preds = %13
%22 = load i32*, i32** %6
%23 = call i32 @feof(i32* %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = load i32, i32* %7
%27 = icmp slt i32 %26, 10000
br label %28
28: ; preds = %25, %21
%29 = phi i1 [ false, %21 ], [ %27, %25 ]
br i1 %29, label %13, label %30
30: ; preds = %28
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i64 -2
store i8 0, i8* %32
%33 = load i32*, i32** %6
%34 = call i32 @fclose(i32* %33)
store i32 1, i32* %3
br label %35
35: ; preds = %30, %11
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define i32 @load_program(i8* nocapture writeonly %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %18, label %5
5: ; preds = %2, %5
%6 = phi i8* [ %9, %5 ], [ %0, %2 ]
%7 = phi i32 [ %10, %5 ], [ 0, %2 ]
%8 = tail call signext i8 @getc(i32* %3)
store i8 %8, i8* %6
%9 = getelementptr inbounds i8, i8* %6, i64 1
%10 = add nuw nsw i32 %7, 1
%11 = tail call i32 @feof(i32* %3)
%12 = icmp eq i32 %11, 0
%13 = icmp ult i32 %7, 9999
%14 = select i1 %12, i1 %13, i1 false
br i1 %14, label %5, label %15
15: ; preds = %5
%16 = getelementptr inbounds i8, i8* %6, i64 -1
store i8 0, i8* %16
%17 = tail call i32 @fclose(i32* %3)
br label %18
18: ; preds = %2, %15
%19 = phi i32 [ 1, %15 ], [ 0, %2 ]
ret i32 %19
}