#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int read(int, char *, int);
char *realloc(char *, size_t);
char *monikor_read_fd(int fd) {
  char *content = NULL;
  size_t content_size = 0;
  ssize_t read_bytes;
  do {
    if (!(content = realloc(content, content_size + 4096 + 1)))
      goto err;
    read_bytes = read(fd, content + content_size, 4096);
    if (read_bytes == -1)
      goto err;
    content_size += read_bytes;
    content[content_size] = 0;
  } while (read_bytes == 4096);
  return content;
err:
  free(content);
  return NULL;
}


x86 assembly (clang, -O0):
<monikor_read_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1000,%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1176 <monikor_read_fd+0x46>
jmp    11cf <monikor_read_fd+0x9f>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    119d <monikor_read_fd+0x6d>
jmp    11cf <monikor_read_fd+0x9f>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x1000,-0x24(%rbp)
je     114b <monikor_read_fd+0x1b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e0 <monikor_read_fd+0xb0>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<monikor_read_fd>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
xor    %ebx,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
lea    0x1001(%rbp),%rsi
mov    %rbx,%rdi
call   1050 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1182 <monikor_read_fd+0x52>
mov    %rbx,%rsi
add    %rbp,%rsi
mov    %r14d,%edi
mov    $0x1000,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     1182 <monikor_read_fd+0x52>
movslq %eax,%rcx
add    %rcx,%rbp
movb   $0x0,(%rbx,%rbp,1)
cmp    $0x1000,%eax
je     1140 <monikor_read_fd+0x10>
jmp    118c <monikor_read_fd+0x5c>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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