#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 stat {
  size_t st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char *, struct stat *);
int read_alloc_input_file(char *fname, char **buf, size_t *bufsize) {
  struct stat statbuf;
  FILE *fp;
  char *p;
  size_t num_bytes;
  if (stat(fname, &statbuf)) {
    perror(fname);
    return (-1);
  }
  fp = fopen(fname, "r");
  if (fp == NULL) {
    perror(fname);
    return (-1);
  }
  assert(NULL != (p = (char *)malloc(statbuf.st_size)));
  num_bytes = fread(p, 1, statbuf.st_size, fp);
  if (ferror(fp) || (num_bytes != statbuf.st_size)) {
    perror(fname);
    return (-1);
  }
  *buf = p;
  *bufsize = num_bytes;
  return 0;
}


x86 assembly (clang, -O0):
<read_alloc_input_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     11b0 <read_alloc_input_file+0x40>
mov    -0x10(%rbp),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <read_alloc_input_file+0x103>
mov    -0x10(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e4 <read_alloc_input_file+0x74>
mov    -0x10(%rbp),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <read_alloc_input_file+0x103>
mov    -0x28(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x38(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
jne    1241 <read_alloc_input_file+0xd1>
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     1256 <read_alloc_input_file+0xe6>
mov    -0x10(%rbp),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <read_alloc_input_file+0x103>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_alloc_input_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
jne    1203 <read_alloc_input_file+0x93>
lea    0xe68(%rip),%rsi
mov    %r12,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1203 <read_alloc_input_file+0x93>
mov    %rax,%rbx
mov    (%rsp),%rdi
call   1070 <malloc@plt>
mov    %rax,%r13
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
mov    (%rsp),%rdx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    1203 <read_alloc_input_file+0x93>
cmp    (%rsp),%rbp
jne    1203 <read_alloc_input_file+0x93>
mov    %r13,(%r15)
mov    %rbp,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    11f4 <read_alloc_input_file+0x84>

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

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [2 x i8] c"r\00"
define i32 @read_alloc_input_file(i8* %0, i8** nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i64 @stat(i8* %0, %struct.stat* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = call i32 @perror(i8* %0)
br label %33
10: ; preds = %3
%11 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @perror(i8* %0)
br label %33
15: ; preds = %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%17 = load i64, i64* %16
%18 = call i64 @malloc(i64 %17)
%19 = inttoptr i64 %18 to i8*
%20 = icmp ne i64 %18, 0
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i64, i64* %16
%24 = call i64 @fread(i8* %19, i32 1, i64 %23, i32* %11)
%25 = call i64 @ferror(i32* %11)
%26 = icmp eq i64 %25, 0
%27 = load i64, i64* %16
%28 = icmp eq i64 %24, %27
%29 = select i1 %26, i1 %28, i1 false
br i1 %29, label %32, label %30
30: ; preds = %15
%31 = call i32 @perror(i8* %0)
br label %33
32: ; preds = %15
store i8* %19, i8** %1
store i64 %24, i64* %2
br label %33
33: ; preds = %32, %30, %13, %8
%34 = phi i32 [ -1, %8 ], [ -1, %13 ], [ -1, %30 ], [ 0, %32 ]
ret i32 %34
}