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

/* Variables and functions */
scalar_t__ EINTR;
size_t USHRT_MAX;
char *cli_malloc(size_t);
scalar_t__ errno;
int /*<<< orphan*/ fatal(char *, ...);
int fstat(int, struct stat *);
int read(int, char *, size_t);
int /*<<< orphan*/ strerror(scalar_t__);
void cli_file_read(int fd, char **buf, size_t *len) {
  struct stat st;
  char *p;
  ssize_t ret;
  size_t offset, bytes;
  if (fstat(fd, &st) == -1)
    fatal("fstat(): %s", strerror(errno));
  if (st.st_size > USHRT_MAX)
    fatal("cli_file_read: way too big");
  offset = 0;
  bytes = st.st_size;
  p = cli_malloc(bytes);
  while (offset != bytes) {
    ret = read(fd, p + offset, bytes - offset);
    if (ret == -1) {
      if (errno == EINTR)
        continue;
      fatal("read(): %s", strerror(errno));
    }
    if (ret == 0)
      fatal("unexpected EOF");
    offset += (size_t)ret;
  }
  *buf = p;
  *len = bytes;
}


x86 assembly (clang, -O0):
<cli_file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1197 <cli_file_read+0x47>
mov    0x2e59(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1060 <fatal@plt>
mov    -0x20(%rbp),%rax
mov    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
jbe    11b9 <cli_file_read+0x69>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1060 <fatal@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <cli_malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x40(%rbp),%rax
je     1270 <cli_file_read+0x120>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdx
sub    -0x38(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1247 <cli_file_read+0xf7>
mov    0x2dc8(%rip),%rax
mov    (%rax),%rax
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%rax
jne    1228 <cli_file_read+0xd8>
jmp    11d6 <cli_file_read+0x86>
mov    0x2da9(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1060 <fatal@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    125f <cli_file_read+0x10f>
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1060 <fatal@plt>
movslq -0x2c(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11d6 <cli_file_read+0x86>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cli_file_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x20(%rsp),%rsi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1194 <cli_file_read+0x44>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
lea    0xe75(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <fatal@plt>
mov    0x20(%rsp),%r15
mov    0x2e48(%rip),%rax
cmp    (%rax),%r15
jbe    11bd <cli_file_read+0x6d>
lea    0xe60(%rip),%rdi
xor    %eax,%eax
call   1060 <fatal@plt>
mov    %r15,%rdi
call   1030 <cli_malloc@plt>
jmp    11ce <cli_file_read+0x7e>
mov    %r15,%rdi
call   1030 <cli_malloc@plt>
test   %r15,%r15
je     1274 <cli_file_read+0x124>
mov    %rbx,(%rsp)
mov    %r14,0x8(%rsp)
mov    0x2df2(%rip),%r14
mov    %r15,%r12
mov    %rax,%rbx
xor    %r13d,%r13d
mov    %r15,0x18(%rsp)
mov    %rax,0x10(%rsp)
jmp    1230 <cli_file_read+0xe0>
call   1050 <strerror@plt>
lea    0xe28(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <fatal@plt>
movslq %r15d,%rax
add    %rax,%r13
mov    0x10(%rsp),%rax
lea    (%rax,%r13,1),%rbx
mov    0x18(%rsp),%r15
mov    %r15,%r12
sub    %r13,%r12
je     126b <cli_file_read+0x11b>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1040 <read@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    1256 <cli_file_read+0x106>
mov    0x2d8c(%rip),%rax
mov    (%rax),%rdi
cmp    (%r14),%rdi
je     1230 <cli_file_read+0xe0>
jmp    11f3 <cli_file_read+0xa3>
test   %r15d,%r15d
jne    1208 <cli_file_read+0xb8>
lea    0xdd0(%rip),%rdi
xor    %eax,%eax
call   1060 <fatal@plt>
jmp    1208 <cli_file_read+0xb8>
mov    0x8(%rsp),%r14
mov    (%rsp),%rbx
mov    %rax,(%rbx)
mov    %r15,(%r14)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [12 x i8] c"fstat(): %s\00"
@errno = global i64 0
@USHRT_MAX = global i64 0
@.str.1 = [27 x i8] c"cli_file_read: way too big\00"
@EINTR = global i64 0
@.str.2 = [11 x i8] c"read(): %s\00"
@.str.3 = [15 x i8] c"unexpected EOF\00"
define void @cli_file_read(i32 %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca %struct.stat
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64* %2, i64** %6
%12 = load i32, i32* %4
%13 = call i32 @fstat(i32 %12, %struct.stat* %7)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %3
%16 = load i64, i64* @errno
%17 = call i32 @strerror(i64 %16)
%18 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %15, %3
%20 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%21 = load i64, i64* %20
%22 = load i64, i64* @USHRT_MAX
%23 = icmp ugt i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %19
store i64 0, i64* %10
%27 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%28 = load i64, i64* %27
store i64 %28, i64* %11
%29 = load i64, i64* %11
%30 = call i8* @cli_malloc(i64 %29)
store i8* %30, i8** %8
br label %31
31: ; preds = %60, %50, %26
%32 = load i64, i64* %10
%33 = load i64, i64* %11
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %65
35: ; preds = %31
%36 = load i32, i32* %4
%37 = load i8*, i8** %8
%38 = load i64, i64* %10
%39 = getelementptr inbounds i8, i8* %37, i64 %38
%40 = load i64, i64* %11
%41 = load i64, i64* %10
%42 = sub i64 %40, %41
%43 = call i32 @read(i32 %36, i8* %39, i64 %42)
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %55
46: ; preds = %35
%47 = load i64, i64* @errno
%48 = load i64, i64* @EINTR
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %46
br label %31
51: ; preds = %46
%52 = load i64, i64* @errno
%53 = call i32 @strerror(i64 %52)
%54 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %51, %35
%56 = load i32, i32* %9
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %60
60: ; preds = %58, %55
%61 = load i32, i32* %9
%62 = sext i32 %61 to i64
%63 = load i64, i64* %10
%64 = add i64 %63, %62
store i64 %64, i64* %10
br label %31
65: ; preds = %31
%66 = load i8*, i8** %8
%67 = load i8**, i8*** %5
store i8* %66, i8** %67
%68 = load i64, i64* %11
%69 = load i64*, i64** %6
store i64 %68, i64* %69
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [12 x i8] c"fstat(): %s\00"
@errno = global i64 0
@USHRT_MAX = global i64 0
@.str.1 = [27 x i8] c"cli_file_read: way too big\00"
@EINTR = global i64 0
@.str.2 = [11 x i8] c"read(): %s\00"
@.str.3 = [15 x i8] c"unexpected EOF\00"
define void @cli_file_read(i32 %0, i8** nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i32 @fstat(i32 %0, %struct.stat* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = load i64, i64* @errno
%10 = tail call i32 @strerror(i64 %9)
%11 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %8, %3
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%14 = load i64, i64* %13
%15 = load i64, i64* @USHRT_MAX
%16 = icmp ugt i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i8* @cli_malloc(i64 %14)
br label %23
20: ; preds = %12
%21 = tail call i8* @cli_malloc(i64 %14)
%22 = icmp eq i64 %14, 0
br i1 %22, label %46, label %23
23: ; preds = %17, %20
%24 = phi i8* [ %19, %17 ], [ %21, %20 ]
br label %25
25: ; preds = %23, %40
%26 = phi i64 [ %45, %40 ], [ %14, %23 ]
%27 = phi i8* [ %44, %40 ], [ %24, %23 ]
%28 = phi i64 [ %42, %40 ], [ 0, %23 ]
br label %29
29: ; preds = %25, %31
%30 = tail call i32 @read(i32 %0, i8* %27, i64 %26)
switch i32 %30, label %40 [
i32 -1, label %31
i32 0, label %38
]
31: ; preds = %29
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp eq i64 %32, %33
br i1 %34, label %29, label %35
35: ; preds = %31
%36 = tail call i32 @strerror(i64 %32)
%37 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %36)
br label %40
38: ; preds = %29
%39 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %29, %35, %38
%41 = sext i32 %30 to i64
%42 = add i64 %28, %41
%43 = icmp eq i64 %14, %42
%44 = getelementptr inbounds i8, i8* %24, i64 %42
%45 = sub i64 %14, %42
br i1 %43, label %46, label %25
46: ; preds = %40, %20
%47 = phi i8* [ %21, %20 ], [ %24, %40 ]
store i8* %47, i8** %1
store i64 %14, i64* %2
ret void
}