#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 */
#define EAGAIN 129
#define EINTR 128
int 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) {
      switch (errno) {
      case EINTR:
        continue;
      case EAGAIN:
        return 0;
      default:
        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    118c <fev_read+0x7c>
mov    0x2e99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
sub    $0x80,%eax
je     116f <fev_read+0x5f>
jmp    115c <fev_read+0x4c>
mov    -0x20(%rbp),%eax
sub    $0x81,%eax
je     1174 <fev_read+0x64>
jmp    1180 <fev_read+0x70>
jmp    11ad <fev_read+0x9d>
movl   $0x0,-0x4(%rbp)
jmp    11bc <fev_read+0xac>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <fev_read+0xac>
cmpl   $0x0,-0x1c(%rbp)
jne    11a2 <fev_read+0x92>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <fev_read+0xac>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bc <fev_read+0xac>
mov    $0x1,%al
test   $0x1,%al
jne    1123 <fev_read+0x13>
jmp    11bc <fev_read+0xac>
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,%r15
mov    %rsi,%rbp
mov    %edi,%ebx
mov    $0xffffffff,%r14d
mov    0x2eb3(%rip),%r12
nopl   (%rax)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    115e <fev_read+0x4e>
mov    (%r12),%ecx
cmp    $0x80,%ecx
je     1130 <fev_read+0x20>
mov    $0x0,%r14d
cmp    $0x81,%ecx
jne    1162 <fev_read+0x52>
jmp    1165 <fev_read+0x55>
test   %eax,%eax
je     1165 <fev_read+0x55>
mov    %eax,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 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 = %27, %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 %21
16: ; preds = %9
%17 = load i32, i32* @errno
switch i32 %17, label %20 [
i32 128, label %18
i32 129, label %19
]
18: ; preds = %16
br label %27
19: ; preds = %16
store i32 0, i32* %4
br label %28
20: ; preds = %16
store i32 -1, i32* %4
br label %28
21: ; preds = %9
%22 = load i32, i32* %8
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 -1, i32* %4
br label %28
25: ; preds = %21
%26 = load i32, i32* %8
store i32 %26, i32* %4
br label %28
27: ; preds = %18
br i1 true, label %9, label %28
28: ; preds = %19, %20, %24, %25, %27
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
@errno = global i32 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 %10 [
i32 -1, label %6
i32 0, label %8
]
6: ; preds = %4
%7 = load i32, i32* @errno
switch i32 %7, label %10 [
i32 128, label %4
i32 129, label %8
]
8: ; preds = %4, %6
%9 = phi i32 [ 0, %6 ], [ -1, %4 ]
br label %10
10: ; preds = %4, %6, %8
%11 = phi i32 [ %9, %8 ], [ %5, %6 ], [ %5, %4 ]
ret i32 %11
}