#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*/ free(char *);
scalar_t__ malloc(size_t);
int read(int, char *, size_t);
char *realloc(char *, size_t);
int read_file(int fd, char **buffer, size_t *length) {
  int result = -1, bytes = 0;
  size_t chunk = 256;
  char *tmp = NULL;
  *length = chunk + 1;
  *buffer = (char *)malloc(*length);
  if (!buffer)
    goto exit;
  while (((result = read(fd, (*buffer + bytes), chunk)) != -1) &&
         (result != 0)) {
    *length += chunk;
    tmp = realloc(*buffer, *length + chunk);
    if (!tmp) {
      result = -1;
      goto error;
    }
    *buffer = tmp;
    bytes += result;
  }
  tmp[bytes] = '\0';
  result = 0;
  goto exit;
error:
  free(*buffer);
  *buffer = NULL;
exit:
  return result;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movq   $0x100,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rcx
add    $0x1,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x10(%rbp)
jne    11a6 <read_file+0x66>
jmp    127a <read_file+0x13a>
jmp    11ab <read_file+0x6b>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x31(%rbp)
je     11e2 <read_file+0xa2>
cmpl   $0x0,-0x1c(%rbp)
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11f2 <read_file+0xb2>
jmp    124b <read_file+0x10b>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
add    -0x28(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1232 <read_file+0xf2>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1263 <read_file+0x123>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11ab <read_file+0x6b>
mov    -0x30(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x1c(%rbp)
jmp    127a <read_file+0x13a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %edi,%r15d
movq   $0x101,(%rdx)
mov    $0x101,%edi
call   1050 <malloc@plt>
mov    %rax,(%r14)
mov    $0x100,%edx
mov    %r15d,%edi
mov    %rax,%rsi
call   1040 <read@plt>
mov    %eax,%r13d
xor    %ebp,%ebp
nopl   (%rax)
mov    (%r12),%rsi
lea    0x100(%rsi),%rax
mov    %rax,(%r12)
mov    (%r14),%rdi
add    $0x200,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
je     11d1 <read_file+0x91>
mov    %rax,(%r14)
add    %r13d,%ebp
movslq %ebp,%rbx
add    %rax,%rbx
mov    $0x100,%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <read@plt>
mov    %eax,%r13d
add    $0xffffffff,%eax
cmp    $0xfffffffe,%eax
jb     1180 <read_file+0x40>
movb   $0x0,(%rbx)
xor    %eax,%eax
jmp    11e5 <read_file+0xa5>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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