#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__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int read(int, void *, size_t);
ssize_t fev_read(int fd, void *pbuf, size_t len) {
  do {
    ssize_t read_size = read(fd, pbuf, len);
    if (read_size == -1) {
      if (errno == EINTR)
        continue;
      else if (errno == EAGAIN)
        return 0;
      return -1;
    } else if (read_size == 0)
      return -1;
    else
      return read_size;
  } while (1);
}


x86 assembly (clang, -O0):
<fev_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1196 <fev_read+0x86>
mov    0x2e99(%rip),%rax
mov    (%rax),%rax
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%rax
jne    115f <fev_read+0x4f>
jmp    11b7 <fev_read+0xa7>
mov    0x2e7a(%rip),%rax
mov    (%rax),%rax
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%rax
jne    1185 <fev_read+0x75>
movl   $0x0,-0x4(%rbp)
jmp    11c6 <fev_read+0xb6>
jmp    118a <fev_read+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <fev_read+0xb6>
cmpl   $0x0,-0x1c(%rbp)
jne    11ac <fev_read+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <fev_read+0xb6>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c6 <fev_read+0xb6>
mov    $0x1,%al
test   $0x1,%al
jne    1123 <fev_read+0x13>
jmp    11c6 <fev_read+0xb6>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fev_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2eb9(%rip),%r15
mov    0x2eaa(%rip),%r12
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    115e <fev_read+0x4e>
mov    (%r15),%rcx
cmp    (%r12),%rcx
je     1130 <fev_read+0x20>
mov    0x2e76(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %eax
jmp    1167 <fev_read+0x57>
test   %eax,%eax
jne    1167 <fev_read+0x57>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @fev_read(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 = %34, %3
%10 = load i32, i32* %5
%11 = load i8*, i8** %6
%12 = load i64, i64* %7
%13 = call i32 @read(i32 %10, i8* %11, i64 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %28
16: ; preds = %9
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16
br label %34
21: ; preds = %16
%22 = load i64, i64* @errno
%23 = load i64, i64* @EAGAIN
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 0, i32* %4
br label %35
26: ; preds = %21
br label %27
27: ; preds = %26
store i32 -1, i32* %4
br label %35
28: ; preds = %9
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 -1, i32* %4
br label %35
32: ; preds = %28
%33 = load i32, i32* %8
store i32 %33, i32* %4
br label %35
34: ; preds = %20
br i1 true, label %9, label %35
35: ; preds = %25, %27, %31, %32, %34
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @fev_read(i32 %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %6, %3
%5 = tail call i32 @read(i32 %0, i8* %1, i64 %2)
switch i32 %5, label %15 [
i32 -1, label %6
i32 0, label %14
]
6: ; preds = %4
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
br i1 %9, label %4, label %10
10: ; preds = %6
%11 = load i64, i64* @EAGAIN
%12 = icmp ne i64 %7, %11
%13 = sext i1 %12 to i32
br label %15
14: ; preds = %4
br label %15
15: ; preds = %4, %14, %10
%16 = phi i32 [ %13, %10 ], [ -1, %14 ], [ %5, %4 ]
ret i32 %16
}