#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 {
  size_t st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, size_t);
int stat(char const *, struct stat *);
char *application_load_rcfile(const char *filename) {
  struct stat st;
  int ret = stat(filename, &st);
  if (ret != 0)
    return NULL;
  size_t size = st.st_size;
  if (size == 0)
    return NULL;
  char *contents = malloc(size + 1);
  if (contents == NULL)
    return NULL;
  int fd = open(filename, O_RDONLY);
  if (fd < 0) {
    free(contents);
    return NULL;
  }
  ret = read(fd, contents, size);
  close(fd);
  if (ret < 0) {
    free(contents);
    return NULL;
  }
  contents[ret] = '\0';
  return contents;
}


x86 assembly (clang, -O0):
<application_load_rcfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1193 <application_load_rcfile+0x33>
movq   $0x0,-0x8(%rbp)
jmp    1260 <application_load_rcfile+0x100>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b3 <application_load_rcfile+0x53>
movq   $0x0,-0x8(%rbp)
jmp    1260 <application_load_rcfile+0x100>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11dc <application_load_rcfile+0x7c>
movq   $0x0,-0x8(%rbp)
jmp    1260 <application_load_rcfile+0x100>
mov    -0x10(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1211 <application_load_rcfile+0xb1>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1260 <application_load_rcfile+0x100>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    124c <application_load_rcfile+0xec>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1260 <application_load_rcfile+0x100>
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<application_load_rcfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %eax,%eax
je     1187 <application_load_rcfile+0x27>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%r14
test   %r14,%r14
je     1176 <application_load_rcfile+0x16>
lea    0x1(%r14),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1176 <application_load_rcfile+0x16>
mov    %rax,%r15
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11db <application_load_rcfile+0x7b>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
test   %ebp,%ebp
js     11db <application_load_rcfile+0x7b>
mov    %ebp,%eax
movb   $0x0,(%r15,%rax,1)
jmp    1179 <application_load_rcfile+0x19>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    1176 <application_load_rcfile+0x16>

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

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
define noalias i8* @application_load_rcfile(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %29
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i64, i64* %7
%9 = icmp eq i64 %8, 0
br i1 %9, label %29, label %10
10: ; preds = %6
%11 = add i64 %8, 1
%12 = tail call i8* @malloc(i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %29, label %14
14: ; preds = %10
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* %0, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @free(i8* %12)
br label %29
20: ; preds = %14
%21 = tail call i32 @read(i32 %16, i8* %12, i64 %8)
%22 = tail call i32 @close(i32 %16)
%23 = icmp slt i32 %21, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @free(i8* %12)
br label %29
26: ; preds = %20
%27 = zext i32 %21 to i64
%28 = getelementptr inbounds i8, i8* %12, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %6, %18, %24, %26, %10, %1
%30 = phi i8* [ null, %1 ], [ null, %6 ], [ null, %10 ], [ null, %18 ], [ null, %24 ], [ %12, %26 ]
ret i8* %30
}