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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *malloc(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
char *readfile(const char *path) {
  char *buf;
  int fd;
  ssize_t size;
  if ((fd = open(path, O_RDONLY)) == -1)
    return NULL;
  if ((size = lseek(fd, 0, SEEK_END)) == -1 || !(buf = malloc(size + 1))) {
    close(fd);
    return NULL;
  }
  lseek(fd, 0, SEEK_SET);
  if ((size = read(fd, buf, size)) == -1) {
    free(buf);
    buf = NULL;
  } else {
    buf[size] = '\0';
  }
  close(fd);
  return buf;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1197 <readfile+0x37>
movq   $0x0,-0x8(%rbp)
jmp    1244 <readfile+0xe4>
mov    -0x1c(%rbp),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
je     11cf <readfile+0x6f>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11e4 <readfile+0x84>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1244 <readfile+0xe4>
mov    -0x1c(%rbp),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1228 <readfile+0xc8>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
jmp    1234 <readfile+0xd4>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11d2 <readfile+0x72>
mov    %eax,%ebp
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     11e0 <readfile+0x80>
mov    %eax,%r14d
lea    0x1(%r14),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11de <readfile+0x7e>
mov    %rax,%rbx
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     11d6 <readfile+0x76>
cltq
movb   $0x0,(%rbx,%rax,1)
jmp    11e0 <readfile+0x80>
xor    %ebx,%ebx
jmp    11e7 <readfile+0x87>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define noalias i8* @readfile(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %26, label %5
5: ; preds = %1
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @lseek(i32 %3, i32 0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %23, label %9
9: ; preds = %5
%10 = add nuw nsw i32 %7, 1
%11 = tail call i8* @malloc(i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %23, label %13
13: ; preds = %9
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @lseek(i32 %3, i32 0, i32 %14)
%16 = tail call i32 @read(i32 %3, i8* %11, i32 %7)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @free(i8* %11)
br label %23
20: ; preds = %13
%21 = sext i32 %16 to i64
%22 = getelementptr inbounds i8, i8* %11, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %18, %20, %5, %9
%24 = phi i8* [ null, %9 ], [ null, %5 ], [ null, %18 ], [ %11, %20 ]
%25 = tail call i32 @close(i32 %3)
br label %26
26: ; preds = %23, %1
%27 = phi i8* [ null, %1 ], [ %24, %23 ]
ret i8* %27
}