#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;
};
typedef int ssize_t;
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
scalar_t__ malloc(int);
int read(int, char *, int);
char *readfile(int fd) {
  char *p;
  struct stat s;
  off_t len;
  ssize_t bytes_read, status;
  assert(fd > 1 && "Invalid file descriptor");
  assert(0 == fstat(fd, &s) && "Couldn't determine file size");
  len = s.st_size;
  assert(len > 0 && "File is empty");
  p = (char *)malloc(len + 1);
  bytes_read = 0;
  while (bytes_read < len) {
    status = read(fd, &p[bytes_read], len - bytes_read);
    assert(status >= 0 && "read() failed");
    bytes_read += status;
  }
  p[len] = (char)0;
  close(fd);
  return p;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x4(%rbp)
mov    %al,-0x25(%rbp)
jle    1174 <readfile+0x24>
mov    $0x1,%al
mov    %al,-0x25(%rbp)
jmp    1174 <readfile+0x24>
mov    -0x25(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <fstat@plt>
mov    %rax,%rdx
xor    %eax,%eax
xor    %ecx,%ecx
cmp    %rdx,%rcx
mov    %al,-0x26(%rbp)
jne    11aa <readfile+0x5a>
mov    $0x1,%al
mov    %al,-0x26(%rbp)
jmp    11aa <readfile+0x5a>
mov    -0x26(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x27(%rbp)
jle    11d6 <readfile+0x86>
mov    $0x1,%al
mov    %al,-0x27(%rbp)
jmp    11d6 <readfile+0x86>
mov    -0x27(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1255 <readfile+0x105>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
sub    -0x20(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x24(%rbp)
mov    %al,-0x28(%rbp)
jl     123a <readfile+0xea>
mov    $0x1,%al
mov    %al,-0x28(%rbp)
jmp    123a <readfile+0xea>
mov    -0x28(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11f9 <readfile+0xa9>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
xor    %edi,%edi
cmp    $0x2,%r14d
setge  %dil
call   1050 <assert@plt>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
call   1070 <fstat@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1050 <assert@plt>
movslq 0x8(%rsp),%r12
xor    %edi,%edi
test   %r12,%r12
setg   %dil
call   1050 <assert@plt>
lea    0x1(%r12),%edi
call   1060 <malloc@plt>
mov    %rax,%r15
test   %r12,%r12
jle    11d9 <readfile+0x89>
xor    %ebp,%ebp
movslq %ebp,%rbx
lea    (%r15,%rbx,1),%rsi
mov    %r12d,%edx
sub    %ebx,%edx
mov    %r14d,%edi
call   1040 <read@plt>
mov    %eax,%ebp
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1050 <assert@plt>
add    %ebx,%ebp
cmp    %ebp,%r12d
jg     11b0 <readfile+0x60>
movb   $0x0,(%r15,%r12,1)
mov    %r14d,%edi
call   1030 <close@plt>
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [24 x i8] c"Invalid file descriptor\00"
@.str.1 = [29 x i8] c"Couldn't determine file size\00"
@.str.2 = [14 x i8] c"File is empty\00"
@.str.3 = [14 x i8] c"read() failed\00"
define i8* @readfile(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
%8 = load i32, i32* %2
%9 = icmp sgt i32 %8, 1
br i1 %9, label %10, label %11
10: ; preds = %1
br label %11
11: ; preds = %10, %1
%12 = phi i1 [ false, %1 ], [ true, %10 ]
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* %2
%16 = call i64 @fstat(i32 %15, %struct.stat* %4)
%17 = icmp eq i64 0, %16
br i1 %17, label %18, label %19
18: ; preds = %11
br label %19
19: ; preds = %18, %11
%20 = phi i1 [ false, %11 ], [ true, %18 ]
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%24 = load i32, i32* %23
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %19
br label %28
28: ; preds = %27, %19
%29 = phi i1 [ false, %19 ], [ true, %27 ]
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1
%34 = call i64 @malloc(i32 %33)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %3
store i32 0, i32* %6
br label %36
36: ; preds = %53, %28
%37 = load i32, i32* %6
%38 = load i32, i32* %5
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %60
40: ; preds = %36
%41 = load i32, i32* %2
%42 = load i8*, i8** %3
%43 = load i32, i32* %6
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i32, i32* %5
%47 = load i32, i32* %6
%48 = sub nsw i32 %46, %47
%49 = call i32 @read(i32 %41, i8* %45, i32 %48)
store i32 %49, i32* %7
%50 = load i32, i32* %7
%51 = icmp sge i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %40
br label %53
53: ; preds = %52, %40
%54 = phi i1 [ false, %40 ], [ true, %52 ]
%55 = zext i1 %54 to i32
%56 = call i32 @assert(i32 %55)
%57 = load i32, i32* %7
%58 = load i32, i32* %6
%59 = add nsw i32 %58, %57
store i32 %59, i32* %6
br label %36
60: ; preds = %36
%61 = load i8*, i8** %3
%62 = load i32, i32* %5
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
store i8 0, i8* %64
%65 = load i32, i32* %2
%66 = call i32 @close(i32 %65)
%67 = load i8*, i8** %3
ret i8* %67
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i8* @readfile(i32 %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = icmp sgt i32 %0, 1
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = call i64 @fstat(i32 %0, %struct.stat* %2)
%8 = icmp eq i64 %7, 0
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = icmp sgt i32 %12, 0
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = add nsw i32 %12, 1
%17 = tail call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
br i1 %13, label %19, label %30
19: ; preds = %1, %19
%20 = phi i32 [ %28, %19 ], [ 0, %1 ]
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
%23 = sub nsw i32 %12, %20
%24 = tail call i32 @read(i32 %0, i8* %22, i32 %23)
%25 = xor i32 %24, -1
%26 = lshr i32 %25, 31
%27 = tail call i32 @assert(i32 %26)
%28 = add nsw i32 %24, %20
%29 = icmp sgt i32 %12, %28
br i1 %29, label %19, label %30
30: ; preds = %19, %1
%31 = sext i32 %12 to i64
%32 = getelementptr inbounds i8, i8* %18, i64 %31
store i8 0, i8* %32
%33 = tail call i32 @close(i32 %0)
ret i8* %18
}