#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 int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int /*<<< orphan*/);
int stat(char const *, struct stat *);
char *exe_read(const char *file_name) {
  int fd;
  int ret;
  struct stat stat_buf;
  char *buf;
  ssize_t size;
  ret = stat(file_name, &stat_buf);
  if (ret == -1) {
    return NULL;
  };
  buf = (char *)malloc(stat_buf.st_size);
  if (buf == NULL) {
    return NULL;
  }
  fd = open(file_name, O_RDONLY);
  if (fd == -1) {
    free(buf);
    return NULL;
  }
  size = read(fd, buf, stat_buf.st_size);
  close(fd);
  if (size == -1) {
    free(buf);
    return NULL;
  }
  return buf;
}


x86 assembly (clang, -O0):
<exe_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1193 <exe_read+0x33>
movq   $0x0,-0x8(%rbp)
jmp    122e <exe_read+0xce>
mov    -0x20(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b7 <exe_read+0x57>
movq   $0x0,-0x8(%rbp)
jmp    122e <exe_read+0xce>
mov    -0x10(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ec <exe_read+0x8c>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    122e <exe_read+0xce>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1226 <exe_read+0xc6>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    122e <exe_read+0xce>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exe_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbp
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     11cc <exe_read+0x6c>
mov    0x8(%rsp),%r14d
mov    %r14d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11cc <exe_read+0x6c>
mov    %rax,%rbx
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11c4 <exe_read+0x64>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <read@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%r14d
jne    11ce <exe_read+0x6e>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
define i8* @exe_read(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32 @stat(i8* %9, %struct.stat* %6)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i8* null, i8** %2
br label %46
14: ; preds = %1
%15 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %14
store i8* null, i8** %2
br label %46
22: ; preds = %14
%23 = load i8*, i8** %3
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i8*, i8** %7
%30 = call i32 @free(i8* %29)
store i8* null, i8** %2
br label %46
31: ; preds = %22
%32 = load i32, i32* %4
%33 = load i8*, i8** %7
%34 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @read(i32 %32, i8* %33, i32 %35)
store i32 %36, i32* %8
%37 = load i32, i32* %4
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %8
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %31
%42 = load i8*, i8** %7
%43 = call i32 @free(i8* %42)
store i8* null, i8** %2
br label %46
44: ; preds = %31
%45 = load i8*, i8** %7
store i8* %45, i8** %2
br label %46
46: ; preds = %44, %41, %28, %21, %13
%47 = load i8*, i8** %2
ret i8* %47
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
define i8* @exe_read(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %22, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = icmp eq i64 %9, 0
br i1 %11, label %22, label %12
12: ; preds = %6
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %0, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = tail call i32 @read(i32 %14, i8* %10, i32 %8)
%18 = tail call i32 @close(i32 %14)
%19 = icmp eq i32 %17, -1
br i1 %19, label %20, label %22
20: ; preds = %16, %12
%21 = tail call i32 @free(i8* %10)
br label %22
22: ; preds = %20, %16, %6, %1
%23 = phi i8* [ null, %1 ], [ null, %6 ], [ %10, %16 ], [ null, %20 ]
ret i8* %23
}