#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*/ tmpbuf;
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fstat(int, struct stat *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, char, int);
int read(int, char *, int);
char *realloc(char *, unsigned long);
char *__bufFromFd(int fd) {
  struct stat sbuf;
  char *buf = NULL;
  unsigned long bytes = 0;
  char tmpbuf[16384];
  fstat(fd, &sbuf);
  if (sbuf.st_size) {
    buf = malloc(sbuf.st_size + 1);
    memset(buf, '\0', sbuf.st_size + 1);
    if (read(fd, buf, sbuf.st_size) == -1) {
      close(fd);
      free(buf);
      return NULL;
    }
    buf[sbuf.st_size] = '\0';
  } else {
    memset(tmpbuf, '\0', sizeof(tmpbuf));
    while (read(fd, tmpbuf, sizeof(tmpbuf)) > 0) {
      buf = realloc(buf, bytes + sizeof(tmpbuf));
      memcpy(buf + bytes, tmpbuf, sizeof(tmpbuf));
      bytes += sizeof(tmpbuf);
      memset(tmpbuf, '\0', sizeof(tmpbuf));
    }
  }
  close(fd);
  return buf;
}


x86 assembly (clang, -O0):
<__bufFromFd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4020,%rsp
mov    %edi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10a0 <fstat@plt>
cmpl   $0x0,-0x10(%rbp)
je     121b <__bufFromFd+0x9b>
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
jne    120a <__bufFromFd+0x8a>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b7 <__bufFromFd+0x137>
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12a7 <__bufFromFd+0x127>
lea    -0x4020(%rbp),%rdi
xor    %esi,%esi
mov    $0x4000,%edx
call   1040 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x4020(%rbp),%rsi
mov    $0x4000,%edx
call   1060 <read@plt>
cmp    $0x0,%eax
jle    12a2 <__bufFromFd+0x122>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x4000,%rsi
call   1090 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    -0x20(%rbp),%rdi
lea    -0x4020(%rbp),%rsi
mov    $0x4000,%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x4000,%rax
mov    %rax,-0x20(%rbp)
lea    -0x4020(%rbp),%rdi
xor    %esi,%esi
mov    $0x4000,%edx
call   1040 <memset@plt>
jmp    122e <__bufFromFd+0xae>
jmp    12a7 <__bufFromFd+0x127>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x4020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__bufFromFd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x4018,%rsp
mov    %edi,%r14d
lea    0x8(%rsp),%rsi
call   10a0 <fstat@plt>
movslq 0x8(%rsp),%rbp
test   %rbp,%rbp
je     11df <__bufFromFd+0x5f>
lea    0x1(%rbp),%r15d
mov    %r15d,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1040 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     127a <__bufFromFd+0xfa>
movb   $0x0,(%rbx,%rbp,1)
jmp    1270 <__bufFromFd+0xf0>
lea    0x10(%rsp),%rbp
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4000,%edx
call   1040 <memset@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x4000,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1270 <__bufFromFd+0xf0>
mov    $0x4000,%ebp
xor    %ebx,%ebx
lea    0x10(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1090 <realloc@plt>
mov    %rax,%rbx
lea    (%rax,%rbp,1),%rdi
add    $0xffffffffffffc000,%rdi
mov    %r15,%rsi
mov    $0x4000,%edx
call   1070 <memcpy@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4000,%edx
call   1040 <memset@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x4000,%edx
call   1060 <read@plt>
add    $0x4000,%rbp
test   %eax,%eax
jg     1220 <__bufFromFd+0xa0>
mov    %r14d,%edi
call   1050 <close@plt>
jmp    128c <__bufFromFd+0x10c>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x4018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i8* @__bufFromFd(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [16384 x i8]
store i32 %0, i32* %3
store i8* null, i8** %5
store i64 0, i64* %6
%8 = load i32, i32* %3
%9 = call i32 @fstat(i32 %8, %struct.stat* %4)
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%11 = load i32, i32* %10
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %40
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 1
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = add nsw i32 %20, 1
%22 = call i32 @memset(i8* %18, i8 signext 0, i32 %21)
%23 = load i32, i32* %3
%24 = load i8*, i8** %5
%25 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @read(i32 %23, i8* %24, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %13
%30 = load i32, i32* %3
%31 = call i32 @close(i32 %30)
%32 = load i8*, i8** %5
%33 = call i32 @free(i8* %32)
store i8* null, i8** %2
br label %67
34: ; preds = %13
%35 = load i8*, i8** %5
%36 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%37 = load i32, i32* %36
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
store i8 0, i8* %39
br label %63
40: ; preds = %1
%41 = getelementptr inbounds [16384 x i8], [16384 x i8]* %7, i64 0, i64 0
%42 = call i32 @memset(i8* %41, i8 signext 0, i32 16384)
br label %43
43: ; preds = %48, %40
%44 = load i32, i32* %3
%45 = getelementptr inbounds [16384 x i8], [16384 x i8]* %7, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 16384)
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %62
48: ; preds = %43
%49 = load i8*, i8** %5
%50 = load i64, i64* %6
%51 = add i64 %50, 16384
%52 = call i8* @realloc(i8* %49, i64 %51)
store i8* %52, i8** %5
%53 = load i8*, i8** %5
%54 = load i64, i64* %6
%55 = getelementptr inbounds i8, i8* %53, i64 %54
%56 = getelementptr inbounds [16384 x i8], [16384 x i8]* %7, i64 0, i64 0
%57 = call i32 @memcpy(i8* %55, i8* %56, i32 16384)
%58 = load i64, i64* %6
%59 = add i64 %58, 16384
store i64 %59, i64* %6
%60 = getelementptr inbounds [16384 x i8], [16384 x i8]* %7, i64 0, i64 0
%61 = call i32 @memset(i8* %60, i8 signext 0, i32 16384)
br label %43
62: ; preds = %43
br label %63
63: ; preds = %62, %34
%64 = load i32, i32* %3
%65 = call i32 @close(i32 %64)
%66 = load i8*, i8** %5
store i8* %66, i8** %2
br label %67
67: ; preds = %63, %29
%68 = load i8*, i8** %2
ret i8* %68
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i8* @__bufFromFd(i32 %0) {
%2 = alloca %struct.stat
%3 = alloca [16384 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%6 = call i32 @fstat(i32 %0, %struct.stat* %2)
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = icmp eq i32 %8, 0
br i1 %9, label %22, label %10
10: ; preds = %1
%11 = add nsw i32 %8, 1
%12 = tail call i8* @malloc(i32 %11)
%13 = tail call i32 @memset(i8* %12, i8 signext 0, i32 %11)
%14 = tail call i32 @read(i32 %0, i8* %12, i32 %8)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = tail call i32 @close(i32 %0)
%18 = tail call i32 @free(i8* %12)
br label %39
19: ; preds = %10
%20 = sext i32 %8 to i64
%21 = getelementptr inbounds i8, i8* %12, i64 %20
store i8 0, i8* %21
br label %36
22: ; preds = %1
%23 = call i32 @memset(i8* %5, i8 signext 0, i32 16384)
%24 = call i32 @read(i32 %0, i8* %5, i32 16384)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %22, %26
%27 = phi i64 [ %29, %26 ], [ 0, %22 ]
%28 = phi i8* [ %30, %26 ], [ null, %22 ]
%29 = add i64 %27, 16384
%30 = call i8* @realloc(i8* %28, i64 %29)
%31 = getelementptr inbounds i8, i8* %30, i64 %27
%32 = call i32 @memcpy(i8* %31, i8* %5, i32 16384)
%33 = call i32 @memset(i8* %5, i8 signext 0, i32 16384)
%34 = call i32 @read(i32 %0, i8* %5, i32 16384)
%35 = icmp sgt i32 %34, 0
br i1 %35, label %26, label %36
36: ; preds = %26, %22, %19
%37 = phi i8* [ %12, %19 ], [ null, %22 ], [ %30, %26 ]
%38 = call i32 @close(i32 %0)
br label %39
39: ; preds = %36, %16
%40 = phi i8* [ null, %16 ], [ %37, %36 ]
ret i8* %40
}