#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*/ exit(int);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
unsigned char *malloc(int);
int /*<<< orphan*/ perror(char *);
unsigned char *filebuf(FILE *pf, int *sz) {
  struct stat buf;
  unsigned char *cdata;
  if (fstat(fileno(pf), &buf) < 0) {
    perror("fstat");
    exit(1);
  }
  *sz = buf.st_size;
  if (*sz == 0)
    *sz = (4 * 1024 * 1024);
  if ((cdata = malloc(*sz + 1)) == NULL) {
    perror("malloc");
    exit(1);
  }
  return cdata;
}


x86 assembly (clang, -O0):
<filebuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <fileno@plt>
mov    %eax,%edi
lea    -0x18(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
jge    1194 <filebuf+0x44>
lea    0xe7b(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jne    11b4 <filebuf+0x64>
mov    -0x10(%rbp),%rax
movl   $0x400000,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11e6 <filebuf+0x96>
lea    0xe2f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filebuf>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
call   1030 <fileno@plt>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
test   %rax,%rax
js     1191 <filebuf+0x41>
mov    0x8(%rsp),%eax
test   %eax,%eax
mov    $0x400000,%edi
cmovne %eax,%edi
mov    %edi,(%rbx)
add    $0x1,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     119a <filebuf+0x4a>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe68(%rip),%rdi
jmp    11a1 <filebuf+0x51>
lea    0xe65(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [6 x i8] c"fstat\00"
@.str.1 = [7 x i8] c"malloc\00"
define i8* @filebuf(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca %struct.stat
%6 = alloca i8*
store i32* %0, i32** %3
store i32* %1, i32** %4
%7 = load i32*, i32** %3
%8 = call i32 @fileno(i32* %7)
%9 = call i64 @fstat(i32 %8, %struct.stat* %5)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32*, i32** %4
store i32 %16, i32* %17
%18 = load i32*, i32** %4
%19 = load i32, i32* %18
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = load i32*, i32** %4
store i32 4194304, i32* %22
br label %23
23: ; preds = %21, %14
%24 = load i32*, i32** %4
%25 = load i32, i32* %24
%26 = add nsw i32 %25, 1
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %6
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %23
%33 = load i8*, i8** %6
ret i8* %33
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [6 x i8] c"fstat\00"
@.str.1 = [7 x i8] c"malloc\00"
define noalias i8* @filebuf(i32* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 @fileno(i32* %0)
%6 = call i64 @fstat(i32 %5, %struct.stat* %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, 0
%15 = select i1 %14, i32 4194304, i32 %13
store i32 %15, i32* %1
%16 = add nsw i32 %15, 1
%17 = tail call i8* @malloc(i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %11
ret i8* %17
}