#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 */
scalar_t__ BUFSIZ;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
char *malloc_or_die(scalar_t__);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, scalar_t__);
char *realloc(char *, scalar_t__);
int read_file(const char *file, char **buffer) {
  char *buf;
  int len = 0;
  int fd;
  int r;
  fd = open(file, O_RDONLY);
  if (fd < 0)
    return -1;
  buf = malloc_or_die(BUFSIZ + 1);
  while ((r = read(fd, buf + len, BUFSIZ)) > 0) {
    len += r;
    buf = realloc(buf, len + BUFSIZ + 1);
    if (!buf) {
      len = -1;
      goto out;
    }
  }
  *buffer = buf;
  buf[len] = 0;
out:
  close(fd);
  return len;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1192 <read_file+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    123b <read_file+0xeb>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1050 <malloc_or_die@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    0x2e1a(%rip),%rax
mov    (%rax),%rdx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jle    1216 <read_file+0xc6>
mov    -0x2c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x24(%rbp),%rsi
mov    0x2dee(%rip),%rax
add    (%rax),%rsi
add    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1211 <read_file+0xc1>
movl   $0xffffffff,-0x24(%rbp)
jmp    122d <read_file+0xdd>
jmp    11a9 <read_file+0x59>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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    %rsi,%r14
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11d1 <read_file+0x81>
mov    %eax,%r15d
mov    0x2e5e(%rip),%r13
mov    0x0(%r13),%rdi
add    $0x1,%rdi
call   1050 <malloc_or_die@plt>
mov    %rax,%r12
xor    %ebp,%ebp
nopl   0x0(%rax)
movslq %ebp,%rbx
add    %r12,%rbx
mov    0x0(%r13),%rdx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <read@plt>
test   %eax,%eax
jle    11d8 <read_file+0x88>
add    %eax,%ebp
movslq %ebp,%rax
mov    0x0(%r13),%rcx
lea    (%rax,%rcx,1),%rsi
add    $0x1,%rsi
mov    %r12,%rdi
call   1060 <realloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1190 <read_file+0x40>
mov    $0xffffffff,%ebp
jmp    11de <read_file+0x8e>
mov    $0xffffffff,%ebp
jmp    11e6 <read_file+0x96>
mov    %r12,(%r14)
movb   $0x0,(%rbx)
mov    %r15d,%edi
call   1030 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@BUFSIZ = global i64 0
define i32 @read_file(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %55
16: ; preds = %2
%17 = load i64, i64* @BUFSIZ
%18 = add nsw i64 %17, 1
%19 = call i8* @malloc_or_die(i64 %18)
store i8* %19, i8** %6
br label %20
20: ; preds = %43, %16
%21 = load i32, i32* %8
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i64, i64* @BUFSIZ
%27 = call i32 @read(i32 %21, i8* %25, i64 %26)
store i32 %27, i32* %9
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %44
29: ; preds = %20
%30 = load i32, i32* %9
%31 = load i32, i32* %7
%32 = add nsw i32 %31, %30
store i32 %32, i32* %7
%33 = load i8*, i8** %6
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = load i64, i64* @BUFSIZ
%37 = add nsw i64 %35, %36
%38 = add nsw i64 %37, 1
%39 = call i8* @realloc(i8* %33, i64 %38)
store i8* %39, i8** %6
%40 = load i8*, i8** %6
%41 = icmp ne i8* %40, null
br i1 %41, label %43, label %42
42: ; preds = %29
store i32 -1, i32* %7
br label %51
43: ; preds = %29
br label %20
44: ; preds = %20
%45 = load i8*, i8** %6
%46 = load i8**, i8*** %5
store i8* %45, i8** %46
%47 = load i8*, i8** %6
%48 = load i32, i32* %7
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 0, i8* %50
br label %51
51: ; preds = %44, %42
%52 = load i32, i32* %8
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %7
store i32 %54, i32* %3
br label %55
55: ; preds = %51, %15
%56 = load i32, i32* %3
ret i32 %56
}

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