#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*/ err(int, char *);
char *malloc(size_t);
int read(int, char *, size_t);
char *realloc(char *, size_t);
char *grab_fd(int fd) {
  int ret;
  size_t max, size = 0;
  char *buffer;
  max = 16384;
  buffer = malloc(max + 1);
  while ((ret = read(fd, buffer + size, max - size)) > 0) {
    size += ret;
    if (size == max)
      buffer = realloc(buffer, max *= 2);
  }
  if (ret < 0)
    err(1, "reading from command");
  buffer[size] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<grab_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x4000,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
add    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jle    11c8 <grab_fd+0x88>
movslq -0x8(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11c3 <grab_fd+0x83>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
shl    $0x1,%rsi
mov    %rsi,-0x10(%rbp)
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    116c <grab_fd+0x2c>
cmpl   $0x0,-0x8(%rbp)
jge    11e3 <grab_fd+0xa3>
mov    $0x1,%edi
lea    0xe22(%rip),%rsi
call   1040 <err@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grab_fd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
mov    $0x4001,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    $0x4000,%r12d
mov    $0x4000,%edx
mov    %r14d,%edi
mov    %rax,%rsi
call   1030 <read@plt>
test   %eax,%eax
jle    11b9 <grab_fd+0x79>
xor    %ebp,%ebp
jmp    1199 <grab_fd+0x59>
cs nopw 0x0(%rax,%rax,1)
lea    (%r15,%rbp,1),%rbx
mov    %r12,%rdx
sub    %rbp,%rdx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1030 <read@plt>
test   %eax,%eax
jle    11bc <grab_fd+0x7c>
mov    %rbp,%rcx
mov    %eax,%ebp
add    %rcx,%rbp
cmp    %r12,%rbp
jne    1180 <grab_fd+0x40>
add    %r12,%r12
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <realloc@plt>
mov    %rax,%r15
jmp    1180 <grab_fd+0x40>
mov    %r15,%rbx
test   %eax,%eax
jns    11d1 <grab_fd+0x91>
lea    0xe39(%rip),%rsi
mov    $0x1,%edi
call   1040 <err@plt>
movb   $0x0,(%rbx)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"reading from command\00"
define i8* @grab_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
store i32 %0, i32* %2
store i64 0, i64* %5
store i64 16384, i64* %4
%7 = load i64, i64* %4
%8 = add i64 %7, 1
%9 = call i8* @malloc(i64 %8)
store i8* %9, i8** %6
br label %10
10: ; preds = %33, %1
%11 = load i32, i32* %2
%12 = load i8*, i8** %6
%13 = load i64, i64* %5
%14 = getelementptr inbounds i8, i8* %12, i64 %13
%15 = load i64, i64* %4
%16 = load i64, i64* %5
%17 = sub i64 %15, %16
%18 = call i32 @read(i32 %11, i8* %14, i64 %17)
store i32 %18, i32* %3
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %34
20: ; preds = %10
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = load i64, i64* %5
%24 = add i64 %23, %22
store i64 %24, i64* %5
%25 = load i64, i64* %5
%26 = load i64, i64* %4
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %33
28: ; preds = %20
%29 = load i8*, i8** %6
%30 = load i64, i64* %4
%31 = mul i64 %30, 2
store i64 %31, i64* %4
%32 = call i8* @realloc(i8* %29, i64 %31)
store i8* %32, i8** %6
br label %33
33: ; preds = %28, %20
br label %10
34: ; preds = %10
%35 = load i32, i32* %3
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = load i8*, i8** %6
%41 = load i64, i64* %5
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8 0, i8* %42
%43 = load i8*, i8** %6
ret i8* %43
}

LLVM IR (-O2):
@.str = [21 x i8] c"reading from command\00"
define noalias i8* @grab_fd(i32 %0) {
%2 = tail call dereferenceable_or_null(16385) i8* @malloc(i64 16385)
%3 = tail call i32 @read(i32 %0, i8* %2, i64 16384)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %23
5: ; preds = %1, %16
%6 = phi i32 [ %21, %16 ], [ %3, %1 ]
%7 = phi i8* [ %18, %16 ], [ %2, %1 ]
%8 = phi i64 [ %11, %16 ], [ 0, %1 ]
%9 = phi i64 [ %17, %16 ], [ 16384, %1 ]
%10 = zext i32 %6 to i64
%11 = add i64 %8, %10
%12 = icmp eq i64 %11, %9
br i1 %12, label %13, label %16
13: ; preds = %5
%14 = shl i64 %9, 1
%15 = tail call i8* @realloc(i8* %7, i64 %14)
br label %16
16: ; preds = %13, %5
%17 = phi i64 [ %14, %13 ], [ %9, %5 ]
%18 = phi i8* [ %15, %13 ], [ %7, %5 ]
%19 = getelementptr inbounds i8, i8* %18, i64 %11
%20 = sub i64 %17, %11
%21 = tail call i32 @read(i32 %0, i8* %19, i64 %20)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %5, label %23
23: ; preds = %16, %1
%24 = phi i8* [ %2, %1 ], [ %18, %16 ]
%25 = phi i8* [ %2, %1 ], [ %19, %16 ]
%26 = phi i32 [ %3, %1 ], [ %21, %16 ]
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %23
store i8 0, i8* %25
ret i8* %24
}