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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, long);
int /*<<< orphan*/ report_errno(char *);
long get_file_contents(char *filepath, char *file_contents) {
  struct stat file_data;
  int file_fd = open(filepath, O_RDONLY);
  if (fstat(file_fd, &file_data) == 0) {
    if (read(file_fd, file_contents, file_data.st_size) == -1) {
      char *message = "Error reading file.";
      report_errno(message);
    }
    return file_data.st_size;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<get_file_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
jne    11b5 <get_file_contents+0x75>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    11a8 <get_file_contents+0x68>
lea    0xe65(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <report_errno@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11bd <get_file_contents+0x7d>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_contents>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
test   %rax,%rax
je     1170 <get_file_contents+0x30>
xor    %ebx,%ebx
jmp    1193 <get_file_contents+0x53>
mov    0x8(%rsp),%rbx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    1193 <get_file_contents+0x53>
lea    0xe72(%rip),%rdi
call   1040 <report_errno@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [20 x i8] c"Error reading file.\00"
define i64 @get_file_contents(i8* %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = call i64 @fstat(i32 %12, %struct.stat* %6)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %2
%16 = load i32, i32* %7
%17 = load i8*, i8** %5
%18 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%19 = load i64, i64* %18
%20 = call i32 @read(i32 %16, i8* %17, i64 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %15
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8** %8
%23 = load i8*, i8** %8
%24 = call i32 @report_errno(i8* %23)
br label %25
25: ; preds = %22, %15
%26 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%27 = load i64, i64* %26
store i64 %27, i64* %3
br label %29
28: ; preds = %2
store i64 0, i64* %3
br label %29
29: ; preds = %28, %25
%30 = load i64, i64* %3
ret i64 %30
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [20 x i8] c"Error reading file.\00"
define i64 @get_file_contents(i8* nocapture %0, i8* 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 = call i64 @fstat(i32 %6, %struct.stat* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %2
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i64, i64* %10
%12 = tail call i32 @read(i32 %6, i8* %1, i64 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @report_errno(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %9, %14, %2
%17 = phi i64 [ 0, %2 ], [ %11, %14 ], [ %11, %9 ]
ret i64 %17
}