#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 */

/* Variables and functions */
int /*<<< orphan*/ abort();
char *calloc(int, int);
int /*<<< orphan*/ free(char *);
int read(int, char *, int);
char *realloc(char *, size_t);
int readFD(int fd, char **buf) {
  char *p;
  size_t size = 4096;
  int s, filesize = 0;
  *buf = calloc(4096, sizeof(char));
  if (*buf == NULL)
    abort();
  do {
    p = &(*buf)[filesize];
    s = read(fd, p, 4096);
    if (s < 0)
      break;
    filesize += s;
    if (s == 0)
      break;
    size += s;
    *buf = realloc(*buf, size);
    if (*buf == NULL)
      abort();
  } while (1);
  if (filesize == 0 && s < 0) {
    free(*buf);
    *buf = NULL;
    return -1;
  }
  return filesize;
}


x86 assembly (clang, -O0):
<readFD>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x1000,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    $0x1000,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    119c <readFD+0x4c>
mov    $0x0,%al
call   1040 <abort@plt>
jmp    11a1 <readFD+0x51>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11d6 <readFD+0x86>
jmp    123d <readFD+0xed>
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11ee <readFD+0x9e>
jmp    123d <readFD+0xed>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1229 <readFD+0xd9>
mov    $0x0,%al
call   1040 <abort@plt>
jmp    122e <readFD+0xde>
mov    $0x1,%al
test   $0x1,%al
jne    11a1 <readFD+0x51>
jmp    123d <readFD+0xed>
cmpl   $0x0,-0x28(%rbp)
jne    1274 <readFD+0x124>
cmpl   $0x0,-0x24(%rbp)
jge    1274 <readFD+0x124>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <readFD+0x12a>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFD>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%r14d
mov    $0x1000,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     11b5 <readFD+0x65>
xor    %ebp,%ebp
mov    $0x1000,%ebx
nopl   0x0(%rax,%rax,1)
movslq %ebp,%rsi
add    %rax,%rsi
mov    %r14d,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11bc <readFD+0x6c>
add    %eax,%ebp
test   %eax,%eax
je     11d4 <readFD+0x84>
mov    %eax,%eax
add    %rax,%rbx
mov    (%r15),%rdi
mov    %rbx,%rsi
call   1070 <realloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
jne    1180 <readFD+0x30>
xor    %eax,%eax
call   1040 <abort@plt>
test   %ebp,%ebp
jne    11d4 <readFD+0x84>
mov    (%r15),%rdi
call   1030 <free@plt>
movq   $0x0,(%r15)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @readFD(i32 %0, i8** nocapture %1) {
%3 = tail call dereferenceable_or_null(4096) i8* @calloc(i32 4096, i32 1)
store i8* %3, i8** %1
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (...) @abort()
unreachable
7: ; preds = %2, %18
%8 = phi i8* [ %22, %18 ], [ %3, %2 ]
%9 = phi i64 [ %20, %18 ], [ 4096, %2 ]
%10 = phi i32 [ %16, %18 ], [ 0, %2 ]
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %8, i64 %11
%13 = tail call i32 @read(i32 %0, i8* %12, i32 4096)
%14 = icmp slt i32 %13, 0
br i1 %14, label %26, label %15
15: ; preds = %7
%16 = add nsw i32 %13, %10
%17 = icmp eq i32 %13, 0
br i1 %17, label %31, label %18
18: ; preds = %15
%19 = zext i32 %13 to i64
%20 = add i64 %9, %19
%21 = load i8*, i8** %1
%22 = tail call i8* @realloc(i8* %21, i64 %20)
store i8* %22, i8** %1
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %7
24: ; preds = %18
%25 = tail call i32 (...) @abort()
unreachable
26: ; preds = %7
%27 = icmp eq i32 %10, 0
br i1 %27, label %28, label %31
28: ; preds = %26
%29 = load i8*, i8** %1
%30 = tail call i32 @free(i8* %29)
store i8* null, i8** %1
br label %31
31: ; preds = %15, %26, %28
%32 = phi i32 [ -1, %28 ], [ %10, %26 ], [ %16, %15 ]
ret i32 %32
}