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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ STDIN_FILENO;
scalar_t__ errno;
size_t read(int /*<<< orphan*/, unsigned char *, size_t);
int restartable_read(unsigned char *buf, size_t buflen) {
  ssize_t rc, readb;
  rc = 0;
READLOOP:
  while (rc < buflen) {
    readb = read(STDIN_FILENO, buf + rc, buflen - rc);
    if (readb == -1) {
      if (errno == EAGAIN || errno == EINTR)
        goto READLOOP;
      return -1;
    } else if (readb == 0) {
      return -1;
    }
    rc += readb;
  }
  return rc;
}


x86 assembly (clang, -O0):
<restartable_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
jmp    112d <restartable_read+0x1d>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11da <restartable_read+0xca>
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    11ad <restartable_read+0x9d>
mov    0x2e71(%rip),%rax
mov    (%rax),%rax
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%rax
je     119c <restartable_read+0x8c>
mov    0x2e57(%rip),%rax
mov    (%rax),%rax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
jne    11a1 <restartable_read+0x91>
jmp    1128 <restartable_read+0x18>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <restartable_read+0xd1>
cmpq   $0x0,-0x28(%rbp)
jne    11c4 <restartable_read+0xb4>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <restartable_read+0xd1>
jmp    11c9 <restartable_read+0xb9>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    112d <restartable_read+0x1d>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<restartable_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
mov    0x2ea6(%rip),%r12
mov    0x2eaf(%rip),%r13
mov    0x2e88(%rip),%rbp
nopl   0x0(%rax,%rax,1)
cmp    %r14,%rbx
jae    1183 <restartable_read+0x73>
mov    (%r12),%edi
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
call   1030 <read@plt>
test   %rax,%rax
je     117e <restartable_read+0x6e>
cmp    $0xffffffffffffffff,%rax
je     1168 <restartable_read+0x58>
add    %rax,%rbx
jmp    1140 <restartable_read+0x30>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
je     1140 <restartable_read+0x30>
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%rax
je     1140 <restartable_read+0x30>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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