#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int read(int, void *, size_t);
int read_nbytes(int fd, void *p, size_t sz) {
  while (sz != 0) {
    ssize_t r;
    while ((r = read(fd, p, sz)) == -1 && errno == EINTR)
      ;
    if (r == -1) {
      return -1;
    } else if (r == 0) {
      errno = 0;
      return -1;
    }
    p = (char *)p + r;
    sz -= r;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_nbytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e7 <read_nbytes+0xd7>
jmp    1133 <read_nbytes+0x23>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x1d(%rbp)
jne    1170 <read_nbytes+0x60>
mov    0x2e83(%rip),%rax
mov    (%rax),%rax
mov    0x2e71(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1180 <read_nbytes+0x70>
jmp    1185 <read_nbytes+0x75>
jmp    1133 <read_nbytes+0x23>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    119b <read_nbytes+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <read_nbytes+0xde>
cmpl   $0x0,-0x1c(%rbp)
jne    11bf <read_nbytes+0xaf>
mov    0x2e34(%rip),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <read_nbytes+0xde>
jmp    11c4 <read_nbytes+0xb4>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1123 <read_nbytes+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_nbytes>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     1176 <read_nbytes+0x66>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2eb4(%rip),%r15
mov    0x2ea5(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
mov    %eax,%ecx
cmp    $0xffffffff,%eax
jne    115d <read_nbytes+0x4d>
mov    (%r12),%rax
cmp    %rax,(%r15)
je     1140 <read_nbytes+0x30>
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
je     1181 <read_nbytes+0x71>
test   %ecx,%ecx
je     117a <read_nbytes+0x6a>
movslq %ecx,%rax
add    %rax,%rbx
sub    %rax,%r14
jne    1140 <read_nbytes+0x30>
xor    %eax,%eax
jmp    1181 <read_nbytes+0x71>
movq   $0x0,(%r15)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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