#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int readfile(char *filename, char **buf) {
  int fd = open(filename, O_RDONLY);
  if (fd < 0) {
    return -1;
  }
  *buf = 0;
  int res = 0;
  struct stat sb;
  if (fstat(fd, &sb)) {
    res = -1;
    goto CLEANUP;
  }
  *buf = malloc(sb.st_size);
  int nread = 0;
  int n = 0;
  do {
    n = read(fd, *buf, sb.st_size);
    if (n < 0) {
      res = -1;
      goto CLEANUP;
    }
    nread += n;
  } while (n != 0 && nread < sb.st_size);
  res = nread;
CLEANUP:
  close(fd);
  return res;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    118b <readfile+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    124d <readfile+0xfd>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
je     11bf <readfile+0x6f>
movl   $0xffffffff,-0x20(%rbp)
jmp    123f <readfile+0xef>
mov    -0x28(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    120a <readfile+0xba>
movl   $0xffffffff,-0x20(%rbp)
jmp    123f <readfile+0xef>
mov    -0x30(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x30(%rbp)
mov    %al,-0x31(%rbp)
je     122e <readfile+0xde>
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
setl   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11df <readfile+0x8f>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     119c <readfile+0x4c>
mov    %eax,%r12d
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11b2 <readfile+0x62>
mov    %r12d,%edi
call   1030 <close@plt>
jmp    11a2 <readfile+0x52>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,(%r15)
mov    %r12d,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
test   %eax,%eax
js     1192 <readfile+0x42>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %eax,%ebx
test   %eax,%eax
je     11fd <readfile+0xad>
cmp    %ebp,%ebx
jge    11fd <readfile+0xad>
mov    (%r15),%rsi
mov    %r12d,%edi
mov    %ebp,%edx
call   1040 <read@plt>
test   %eax,%eax
jns    11e0 <readfile+0x90>
jmp    1192 <readfile+0x42>
mov    %ebx,%r14d
jmp    1192 <readfile+0x42>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
define i32 @readfile(i8* nocapture %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%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 %31, label %7
7: ; preds = %2
store i8* null, i8** %1
%8 = bitcast %struct.stat* %3 to i8*
%9 = call i64 @fstat(i32 %5, %struct.stat* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %28
11: ; preds = %7
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i8* @malloc(i32 %13)
store i8* %14, i8** %1
%15 = tail call i32 @read(i32 %5, i8* %14, i32 %13)
%16 = icmp slt i32 %15, 0
br i1 %16, label %28, label %17
17: ; preds = %11, %24
%18 = phi i32 [ %26, %24 ], [ %15, %11 ]
%19 = phi i32 [ %20, %24 ], [ 0, %11 ]
%20 = add nsw i32 %18, %19
%21 = icmp ne i32 %18, 0
%22 = icmp slt i32 %20, %13
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = load i8*, i8** %1
%26 = tail call i32 @read(i32 %5, i8* %25, i32 %13)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %17
28: ; preds = %24, %17, %11, %7
%29 = phi i32 [ -1, %7 ], [ -1, %11 ], [ %20, %17 ], [ -1, %24 ]
%30 = tail call i32 @close(i32 %5)
br label %31
31: ; preds = %2, %28
%32 = phi i32 [ %29, %28 ], [ -1, %2 ]
ret i32 %32
}