#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*/ close(int);
int errno;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char const *, int /*<<< orphan*/);
int safe_pread(int, char *, int, int /*<<< orphan*/);
ssize_t simple_io_read_whole_file_zt(const char *fname, char *buf, int sz) {
  ssize_t res;
  int fd = open(fname, O_RDONLY);
  if (fd < 0) {
    int err = errno;
    return -err;
  }
  memset(buf, 0, sz);
  if (sz == 0)
    return 0;
  res = safe_pread(fd, buf, sz - 1, 0);
  if (res < 0) {
    close(fd);
    return res;
  }
  close(fd);
  return res;
}


x86 assembly (clang, -O0):
<simple_io_read_whole_file_zt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    118b <simple_io_read_whole_file_zt+0x4b>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
xor    %eax,%eax
sub    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f1 <simple_io_read_whole_file_zt+0xb1>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    11af <simple_io_read_whole_file_zt+0x6f>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <simple_io_read_whole_file_zt+0xb1>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
xor    %ecx,%ecx
call   1030 <safe_pread@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11e3 <simple_io_read_whole_file_zt+0xa3>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f1 <simple_io_read_whole_file_zt+0xb1>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<simple_io_read_whole_file_zt>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     1193 <simple_io_read_whole_file_zt+0x53>
mov    %eax,%r15d
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1040 <memset@plt>
test   %ebx,%ebx
je     119e <simple_io_read_whole_file_zt+0x5e>
add    $0xffffffff,%ebx
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1030 <safe_pread@plt>
mov    %eax,%ebx
mov    %r15d,%edi
call   1050 <close@plt>
mov    %ebx,%eax
jmp    11a0 <simple_io_read_whole_file_zt+0x60>
mov    0x2e3e(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @simple_io_read_whole_file_zt(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i32, i32* @errno
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = sub nsw i32 0, %18
store i32 %19, i32* %4
br label %43
20: ; preds = %3
%21 = load i8*, i8** %6
%22 = load i32, i32* %7
%23 = call i32 @memset(i8* %21, i32 0, i32 %22)
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 0, i32* %4
br label %43
27: ; preds = %20
%28 = load i32, i32* %9
%29 = load i8*, i8** %6
%30 = load i32, i32* %7
%31 = sub nsw i32 %30, 1
%32 = call i32 @safe_pread(i32 %28, i8* %29, i32 %31, i32 0)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %27
%36 = load i32, i32* %9
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %8
store i32 %38, i32* %4
br label %43
39: ; preds = %27
%40 = load i32, i32* %9
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %8
store i32 %42, i32* %4
br label %43
43: ; preds = %39, %35, %26, %16
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @simple_io_read_whole_file_zt(i8* nocapture %0, i8* %1, i32 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @errno
%9 = sub nsw i32 0, %8
br label %17
10: ; preds = %3
%11 = tail call i32 @memset(i8* %1, i32 0, i32 %2)
%12 = icmp eq i32 %2, 0
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = add nsw i32 %2, -1
%15 = tail call i32 @safe_pread(i32 %5, i8* %1, i32 %14, i32 0)
%16 = tail call i32 @close(i32 %5)
br label %17
17: ; preds = %13, %10, %7
%18 = phi i32 [ %9, %7 ], [ 0, %10 ], [ %15, %13 ]
ret i32 %18
}