#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 fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ mem0_store(int, int);
int /*<<< orphan*/ mem1_store(int, int);
int /*<<< orphan*/ sim_failure(char *);
void load_images(void) {
  int c;
  FILE *fp;
  int i;
  fp = fopen("image1.raw", "rb");
  if (!fp) {
    sim_failure("Could not open image1.raw");
  }
  for (i = 0; i < 176 * 144; i++) {
    c = fgetc(fp);
    if (c < 0) {
      break;
    }
    mem0_store(i, c);
  }
  fclose(fp);
  fp = fopen("image2.raw", "rb");
  if (!fp) {
    sim_failure("Could not open image2.raw");
  }
  for (i = 0; i < 176 * 144; i++) {
    c = fgetc(fp);
    if (c < 0) {
      break;
    }
    mem1_store(i, c);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<load_images>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea3(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1196 <load_images+0x36>
lea    0xe72(%rip),%rdi
call   1050 <sim_failure@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x6300,-0x14(%rbp)
jge    11de <load_images+0x7e>
mov    -0x10(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11c5 <load_images+0x65>
jmp    11de <load_images+0x7e>
mov    -0x14(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1030 <mem0_store@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    119d <load_images+0x3d>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe3e(%rip),%rdi
lea    0xe0b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1215 <load_images+0xb5>
lea    0xe0d(%rip),%rdi
call   1050 <sim_failure@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x6300,-0x14(%rbp)
jge    125d <load_images+0xfd>
mov    -0x10(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1244 <load_images+0xe4>
jmp    125d <load_images+0xfd>
mov    -0x14(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1080 <mem1_store@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    121c <load_images+0xbc>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_images>:
push   %rbp
push   %rbx
push   %rax
lea    0xea8(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    118a <load_images+0x2a>
lea    0xe7e(%rip),%rdi
call   1050 <sim_failure@plt>
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1060 <fgetc@plt>
test   %eax,%eax
js     11b0 <load_images+0x50>
mov    %ebp,%edi
mov    %eax,%esi
call   1030 <mem0_store@plt>
add    $0x1,%ebp
cmp    $0x6300,%ebp
jne    1190 <load_images+0x30>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe6d(%rip),%rdi
lea    0xe3a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11df <load_images+0x7f>
lea    0xe43(%rip),%rdi
call   1050 <sim_failure@plt>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <fgetc@plt>
test   %eax,%eax
js     1210 <load_images+0xb0>
mov    %ebp,%edi
mov    %eax,%esi
call   1080 <mem1_store@plt>
add    $0x1,%ebp
cmp    $0x6300,%ebp
jne    11f0 <load_images+0x90>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"image1.raw\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"Could not open image1.raw\00"
@.str.3 = [11 x i8] c"image2.raw\00"
@.str.4 = [26 x i8] c"Could not open image2.raw\00"
define void @load_images() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp ne i32* %5, null
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = call i32 @sim_failure(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
store i32 0, i32* %3
br label %10
10: ; preds = %23, %9
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 25344
br i1 %12, label %13, label %26
13: ; preds = %10
%14 = load i32*, i32** %2
%15 = call i32 @fgetc(i32* %14)
store i32 %15, i32* %1
%16 = load i32, i32* %1
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %13
br label %26
19: ; preds = %13
%20 = load i32, i32* %3
%21 = load i32, i32* %1
%22 = call i32 @mem0_store(i32 %20, i32 %21)
br label %23
23: ; preds = %19
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
br label %10
26: ; preds = %18, %10
%27 = load i32*, i32** %2
%28 = call i32 @fclose(i32* %27)
%29 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %29, i32** %2
%30 = load i32*, i32** %2
%31 = icmp ne i32* %30, null
br i1 %31, label %34, label %32
32: ; preds = %26
%33 = call i32 @sim_failure(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %26
store i32 0, i32* %3
br label %35
35: ; preds = %48, %34
%36 = load i32, i32* %3
%37 = icmp slt i32 %36, 25344
br i1 %37, label %38, label %51
38: ; preds = %35
%39 = load i32*, i32** %2
%40 = call i32 @fgetc(i32* %39)
store i32 %40, i32* %1
%41 = load i32, i32* %1
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %38
br label %51
44: ; preds = %38
%45 = load i32, i32* %3
%46 = load i32, i32* %1
%47 = call i32 @mem1_store(i32 %45, i32 %46)
br label %48
48: ; preds = %44
%49 = load i32, i32* %3
%50 = add nsw i32 %49, 1
store i32 %50, i32* %3
br label %35
51: ; preds = %43, %35
%52 = load i32*, i32** %2
%53 = call i32 @fclose(i32* %52)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"image1.raw\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [26 x i8] c"Could not open image1.raw\00"
@.str.3 = [11 x i8] c"image2.raw\00"
@.str.4 = [26 x i8] c"Could not open image2.raw\00"
define void @load_images() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @sim_failure(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %5
5: ; preds = %3, %0
br label %6
6: ; preds = %5, %10
%7 = phi i32 [ %12, %10 ], [ 0, %5 ]
%8 = tail call i32 @fgetc(i32* %1)
%9 = icmp slt i32 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = tail call i32 @mem0_store(i32 %7, i32 %8)
%12 = add nuw nsw i32 %7, 1
%13 = icmp eq i32 %12, 25344
br i1 %13, label %14, label %6
14: ; preds = %6, %10
%15 = tail call i32 @fclose(i32* %1)
%16 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @sim_failure(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
br label %21
21: ; preds = %20, %25
%22 = phi i32 [ %27, %25 ], [ 0, %20 ]
%23 = tail call i32 @fgetc(i32* %16)
%24 = icmp slt i32 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = tail call i32 @mem1_store(i32 %22, i32 %23)
%27 = add nuw nsw i32 %22, 1
%28 = icmp eq i32 %27, 25344
br i1 %28, label %29, label %21
29: ; preds = %21, %25
%30 = tail call i32 @fclose(i32* %16)
ret void
}