#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 {
  size_t st_size;
};
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
scalar_t__ malloc(size_t);
int open(char *, int /*<<< orphan*/);
size_t read(int, char *, size_t);
char *mpui_read_filename(char *filename) {
  int fd;
  ssize_t r;
  struct stat st;
  char *buffer;
  fd = open(filename, O_RDONLY);
  if (fd == -1)
    return NULL;
  if (fstat(fd, &st) == -1) {
    close(fd);
    return NULL;
  }
  buffer = (char *)malloc(st.st_size + 1);
  if (!buffer)
    return NULL;
  r = read(fd, buffer, st.st_size);
  close(fd);
  if (r != st.st_size) {
    free(buffer);
    return NULL;
  }
  buffer[r] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<mpui_read_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1198 <mpui_read_filename+0x38>
movq   $0x0,-0x8(%rbp)
jmp    123f <mpui_read_filename+0xdf>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11c2 <mpui_read_filename+0x62>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    123f <mpui_read_filename+0xdf>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11eb <mpui_read_filename+0x8b>
movq   $0x0,-0x8(%rbp)
jmp    123f <mpui_read_filename+0xdf>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <read@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     122b <mpui_read_filename+0xcb>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    123f <mpui_read_filename+0xdf>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mpui_read_filename>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11d4 <mpui_read_filename+0x74>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
je     11c3 <mpui_read_filename+0x63>
mov    (%rsp),%r14
lea    0x1(%r14),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11d4 <mpui_read_filename+0x74>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r14,%rdx
call   1050 <read@plt>
mov    %rax,%r15
mov    %ebp,%edi
call   1040 <close@plt>
cmp    %r14,%r15
jne    11cc <mpui_read_filename+0x6c>
movb   $0x0,(%rbx,%r14,1)
jmp    11d6 <mpui_read_filename+0x76>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11d4 <mpui_read_filename+0x74>
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 { i64 }
@O_RDONLY = global i32 0
define i8* @mpui_read_filename(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca %struct.stat
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i8* null, i8** %2
br label %50
14: ; preds = %1
%15 = load i32, i32* %4
%16 = call i32 @fstat(i32 %15, %struct.stat* %6)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
store i8* null, i8** %2
br label %50
21: ; preds = %14
%22 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%23 = load i64, i64* %22
%24 = add i64 %23, 1
%25 = call i64 @malloc(i64 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = icmp ne i8* %27, null
br i1 %28, label %30, label %29
29: ; preds = %21
store i8* null, i8** %2
br label %50
30: ; preds = %21
%31 = load i32, i32* %4
%32 = load i8*, i8** %7
%33 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%34 = load i64, i64* %33
%35 = call i64 @read(i32 %31, i8* %32, i64 %34)
store i64 %35, i64* %5
%36 = load i32, i32* %4
%37 = call i32 @close(i32 %36)
%38 = load i64, i64* %5
%39 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%40 = load i64, i64* %39
%41 = icmp ne i64 %38, %40
br i1 %41, label %42, label %45
42: ; preds = %30
%43 = load i8*, i8** %7
%44 = call i32 @free(i8* %43)
store i8* null, i8** %2
br label %50
45: ; preds = %30
%46 = load i8*, i8** %7
%47 = load i64, i64* %5
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8 0, i8* %48
%49 = load i8*, i8** %7
store i8* %49, i8** %2
br label %50
50: ; preds = %45, %42, %29, %18, %13
%51 = load i8*, i8** %2
ret i8* %51
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
define i8* @mpui_read_filename(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 %27, label %7
7: ; preds = %1
%8 = call i32 @fstat(i32 %5, %struct.stat* %2)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @close(i32 %5)
br label %27
12: ; preds = %7
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%14 = load i64, i64* %13
%15 = add i64 %14, 1
%16 = tail call i64 @malloc(i64 %15)
%17 = inttoptr i64 %16 to i8*
%18 = icmp eq i64 %16, 0
br i1 %18, label %27, label %19
19: ; preds = %12
%20 = tail call i64 @read(i32 %5, i8* %17, i64 %14)
%21 = tail call i32 @close(i32 %5)
%22 = icmp eq i64 %20, %14
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = tail call i32 @free(i8* %17)
br label %27
25: ; preds = %19
%26 = getelementptr inbounds i8, i8* %17, i64 %14
store i8 0, i8* %26
br label %27
27: ; preds = %12, %1, %25, %23, %10
%28 = phi i8* [ null, %10 ], [ null, %23 ], [ %17, %25 ], [ null, %1 ], [ null, %12 ]
ret i8* %28
}