#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__ EWOULDBLOCK;
scalar_t__ errno;
scalar_t__ write(int, char const *, size_t);
int write_bytes(int fd, const char *buf, size_t len) {
  ssize_t bytes;
  do {
    bytes = write(fd, buf, len);
    if (bytes >= 0) {
      len -= bytes;
    } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
      return -1;
    }
  } while (len > 0);
  return 0;
}


x86 assembly (clang, -O0):
<write_bytes>:
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 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jl     1156 <write_bytes+0x46>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    11b5 <write_bytes+0xa5>
mov    0x2e83(%rip),%rax
mov    (%rax),%rax
mov    0x2e59(%rip),%rcx
cmp    (%rcx),%rax
je     11b0 <write_bytes+0xa0>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%rax
je     11b0 <write_bytes+0xa0>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%rax
je     11b0 <write_bytes+0xa0>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <write_bytes+0xbc>
jmp    11b5 <write_bytes+0xa5>
jmp    11ba <write_bytes+0xaa>
cmpq   $0x0,-0x18(%rbp)
ja     1123 <write_bytes+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2eb6(%rip),%r15
mov    0x2e8f(%rip),%r12
mov    0x2ea0(%rip),%r13
jmp    1148 <write_bytes+0x38>
nopw   0x0(%rax,%rax,1)
sub    %rax,%rbx
test   %rbx,%rbx
je     117c <write_bytes+0x6c>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
jns    1140 <write_bytes+0x30>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1143 <write_bytes+0x33>
cmp    0x0(%r13),%rax
je     1143 <write_bytes+0x33>
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%rax
je     1143 <write_bytes+0x33>
mov    $0xffffffff,%eax
jmp    117e <write_bytes+0x6e>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@EINTR = global i64 0
define i32 @write_bytes(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%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 = %35, %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 sge i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %9
%17 = load i64, i64* %8
%18 = load i64, i64* %7
%19 = sub i64 %18, %17
store i64 %19, i64* %7
br label %34
20: ; preds = %9
%21 = load i64, i64* @errno
%22 = load i64, i64* @EAGAIN
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = load i64, i64* @EWOULDBLOCK
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 -1, i32* %4
br label %39
33: ; preds = %28, %24, %20
br label %34
34: ; preds = %33, %16
br label %35
35: ; preds = %34
%36 = load i64, i64* %7
%37 = icmp ugt i64 %36, 0
br i1 %37, label %9, label %38
38: ; preds = %35
store i32 0, i32* %4
br label %39
39: ; preds = %38, %32
%40 = load i32, i32* %4
ret i32 %40
}

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