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

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(size_t);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
size_t read(int, char *, size_t);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
char *load_json(const char *file_path, size_t *len) {
  struct stat buf;
  if (stat(file_path, &buf)) {
    perror("stat");
    exit(1);
  }
  if (!S_ISREG(buf.st_mode)) {
    fprintf(stderr, "not regular file");
    exit(1);
  }
  size_t file_len = buf.st_size;
  int fd = open(file_path, 0);
  if (fd == -1) {
    perror("open");
    exit(1);
  }
  char *payload = malloc(file_len);
  if (payload == NULL) {
    perror("malloc");
    exit(1);
  }
  if (read(fd, payload, file_len) != file_len) {
    perror("read");
    exit(1);
  }
  close(fd);
  *len = file_len;
  return payload;
}


x86 assembly (clang, -O0):
<load_json>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     11cd <load_json+0x3d>
lea    0xe42(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11fd <load_json+0x6d>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1090 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1233 <load_json+0xa3>
lea    0xdf2(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1261 <load_json+0xd1>
lea    0xdc9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <read@plt>
cmp    -0x28(%rbp),%rax
je     1291 <load_json+0x101>
lea    0xda0(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_json>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
jne    120f <load_json+0x7f>
mov    0x10(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
je     1218 <load_json+0x88>
mov    0x8(%rsp),%rbx
mov    %rbp,%rdi
xor    %esi,%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1237 <load_json+0xa7>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1240 <load_json+0xb0>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1040 <read@plt>
cmp    %rbx,%rax
jne    1249 <load_json+0xb9>
mov    %r15d,%edi
call   1030 <close@plt>
mov    %rbx,(%r14)
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdea(%rip),%rdi
jmp    1250 <load_json+0xc0>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xdd8(%rip),%rdi
jmp    1250 <load_json+0xc0>
lea    0xdd4(%rip),%rdi
jmp    1250 <load_json+0xc0>
lea    0xdd2(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@.str = [5 x i8] c"stat\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"not regular file\00"
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [5 x i8] c"read\00"
define i8* @load_json(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca %struct.stat
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i64* %1, i64** %4
%9 = load i8*, i8** %3
%10 = call i64 @stat(i8* %9, %struct.stat* %5)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%17 = load i32, i32* %16
%18 = call i32 @S_ISREG(i32 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%26 = load i64, i64* %25
store i64 %26, i64* %6
%27 = load i8*, i8** %3
%28 = call i32 @open(i8* %27, i32 0)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %24
%35 = load i64, i64* %6
%36 = call i8* @malloc(i64 %35)
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %34
%43 = load i32, i32* %7
%44 = load i8*, i8** %8
%45 = load i64, i64* %6
%46 = call i64 @read(i32 %43, i8* %44, i64 %45)
%47 = load i64, i64* %6
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %52
49: ; preds = %42
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %42
%53 = load i32, i32* %7
%54 = call i32 @close(i32 %53)
%55 = load i64, i64* %6
%56 = load i64*, i64** %4
store i64 %55, i64* %56
%57 = load i8*, i8** %8
ret i8* %57
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@.str = [5 x i8] c"stat\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"not regular file\00"
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [5 x i8] c"read\00"
define noalias i8* @load_json(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%12 = load i32, i32* %11
%13 = call i32 @S_ISREG(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = load i64, i64* %20
%22 = call i32 @open(i8* %0, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %19
%28 = call i8* @malloc(i64 %21)
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = call i64 @read(i32 %22, i8* %28, i64 %21)
%35 = icmp eq i64 %34, %21
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %33
%40 = call i32 @close(i32 %22)
store i64 %21, i64* %1
ret i8* %28
}