#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__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_msg(char *, char *);
int /*<<< orphan*/ err_ret(char *, char *);
scalar_t__ errno;
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
char *realloc(char *, int);
int slurpfile(char *filename, char **buffer, int buflen) {
  int fd, read_len;
  int dynamic = 0, sl = 0;
  char *db;
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    err_ret("slurpfile() open() error on file %s", filename);
    return -1;
  }
  if (*buffer == NULL) {
    db = malloc(buflen);
    dynamic = buflen;
    *buffer = db;
  } else
    db = *buffer;
read:
  read_len = read(fd, db, buflen);
  if (read_len < 0) {
    if (errno == EINTR)
      goto read;
    err_ret("slurpfile() read() error on file %s", filename);
    close(fd);
    return -1;
  } else
    sl += read_len;
  if (read_len == buflen) {
    if (dynamic) {
      dynamic += buflen;
      db = realloc(*buffer, dynamic);
      *buffer = db;
      db = *buffer + dynamic - buflen;
      goto read;
    } else {
      --read_len;
      err_msg("slurpfile() read() buffer overflow on file %s", filename);
    }
  }
  db[read_len] = '\0';
  close(fd);
  return sl;
}


x86 assembly (clang, -O0):
<slurpfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11cc <slurpfile+0x5c>
mov    -0x10(%rbp),%rsi
lea    0xe45(%rip),%rdi
call   1090 <err_ret@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130c <slurpfile+0x19c>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11fc <slurpfile+0x8c>
mov    -0x1c(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    1207 <slurpfile+0x97>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    120c <slurpfile+0x9c>
mov    -0x20(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    126b <slurpfile+0xfb>
mov    0x2da9(%rip),%rax
mov    (%rax),%rax
mov    0x2d97(%rip),%rcx
cmp    (%rcx),%rax
jne    1247 <slurpfile+0xd7>
jmp    120c <slurpfile+0x9c>
mov    -0x10(%rbp),%rsi
lea    0xdd2(%rip),%rdi
call   1090 <err_ret@plt>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130c <slurpfile+0x19c>
mov    -0x24(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    12f2 <slurpfile+0x182>
cmpl   $0x0,-0x28(%rbp)
je     12d4 <slurpfile+0x164>
mov    -0x1c(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x28(%rbp),%rcx
add    %rcx,%rax
movslq -0x1c(%rbp),%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    120c <slurpfile+0x9c>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xd60(%rip),%rdi
call   1080 <err_msg@plt>
jmp    12f2 <slurpfile+0x182>
mov    -0x38(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slurpfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11bd <slurpfile+0x4d>
mov    %eax,%r13d
mov    %rbx,0x10(%rsp)
mov    (%r15),%rbx
xor    %r14d,%r14d
movslq %ebp,%r12
test   %rbx,%rbx
mov    %r15,0x30(%rsp)
mov    %r12,0x28(%rsp)
je     11d1 <slurpfile+0x61>
xor    %r12d,%r12d
jmp    11de <slurpfile+0x6e>
lea    0xe3c(%rip),%rdi
mov    %rbx,%rsi
call   1090 <err_ret@plt>
jmp    128b <slurpfile+0x11b>
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,(%r15)
mov    %r12,0x8(%rsp)
mov    0x2dee(%rip),%r12
mov    0x2ddf(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
test   %eax,%eax
jns    121c <slurpfile+0xac>
mov    (%r12),%rax
cmp    (%r15),%rax
je     1200 <slurpfile+0x90>
jmp    1272 <slurpfile+0x102>
add    %eax,%r14d
cmp    %ebp,%eax
mov    0x8(%rsp),%rcx
jne    12b9 <slurpfile+0x149>
test   %rcx,%rcx
je     12a3 <slurpfile+0x133>
mov    0x28(%rsp),%rax
lea    (%rcx,%rax,1),%rsi
mov    %rsi,0x18(%rsp)
mov    %r14,0x20(%rsp)
mov    0x30(%rsp),%r14
mov    (%r14),%rdi
mov    %rcx,0x8(%rsp)
call   1060 <realloc@plt>
mov    %rax,%rbx
mov    %rax,(%r14)
mov    0x20(%rsp),%r14
add    0x8(%rsp),%rbx
mov    0x18(%rsp),%rax
mov    %rax,0x8(%rsp)
jmp    1200 <slurpfile+0x90>
lea    0xdab(%rip),%rdi
mov    0x10(%rsp),%rsi
call   1090 <err_ret@plt>
mov    %r13d,%edi
call   1030 <close@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
add    $0xffffffff,%ebp
lea    0xd9b(%rip),%rdi
mov    0x10(%rsp),%rsi
call   1080 <err_msg@plt>
mov    %ebp,%eax
cltq
movb   $0x0,(%rbx,%rax,1)
mov    %r13d,%edi
call   1030 <close@plt>
jmp    1291 <slurpfile+0x121>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [36 x i8] c"slurpfile() open() error on file %s\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [36 x i8] c"slurpfile() read() error on file %s\00"
@.str.2 = [46 x i8] c"slurpfile() read() buffer overflow on file %s\00"
define i32 @slurpfile(i8* %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
store i32 0, i32* %10
store i32 0, i32* %11
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = call i32 @err_ret(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 -1, i32* %4
br label %96
21: ; preds = %3
%22 = load i8**, i8*** %6
%23 = load i8*, i8** %22
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = load i32, i32* %7
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %12
%28 = load i32, i32* %7
store i32 %28, i32* %10
%29 = load i8*, i8** %12
%30 = load i8**, i8*** %6
store i8* %29, i8** %30
br label %34
31: ; preds = %21
%32 = load i8**, i8*** %6
%33 = load i8*, i8** %32
store i8* %33, i8** %12
br label %34
34: ; preds = %31, %25
br label %35
35: ; preds = %63, %46, %34
%36 = load i32, i32* %8
%37 = load i8*, i8** %12
%38 = load i32, i32* %7
%39 = call i32 @read(i32 %36, i8* %37, i32 %38)
store i32 %39, i32* %9
%40 = load i32, i32* %9
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %52
42: ; preds = %35
%43 = load i64, i64* @errno
%44 = load i64, i64* @EINTR
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %42
br label %35
47: ; preds = %42
%48 = load i8*, i8** %5
%49 = call i32 @err_ret(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %48)
%50 = load i32, i32* %8
%51 = call i32 @close(i32 %50)
store i32 -1, i32* %4
br label %96
52: ; preds = %35
%53 = load i32, i32* %9
%54 = load i32, i32* %11
%55 = add nsw i32 %54, %53
store i32 %55, i32* %11
br label %56
56: ; preds = %52
%57 = load i32, i32* %9
%58 = load i32, i32* %7
%59 = icmp eq i32 %57, %58
br i1 %59, label %60, label %88
60: ; preds = %56
%61 = load i32, i32* %10
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %82
63: ; preds = %60
%64 = load i32, i32* %7
%65 = load i32, i32* %10
%66 = add nsw i32 %65, %64
store i32 %66, i32* %10
%67 = load i8**, i8*** %6
%68 = load i8*, i8** %67
%69 = load i32, i32* %10
%70 = call i8* @realloc(i8* %68, i32 %69)
store i8* %70, i8** %12
%71 = load i8*, i8** %12
%72 = load i8**, i8*** %6
store i8* %71, i8** %72
%73 = load i8**, i8*** %6
%74 = load i8*, i8** %73
%75 = load i32, i32* %10
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %74, i64 %76
%78 = load i32, i32* %7
%79 = sext i32 %78 to i64
%80 = sub i64 0, %79
%81 = getelementptr inbounds i8, i8* %77, i64 %80
store i8* %81, i8** %12
br label %35
82: ; preds = %60
%83 = load i32, i32* %9
%84 = add nsw i32 %83, -1
store i32 %84, i32* %9
%85 = load i8*, i8** %5
%86 = call i32 @err_msg(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i8* %85)
br label %87
87: ; preds = %82
br label %88
88: ; preds = %87, %56
%89 = load i8*, i8** %12
%90 = load i32, i32* %9
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i8, i8* %89, i64 %91
store i8 0, i8* %92
%93 = load i32, i32* %8
%94 = call i32 @close(i32 %93)
%95 = load i32, i32* %11
store i32 %95, i32* %4
br label %96
96: ; preds = %88, %47, %18
%97 = load i32, i32* %4
ret i32 %97
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [36 x i8] c"slurpfile() open() error on file %s\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [36 x i8] c"slurpfile() read() error on file %s\00"
@.str.2 = [46 x i8] c"slurpfile() read() buffer overflow on file %s\00"
define i32 @slurpfile(i8* %0, i8** nocapture %1, i32 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @err_ret(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %52
9: ; preds = %3
%10 = load i8*, i8** %1
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i8* @malloc(i32 %2)
store i8* %13, i8** %1
%14 = sext i32 %2 to i64
br label %15
15: ; preds = %9, %12
%16 = phi i64 [ %14, %12 ], [ 0, %9 ]
%17 = phi i8* [ %13, %12 ], [ %10, %9 ]
%18 = sext i32 %2 to i64
br label %19
19: ; preds = %38, %15
%20 = phi i64 [ %39, %38 ], [ %16, %15 ]
%21 = phi i32 [ %34, %38 ], [ 0, %15 ]
%22 = phi i8* [ %43, %38 ], [ %17, %15 ]
br label %23
23: ; preds = %19, %26
%24 = tail call i32 @read(i32 %5, i8* %22, i32 %2)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %23
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %23, label %30
30: ; preds = %26
%31 = tail call i32 @err_ret(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%32 = tail call i32 @close(i32 %5)
br label %52
33: ; preds = %23
%34 = add nsw i32 %24, %21
%35 = icmp eq i32 %24, %2
br i1 %35, label %36, label %47
36: ; preds = %33
%37 = icmp eq i64 %20, 0
br i1 %37, label %44, label %38
38: ; preds = %36
%39 = add i64 %20, %18
%40 = load i8*, i8** %1
%41 = trunc i64 %39 to i32
%42 = tail call i8* @realloc(i8* %40, i32 %41)
store i8* %42, i8** %1
%43 = getelementptr inbounds i8, i8* %42, i64 %20
br label %19
44: ; preds = %36
%45 = add nsw i32 %2, -1
%46 = tail call i32 @err_msg(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %47
47: ; preds = %33, %44
%48 = phi i32 [ %45, %44 ], [ %24, %33 ]
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %22, i64 %49
store i8 0, i8* %50
%51 = tail call i32 @close(i32 %5)
br label %52
52: ; preds = %47, %30, %7
%53 = phi i32 [ -1, %7 ], [ -1, %30 ], [ %34, %47 ]
ret i32 %53
}