#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);
  p[len] = 0;
  bytes_read = 0;
  while (bytes_read < len) {
    status = read(fd, &p[bytes_read], len - bytes_read);
    assert(status >= 0 && "read() failed");
    bytes_read += status;
  }
  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)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1261 <readfile+0x111>
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     1246 <readfile+0xf6>
mov    $0x1,%al
mov    %al,-0x28(%rbp)
jmp    1246 <readfile+0xf6>
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    1205 <readfile+0xb5>
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
movb   $0x0,(%rax,%r12,1)
test   %r12,%r12
jle    11e9 <readfile+0x99>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
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     11c0 <readfile+0x70>
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
%36 = load i8*, i8** %3
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 0, i8* %39
store i32 0, i32* %6
br label %40
40: ; preds = %57, %28
%41 = load i32, i32* %6
%42 = load i32, i32* %5
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %64
44: ; preds = %40
%45 = load i32, i32* %2
%46 = load i8*, i8** %3
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = load i32, i32* %5
%51 = load i32, i32* %6
%52 = sub nsw i32 %50, %51
%53 = call i32 @read(i32 %45, i8* %49, i32 %52)
store i32 %53, i32* %7
%54 = load i32, i32* %7
%55 = icmp sge i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %44
br label %57
57: ; preds = %56, %44
%58 = phi i1 [ false, %44 ], [ true, %56 ]
%59 = zext i1 %58 to i32
%60 = call i32 @assert(i32 %59)
%61 = load i32, i32* %7
%62 = load i32, i32* %6
%63 = add nsw i32 %62, %61
store i32 %63, i32* %6
br label %40
64: ; preds = %40
%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*
%19 = sext i32 %12 to i64
%20 = getelementptr inbounds i8, i8* %18, i64 %19
store i8 0, i8* %20
br i1 %13, label %21, label %32
21: ; preds = %1, %21
%22 = phi i32 [ %30, %21 ], [ 0, %1 ]
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %18, i64 %23
%25 = sub nsw i32 %12, %22
%26 = tail call i32 @read(i32 %0, i8* %24, i32 %25)
%27 = xor i32 %26, -1
%28 = lshr i32 %27, 31
%29 = tail call i32 @assert(i32 %28)
%30 = add nsw i32 %26, %22
%31 = icmp sgt i32 %12, %30
br i1 %31, label %21, label %32
32: ; preds = %21, %1
%33 = tail call i32 @close(i32 %0)
ret i8* %18
}