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

/* Variables and functions */
scalar_t__ E2BIG;
scalar_t__ EINTR;
size_t SSIZE_MAX;
scalar_t__ errno;
int pread(int, char *, size_t, int);
ssize_t pread_in_full(int fd, void *buf, size_t count, off_t offset) {
  ssize_t total = 0;
  char *p = buf;
  if (count > SSIZE_MAX) {
    errno = E2BIG;
    return -1;
  }
  while (count > 0) {
    ssize_t nr;
    nr = pread(fd, p, count, offset);
    if (nr == 0)
      return total;
    else if (nr == -1 && errno == EINTR)
      continue;
    else if (nr == -1)
      return -1;
    count -= nr;
    total += nr;
    p += nr;
    offset += nr;
  }
  return total;
}


x86 assembly (clang, -O0):
<pread_in_full>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2ea0(%rip),%rcx
cmp    (%rcx),%rax
jbe    1169 <pread_in_full+0x59>
mov    0x2e98(%rip),%rax
mov    (%rax),%rcx
mov    0x2e76(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <pread_in_full+0x11a>
jmp    116e <pread_in_full+0x5e>
cmpq   $0x0,-0x18(%rbp)
jbe    1224 <pread_in_full+0x114>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <pread@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11a4 <pread_in_full+0x94>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122a <pread_in_full+0x11a>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11cd <pread_in_full+0xbd>
mov    0x2e1b(%rip),%rax
mov    (%rax),%rax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%rax
jne    11cd <pread_in_full+0xbd>
jmp    116e <pread_in_full+0x5e>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11e3 <pread_in_full+0xd3>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <pread_in_full+0x11a>
jmp    11e8 <pread_in_full+0xd8>
jmp    11ed <pread_in_full+0xdd>
movslq -0x2c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    116e <pread_in_full+0x5e>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pread_in_full>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2ebe(%rip),%rax
cmp    %rdx,(%rax)
jae    1143 <pread_in_full+0x33>
mov    0x2eba(%rip),%rax
mov    (%rax),%rax
mov    0x2e98(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%r14d
jmp    11a3 <pread_in_full+0x93>
mov    %rdx,%rbx
test   %rdx,%rdx
je     11a0 <pread_in_full+0x90>
mov    %ecx,%r15d
mov    %rsi,%rbp
mov    %edi,%r12d
xor    %r14d,%r14d
mov    0x2e6a(%rip),%r13
jmp    1175 <pread_in_full+0x65>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
jne    113b <pread_in_full+0x2b>
test   %rbx,%rbx
je     11a3 <pread_in_full+0x93>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1030 <pread@plt>
cmp    $0xffffffff,%eax
je     1160 <pread_in_full+0x50>
test   %eax,%eax
je     11a3 <pread_in_full+0x93>
movslq %eax,%rcx
sub    %rcx,%rbx
add    %eax,%r14d
add    %rcx,%rbp
add    %eax,%r15d
jmp    1170 <pread_in_full+0x60>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SSIZE_MAX = global i64 0
@E2BIG = global i64 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @pread_in_full(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
store i32 0, i32* %10
%13 = load i8*, i8** %7
store i8* %13, i8** %11
%14 = load i64, i64* %8
%15 = load i64, i64* @SSIZE_MAX
%16 = icmp ugt i64 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = load i64, i64* @E2BIG
store i64 %18, i64* @errno
store i32 -1, i32* %5
br label %64
19: ; preds = %4
br label %20
20: ; preds = %47, %40, %19
%21 = load i64, i64* %8
%22 = icmp ugt i64 %21, 0
br i1 %22, label %23, label %62
23: ; preds = %20
%24 = load i32, i32* %6
%25 = load i8*, i8** %11
%26 = load i64, i64* %8
%27 = load i32, i32* %9
%28 = call i32 @pread(i32 %24, i8* %25, i64 %26, i32 %27)
store i32 %28, i32* %12
%29 = load i32, i32* %12
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = load i32, i32* %10
store i32 %32, i32* %5
br label %64
33: ; preds = %23
%34 = load i32, i32* %12
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = load i64, i64* @errno
%38 = load i64, i64* @EINTR
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %36
br label %20
41: ; preds = %36, %33
%42 = load i32, i32* %12
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %41
store i32 -1, i32* %5
br label %64
45: ; preds = %41
br label %46
46: ; preds = %45
br label %47
47: ; preds = %46
%48 = load i32, i32* %12
%49 = sext i32 %48 to i64
%50 = load i64, i64* %8
%51 = sub i64 %50, %49
store i64 %51, i64* %8
%52 = load i32, i32* %12
%53 = load i32, i32* %10
%54 = add nsw i32 %53, %52
store i32 %54, i32* %10
%55 = load i32, i32* %12
%56 = load i8*, i8** %11
%57 = sext i32 %55 to i64
%58 = getelementptr inbounds i8, i8* %56, i64 %57
store i8* %58, i8** %11
%59 = load i32, i32* %12
%60 = load i32, i32* %9
%61 = add nsw i32 %60, %59
store i32 %61, i32* %9
br label %20
62: ; preds = %20
%63 = load i32, i32* %10
store i32 %63, i32* %5
br label %64
64: ; preds = %62, %44, %31, %17
%65 = load i32, i32* %5
ret i32 %65
}

LLVM IR (-O2):
@SSIZE_MAX = global i64 0
@E2BIG = global i64 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @pread_in_full(i32 %0, i8* nocapture %1, i64 %2, i32 %3) {
%5 = load i64, i64* @SSIZE_MAX
%6 = icmp ult i64 %5, %2
br i1 %6, label %9, label %7
7: ; preds = %4
%8 = icmp eq i64 %2, 0
br i1 %8, label %33, label %11
9: ; preds = %4
%10 = load i64, i64* @E2BIG
store i64 %10, i64* @errno
br label %33
11: ; preds = %7, %27
%12 = phi i8* [ %31, %27 ], [ %1, %7 ]
%13 = phi i32 [ %30, %27 ], [ 0, %7 ]
%14 = phi i32 [ %29, %27 ], [ %3, %7 ]
%15 = phi i64 [ %28, %27 ], [ %2, %7 ]
%16 = tail call i32 @pread(i32 %0, i8* %12, i64 %15, i32 %14)
switch i32 %16, label %21 [
i32 0, label %33
i32 -1, label %17
]
17: ; preds = %11
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %27, label %33
21: ; preds = %11
%22 = sext i32 %16 to i64
%23 = sub i64 %15, %22
%24 = add nsw i32 %16, %13
%25 = getelementptr inbounds i8, i8* %12, i64 %22
%26 = add nsw i32 %16, %14
br label %27
27: ; preds = %17, %21
%28 = phi i64 [ %23, %21 ], [ %15, %17 ]
%29 = phi i32 [ %26, %21 ], [ %14, %17 ]
%30 = phi i32 [ %24, %21 ], [ %13, %17 ]
%31 = phi i8* [ %25, %21 ], [ %12, %17 ]
%32 = icmp eq i64 %28, 0
br i1 %32, label %33, label %11
33: ; preds = %27, %17, %11, %7, %9
%34 = phi i32 [ -1, %9 ], [ 0, %7 ], [ %30, %27 ], [ -1, %17 ], [ %13, %11 ]
ret i32 %34
}