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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
scalar_t__ write(int, char const *, size_t);
ssize_t fev_write(int fd, const char *pbuf, size_t len) {
  do {
    ssize_t bytes = write(fd, pbuf, len);
    if (bytes > 0)
      return bytes;
    else {
      if (errno == EINTR)
        continue;
      else if (errno == EAGAIN)
        return 0;
      else
        return -1;
    }
  } while (1);
}


x86 assembly (clang, -O0):
<fev_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jle    114f <fev_write+0x3f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b1 <fev_write+0xa1>
mov    0x2e8a(%rip),%rax
mov    (%rax),%rax
mov    0x2e78(%rip),%rcx
cmp    (%rcx),%rax
jne    116e <fev_write+0x5e>
jmp    11a2 <fev_write+0x92>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
jne    1195 <fev_write+0x85>
movq   $0x0,-0x8(%rbp)
jmp    11b1 <fev_write+0xa1>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11b1 <fev_write+0xa1>
mov    $0x1,%al
test   $0x1,%al
jne    1123 <fev_write+0x13>
jmp    11b1 <fev_write+0xa1>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fev_write>:
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 <write@plt>
test   %rax,%rax
jg     115d <fev_write+0x4d>
mov    (%r15),%rcx
cmp    (%r12),%rcx
je     1130 <fev_write+0x20>
mov    0x2e76(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %rax
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 i64 @fev_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
br label %9
9: ; preds = %29, %3
%10 = load i32, i32* %5
%11 = load i8*, i8** %6
%12 = load i64, i64* %7
%13 = call i64 @write(i32 %10, i8* %11, i64 %12)
store i64 %13, i64* %8
%14 = load i64, i64* %8
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = load i64, i64* %8
store i64 %17, i64* %4
br label %30
18: ; preds = %9
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %18
br label %29
23: ; preds = %18
%24 = load i64, i64* @errno
%25 = load i64, i64* @EAGAIN
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
store i64 0, i64* %4
br label %30
28: ; preds = %23
store i64 -1, i64* %4
br label %30
29: ; preds = %22
br i1 true, label %9, label %30
30: ; preds = %16, %27, %28, %29
%31 = load i64, i64* %4
ret i64 %31
}

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