#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_mode;
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int __S_IFMT;
int __S_IFREG;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
scalar_t__ stat(char *, struct stat *);
char *read_small_file(char *filename) {
  int fd;
  char *buffer;
  struct stat sbuf;
  if (stat(filename, &sbuf) < 0 || (sbuf.st_mode & __S_IFMT) != __S_IFREG)
    return NULL;
  fd = -1;
  buffer = NULL;
  if ((buffer = (char *)malloc(sbuf.st_size + 1)) == NULL ||
      (fd = open(filename, O_RDONLY)) < 0 ||
      read(fd, buffer, sbuf.st_size) != sbuf.st_size) {
    if (buffer != NULL)
      free(buffer);
    if (fd >= 0)
      close(fd);
    return NULL;
  }
  close(fd);
  buffer[sbuf.st_size] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<read_small_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jl     119e <read_small_file+0x3e>
mov    -0x28(%rbp),%eax
mov    0x2e53(%rip),%rcx
and    (%rcx),%eax
mov    0x2e3a(%rip),%rcx
cmp    (%rcx),%eax
je     11ab <read_small_file+0x4b>
movq   $0x0,-0x8(%rbp)
jmp    1258 <read_small_file+0xf8>
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1209 <read_small_file+0xa9>
mov    -0x10(%rbp),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jl     1209 <read_small_file+0xa9>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <read@plt>
cmp    -0x24(%rbp),%eax
je     123c <read_small_file+0xdc>
cmpq   $0x0,-0x20(%rbp)
je     121d <read_small_file+0xbd>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x14(%rbp)
jl     122f <read_small_file+0xcf>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1258 <read_small_file+0xf8>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_small_file>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     11fa <read_small_file+0x9a>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
and    (%rsp),%eax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%eax
jne    11fa <read_small_file+0x9a>
mov    0x4(%rsp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11fa <read_small_file+0x9a>
mov    %rax,%r14
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11e1 <read_small_file+0x81>
mov    %eax,%ebx
mov    0x4(%rsp),%edx
mov    %eax,%edi
mov    %r14,%rsi
call   1050 <read@plt>
cmp    0x4(%rsp),%eax
jne    11eb <read_small_file+0x8b>
mov    %ebx,%edi
call   1040 <close@plt>
movslq 0x4(%rsp),%rax
movb   $0x0,(%r14,%rax,1)
jmp    11fd <read_small_file+0x9d>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11fa <read_small_file+0x9a>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@__S_IFMT = global i32 0
@__S_IFREG = global i32 0
@O_RDONLY = global i32 0
define i8* @read_small_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 slt i64 %4, 0
br i1 %5, label %39, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = load i32, i32* @__S_IFMT
%10 = and i32 %9, %8
%11 = load i32, i32* @__S_IFREG
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %39
13: ; preds = %6
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%15 = load i32, i32* %14
%16 = add nsw i32 %15, 1
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
%19 = icmp eq i64 %17, 0
br i1 %19, label %39, label %20
20: ; preds = %13
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %0, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %20
%25 = load i32, i32* %14
%26 = call i32 @read(i32 %22, i8* %18, i32 %25)
%27 = load i32, i32* %14
%28 = icmp eq i32 %26, %27
br i1 %28, label %34, label %31
29: ; preds = %20
%30 = call i32 @free(i8* %18)
br label %39
31: ; preds = %24
%32 = call i32 @free(i8* %18)
%33 = call i32 @close(i32 %22)
br label %39
34: ; preds = %24
%35 = call i32 @close(i32 %22)
%36 = load i32, i32* %14
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %18, i64 %37
store i8 0, i8* %38
br label %39
39: ; preds = %13, %29, %31, %1, %6, %34
%40 = phi i8* [ %18, %34 ], [ null, %6 ], [ null, %1 ], [ null, %31 ], [ null, %29 ], [ null, %13 ]
ret i8* %40
}