#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*/ close(int);
int /*<<< orphan*/ free(char *);
size_t read(int, char *, int);
char *realloc(char *, size_t);
char *fd2str(int fd) {
  char *buf = NULL;
  size_t count = 0, n;
  do {
    buf = realloc(buf, count + 512);
    n = read(fd, buf + count, 512);
    if (n < 0) {
      free(buf);
      buf = NULL;
    }
    count += n;
  } while (n == 512);
  close(fd);
  if (buf)
    buf[count] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<fd2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x200,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x18(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    11a8 <fd2str+0x68>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x20(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x200,-0x20(%rbp)
je     115b <fd2str+0x1b>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x10(%rbp)
je     11e1 <fd2str+0xa1>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd2str>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
xor    %ebp,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
lea    0x200(%rbp),%rsi
mov    %rbx,%rdi
call   1050 <realloc@plt>
mov    %rax,%rbx
lea    (%rax,%rbp,1),%rsi
mov    %r14d,%edi
mov    $0x200,%edx
call   1040 <read@plt>
add    %rax,%rbp
cmp    $0x200,%rax
je     1140 <fd2str+0x10>
mov    %r14d,%edi
call   1030 <close@plt>
test   %rbx,%rbx
je     117f <fd2str+0x4f>
movb   $0x0,(%rbx,%rbp,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @fd2str(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
store i32 %0, i32* %2
store i8* null, i8** %3
store i64 0, i64* %4
br label %6
6: ; preds = %25, %1
%7 = load i8*, i8** %3
%8 = load i64, i64* %4
%9 = add i64 %8, 512
%10 = call i8* @realloc(i8* %7, i64 %9)
store i8* %10, i8** %3
%11 = load i32, i32* %2
%12 = load i8*, i8** %3
%13 = load i64, i64* %4
%14 = getelementptr inbounds i8, i8* %12, i64 %13
%15 = call i64 @read(i32 %11, i8* %14, i32 512)
store i64 %15, i64* %5
%16 = load i64, i64* %5
%17 = icmp ult i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %6
%19 = load i8*, i8** %3
%20 = call i32 @free(i8* %19)
store i8* null, i8** %3
br label %21
21: ; preds = %18, %6
%22 = load i64, i64* %5
%23 = load i64, i64* %4
%24 = add i64 %23, %22
store i64 %24, i64* %4
br label %25
25: ; preds = %21
%26 = load i64, i64* %5
%27 = icmp eq i64 %26, 512
br i1 %27, label %6, label %28
28: ; preds = %25
%29 = load i32, i32* %2
%30 = call i32 @close(i32 %29)
%31 = load i8*, i8** %3
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = load i8*, i8** %3
%35 = load i64, i64* %4
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %33, %28
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
define noalias i8* @fd2str(i32 %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i64 [ 0, %1 ], [ %9, %2 ]
%4 = phi i8* [ null, %1 ], [ %6, %2 ]
%5 = add i64 %3, 512
%6 = tail call i8* @realloc(i8* %4, i64 %5)
%7 = getelementptr inbounds i8, i8* %6, i64 %3
%8 = tail call i64 @read(i32 %0, i8* %7, i32 512)
%9 = add i64 %8, %3
%10 = icmp eq i64 %8, 512
br i1 %10, label %2, label %11
11: ; preds = %2
%12 = tail call i32 @close(i32 %0)
%13 = icmp eq i8* %6, null
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = getelementptr inbounds i8, i8* %6, i64 %9
store i8 0, i8* %15
br label %16
16: ; preds = %14, %11
ret i8* %6
}