#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 EOF;
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int readFileIntoString(char *file_str, char *file, long int num_bytes) {
  FILE *fp = fopen(file, "r");
  if (fp == NULL) {
    perror("fopen");
    fprintf(stderr, "Could not open file: %s\n", file);
    return -1;
  }
  size_t read = fread(file_str, 1, num_bytes, fp);
  if (read < num_bytes) {
    fprintf(stderr,
            "Error reading file: Expected to read %ld bytes but got %d bytes\n",
            num_bytes, (int)read);
    return -1;
  }
  *(file_str + sizeof(char) * num_bytes) = '\0';
  int closed = fclose(fp);
  if (closed == EOF) {
    perror("fclose");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<readFileIntoString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b6 <readFileIntoString+0x66>
lea    0xe78(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe65(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <readFileIntoString+0x108>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    120b <readFileIntoString+0xbb>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    %eax,%ecx
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <readFileIntoString+0x108>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x0,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    0x2db7(%rip),%rcx
cmp    (%rcx),%eax
jne    1251 <readFileIntoString+0x101>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <readFileIntoString+0x108>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFileIntoString>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe98(%rip),%rsi
mov    %r15,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11db <readFileIntoString+0x8b>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    %r12,%rax
jae    11b3 <readFileIntoString+0x63>
mov    0x2e41(%rip),%rcx
mov    (%rcx),%edi
lea    0xe81(%rip),%rsi
mov    %r12,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11cf <readFileIntoString+0x7f>
movb   $0x0,(%r14,%r12,1)
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %eax,%ecx
mov    0x2e1f(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
je     1203 <readFileIntoString+0xb3>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe20(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11ac <readFileIntoString+0x5c>
lea    0xe58(%rip),%rdi
call   1070 <perror@plt>
jmp    11ac <readFileIntoString+0x5c>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Could not open file: %s\0A\00"
@.str.3 = [65 x i8] c"Error reading file: Expected to read %ld bytes but got %d bytes\0A\00"
@EOF = global i32 0
@.str.4 = [7 x i8] c"fclose\00"
define i32 @readFileIntoString(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
%13 = load i32*, i32** %8
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %6
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %18)
store i32 -1, i32* %4
br label %47
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = load i64, i64* %7
%23 = load i32*, i32** %8
%24 = call i64 @fread(i8* %21, i32 1, i64 %22, i32* %23)
store i64 %24, i64* %9
%25 = load i64, i64* %9
%26 = load i64, i64* %7
%27 = icmp ult i64 %25, %26
br i1 %27, label %28, label %34
28: ; preds = %20
%29 = load i32, i32* @stderr
%30 = load i64, i64* %7
%31 = load i64, i64* %9
%32 = trunc i64 %31 to i32
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i64 %30, i32 %32)
store i32 -1, i32* %4
br label %47
34: ; preds = %20
%35 = load i8*, i8** %5
%36 = load i64, i64* %7
%37 = mul i64 1, %36
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
%39 = load i32*, i32** %8
%40 = call i32 @fclose(i32* %39)
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = load i32, i32* @EOF
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %34
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
br label %47
46: ; preds = %34
store i32 0, i32* %4
br label %47
47: ; preds = %46, %44, %28, %15
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Could not open file: %s\0A\00"
@.str.3 = [65 x i8] c"Error reading file: Expected to read %ld bytes but got %d bytes\0A\00"
@EOF = global i32 0
@.str.4 = [7 x i8] c"fclose\00"
define i32 @readFileIntoString(i8* nocapture %0, i8* %1, i64 %2) {
%4 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %24
10: ; preds = %3
%11 = tail call i64 @fread(i8* %0, i32 1, i64 %2, i32* %4)
%12 = icmp ult i64 %11, %2
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = trunc i64 %11 to i32
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0), i64 %2, i32 %15)
br label %24
17: ; preds = %10
%18 = getelementptr inbounds i8, i8* %0, i64 %2
store i8 0, i8* %18
%19 = tail call i32 @fclose(i32* %4)
%20 = load i32, i32* @EOF
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %13, %17, %22, %6
%25 = phi i32 [ -1, %6 ], [ -1, %13 ], [ -1, %22 ], [ 0, %17 ]
ret i32 %25
}