#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 */
#define EAGAIN 129
#define EINTR 128
int 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 {
      switch (errno) {
      case EINTR:
        continue;
      case EAGAIN:
        return 0;
      default:
        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    11ac <fev_write+0x9c>
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x80,%eax
je     117e <fev_write+0x6e>
jmp    116b <fev_write+0x5b>
mov    -0x2c(%rbp),%eax
sub    $0x81,%eax
je     1183 <fev_write+0x73>
jmp    1190 <fev_write+0x80>
jmp    119d <fev_write+0x8d>
movq   $0x0,-0x8(%rbp)
jmp    11ac <fev_write+0x9c>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11ac <fev_write+0x9c>
mov    $0x1,%al
test   $0x1,%al
jne    1123 <fev_write+0x13>
jmp    11ac <fev_write+0x9c>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fev_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2eba(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
test   %rax,%rax
jg     115e <fev_write+0x4e>
mov    (%r15),%eax
cmp    $0x80,%eax
je     1130 <fev_write+0x20>
cmp    $0x81,%eax
jne    1157 <fev_write+0x47>
xor    %eax,%eax
jmp    115e <fev_write+0x4e>
mov    $0xffffffffffffffff,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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