#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 size_t ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int O_CLOEXEC;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
char *must_realloc(char *, size_t);
int open(char *, int);
size_t read(int, char *, int);
char *read_file(char *path, size_t *length) {
  int fd;
  char buf[4096], *copy = NULL;
  if (!length)
    return NULL;
  fd = open(path, O_RDONLY | O_CLOEXEC);
  if (fd < 0)
    return NULL;
  *length = 0;
  for (;;) {
    ssize_t n;
    n = read(fd, buf, sizeof(buf));
    if (n < 0)
      goto error;
    if (!n)
      break;
    copy = must_realloc(copy, (*length + n) * sizeof(*copy));
    memcpy(copy + *length, buf, n);
    *length += n;
  }
  close(fd);
  return copy;
error:
  close(fd);
  free(copy);
  return NULL;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x1028(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1196 <read_file+0x36>
movq   $0x0,-0x8(%rbp)
jmp    12a7 <read_file+0x147>
mov    -0x10(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11cb <read_file+0x6b>
movq   $0x0,-0x8(%rbp)
jmp    12a7 <read_file+0x147>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x1c(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %rax,-0x1030(%rbp)
cmpq   $0x0,-0x1030(%rbp)
jae    1204 <read_file+0xa4>
jmp    128b <read_file+0x12b>
cmpq   $0x0,-0x1030(%rbp)
jne    1217 <read_file+0xb7>
jmp    1273 <read_file+0x113>
mov    -0x1028(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
add    -0x1030(%rbp),%rsi
shl    $0x0,%rsi
call   1070 <must_realloc@plt>
mov    %rax,-0x1028(%rbp)
mov    -0x1028(%rbp),%rdi
mov    -0x18(%rbp),%rax
add    (%rax),%rdi
lea    -0x1020(%rbp),%rsi
mov    -0x1030(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x1030(%rbp),%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
jmp    11d6 <read_file+0x76>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x1028(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a7 <read_file+0x147>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x1028(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
test   %rsi,%rsi
je     11f2 <read_file+0xa2>
mov    %rsi,%r12
mov    0x2e76(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11f2 <read_file+0xa2>
mov    %eax,%r14d
movq   $0x0,(%r12)
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %rax,%rax
je     11f6 <read_file+0xa6>
mov    %rax,%rbx
xor    %ebp,%ebp
mov    %rsp,%r15
nopl   (%rax)
mov    (%r12),%rsi
add    %rbx,%rsi
mov    %rbp,%rdi
call   1060 <must_realloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
add    (%r12),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
add    %rbx,(%r12)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b0 <read_file+0x60>
jmp    11f8 <read_file+0xa8>
xor    %ebp,%ebp
jmp    1200 <read_file+0xb0>
xor    %ebp,%ebp
mov    %r14d,%edi
call   1030 <close@plt>
mov    %rbp,%rax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
define i8* @read_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
store i8* null, i8** %8
%10 = load i64*, i64** %5
%11 = icmp ne i64* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
store i8* null, i8** %3
br label %63
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i32, i32* @O_RDONLY
%16 = load i32, i32* @O_CLOEXEC
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %14, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %13
store i8* null, i8** %3
br label %63
22: ; preds = %13
%23 = load i64*, i64** %5
store i64 0, i64* %23
br label %24
24: ; preds = %35, %22
%25 = load i32, i32* %6
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%27 = call i64 @read(i32 %25, i8* %26, i32 4096)
store i64 %27, i64* %9
%28 = load i64, i64* %9
%29 = icmp ult i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
br label %58
31: ; preds = %24
%32 = load i64, i64* %9
%33 = icmp ne i64 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %31
br label %54
35: ; preds = %31
%36 = load i8*, i8** %8
%37 = load i64*, i64** %5
%38 = load i64, i64* %37
%39 = load i64, i64* %9
%40 = add i64 %38, %39
%41 = mul i64 %40, 1
%42 = call i8* @must_realloc(i8* %36, i64 %41)
store i8* %42, i8** %8
%43 = load i8*, i8** %8
%44 = load i64*, i64** %5
%45 = load i64, i64* %44
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%48 = load i64, i64* %9
%49 = call i32 @memcpy(i8* %46, i8* %47, i64 %48)
%50 = load i64, i64* %9
%51 = load i64*, i64** %5
%52 = load i64, i64* %51
%53 = add i64 %52, %50
store i64 %53, i64* %51
br label %24
54: ; preds = %34
%55 = load i32, i32* %6
%56 = call i32 @close(i32 %55)
%57 = load i8*, i8** %8
store i8* %57, i8** %3
br label %63
58: ; preds = %30
%59 = load i32, i32* %6
%60 = call i32 @close(i32 %59)
%61 = load i8*, i8** %8
%62 = call i32 @free(i8* %61)
store i8* null, i8** %3
br label %63
63: ; preds = %58, %54, %21, %12
%64 = load i8*, i8** %3
ret i8* %64
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
define i8* @read_file(i8* nocapture %0, i64* %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp eq i64* %1, null
br i1 %5, label %31, label %6
6: ; preds = %2
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_CLOEXEC
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %31, label %12
12: ; preds = %6
store i64 0, i64* %1
%13 = call i64 @read(i32 %10, i8* %4, i32 4096)
%14 = icmp eq i64 %13, 0
br i1 %14, label %28, label %15
15: ; preds = %12, %15
%16 = phi i64 [ %26, %15 ], [ %13, %12 ]
%17 = phi i8* [ %20, %15 ], [ null, %12 ]
%18 = load i64, i64* %1
%19 = add i64 %18, %16
%20 = call i8* @must_realloc(i8* %17, i64 %19)
%21 = load i64, i64* %1
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = call i32 @memcpy(i8* %22, i8* %4, i64 %16)
%24 = load i64, i64* %1
%25 = add i64 %24, %16
store i64 %25, i64* %1
%26 = call i64 @read(i32 %10, i8* %4, i32 4096)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %15
28: ; preds = %15, %12
%29 = phi i8* [ null, %12 ], [ %20, %15 ]
%30 = call i32 @close(i32 %10)
br label %31
31: ; preds = %6, %2, %28
%32 = phi i8* [ %29, %28 ], [ null, %2 ], [ null, %6 ]
ret i8* %32
}