#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 */
struct string {
  int dummy;
};
typedef int /*<<< orphan*/ chunk;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ string_concatb(struct string *, char *, size_t);
bool fs_read_file(struct string *buffer, char *fname) {
  bool retval = false;
  FILE *fp;
  char chunk[4096];
  size_t read_size;
  fp = fopen(fname, "r");
  if (fp == NULL)
    return false;
  while ((read_size = fread(&chunk, 1, sizeof(chunk), fp)) > 0) {
    if (!string_concatb(buffer, chunk, read_size))
      goto bail_out;
  }
  if (!ferror(fp))
    retval = true;
bail_out:
  fclose(fp);
  return retval;
}


x86 assembly (clang, -O0):
<fs_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1195 <fs_read_file+0x45>
movl   $0x0,-0x4(%rbp)
jmp    121c <fs_read_file+0xcc>
jmp    119a <fs_read_file+0x4a>
lea    -0x1030(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %rax,-0x1038(%rbp)
cmp    $0x0,%rax
jbe    11ef <fs_read_file+0x9f>
mov    -0x10(%rbp),%rdi
lea    -0x1030(%rbp),%rsi
mov    -0x1038(%rbp),%rdx
call   1060 <string_concatb@plt>
cmp    $0x0,%eax
jne    11ea <fs_read_file+0x9a>
jmp    120d <fs_read_file+0xbd>
jmp    119a <fs_read_file+0x4a>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%eax
jne    1208 <fs_read_file+0xb8>
movl   $0x1,-0x1c(%rbp)
jmp    120d <fs_read_file+0xbd>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fs_read_file>:
push   %r15
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xe97(%rip),%rsi
mov    %rax,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11b0 <fs_read_file+0x60>
mov    %rax,%r15
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     11b4 <fs_read_file+0x64>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1060 <string_concatb@plt>
test   %eax,%eax
jne    1180 <fs_read_file+0x30>
xor    %ebx,%ebx
jmp    11c3 <fs_read_file+0x73>
xor    %ebx,%ebx
jmp    11cb <fs_read_file+0x7b>
mov    %r15,%rdi
call   1030 <ferror@plt>
xor    %ebx,%ebx
test   %eax,%eax
sete   %bl
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %ebx,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.string = type { i32 }
@.str = [2 x i8] c"r\00"
define i32 @fs_read_file(%struct.string* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.string*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [4096 x i8]
%9 = alloca i64
store %struct.string* %0, %struct.string** %4
store i8* %1, i8** %5
store i32 0, i32* %6
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 0, i32* %3
br label %39
15: ; preds = %2
br label %16
16: ; preds = %28, %15
%17 = bitcast [4096 x i8]* %8 to i8**
%18 = load i32*, i32** %7
%19 = call i64 @fread(i8** %17, i32 1, i32 4096, i32* %18)
store i64 %19, i64* %9
%20 = icmp ugt i64 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %16
%22 = load %struct.string*, %struct.string** %4
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%24 = load i64, i64* %9
%25 = call i32 @string_concatb(%struct.string* %22, i8* %23, i64 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %21
br label %35
28: ; preds = %21
br label %16
29: ; preds = %16
%30 = load i32*, i32** %7
%31 = call i32 @ferror(i32* %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %29
store i32 1, i32* %6
br label %34
34: ; preds = %33, %29
br label %35
35: ; preds = %34, %27
%36 = load i32*, i32** %7
%37 = call i32 @fclose(i32* %36)
%38 = load i32, i32* %6
store i32 %38, i32* %3
br label %39
39: ; preds = %35, %14
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.string = type { i32 }
@.str = [2 x i8] c"r\00"
define i32 @fs_read_file(%struct.string* %0, i8* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %22, label %7
7: ; preds = %2
%8 = bitcast [4096 x i8]* %3 to i8**
br label %9
9: ; preds = %7, %12
%10 = call i64 @fread(i8** %8, i32 1, i32 4096, i32* %5)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = call i32 @string_concatb(%struct.string* %0, i8* %4, i64 %10)
%14 = icmp eq i32 %13, 0
br i1 %14, label %19, label %9
15: ; preds = %9
%16 = call i32 @ferror(i32* %5)
%17 = icmp eq i32 %16, 0
%18 = zext i1 %17 to i32
br label %19
19: ; preds = %12, %15
%20 = phi i32 [ %18, %15 ], [ 0, %12 ]
%21 = call i32 @fclose(i32* %5)
br label %22
22: ; preds = %2, %19
%23 = phi i32 [ %20, %19 ], [ 0, %2 ]
ret i32 %23
}