#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 read(int, char *, int);
int /*<<< orphan*/ xfree(char *);
char *xmalloc(int);
char *xrealloc(char *, int);
int Pipe2Buf(int fd, char **buf) {
  int got;
  int clen = 0;
  int blen = 0;
  blen = 4096 * 10;
  *buf = xmalloc(blen);
  while (1) {
    if ((clen + 4096) >= blen) {
      blen += 4096 * 10;
      *buf = xrealloc(*buf, blen);
    }
    got = read(fd, *buf + clen, 4096);
    if (got > 0) {
      clen += got;
      continue;
    } else if (got == 0) {
      blen = clen + 1;
      *buf = xrealloc(*buf, blen);
      *(*buf + clen) = '\0';
      break;
    } else {
      xfree(*buf);
      blen = -1;
      break;
    }
  }
  return blen;
}


x86 assembly (clang, -O0):
<Pipe2Buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0xa000,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <xmalloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%eax
add    $0x1000,%eax
cmp    -0x1c(%rbp),%eax
jl     11ab <Pipe2Buf+0x6b>
mov    -0x1c(%rbp),%eax
add    $0xa000,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <xrealloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11e1 <Pipe2Buf+0xa1>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1176 <Pipe2Buf+0x36>
cmpl   $0x0,-0x14(%rbp)
jne    1221 <Pipe2Buf+0xe1>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <xrealloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1234 <Pipe2Buf+0xf4>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <xfree@plt>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Pipe2Buf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%r15d
mov    $0xa000,%r12d
mov    $0xa000,%edi
call   1050 <xmalloc@plt>
mov    %rax,(%r14)
xor    %ecx,%ecx
jmp    11a2 <Pipe2Buf+0x62>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0xa000,%r12d
mov    (%r14),%rdi
mov    %r12d,%esi
call   1040 <xrealloc@plt>
mov    %rax,%rsi
mov    %rax,(%r14)
movslq %ebx,%rbp
add    %rbp,%rsi
mov    %r15d,%edi
mov    $0x1000,%edx
call   1060 <read@plt>
lea    (%rax,%rbx,1),%ecx
test   %eax,%eax
jle    11b4 <Pipe2Buf+0x74>
mov    %ecx,%ebx
lea    0x1000(%rbx),%eax
cmp    %r12d,%eax
jge    1170 <Pipe2Buf+0x30>
mov    (%r14),%rsi
jmp    1188 <Pipe2Buf+0x48>
je     11c5 <Pipe2Buf+0x85>
mov    (%r14),%rdi
call   1030 <xfree@plt>
mov    $0xffffffff,%ebx
jmp    11d9 <Pipe2Buf+0x99>
add    $0x1,%ebx
mov    (%r14),%rdi
mov    %ebx,%esi
call   1040 <xrealloc@plt>
mov    %rax,(%r14)
movb   $0x0,(%rax,%rbp,1)
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @Pipe2Buf(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %6
store i32 0, i32* %7
store i32 40960, i32* %7
%8 = load i32, i32* %7
%9 = call i8* @xmalloc(i32 %8)
%10 = load i8**, i8*** %4
store i8* %9, i8** %10
br label %11
11: ; preds = %2, %34
%12 = load i32, i32* %6
%13 = add nsw i32 %12, 4096
%14 = load i32, i32* %7
%15 = icmp sge i32 %13, %14
br i1 %15, label %16, label %24
16: ; preds = %11
%17 = load i32, i32* %7
%18 = add nsw i32 %17, 40960
store i32 %18, i32* %7
%19 = load i8**, i8*** %4
%20 = load i8*, i8** %19
%21 = load i32, i32* %7
%22 = call i8* @xrealloc(i8* %20, i32 %21)
%23 = load i8**, i8*** %4
store i8* %22, i8** %23
br label %24
24: ; preds = %16, %11
%25 = load i32, i32* %3
%26 = load i8**, i8*** %4
%27 = load i8*, i8** %26
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = call i32 @read(i32 %25, i8* %30, i32 4096)
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %24
%35 = load i32, i32* %5
%36 = load i32, i32* %6
%37 = add nsw i32 %36, %35
store i32 %37, i32* %6
br label %11
38: ; preds = %24
%39 = load i32, i32* %5
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %54
41: ; preds = %38
%42 = load i32, i32* %6
%43 = add nsw i32 %42, 1
store i32 %43, i32* %7
%44 = load i8**, i8*** %4
%45 = load i8*, i8** %44
%46 = load i32, i32* %7
%47 = call i8* @xrealloc(i8* %45, i32 %46)
%48 = load i8**, i8*** %4
store i8* %47, i8** %48
%49 = load i8**, i8*** %4
%50 = load i8*, i8** %49
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 0, i8* %53
br label %58
54: ; preds = %38
%55 = load i8**, i8*** %4
%56 = load i8*, i8** %55
%57 = call i32 @xfree(i8* %56)
store i32 -1, i32* %7
br label %58
58: ; preds = %54, %41
%59 = load i32, i32* %7
ret i32 %59
}

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