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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fstat(int /*<<< orphan*/, struct stat *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
int readfile_into_buf(char *filename, char **buf) {
  FILE *file = fopen(filename, "r");
  int i;
  if (!file) {
    fprintf(stderr, "Could not open %s\n", filename);
    return 0;
  }
  struct stat fileinfo;
  fstat(fileno(file), &fileinfo);
  int size = fileinfo.st_size;
  *buf = (char *)malloc(sizeof(char) * size);
  for (i = 0; i < size;) {
    i += fread((void *)&((*buf)[i]), sizeof(char), size - i, file);
  }
  fclose(file);
  return size;
}


x86 assembly (clang, -O0):
<readfile_into_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c4 <readfile_into_buf+0x54>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4f(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    124d <readfile_into_buf+0xdd>
mov    -0x20(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x28(%rbp),%rsi
call   1090 <fstat@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    123e <readfile_into_buf+0xce>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x2c(%rbp),%edx
sub    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
jmp    11fe <readfile_into_buf+0x8e>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile_into_buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0xe7c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11fa <readfile_into_buf+0x8a>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,(%r14)
test   %ebp,%ebp
jle    11f0 <readfile_into_buf+0x80>
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    %eax,%ebp
jle    11f0 <readfile_into_buf+0x80>
nopw   0x0(%rax,%rax,1)
movslq %eax,%rbx
mov    (%r14),%rdi
add    %rbx,%rdi
mov    %ebp,%edx
sub    %ebx,%edx
mov    $0x1,%esi
mov    %r15,%rcx
call   1030 <fread@plt>
add    %ebx,%eax
cmp    %eax,%ebp
jg     11d0 <readfile_into_buf+0x60>
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    1214 <readfile_into_buf+0xa4>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
mov    %rbp,%rdx
call   1050 <fprintf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Could not open %s\0A\00"
define i32 @readfile_into_buf(i8* %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%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 %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %34
9: ; preds = %2
%10 = bitcast %struct.stat* %3 to i8*
%11 = tail call i32 @fileno(i32* %4)
%12 = call i32 @fstat(i32 %11, %struct.stat* %3)
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %1
%17 = icmp sgt i32 %14, 0
br i1 %17, label %18, label %32
18: ; preds = %9
%19 = tail call i64 @fread(i8* %16, i32 1, i32 %14, i32* %4)
%20 = trunc i64 %19 to i32
%21 = icmp sgt i32 %14, %20
br i1 %21, label %22, label %32
22: ; preds = %18, %22
%23 = phi i32 [ %30, %22 ], [ %20, %18 ]
%24 = load i8*, i8** %1
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = sub nsw i32 %14, %23
%28 = tail call i64 @fread(i8* %26, i32 1, i32 %27, i32* %4)
%29 = trunc i64 %28 to i32
%30 = add i32 %23, %29
%31 = icmp sgt i32 %14, %30
br i1 %31, label %22, label %32
32: ; preds = %22, %18, %9
%33 = tail call i32 @fclose(i32* %4)
br label %34
34: ; preds = %32, %6
%35 = phi i32 [ %14, %32 ], [ 0, %6 ]
ret i32 %35
}