#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 /*<<< orphan*/ st_size;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *, char *);
scalar_t__ read(int, char *, size_t);
scalar_t__ stat(char const *, struct stat *);
char *strerror(int /*<<< orphan*/);
char *config_read(const char *file) {
  char *config_data = NULL;
  struct stat file_properties;
  if (stat(file, &file_properties) != 0) {
    return NULL;
  }
  int fd = open(file, O_RDONLY);
  if (fd < 0) {
    goto error;
  }
  size_t file_size = (size_t)file_properties.st_size;
  config_data = malloc(file_size);
  size_t remaining_bytes = file_size;
  size_t offset_bytes = 0;
  while (remaining_bytes > 0) {
    ssize_t bytes_read = read(fd, config_data + offset_bytes, remaining_bytes);
    if (bytes_read == 0) {
      break;
    } else if (bytes_read < 0) {
      goto error;
    }
  }
  close(fd);
  return config_data;
error:
  free(config_data);
  if (fd > 0) {
    close(fd);
  }
  printf("Failed to open/read '%s': %s\n", file, strerror(errno));
  return NULL;
}


x86 assembly (clang, -O0):
<config_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11b8 <config_read+0x38>
movq   $0x0,-0x8(%rbp)
jmp    12af <config_read+0x12f>
mov    -0x10(%rbp),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11dc <config_read+0x5c>
jmp    1263 <config_read+0xe3>
movslq -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
cmpq   $0x0,-0x38(%rbp)
jbe    124e <config_read+0xce>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <read@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1234 <config_read+0xb4>
jmp    124e <config_read+0xce>
cmpq   $0x0,-0x48(%rbp)
jge    1244 <config_read+0xc4>
jmp    1263 <config_read+0xe3>
jmp    1249 <config_read+0xc9>
jmp    1201 <config_read+0x81>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12af <config_read+0x12f>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x24(%rbp)
jle    127e <config_read+0xfe>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
lea    0xd5e(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<config_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
je     11a5 <config_read+0x25>
xor    %ebx,%ebx
jmp    1259 <config_read+0xd9>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1217 <config_read+0x97>
mov    %eax,%r15d
movslq 0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
test   %rbp,%rbp
je     120d <config_read+0x8d>
mov    $0x4,%r12d
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1060 <read@plt>
mov    %rax,%rcx
shr    $0x3e,%rcx
and    $0xfffffffe,%ecx
test   %rax,%rax
cmove  %r12d,%ecx
test   %ecx,%ecx
je     11e0 <config_read+0x60>
cmp    $0x2,%ecx
je     1242 <config_read+0xc2>
cmp    $0x4,%ecx
jne    1259 <config_read+0xd9>
mov    %r15d,%edi
call   1050 <close@plt>
jmp    1259 <config_read+0xd9>
xor    %edi,%edi
call   1030 <free@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdcd(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <printf@plt>
xor    %ebx,%ebx
jmp    1259 <config_read+0xd9>
mov    %rbx,%rdi
call   1030 <free@plt>
test   %r15d,%r15d
jle    121e <config_read+0x9e>
mov    %r15d,%edi
call   1050 <close@plt>
jmp    121e <config_read+0x9e>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [30 x i8] c"Failed to open/read '%s': %s\0A\00"
@errno = global i32 0
define noalias i8* @config_read(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 %6, label %36
6: ; preds = %1
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = call i32 @free(i8* null)
br label %32
12: ; preds = %6
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%14 = load i32, i32* %13
%15 = sext i32 %14 to i64
%16 = call i8* @malloc(i64 %15)
%17 = icmp eq i32 %14, 0
br i1 %17, label %25, label %18
18: ; preds = %12, %18
%19 = call i64 @read(i32 %8, i8* %16, i64 %15)
%20 = icmp eq i64 %19, 0
%21 = lshr i64 %19, 62
%22 = trunc i64 %21 to i32
%23 = and i32 %22, 2
%24 = select i1 %20, i32 4, i32 %23
switch i32 %24, label %36 [
i32 0, label %18
i32 4, label %25
i32 2, label %27
]
25: ; preds = %18, %12
%26 = call i32 @close(i32 %8)
br label %36
27: ; preds = %18
%28 = call i32 @free(i8* %16)
%29 = icmp sgt i32 %8, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @close(i32 %8)
br label %32
32: ; preds = %10, %30, %27
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %34)
br label %36
36: ; preds = %18, %25, %32, %1
%37 = phi i8* [ null, %1 ], [ null, %32 ], [ %16, %25 ], [ undef, %18 ]
ret i8* %37
}