#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;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error_reason(char *);
int /*<<< orphan*/ free(void *);
void *misc_malloc(int);
int misc_read(int, void *, int);
int open(char const *, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
char *strerror(int /*<<< orphan*/);
int misc_read_file(const char *filename, char **buffer, size_t *size,
                   int nil_terminate) {
  struct stat stats;
  void *data;
  int fd;
  int rc;
  if (stat(filename, &stats)) {
    error_reason(strerror(errno));
    return -1;
  }
  if (!S_ISREG(stats.st_mode)) {
    error_reason("Not a regular file");
    return -1;
  }
  data = misc_malloc(stats.st_size + (nil_terminate ? 1 : 0));
  if (data == NULL)
    return -1;
  fd = open(filename, O_RDONLY);
  if (fd == -1) {
    error_reason(strerror(errno));
    free(data);
    return -1;
  }
  rc = misc_read(fd, data, stats.st_size);
  close(fd);
  if (rc) {
    free(data);
    return rc;
  }
  *buffer = data;
  if (size != NULL)
    *size = stats.st_size;
  if (nil_terminate) {
    if (size != NULL)
      (*size)++;
    (*buffer)[stats.st_size] = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<misc_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     11e0 <misc_read_file+0x50>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131a <misc_read_file+0x18a>
mov    -0x2c(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1209 <misc_read_file+0x79>
lea    0xe08(%rip),%rdi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131a <misc_read_file+0x18a>
mov    -0x30(%rbp),%edi
mov    -0x24(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
add    %eax,%edi
call   1080 <misc_malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    123e <misc_read_file+0xae>
movl   $0xffffffff,-0x4(%rbp)
jmp    131a <misc_read_file+0x18a>
mov    -0x10(%rbp),%rdi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1288 <misc_read_file+0xf8>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131a <misc_read_file+0x18a>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   10b0 <misc_read@plt>
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x40(%rbp)
je     12c0 <misc_read_file+0x130>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    131a <misc_read_file+0x18a>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     12e1 <misc_read_file+0x151>
movslq -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
cmpl   $0x0,-0x24(%rbp)
je     1313 <misc_read_file+0x183>
cmpq   $0x0,-0x20(%rbp)
je     1304 <misc_read_file+0x174>
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<misc_read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r13d
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbp
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     11e0 <misc_read_file+0x50>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x4(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
je     1259 <misc_read_file+0xc9>
mov    (%rsp),%edi
cmp    $0x1,%r13d
sbb    $0xffffffff,%edi
call   1080 <misc_malloc@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     11cf <misc_read_file+0x3f>
mov    %rax,%r14
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1265 <misc_read_file+0xd5>
mov    %eax,%ebp
mov    (%rsp),%edx
mov    %eax,%edi
mov    %r14,%rsi
call   10b0 <misc_read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
test   %ebx,%ebx
jne    127b <misc_read_file+0xeb>
mov    %r14,(%r12)
test   %r15,%r15
je     1288 <misc_read_file+0xf8>
movslq (%rsp),%rax
mov    %rax,(%r15)
test   %r13d,%r13d
je     1296 <misc_read_file+0x106>
lea    0x1(%rax),%rcx
mov    %rcx,(%r15)
jmp    1291 <misc_read_file+0x101>
lea    0xda0(%rip),%rdi
jmp    11c5 <misc_read_file+0x35>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
mov    %r14,%rdi
call   1040 <free@plt>
jmp    11cf <misc_read_file+0x3f>
test   %r13d,%r13d
je     1296 <misc_read_file+0x106>
movslq (%rsp),%rax
movb   $0x0,(%r14,%rax,1)
xor    %ebx,%ebx
jmp    11cf <misc_read_file+0x3f>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@errno = global i32 0
@.str = [19 x i8] c"Not a regular file\00"
@O_RDONLY = global i32 0
define i32 @misc_read_file(i8* %0, i8** %1, i64* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i64*
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i64* %2, i64** %8
store i32 %3, i32* %9
%14 = load i8*, i8** %6
%15 = call i64 @stat(i8* %14, %struct.stat* %10)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @error_reason(i8* %19)
store i32 -1, i32* %5
br label %94
21: ; preds = %4
%22 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%23 = load i32, i32* %22
%24 = call i32 @S_ISREG(i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %21
%27 = call i32 @error_reason(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %94
28: ; preds = %21
%29 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* %9
%32 = icmp ne i32 %31, 0
%33 = zext i1 %32 to i64
%34 = select i1 %32, i32 1, i32 0
%35 = add nsw i32 %30, %34
%36 = call i8* @misc_malloc(i32 %35)
store i8* %36, i8** %11
%37 = load i8*, i8** %11
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %40
39: ; preds = %28
store i32 -1, i32* %5
br label %94
40: ; preds = %28
%41 = load i8*, i8** %6
%42 = load i32, i32* @O_RDONLY
%43 = call i32 @open(i8* %41, i32 %42)
store i32 %43, i32* %12
%44 = load i32, i32* %12
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %52
46: ; preds = %40
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 @error_reason(i8* %48)
%50 = load i8*, i8** %11
%51 = call i32 @free(i8* %50)
store i32 -1, i32* %5
br label %94
52: ; preds = %40
%53 = load i32, i32* %12
%54 = load i8*, i8** %11
%55 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @misc_read(i32 %53, i8* %54, i32 %56)
store i32 %57, i32* %13
%58 = load i32, i32* %12
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %13
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %52
%63 = load i8*, i8** %11
%64 = call i32 @free(i8* %63)
%65 = load i32, i32* %13
store i32 %65, i32* %5
br label %94
66: ; preds = %52
%67 = load i8*, i8** %11
%68 = load i8**, i8*** %7
store i8* %67, i8** %68
%69 = load i64*, i64** %8
%70 = icmp ne i64* %69, null
br i1 %70, label %71, label %76
71: ; preds = %66
%72 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%73 = load i32, i32* %72
%74 = sext i32 %73 to i64
%75 = load i64*, i64** %8
store i64 %74, i64* %75
br label %76
76: ; preds = %71, %66
%77 = load i32, i32* %9
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %93
79: ; preds = %76
%80 = load i64*, i64** %8
%81 = icmp ne i64* %80, null
br i1 %81, label %82, label %86
82: ; preds = %79
%83 = load i64*, i64** %8
%84 = load i64, i64* %83
%85 = add i64 %84, 1
store i64 %85, i64* %83
br label %86
86: ; preds = %82, %79
%87 = load i8**, i8*** %7
%88 = load i8*, i8** %87
%89 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%90 = load i32, i32* %89
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i8, i8* %88, i64 %91
store i8 0, i8* %92
br label %93
93: ; preds = %86, %76
store i32 0, i32* %5
br label %94
94: ; preds = %93, %62, %46, %39, %26, %17
%95 = load i32, i32* %5
ret i32 %95
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@errno = global i32 0
@.str = [19 x i8] c"Not a regular file\00"
@O_RDONLY = global i32 0
define i32 @misc_read_file(i8* %0, i8** nocapture %1, i64* %2, i32 %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = call i64 @stat(i8* %0, %struct.stat* %5)
%8 = icmp eq i64 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %4
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @error_reason(i8* %11)
br label %59
13: ; preds = %4
%14 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%15 = load i32, i32* %14
%16 = call i32 @S_ISREG(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @error_reason(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %59
20: ; preds = %13
%21 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%22 = load i32, i32* %21
%23 = icmp eq i32 %3, 0
%24 = xor i1 %23, true
%25 = zext i1 %24 to i32
%26 = add nsw i32 %22, %25
%27 = call i8* @misc_malloc(i32 %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %59, label %29
29: ; preds = %20
%30 = load i32, i32* @O_RDONLY
%31 = call i32 @open(i8* %0, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @error_reason(i8* %35)
%37 = call i32 @free(i8* %27)
br label %59
38: ; preds = %29
%39 = load i32, i32* %21
%40 = call i32 @misc_read(i32 %31, i8* %27, i32 %39)
%41 = call i32 @close(i32 %31)
%42 = icmp eq i32 %40, 0
br i1 %42, label %45, label %43
43: ; preds = %38
%44 = call i32 @free(i8* %27)
br label %59
45: ; preds = %38
store i8* %27, i8** %1
%46 = icmp eq i64* %2, null
br i1 %46, label %47, label %51
47: ; preds = %45
br i1 %23, label %59, label %48
48: ; preds = %47
%49 = load i32, i32* %21
%50 = sext i32 %49 to i64
br label %56
51: ; preds = %45
%52 = load i32, i32* %21
%53 = sext i32 %52 to i64
store i64 %53, i64* %2
br i1 %23, label %59, label %54
54: ; preds = %51
%55 = add nsw i64 %53, 1
store i64 %55, i64* %2
br label %56
56: ; preds = %48, %54
%57 = phi i64 [ %50, %48 ], [ %53, %54 ]
%58 = getelementptr inbounds i8, i8* %27, i64 %57
store i8 0, i8* %58
br label %59
59: ; preds = %51, %47, %56, %20, %43, %33, %18, %9
%60 = phi i32 [ -1, %9 ], [ -1, %33 ], [ %40, %43 ], [ -1, %18 ], [ -1, %20 ], [ 0, %56 ], [ 0, %47 ], [ 0, %51 ]
ret i32 %60
}