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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *);
scalar_t__ stat(char *, struct stat *);
int read_json_file(char **buf, char *filename) {
  struct stat st;
  if (stat(filename, &st) != 0) {
    return 1;
  }
  *buf = malloc(sizeof(char) * (st.st_size + 1));
  if (*buf == NULL) {
    printf("failed to malloc buf\n");
    return 2;
  }
  FILE *fp = fopen(filename, "rb");
  fread((*buf), 1, st.st_size, fp);
  (*buf)[st.st_size] = '\0';
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<read_json_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     1193 <read_json_file+0x33>
movl   $0x1,-0x4(%rbp)
jmp    1221 <read_json_file+0xc1>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11d6 <read_json_file+0x76>
lea    0xe3b(%rip),%rdi
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1221 <read_json_file+0xc1>
mov    -0x18(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_json_file>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    11e2 <read_json_file+0x82>
mov    (%rsp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11d1 <read_json_file+0x71>
lea    0xe64(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    (%r14),%rdi
mov    (%rsp),%edx
mov    $0x1,%esi
mov    %rax,%rcx
call   1040 <fread@plt>
mov    (%r14),%rax
movslq (%rsp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    11e2 <read_json_file+0x82>
lea    0xe2b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [22 x i8] c"failed to malloc buf\0A\00"
@.str.1 = [3 x i8] c"rb\00"
define i32 @read_json_file(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i32*
store i8** %0, i8*** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i64 @stat(i8* %8, %struct.stat* %6)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 1, i32* %3
br label %43
12: ; preds = %2
%13 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%14 = load i32, i32* %13
%15 = add nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = mul i64 1, %16
%18 = trunc i64 %17 to i32
%19 = call i8* @malloc(i32 %18)
%20 = load i8**, i8*** %4
store i8* %19, i8** %20
%21 = load i8**, i8*** %4
%22 = load i8*, i8** %21
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %12
%25 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %3
br label %43
26: ; preds = %12
%27 = load i8*, i8** %5
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %28, i32** %7
%29 = load i8**, i8*** %4
%30 = load i8*, i8** %29
%31 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i32*, i32** %7
%34 = call i32 @fread(i8* %30, i32 1, i32 %32, i32* %33)
%35 = load i8**, i8*** %4
%36 = load i8*, i8** %35
%37 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%38 = load i32, i32* %37
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 0, i8* %40
%41 = load i32*, i32** %7
%42 = call i32 @fclose(i32* %41)
store i32 0, i32* %3
br label %43
43: ; preds = %26, %24, %11
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.1 = [3 x i8] c"rb\00"
@str = [21 x i8] c"failed to malloc buf\00"
define i32 @read_json_file(i8** nocapture %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %1, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %25
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = add nsw i32 %9, 1
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %0
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %25
15: ; preds = %7
%16 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%17 = load i8*, i8** %0
%18 = load i32, i32* %8
%19 = call i32 @fread(i8* %17, i32 1, i32 %18, i32* %16)
%20 = load i8*, i8** %0
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 0, i8* %23
%24 = call i32 @fclose(i32* %16)
br label %25
25: ; preds = %2, %15, %13
%26 = phi i32 [ 2, %13 ], [ 0, %15 ], [ 1, %2 ]
ret i32 %26
}