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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int file_open(char const *, int /*<<< orphan*/);
scalar_t__ file_read(int, char *, scalar_t__);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char const *, struct stat *);
char *file_read_contents(const char *path) {
  int fd;
  struct stat st;
  char *buf;
  if (stat(path, &st)) {
    perror("libsoc-file-debug");
    return NULL;
  }
  fd = file_open(path, O_RDONLY);
  if (fd < 0)
    return NULL;
  buf = malloc(st.st_size);
  if (buf) {
    if (file_read(fd, buf, st.st_size) != st.st_size) {
      free(buf);
      return NULL;
    }
  }
  return buf;
}


x86 assembly (clang, -O0):
<file_read_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     119c <file_read_contents+0x3c>
lea    0xe76(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    121d <file_read_contents+0xbd>
mov    -0x10(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1040 <file_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c8 <file_read_contents+0x68>
movq   $0x0,-0x8(%rbp)
jmp    121d <file_read_contents+0xbd>
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1215 <file_read_contents+0xb5>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <file_read@plt>
cmp    -0x20(%rbp),%rax
je     1210 <file_read_contents+0xb0>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    121d <file_read_contents+0xbd>
jmp    1215 <file_read_contents+0xb5>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read_contents>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
jne    11c4 <file_read_contents+0x64>
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <file_open@plt>
test   %eax,%eax
js     11d0 <file_read_contents+0x70>
mov    %eax,%ebp
mov    0x8(%rsp),%r14
mov    %r14,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11d0 <file_read_contents+0x70>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r14,%rdx
call   1070 <file_read@plt>
cmp    0x8(%rsp),%rax
je     11d2 <file_read_contents+0x72>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11d0 <file_read_contents+0x70>
lea    0xe35(%rip),%rdi
call   1080 <perror@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [18 x i8] c"libsoc-file-debug\00"
@O_RDONLY = global i32 0
define i8* @file_read_contents(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @stat(i8* %7, %struct.stat* %5)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %40
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @file_open(i8* %13, i32 %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %12
store i8* null, i8** %2
br label %40
19: ; preds = %12
%20 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%21 = load i64, i64* %20
%22 = call i8* @malloc(i64 %21)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %38
25: ; preds = %19
%26 = load i32, i32* %4
%27 = load i8*, i8** %6
%28 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%29 = load i64, i64* %28
%30 = call i64 @file_read(i32 %26, i8* %27, i64 %29)
%31 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%32 = load i64, i64* %31
%33 = icmp ne i64 %30, %32
br i1 %33, label %34, label %37
34: ; preds = %25
%35 = load i8*, i8** %6
%36 = call i32 @free(i8* %35)
store i8* null, i8** %2
br label %40
37: ; preds = %25
br label %38
38: ; preds = %37, %19
%39 = load i8*, i8** %6
store i8* %39, i8** %2
br label %40
40: ; preds = %38, %34, %18, %10
%41 = load i8*, i8** %2
ret i8* %41
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [18 x i8] c"libsoc-file-debug\00"
@O_RDONLY = global i32 0
define i8* @file_read_contents(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %23
8: ; preds = %1
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @file_open(i8* %0, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %23, label %12
12: ; preds = %8
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%14 = load i64, i64* %13
%15 = call i8* @malloc(i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %23, label %17
17: ; preds = %12
%18 = call i64 @file_read(i32 %10, i8* %15, i64 %14)
%19 = load i64, i64* %13
%20 = icmp eq i64 %18, %19
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = call i32 @free(i8* %15)
br label %23
23: ; preds = %12, %17, %8, %21, %6
%24 = phi i8* [ null, %6 ], [ null, %21 ], [ null, %8 ], [ %15, %17 ], [ null, %12 ]
ret i8* %24
}