#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);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int read_file(char *filepath, char **data) {
  struct stat st;
  int fd;
  int i;
  char *w;
  int rsize;
  if (stat(filepath, &st)) {
    perror("stat");
    return (1);
  } else if (st.st_size == 0) {
    fprintf(stderr, "file seems empty\n");
    return (1);
  }
  fd = open(filepath, O_RDONLY);
  if (fd == -1) {
    perror("open");
    return (1);
  }
  *data = malloc(sizeof(char) * (st.st_size + 1));
  if (!(*data)) {
    perror("malloc");
    close(fd);
    return (1);
  }
  w = *data;
  i = 0;
  rsize = st.st_size;
  do {
    i = read(fd, w, rsize);
    if (i == -1) {
      perror("read");
      close(fd);
      free(*data);
      return (1);
    }
    w += i;
    rsize -= i;
  } while (i > 0);
  *w = '\0';
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11bf <read_file+0x3f>
lea    0xe52(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    130d <read_file+0x18d>
cmpl   $0x0,-0x20(%rbp)
jne    11ea <read_file+0x6a>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    130d <read_file+0x18d>
jmp    11ef <read_file+0x6f>
mov    -0x10(%rbp),%rdi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1226 <read_file+0xa6>
lea    0xe02(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    130d <read_file+0x18d>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1271 <read_file+0xf1>
lea    0xdc4(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    130d <read_file+0x18d>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    12d1 <read_file+0x151>
lea    0xd77(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    130d <read_file+0x18d>
mov    -0x28(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%ecx
mov    -0x34(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x28(%rbp)
jg     1289 <read_file+0x109>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    1215 <read_file+0x95>
cmpl   $0x0,(%rsp)
je     11fe <read_file+0x7e>
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     121e <read_file+0x9e>
mov    %eax,%r15d
mov    (%rsp),%ebx
lea    0x1(%rbx),%edi
call   1080 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     122c <read_file+0xac>
mov    %rax,%rbp
nop
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     1242 <read_file+0xc2>
movslq %eax,%rcx
add    %rcx,%rbp
sub    %eax,%ebx
test   %eax,%eax
jg     11d0 <read_file+0x50>
movb   $0x0,0x0(%rbp)
mov    %r15d,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    1263 <read_file+0xe3>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
call   1060 <fprintf@plt>
jmp    125e <read_file+0xde>
lea    0xde4(%rip),%rdi
jmp    1225 <read_file+0xa5>
lea    0xdf2(%rip),%rdi
call   10a0 <perror@plt>
jmp    125e <read_file+0xde>
lea    0xde9(%rip),%rdi
call   10a0 <perror@plt>
mov    %r15d,%edi
call   1040 <close@plt>
jmp    125e <read_file+0xde>
lea    0xdda(%rip),%rdi
call   10a0 <perror@plt>
mov    %r15d,%edi
call   1040 <close@plt>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"file seems empty\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [5 x i8] c"read\00"
define i32 @read_file(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
%11 = load i8*, i8** %4
%12 = call i64 @stat(i8* %11, %struct.stat* %6)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %82
16: ; preds = %2
%17 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%18 = load i32, i32* %17
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %82
23: ; preds = %16
br label %24
24: ; preds = %23
%25 = load i8*, i8** %4
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %82
32: ; preds = %24
%33 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%34 = load i32, i32* %33
%35 = add nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = mul i64 1, %36
%38 = trunc i64 %37 to i32
%39 = call i8* @malloc(i32 %38)
%40 = load i8**, i8*** %5
store i8* %39, i8** %40
%41 = load i8**, i8*** %5
%42 = load i8*, i8** %41
%43 = icmp ne i8* %42, null
br i1 %43, label %48, label %44
44: ; preds = %32
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
store i32 1, i32* %3
br label %82
48: ; preds = %32
%49 = load i8**, i8*** %5
%50 = load i8*, i8** %49
store i8* %50, i8** %9
store i32 0, i32* %8
%51 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%52 = load i32, i32* %51
store i32 %52, i32* %10
br label %53
53: ; preds = %75, %48
%54 = load i32, i32* %7
%55 = load i8*, i8** %9
%56 = load i32, i32* %10
%57 = call i32 @read(i32 %54, i8* %55, i32 %56)
store i32 %57, i32* %8
%58 = load i32, i32* %8
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %67
60: ; preds = %53
%61 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%62 = load i32, i32* %7
%63 = call i32 @close(i32 %62)
%64 = load i8**, i8*** %5
%65 = load i8*, i8** %64
%66 = call i32 @free(i8* %65)
store i32 1, i32* %3
br label %82
67: ; preds = %53
%68 = load i32, i32* %8
%69 = load i8*, i8** %9
%70 = sext i32 %68 to i64
%71 = getelementptr inbounds i8, i8* %69, i64 %70
store i8* %71, i8** %9
%72 = load i32, i32* %8
%73 = load i32, i32* %10
%74 = sub nsw i32 %73, %72
store i32 %74, i32* %10
br label %75
75: ; preds = %67
%76 = load i32, i32* %8
%77 = icmp sgt i32 %76, 0
br i1 %77, label %53, label %78
78: ; preds = %75
%79 = load i8*, i8** %9
store i8 0, i8* %79
%80 = load i32, i32* %7
%81 = call i32 @close(i32 %80)
store i32 0, i32* %3
br label %82
82: ; preds = %78, %60, %44, %30, %20, %14
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"stat\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"file seems empty\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [5 x i8] c"read\00"
define i32 @read_file(i8* %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %47
9: ; preds = %2
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i32, i32* %10
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %47
16: ; preds = %9
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %0, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %47
22: ; preds = %16
%23 = load i32, i32* %10
%24 = add nsw i32 %23, 1
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %1
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @close(i32 %18)
br label %47
30: ; preds = %22, %40
%31 = phi i8* [ %42, %40 ], [ %25, %22 ]
%32 = phi i32 [ %43, %40 ], [ %23, %22 ]
%33 = call i32 @read(i32 %18, i8* %31, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %40
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @close(i32 %18)
%38 = load i8*, i8** %1
%39 = call i32 @free(i8* %38)
br label %47
40: ; preds = %30
%41 = sext i32 %33 to i64
%42 = getelementptr inbounds i8, i8* %31, i64 %41
%43 = sub nsw i32 %32, %33
%44 = icmp sgt i32 %33, 0
br i1 %44, label %30, label %45
45: ; preds = %40
store i8 0, i8* %42
%46 = call i32 @close(i32 %18)
br label %47
47: ; preds = %45, %35, %27, %20, %13, %7
%48 = phi i32 [ 1, %7 ], [ 1, %13 ], [ 1, %20 ], [ 1, %35 ], [ 0, %45 ], [ 1, %27 ]
ret i32 %48
}