#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 /*<<< orphan*/ uint8_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
scalar_t__ read(int, int /*<<< orphan*/ *, size_t);
ssize_t files_readFromFd(int fd, uint8_t *buf, size_t fileSz) {
  size_t readSz = 0;
  while (readSz < fileSz) {
    ssize_t sz = read(fd, &buf[readSz], fileSz - readSz);
    if (sz < 0 && errno == EINTR)
      continue;
    if (sz == 0)
      break;
    if (sz < 0)
      return -1;
    readSz += sz;
  }
  return (ssize_t)readSz;
}


x86 assembly (clang, -O0):
<files_readFromFd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11bf <files_readFromFd+0xaf>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    1186 <files_readFromFd+0x76>
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%rax
jne    1186 <files_readFromFd+0x76>
jmp    112b <files_readFromFd+0x1b>
cmpq   $0x0,-0x30(%rbp)
jne    1196 <files_readFromFd+0x86>
jmp    11bf <files_readFromFd+0xaf>
cmpq   $0x0,-0x30(%rbp)
jge    11ae <files_readFromFd+0x9e>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c7 <files_readFromFd+0xb7>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    112b <files_readFromFd+0x1b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<files_readFromFd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1171 <files_readFromFd+0x61>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
mov    0x2eaf(%rip),%r12
mov    0x2ea0(%rip),%r13
jmp    114f <files_readFromFd+0x3f>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    1175 <files_readFromFd+0x65>
cmp    %r14,%rbx
jae    117c <files_readFromFd+0x6c>
lea    (%r15,%rbx,4),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
call   1030 <read@plt>
test   %rax,%rax
js     1140 <files_readFromFd+0x30>
je     117c <files_readFromFd+0x6c>
add    %rax,%rbx
cmp    %r14,%rbx
jb     114f <files_readFromFd+0x3f>
jmp    117c <files_readFromFd+0x6c>
xor    %ebx,%ebx
jmp    117c <files_readFromFd+0x6c>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i64 @files_readFromFd(i32 %0, i32* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %22, label %5
5: ; preds = %3, %19
%6 = phi i64 [ %20, %19 ], [ 0, %3 ]
%7 = getelementptr inbounds i32, i32* %1, i64 %6
%8 = sub i64 %2, %6
%9 = tail call i64 @read(i32 %0, i32* %7, i64 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %5
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %19, label %22
15: ; preds = %5
%16 = icmp eq i64 %9, 0
br i1 %16, label %22, label %17
17: ; preds = %15
%18 = add i64 %9, %6
br label %19
19: ; preds = %17, %11
%20 = phi i64 [ %6, %11 ], [ %18, %17 ]
%21 = icmp ult i64 %20, %2
br i1 %21, label %5, label %22
22: ; preds = %19, %15, %11, %3
%23 = phi i64 [ 0, %3 ], [ -1, %11 ], [ %6, %15 ], [ %20, %19 ]
ret i64 %23
}