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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ fstat(int, struct stat *);
char *malloc(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, size_t);
char *como_read_file(const char *path) {
  int fd;
  off_t length;
  char *buffer;
  ssize_t nread;
  struct stat stat_buffer;
  fd = open(path, O_RDONLY);
  if (fd == -1) {
    return NULL;
  }
  if ((fstat(fd, &stat_buffer) != 0)) {
    close(fd);
    return NULL;
  }
  if (!S_ISREG(stat_buffer.st_mode)) {
    close(fd);
    return NULL;
  }
  length = stat_buffer.st_size;
  if (length + 1 < length) {
    close(fd);
    return NULL;
  }
  buffer = malloc(length + 1);
  if (buffer == NULL) {
    close(fd);
    free(buffer);
    return NULL;
  }
  nread = read(fd, buffer, (size_t)length);
  if (nread != length) {
    close(fd);
    free(buffer);
    return NULL;
  }
  close(fd);
  buffer[length] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<como_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a8 <como_read_file+0x38>
movq   $0x0,-0x8(%rbp)
jmp    12b4 <como_read_file+0x144>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
je     11d3 <como_read_file+0x63>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b4 <como_read_file+0x144>
mov    -0x2c(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11f9 <como_read_file+0x89>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b4 <como_read_file+0x144>
mov    -0x30(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cmp    -0x18(%rbp),%eax
jge    1223 <como_read_file+0xb3>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b4 <como_read_file+0x144>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    125b <como_read_file+0xeb>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b4 <como_read_file+0x144>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x18(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     1298 <como_read_file+0x128>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b4 <como_read_file+0x144>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rax
movslq -0x18(%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):
<como_read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11a2 <como_read_file+0x32>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
je     11b3 <como_read_file+0x43>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x4(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
je     119b <como_read_file+0x2b>
movslq (%rsp),%r15
lea    0x1(%r15),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11f7 <como_read_file+0x87>
mov    %rax,%r14
mov    %ebx,%edi
mov    %rax,%rsi
mov    %r15,%rdx
call   1050 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
cmp    %r15d,%ebp
jne    120a <como_read_file+0x9a>
movb   $0x0,(%r14,%r15,1)
jmp    11a5 <como_read_file+0x35>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %r14d,%r14d
xor    %edi,%edi
call   1030 <free@plt>
jmp    11a5 <como_read_file+0x35>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11a2 <como_read_file+0x32>

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

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@O_RDONLY = global i32 0
define noalias i8* @como_read_file(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %37, label %7
7: ; preds = %1
%8 = call i64 @fstat(i32 %5, %struct.stat* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @close(i32 %5)
br label %37
12: ; preds = %7
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%14 = load i32, i32* %13
%15 = tail call i32 @S_ISREG(i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @close(i32 %5)
br label %37
19: ; preds = %12
%20 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%21 = load i32, i32* %20
%22 = add nsw i32 %21, 1
%23 = tail call i8* @malloc(i32 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = tail call i32 @close(i32 %5)
%27 = tail call i32 @free(i8* null)
br label %37
28: ; preds = %19
%29 = sext i32 %21 to i64
%30 = tail call i32 @read(i32 %5, i8* %23, i64 %29)
%31 = icmp eq i32 %30, %21
%32 = tail call i32 @close(i32 %5)
br i1 %31, label %35, label %33
33: ; preds = %28
%34 = tail call i32 @free(i8* %23)
br label %37
35: ; preds = %28
%36 = getelementptr inbounds i8, i8* %23, i64 %29
store i8 0, i8* %36
br label %37
37: ; preds = %1, %35, %33, %25, %17, %10
%38 = phi i8* [ null, %10 ], [ null, %25 ], [ null, %33 ], [ %23, %35 ], [ null, %17 ], [ null, %1 ]
ret i8* %38
}