#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int stat(char const *, struct stat *);
char *file2buf(const char *filename) {
  char *buf;
  int filesize;
  struct stat sb;
  FILE *file;
  int res;
  res = stat(filename, &sb);
  if (-1 == res)
    return NULL;
  filesize = sb.st_size - 1;
  if (filesize >= 0) {
    buf = (char *)calloc(filesize + 1, 1);
    file = fopen(filename, "r");
    fread(buf, filesize, 1, file);
    fclose(file);
    buf[filesize] = '\0';
  } else {
    buf = (char *)calloc(1, 1);
  }
  return buf;
}


x86 assembly (clang, -O0):
<file2buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x2c(%rbp)
mov    $0xffffffff,%eax
cmp    -0x2c(%rbp),%eax
jne    1187 <file2buf+0x37>
movq   $0x0,-0x8(%rbp)
jmp    1209 <file2buf+0xb9>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11f1 <file2buf+0xa1>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe47(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1201 <file2buf+0xb1>
mov    $0x1,%esi
mov    %esi,%edi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file2buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsp,%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     11b5 <file2buf+0x65>
mov    (%rsp),%edi
test   %edi,%edi
jle    11b9 <file2buf+0x69>
lea    -0x1(%rdi),%r15d
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
lea    0xe7a(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r15d,%eax
movb   $0x0,(%rbx,%rax,1)
jmp    11cb <file2buf+0x7b>
xor    %ebx,%ebx
jmp    11cb <file2buf+0x7b>
mov    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
mov    %rbx,%rax
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"
define i8* @file2buf(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32 @stat(i8* %9, %struct.stat* %6)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp eq i32 -1, %11
br i1 %12, label %13, label %14
13: ; preds = %1
store i8* null, i8** %2
br label %42
14: ; preds = %1
%15 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%16 = load i32, i32* %15
%17 = sub nsw i32 %16, 1
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp sge i32 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %14
%21 = load i32, i32* %5
%22 = add nsw i32 %21, 1
%23 = call i64 @calloc(i32 %22, i32 1)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %4
%25 = load i8*, i8** %3
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %26, i32** %7
%27 = load i8*, i8** %4
%28 = load i32, i32* %5
%29 = load i32*, i32** %7
%30 = call i32 @fread(i8* %27, i32 %28, i32 1, i32* %29)
%31 = load i32*, i32** %7
%32 = call i32 @fclose(i32* %31)
%33 = load i8*, i8** %4
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
br label %40
37: ; preds = %14
%38 = call i64 @calloc(i32 1, i32 1)
%39 = inttoptr i64 %38 to i8*
store i8* %39, i8** %4
br label %40
40: ; preds = %37, %20
%41 = load i8*, i8** %4
store i8* %41, i8** %2
br label %42
42: ; preds = %40, %13
%43 = load i8*, i8** %2
ret i8* %43
}

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