#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 */
char *calloc(int, int);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
char *realloc(char *, int);
int read_file_to_buffer(char *fname, char **gbuf, int *fsize) {
  int maxsize, i = 0;
  char *buf = calloc(32, sizeof(char));
  FILE *fp = fopen(fname, "r");
  maxsize = 32;
  if (fp == NULL) {
    printf("Unable to open file %s\n", fname);
    return -1;
  }
  while (!feof(fp)) {
    if (i >= maxsize) {
      buf = realloc(buf, (maxsize + 32) * sizeof(char));
      if (buf == NULL) {
        printf("Unable to reallocate memory\n");
        return -1;
      }
      maxsize += 32;
    }
    buf[i++] = fgetc(fp);
  }
  buf[i - 1] = 0;
  *fsize = i - 1;
  *gbuf = buf;
  return 0;
}


x86 assembly (clang, -O0):
<read_file_to_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    $0x20,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
movl   $0x20,-0x24(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11d2 <read_file_to_buffer+0x72>
mov    -0x10(%rbp),%rsi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <read_file_to_buffer+0x13e>
jmp    11d7 <read_file_to_buffer+0x77>
mov    -0x38(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11f5 <read_file_to_buffer+0x95>
jmp    126f <read_file_to_buffer+0x10f>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jl     124a <read_file_to_buffer+0xea>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%eax
add    $0x20,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1241 <read_file_to_buffer+0xe1>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <read_file_to_buffer+0x13e>
mov    -0x24(%rbp),%eax
add    $0x20,%eax
mov    %eax,-0x24(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,%dl
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11d7 <read_file_to_buffer+0x77>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%ecx
sub    $0x1,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_to_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbp
mov    $0x20,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r13
lea    0xe63(%rip),%rsi
mov    %rbp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     121d <read_file_to_buffer+0xad>
mov    %rax,%r12
mov    %rax,%rdi
call   1070 <feof@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
jne    1238 <read_file_to_buffer+0xc8>
mov    $0x20,%ebp
xor    %ebx,%ebx
jmp    11ed <read_file_to_buffer+0x7d>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1050 <fgetc@plt>
mov    %al,0x0(%r13,%rbx,1)
add    $0x1,%rbx
mov    %r12,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1235 <read_file_to_buffer+0xc5>
movslq %ebp,%rax
cmp    %rax,%rbx
jl     11d0 <read_file_to_buffer+0x60>
add    $0x20,%ebp
mov    %r13,%rdi
mov    %ebp,%esi
call   1080 <realloc@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11d0 <read_file_to_buffer+0x60>
lea    0xe09(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    1249 <read_file_to_buffer+0xd9>
lea    0xdde(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
jmp    1249 <read_file_to_buffer+0xd9>
add    $0xffffffff,%ebx
movslq %ebx,%rax
movb   $0x0,0x0(%r13,%rax,1)
mov    %eax,(%r14)
mov    %r13,(%r15)
xor    %eax,%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"
@.str.1 = [24 x i8] c"Unable to open file %s\0A\00"
@.str.2 = [29 x i8] c"Unable to reallocate memory\0A\00"
define i32 @read_file_to_buffer(i8* %0, i8** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32*
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
store i32 0, i32* %9
%12 = call i8* @calloc(i32 32, i32 1)
store i8* %12, i8** %10
%13 = load i8*, i8** %5
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %11
store i32 32, i32* %8
%15 = load i32*, i32** %11
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i32 -1, i32* %4
br label %64
20: ; preds = %3
br label %21
21: ; preds = %45, %20
%22 = load i32*, i32** %11
%23 = call i32 @feof(i32* %22)
%24 = icmp ne i32 %23, 0
%25 = xor i1 %24, true
br i1 %25, label %26, label %53
26: ; preds = %21
%27 = load i32, i32* %9
%28 = load i32, i32* %8
%29 = icmp sge i32 %27, %28
br i1 %29, label %30, label %45
30: ; preds = %26
%31 = load i8*, i8** %10
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 32
%34 = sext i32 %33 to i64
%35 = mul i64 %34, 1
%36 = trunc i64 %35 to i32
%37 = call i8* @realloc(i8* %31, i32 %36)
store i8* %37, i8** %10
%38 = load i8*, i8** %10
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %42
40: ; preds = %30
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %64
42: ; preds = %30
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 32
store i32 %44, i32* %8
br label %45
45: ; preds = %42, %26
%46 = load i32*, i32** %11
%47 = call signext i8 @fgetc(i32* %46)
%48 = load i8*, i8** %10
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 1
store i32 %50, i32* %9
%51 = sext i32 %49 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
store i8 %47, i8* %52
br label %21
53: ; preds = %21
%54 = load i8*, i8** %10
%55 = load i32, i32* %9
%56 = sub nsw i32 %55, 1
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %54, i64 %57
store i8 0, i8* %58
%59 = load i32, i32* %9
%60 = sub nsw i32 %59, 1
%61 = load i32*, i32** %7
store i32 %60, i32* %61
%62 = load i8*, i8** %10
%63 = load i8**, i8*** %6
store i8* %62, i8** %63
store i32 0, i32* %4
br label %64
64: ; preds = %53, %40, %17
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"Unable to open file %s\0A\00"
@str = [28 x i8] c"Unable to reallocate memory\00"
define i32 @read_file_to_buffer(i8* %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = tail call dereferenceable_or_null(32) i8* @calloc(i32 32, i32 1)
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %3
%8 = tail call i32 @feof(i32* %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %35
10: ; preds = %3
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %40
12: ; preds = %7, %24
%13 = phi i64 [ %28, %24 ], [ 0, %7 ]
%14 = phi i8* [ %26, %24 ], [ %4, %7 ]
%15 = phi i32 [ %25, %24 ], [ 32, %7 ]
%16 = sext i32 %15 to i64
%17 = icmp slt i64 %13, %16
br i1 %17, label %24, label %18
18: ; preds = %12
%19 = add nsw i32 %15, 32
%20 = tail call i8* @realloc(i8* %14, i32 %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %40
24: ; preds = %18, %12
%25 = phi i32 [ %15, %12 ], [ %19, %18 ]
%26 = phi i8* [ %14, %12 ], [ %20, %18 ]
%27 = tail call signext i8 @fgetc(i32* %5)
%28 = add nuw i64 %13, 1
%29 = getelementptr inbounds i8, i8* %26, i64 %13
store i8 %27, i8* %29
%30 = tail call i32 @feof(i32* %5)
%31 = icmp eq i32 %30, 0
br i1 %31, label %12, label %32
32: ; preds = %24
%33 = trunc i64 %28 to i32
%34 = add i32 %33, -1
br label %35
35: ; preds = %32, %7
%36 = phi i32 [ -1, %7 ], [ %34, %32 ]
%37 = phi i8* [ %4, %7 ], [ %26, %32 ]
%38 = sext i32 %36 to i64
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8 0, i8* %39
store i32 %36, i32* %2
store i8* %37, i8** %1
br label %40
40: ; preds = %35, %22, %10
%41 = phi i32 [ -1, %10 ], [ -1, %22 ], [ 0, %35 ]
ret i32 %41
}