#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 */
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
size_t fsize(int /*<<< orphan*/ *);
char *malloc(size_t);
int strload(const char *path, char **pbuf, int retry) {
  FILE *fp = fopen(path, "r");
  if (fp != NULL) {
    size_t size = fsize(fp);
    size_t nbytes = 0;
    char *buf = malloc(size + 1);
    buf[size] = '\0';
    do {
      nbytes += fread(buf + nbytes, 1, size - nbytes, fp);
      if (errno != 0) {
        free(buf);
        break;
      }
    } while (retry == 1 && nbytes < size);
    *pbuf = buf;
    fclose(fp);
  }
  return errno;
}


x86 assembly (clang, -O0):
<strload>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     124a <strload+0xea>
mov    -0x20(%rbp),%rdi
call   1070 <fsize@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
sub    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2def(%rip),%rax
cmpq   $0x0,(%rax)
je     1209 <strload+0xa9>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    1236 <strload+0xd6>
jmp    120e <strload+0xae>
xor    %eax,%eax
cmpl   $0x1,-0x14(%rbp)
mov    %al,-0x39(%rbp)
jne    122b <strload+0xcb>
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
setb   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11c4 <strload+0x64>
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strload>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
lea    0xe88(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f6 <strload+0x96>
mov    %rax,%r12
mov    %rbx,(%rsp)
mov    %rax,%rdi
call   1070 <fsize@plt>
mov    %rax,%rbx
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%r13
movb   $0x0,(%rax,%rbx,1)
xor    %ebp,%ebp
mov    0x2e33(%rip),%r14
nopl   (%rax)
mov    %r13,%rdi
add    %rbp,%rdi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    $0x1,%esi
mov    %r12,%rcx
call   1040 <fread@plt>
cmpq   $0x0,(%r14)
jne    11df <strload+0x7f>
cmp    $0x1,%r15d
jne    11e7 <strload+0x87>
add    %rax,%rbp
cmp    %rbx,%rbp
jb     11b0 <strload+0x50>
jmp    11e7 <strload+0x87>
mov    %r13,%rdi
call   1030 <free@plt>
mov    (%rsp),%rax
mov    %r13,(%rax)
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i64 0
define i32 @strload(i8* %0, i8** %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %7
%13 = load i32*, i32** %7
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %55
15: ; preds = %3
%16 = load i32*, i32** %7
%17 = call i64 @fsize(i32* %16)
store i64 %17, i64* %8
store i64 0, i64* %9
%18 = load i64, i64* %8
%19 = add i64 %18, 1
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %10
%21 = load i8*, i8** %10
%22 = load i64, i64* %8
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %48, %15
%25 = load i8*, i8** %10
%26 = load i64, i64* %9
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i64, i64* %8
%29 = load i64, i64* %9
%30 = sub i64 %28, %29
%31 = load i32*, i32** %7
%32 = call i64 @fread(i8* %27, i32 1, i64 %30, i32* %31)
%33 = load i64, i64* %9
%34 = add i64 %33, %32
store i64 %34, i64* %9
%35 = load i64, i64* @errno
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %24
%38 = load i8*, i8** %10
%39 = call i32 @free(i8* %38)
br label %50
40: ; preds = %24
br label %41
41: ; preds = %40
%42 = load i32, i32* %6
%43 = icmp eq i32 %42, 1
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = load i64, i64* %9
%46 = load i64, i64* %8
%47 = icmp ult i64 %45, %46
br label %48
48: ; preds = %44, %41
%49 = phi i1 [ false, %41 ], [ %47, %44 ]
br i1 %49, label %24, label %50
50: ; preds = %48, %37
%51 = load i8*, i8** %10
%52 = load i8**, i8*** %5
store i8* %51, i8** %52
%53 = load i32*, i32** %7
%54 = call i32 @fclose(i32* %53)
br label %55
55: ; preds = %50, %3
%56 = load i64, i64* @errno
%57 = trunc i64 %56 to i32
ret i32 %57
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i64 0
define i32 @strload(i8* nocapture %0, i8** nocapture writeonly %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %27, label %6
6: ; preds = %3
%7 = tail call i64 @fsize(i32* %4)
%8 = add i64 %7, 1
%9 = tail call i8* @malloc(i64 %8)
%10 = getelementptr inbounds i8, i8* %9, i64 %7
store i8 0, i8* %10
%11 = icmp eq i32 %2, 1
br label %12
12: ; preds = %22, %6
%13 = phi i64 [ 0, %6 ], [ %17, %22 ]
%14 = getelementptr inbounds i8, i8* %9, i64 %13
%15 = sub i64 %7, %13
%16 = tail call i64 @fread(i8* %14, i32 1, i64 %15, i32* %4)
%17 = add i64 %16, %13
%18 = load i64, i64* @errno
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %12
%21 = tail call i32 @free(i8* %9)
br label %25
22: ; preds = %12
%23 = icmp ult i64 %17, %7
%24 = select i1 %11, i1 %23, i1 false
br i1 %24, label %12, label %25
25: ; preds = %22, %20
store i8* %9, i8** %1
%26 = tail call i32 @fclose(i32* %4)
br label %27
27: ; preds = %25, %3
%28 = load i64, i64* @errno
%29 = trunc i64 %28 to i32
ret i32 %29
}