#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;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
char *malloc(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int xread(int, char *, int);
ssize_t read_file(const char *filename, char **bufp) {
  int fd = open(filename, O_RDONLY);
  if (fd < 0) {
    return -1;
  }
  struct stat st;
  if (fstat(fd, &st) == -1) {
    close(fd);
    return -1;
  }
  char *buf = malloc(sizeof(char) * (st.st_size + 1));
  if (buf == NULL) {
    perror("malloc");
    exit(1);
  }
  ssize_t r = xread(fd, buf, st.st_size);
  close(fd);
  if (r > 0) {
    buf[r] = '\0';
    *bufp = buf;
  } else {
    free(buf);
  }
  return r;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11bb <read_file+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <read_file+0xeb>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11e4 <read_file+0x64>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <read_file+0xeb>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    121c <read_file+0x9c>
lea    0xdf3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1050 <xread@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    125c <read_file+0xdc>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    1265 <read_file+0xe5>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%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   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11e5 <read_file+0x65>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
cmp    $0xffffffff,%eax
je     11ec <read_file+0x6c>
mov    (%rsp),%ebx
lea    0x1(%rbx),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     120f <read_file+0x8f>
mov    %rax,%r15
mov    %ebp,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1050 <xread@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
test   %ebx,%ebx
jle    11fa <read_file+0x7a>
mov    %ebx,%eax
movb   $0x0,(%r15,%rax,1)
mov    %r15,(%r14)
jmp    1202 <read_file+0x82>
mov    $0xffffffff,%ebx
jmp    1202 <read_file+0x82>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    1202 <read_file+0x82>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdea(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [7 x i8] c"malloc\00"
define i32 @read_file(i8* nocapture %0, i8** nocapture writeonly %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 %33, label %7
7: ; preds = %2
%8 = bitcast %struct.stat* %3 to i8*
%9 = call i32 @fstat(i32 %5, %struct.stat* %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @close(i32 %5)
br label %31
13: ; preds = %7
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 1
%17 = tail call i8* @malloc(i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = tail call i32 @xread(i32 %5, i8* %17, i32 %15)
%24 = tail call i32 @close(i32 %5)
%25 = icmp sgt i32 %23, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = zext i32 %23 to i64
%28 = getelementptr inbounds i8, i8* %17, i64 %27
store i8 0, i8* %28
store i8* %17, i8** %1
br label %31
29: ; preds = %22
%30 = tail call i32 @free(i8* %17)
br label %31
31: ; preds = %26, %29, %11
%32 = phi i32 [ -1, %11 ], [ %23, %29 ], [ %23, %26 ]
br label %33
33: ; preds = %2, %31
%34 = phi i32 [ %32, %31 ], [ -1, %2 ]
ret i32 %34
}