#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*/ fprintf(int /*<<< orphan*/, char *, int, char *, int);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *open_file_or_die(char *, char *);
int /*<<< orphan*/ stderr;
int read_file_to_region(char *file_name, int file_size, char *region) {
  FILE *fp;
  int bytes_read;
  int rc = 0;
  fp = open_file_or_die(file_name, "rb");
  bytes_read = fread(region, sizeof(char), file_size, fp);
  if (bytes_read != file_size) {
    fprintf(stderr, "Could not read %d bytes from file \"%s\"; got %d.\n",
            file_size, file_name, bytes_read);
    rc = 0;
  } else {
    rc = 1;
  }
  fclose(fp);
  return rc;
}


x86 assembly (clang, -O0):
<read_file_to_region>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe9b(%rip),%rsi
call   1060 <open_file_or_die@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     11be <read_file_to_region+0x7e>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    -0x24(%rbp),%r8d
lea    0xe56(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x28(%rbp)
jmp    11c5 <read_file_to_region+0x85>
movl   $0x1,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_to_region>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xea9(%rip),%rsi
call   1060 <open_file_or_die@plt>
mov    %rax,%r15
mov    $0x1,%ebx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    %ebp,%eax
je     1199 <read_file_to_region+0x59>
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%edi
lea    0xe79(%rip),%rsi
mov    %ebp,%edx
mov    %r14,%rcx
mov    %eax,%r8d
call   1050 <fprintf@plt>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [49 x i8] c"Could not read %d bytes from file \22%s\22; got %d.\0A\00"
define i32 @read_file_to_region(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i32 0, i32* %9
%10 = load i8*, i8** %4
%11 = call i32* @open_file_or_die(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i8*, i8** %6
%13 = load i32, i32* %5
%14 = load i32*, i32** %7
%15 = call i32 @fread(i8* %12, i32 1, i32 %13, i32* %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = load i32, i32* %5
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %25
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = load i32, i32* %5
%22 = load i8*, i8** %4
%23 = load i32, i32* %8
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %21, i8* %22, i32 %23)
store i32 0, i32* %9
br label %26
25: ; preds = %3
store i32 1, i32* %9
br label %26
26: ; preds = %25, %19
%27 = load i32*, i32** %7
%28 = call i32 @fclose(i32* %27)
%29 = load i32, i32* %9
ret i32 %29
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [49 x i8] c"Could not read %d bytes from file \22%s\22; got %d.\0A\00"
define i32 @read_file_to_region(i8* %0, i32 %1, i8* nocapture %2) {
%4 = tail call i32* @open_file_or_die(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @fread(i8* %2, i32 1, i32 %1, i32* %4)
%6 = icmp eq i32 %5, %1
br i1 %6, label %10, label %7
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %1, i8* %0, i32 %5)
br label %10
10: ; preds = %3, %7
%11 = phi i32 [ 0, %7 ], [ 1, %3 ]
%12 = tail call i32 @fclose(i32* %4)
ret i32 %11
}