#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 fstat(int, struct stat *);
unsigned char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, unsigned char *, int);
unsigned char *load_test_file(int *len) {
  unsigned char *data;
  int fd;
  struct stat stat;
  fd = open("../UTF-8-demo.txt", O_RDONLY);
  if (fd == -1) {
    printf("Failed to open ../UTF-8-demo.txt!\n");
    exit(1);
  }
  if (fstat(fd, &stat) == -1) {
    printf("Failed to get file size!\n");
    exit(1);
  }
  *len = stat.st_size;
  data = malloc(*len);
  if (read(fd, data, *len) != *len) {
    printf("Failed to read file!\n");
    exit(1);
  }
  close(fd);
  return data;
}


x86 assembly (clang, -O0):
<load_test_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b4 <load_test_file+0x44>
lea    0xe6d(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11df <load_test_file+0x6f>
lea    0xe65(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
call   1050 <read@plt>
mov    -0x8(%rbp),%rcx
cmp    (%rcx),%eax
je     122b <load_test_file+0xbb>
lea    0xe33(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_test_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    0xe76(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11db <load_test_file+0x6b>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
je     11e4 <load_test_file+0x74>
mov    (%rsp),%ebx
mov    %ebx,(%r14)
mov    %ebx,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %ebp,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1050 <read@plt>
cmp    (%r14),%eax
jne    11ed <load_test_file+0x7d>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe5e(%rip),%rdi
jmp    11f4 <load_test_file+0x84>
lea    0xe3c(%rip),%rdi
jmp    11f4 <load_test_file+0x84>
lea    0xe1e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [18 x i8] c"../UTF-8-demo.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [35 x i8] c"Failed to open ../UTF-8-demo.txt!\0A\00"
@.str.2 = [26 x i8] c"Failed to get file size!\0A\00"
@.str.3 = [22 x i8] c"Failed to read file!\0A\00"
define i8* @load_test_file(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
store i32* %0, i32** %2
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = load i32, i32* %4
%15 = call i32 @fstat(i32 %14, %struct.stat* %5)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%22 = load i32, i32* %21
%23 = load i32*, i32** %2
store i32 %22, i32* %23
%24 = load i32*, i32** %2
%25 = load i32, i32* %24
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %3
%27 = load i32, i32* %4
%28 = load i8*, i8** %3
%29 = load i32*, i32** %2
%30 = load i32, i32* %29
%31 = call i32 @read(i32 %27, i8* %28, i32 %30)
%32 = load i32*, i32** %2
%33 = load i32, i32* %32
%34 = icmp ne i32 %31, %33
br i1 %34, label %35, label %38
35: ; preds = %20
%36 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %20
%39 = load i32, i32* %4
%40 = call i32 @close(i32 %39)
%41 = load i8*, i8** %3
ret i8* %41
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [18 x i8] c"../UTF-8-demo.txt\00"
@O_RDONLY = global i32 0
@str = [21 x i8] c"Failed to read file!\00"
@str.4 = [25 x i8] c"Failed to get file size!\00"
@str.5 = [34 x i8] c"Failed to open ../UTF-8-demo.txt!\00"
define noalias i8* @load_test_file(i32* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.5, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = call i32 @fstat(i32 %5, %struct.stat* %2)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.4, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%18 = load i32, i32* %17
store i32 %18, i32* %0
%19 = tail call i8* @malloc(i32 %18)
%20 = tail call i32 @read(i32 %5, i8* %19, i32 %18)
%21 = load i32, i32* %0
%22 = icmp eq i32 %20, %21
br i1 %22, label %26, label %23
23: ; preds = %16
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %16
%27 = tail call i32 @close(i32 %5)
ret i8* %19
}