#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 *lfd_read(const char *file_name, ssize_t *size) {
  int fd;
  int ret;
  struct stat stat_buf;
  char *buf;
  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):
<lfd_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1197 <lfd_read+0x37>
movq   $0x0,-0x8(%rbp)
jmp    123a <lfd_read+0xda>
mov    -0x28(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11bb <lfd_read+0x5b>
movq   $0x0,-0x8(%rbp)
jmp    123a <lfd_read+0xda>
mov    -0x10(%rbp),%rdi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11f0 <lfd_read+0x90>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    123a <lfd_read+0xda>
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1050 <read@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    1232 <lfd_read+0xd2>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    123a <lfd_read+0xda>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lfd_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbp
mov    %rsp,%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     11cb <lfd_read+0x6b>
mov    (%rsp),%r15d
mov    %r15d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11cb <lfd_read+0x6b>
mov    %rax,%rbx
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11c3 <lfd_read+0x63>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1050 <read@plt>
mov    %eax,(%r14)
mov    %ebp,%edi
call   1040 <close@plt>
cmpl   $0xffffffff,(%r14)
jne    11cd <lfd_read+0x6d>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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