#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 /*<<< orphan*/ buf;

/* Variables and functions */
int O_CLOEXEC;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
char *must_realloc(char *, size_t);
int open(char *, int);
int 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 (;;) {
    int 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    12a6 <read_file+0x146>
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    12a6 <read_file+0x146>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x1c(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x102c(%rbp)
cmpl   $0x0,-0x102c(%rbp)
jge    1202 <read_file+0xa2>
jmp    128a <read_file+0x12a>
cmpl   $0x0,-0x102c(%rbp)
jne    1214 <read_file+0xb4>
jmp    1272 <read_file+0x112>
mov    -0x1028(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
movslq -0x102c(%rbp),%rax
add    %rax,%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    -0x102c(%rbp),%edx
call   1060 <memcpy@plt>
movslq -0x102c(%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    12a6 <read_file+0x146>
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   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
test   %rsi,%rsi
je     1228 <read_file+0xc8>
mov    %rsi,%r12
mov    0x2e64(%rip),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1228 <read_file+0xc8>
mov    %eax,%r14d
movq   $0x0,(%r12)
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
js     1215 <read_file+0xb5>
mov    %eax,%ebx
xor    %r13d,%r13d
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %ebx,%ebx
je     1240 <read_file+0xe0>
mov    %ebx,%ebp
mov    (%r12),%rsi
add    %rbp,%rsi
mov    %r13,%rdi
call   1070 <must_realloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
add    (%r12),%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
add    %rbp,(%r12)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11d0 <read_file+0x70>
jmp    1218 <read_file+0xb8>
xor    %r13d,%r13d
mov    %r14d,%edi
call   1040 <close@plt>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %r13d,%r13d
mov    %r13,%rax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14d,%edi
call   1040 <close@plt>
jmp    122b <read_file+0xcb>

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 i32
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 %65
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 %65
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 i32 @read(i32 %25, i8* %26, i32 4096)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
br label %60
31: ; preds = %24
%32 = load i32, i32* %9
%33 = icmp ne i32 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %31
br label %56
35: ; preds = %31
%36 = load i8*, i8** %8
%37 = load i64*, i64** %5
%38 = load i64, i64* %37
%39 = load i32, i32* %9
%40 = sext i32 %39 to i64
%41 = add i64 %38, %40
%42 = mul i64 %41, 1
%43 = call i8* @must_realloc(i8* %36, i64 %42)
store i8* %43, i8** %8
%44 = load i8*, i8** %8
%45 = load i64*, i64** %5
%46 = load i64, i64* %45
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%49 = load i32, i32* %9
%50 = call i32 @memcpy(i8* %47, i8* %48, i32 %49)
%51 = load i32, i32* %9
%52 = sext i32 %51 to i64
%53 = load i64*, i64** %5
%54 = load i64, i64* %53
%55 = add i64 %54, %52
store i64 %55, i64* %53
br label %24
56: ; preds = %34
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
%59 = load i8*, i8** %8
store i8* %59, i8** %3
br label %65
60: ; preds = %30
%61 = load i32, i32* %6
%62 = call i32 @close(i32 %61)
%63 = load i8*, i8** %8
%64 = call i32 @free(i8* %63)
store i8* null, i8** %3
br label %65
65: ; preds = %60, %56, %21, %12
%66 = load i8*, i8** %3
ret i8* %66
}

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 %37, 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 %37, label %12
12: ; preds = %6
store i64 0, i64* %1
%13 = call i32 @read(i32 %10, i8* %4, i32 4096)
%14 = icmp slt i32 %13, 0
br i1 %14, label %33, label %15
15: ; preds = %12, %19
%16 = phi i32 [ %29, %19 ], [ %13, %12 ]
%17 = phi i8* [ %23, %19 ], [ null, %12 ]
%18 = icmp eq i32 %16, 0
br i1 %18, label %31, label %19
19: ; preds = %15
%20 = load i64, i64* %1
%21 = zext i32 %16 to i64
%22 = add i64 %20, %21
%23 = call i8* @must_realloc(i8* %17, i64 %22)
%24 = load i64, i64* %1
%25 = getelementptr inbounds i8, i8* %23, i64 %24
%26 = call i32 @memcpy(i8* %25, i8* %4, i32 %16)
%27 = load i64, i64* %1
%28 = add i64 %27, %21
store i64 %28, i64* %1
%29 = call i32 @read(i32 %10, i8* %4, i32 4096)
%30 = icmp slt i32 %29, 0
br i1 %30, label %33, label %15
31: ; preds = %15
%32 = call i32 @close(i32 %10)
br label %37
33: ; preds = %19, %12
%34 = phi i8* [ null, %12 ], [ %23, %19 ]
%35 = call i32 @close(i32 %10)
%36 = call i32 @free(i8* %34)
br label %37
37: ; preds = %6, %2, %33, %31
%38 = phi i8* [ null, %33 ], [ %17, %31 ], [ null, %2 ], [ null, %6 ]
ret i8* %38
}