#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 /*<<< orphan*/ perror(char *);
int read(int, char *, int);
char *read_file(char *file_name, int *size) {
  struct stat st;
  char *buf;
  int fd;
  fd = open(file_name, O_RDONLY);
  if (fd < 0) {
    perror(file_name);
    return NULL;
  }
  if (fstat(fd, &st)) {
    perror("Could not determine file size");
    close(fd);
    return NULL;
  }
  *size = st.st_size;
  buf = malloc(*size);
  if (!buf) {
    perror("Allocating memory failed");
    close(fd);
    return NULL;
  }
  if (read(fd, buf, *size) != *size) {
    perror("File read failed");
    close(fd);
    return NULL;
  }
  close(fd);
  return buf;
}


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    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11a5 <read_file+0x45>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    126f <read_file+0x10f>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     11dc <read_file+0x7c>
lea    0xe3e(%rip),%rdi
call   1070 <perror@plt>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    126f <read_file+0x10f>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1220 <read_file+0xc0>
lea    0xe18(%rip),%rdi
call   1070 <perror@plt>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    126f <read_file+0x10f>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1040 <read@plt>
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
je     125f <read_file+0xff>
lea    0xdf2(%rip),%rdi
call   1070 <perror@plt>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    126f <read_file+0x10f>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
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    %rdi,%rbx
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11ce <read_file+0x6e>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %rax,%rax
jne    11da <read_file+0x7a>
mov    (%rsp),%ebp
mov    %ebp,(%r14)
mov    %ebp,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11e3 <read_file+0x83>
mov    %rax,%rbx
mov    %r15d,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
cmp    (%r14),%eax
jne    11ec <read_file+0x8c>
mov    %r15d,%edi
call   1030 <close@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1070 <perror@plt>
xor    %ebx,%ebx
jmp    11c0 <read_file+0x60>
lea    0xe1f(%rip),%rdi
jmp    11f3 <read_file+0x93>
lea    0xe34(%rip),%rdi
jmp    11f3 <read_file+0x93>
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
mov    %r15d,%edi
call   1030 <close@plt>
xor    %ebx,%ebx
jmp    11c0 <read_file+0x60>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [30 x i8] c"Could not determine file size\00"
@.str.1 = [25 x i8] c"Allocating memory failed\00"
@.str.2 = [17 x i8] c"File read failed\00"
define noalias i8* @read_file(i8* nocapture %0, i32* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* %0)
br label %33
10: ; preds = %2
%11 = call i64 @fstat(i32 %6, %struct.stat* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @close(i32 %6)
br label %33
16: ; preds = %10
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%18 = load i32, i32* %17
store i32 %18, i32* %1
%19 = tail call i8* @malloc(i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @close(i32 %6)
br label %33
24: ; preds = %16
%25 = tail call i32 @read(i32 %6, i8* %19, i32 %18)
%26 = load i32, i32* %1
%27 = icmp eq i32 %25, %26
br i1 %27, label %31, label %28
28: ; preds = %24
%29 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @close(i32 %6)
br label %33
31: ; preds = %24
%32 = tail call i32 @close(i32 %6)
br label %33
33: ; preds = %31, %28, %21, %13, %8
%34 = phi i8* [ null, %8 ], [ null, %13 ], [ null, %28 ], [ %19, %31 ], [ null, %21 ]
ret i8* %34
}