#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;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
char *calloc(size_t, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, size_t);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *read_file(char *path) {
  struct stat st;
  if (stat(path, &st)) {
    fprintf(stderr, "%s: %s\n", "stat", strerror(errno));
    return NULL;
  }
  size_t buffer_size = st.st_size + 1;
  char *buffer = calloc(buffer_size, 1);
  int fd = open(path, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "%s: %s\n", "open", strerror(errno));
    free(buffer);
    return NULL;
  }
  if (read(fd, buffer, buffer_size) < 0) {
    fprintf(stderr, "%s: %s\n", "read", strerror(errno));
    free(buffer);
    close(fd);
    return NULL;
  }
  close(fd);
  return buffer;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     11e3 <read_file+0x63>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rcx
lea    0xe36(%rip),%rsi
lea    0xe37(%rip),%rdx
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e4 <read_file+0x164>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1269 <read_file+0xe9>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rcx
lea    0xdb9(%rip),%rsi
lea    0xdbf(%rip),%rdx
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e4 <read_file+0x164>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <read@plt>
cmp    $0x0,%rax
jge    12d4 <read_file+0x154>
mov    0x2d4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rcx
lea    0xd56(%rip),%rsi
lea    0xd61(%rip),%rdx
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e4 <read_file+0x164>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1080 <stat@plt>
test   %rax,%rax
je     11d7 <read_file+0x57>
mov    0x2e3a(%rip),%rax
mov    (%rax),%ebx
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe4b(%rip),%rsi
lea    0xe4c(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1070 <fprintf@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
movslq (%rsp),%rbp
add    $0x1,%rbp
mov    %rbp,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r15
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1224 <read_file+0xa4>
mov    %eax,%r14d
mov    %eax,%edi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1050 <read@plt>
test   %rax,%rax
js     1260 <read_file+0xe0>
mov    %r14d,%edi
call   1040 <close@plt>
jmp    11c9 <read_file+0x49>
mov    0x2dad(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdbe(%rip),%rsi
lea    0xdc4(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    11c6 <read_file+0x46>
mov    0x2d71(%rip),%rax
mov    (%rax),%ebx
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd82(%rip),%rsi
lea    0xd8d(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
call   1040 <close@plt>
jmp    11c6 <read_file+0x46>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [8 x i8] c"%s: %s\0A\00"
@.str.1 = [5 x i8] c"stat\00"
@errno = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [5 x i8] c"read\00"
define i8* @read_file(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 %11, label %6
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = call i8* @strerror(i32 %8)
%10 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %9)
br label %38
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = call i8* @calloc(i64 %15, i32 1)
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %0, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %11
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = call i32 @free(i8* %16)
br label %38
26: ; preds = %11
%27 = call i64 @read(i32 %18, i8* %16, i64 %15)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %32)
%34 = call i32 @free(i8* %16)
%35 = call i32 @close(i32 %18)
br label %38
36: ; preds = %26
%37 = call i32 @close(i32 %18)
br label %38
38: ; preds = %20, %29, %36, %6
%39 = phi i8* [ null, %6 ], [ null, %20 ], [ null, %29 ], [ %16, %36 ]
ret i8* %39
}