#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *read_local_file(char *file_name, unsigned int *file_size_result) {
  struct stat stat_buf;
  int fd;
  int data_size;
  char *data_buffer;
  if (stat(file_name, &stat_buf) != 0) {
    fprintf(stderr, "Could not stat %s\n", file_name);
    exit(1);
  }
  data_size = stat_buf.st_size;
  data_buffer = (char *)malloc(data_size + 1);
  if (data_buffer == NULL) {
    fprintf(stderr, "Could not malloc buffer of %d bytes\n", data_size + 1);
    exit(1);
  }
  fd = open(file_name, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "Could not open file %s\n", file_name);
    exit(1);
  }
  if (read(fd, data_buffer, data_size) != data_size) {
    fprintf(stderr, "Could not read whole file %s (%d bytes)\n", file_name,
            data_size);
    exit(1);
  }
  close(fd);
  fprintf(stderr, "Read file of %d bytes\n", data_size);
  data_buffer[data_size] = '\0';
  *file_size_result = data_size;
  return data_buffer;
}


x86 assembly (clang, -O0):
<read_local_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     11bc <read_local_file+0x4c>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe55(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1203 <read_local_file+0x93>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
add    $0x1,%edx
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1247 <read_local_file+0xd7>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <read@plt>
cmp    -0x20(%rbp),%eax
je     1287 <read_local_file+0x117>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x20(%rbp),%ecx
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_local_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
jne    121a <read_local_file+0xaa>
mov    0x8(%rsp),%ebx
lea    0x1(%rbx),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     122c <read_local_file+0xbc>
mov    %rax,%r12
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     124f <read_local_file+0xdf>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
cmp    %ebx,%eax
jne    1273 <read_local_file+0x103>
movslq %ebx,%r15
mov    %ebp,%edi
call   1030 <close@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
movb   $0x0,(%r12,%r15,1)
mov    %ebx,(%r14)
mov    %r12,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
jmp    125f <read_local_file+0xef>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
mov    %r15,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"Could not stat %s\0A\00"
@.str.1 = [37 x i8] c"Could not malloc buffer of %d bytes\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [24 x i8] c"Could not open file %s\0A\00"
@.str.3 = [41 x i8] c"Could not read whole file %s (%d bytes)\0A\00"
@.str.4 = [23 x i8] c"Read file of %d bytes\0A\00"
define i8* @read_local_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i32* %1, i32** %4
%9 = load i8*, i8** %3
%10 = call i64 @stat(i8* %9, %struct.stat* %5)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %3
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%19 = load i32, i32* %18
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = add nsw i32 %20, 1
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %32
26: ; preds = %17
%27 = load i32, i32* @stderr
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %17
%33 = load i8*, i8** %3
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %43
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %3
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %32
%44 = load i32, i32* %6
%45 = load i8*, i8** %8
%46 = load i32, i32* %7
%47 = call i32 @read(i32 %44, i8* %45, i32 %46)
%48 = load i32, i32* %7
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %56
50: ; preds = %43
%51 = load i32, i32* @stderr
%52 = load i8*, i8** %3
%53 = load i32, i32* %7
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %52, i32 %53)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %43
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* @stderr
%60 = load i32, i32* %7
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %60)
%62 = load i8*, i8** %8
%63 = load i32, i32* %7
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
store i8 0, i8* %65
%66 = load i32, i32* %7
%67 = load i32*, i32** %4
store i32 %66, i32* %67
%68 = load i8*, i8** %8
ret i8* %68
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"Could not stat %s\0A\00"
@.str.1 = [37 x i8] c"Could not malloc buffer of %d bytes\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [24 x i8] c"Could not open file %s\0A\00"
@.str.3 = [41 x i8] c"Could not read whole file %s (%d bytes)\0A\00"
@.str.4 = [23 x i8] c"Read file of %d bytes\0A\00"
define i8* @read_local_file(i8* %0, i32* nocapture writeonly %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 %11, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = add nsw i32 %13, 1
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i64 %15, 0
br i1 %17, label %18, label %22
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %11
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %0, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = call i32 @read(i32 %24, i8* %16, i32 %13)
%32 = icmp eq i32 %31, %13
br i1 %32, label %37, label %33
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %13)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30
%38 = call i32 @close(i32 %24)
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %13)
%41 = sext i32 %13 to i64
%42 = getelementptr inbounds i8, i8* %16, i64 %41
store i8 0, i8* %42
store i32 %13, i32* %1
ret i8* %16
}