#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 */

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int write(int, char const *, int);
int write_holder(int fd, const char *buf, int len) {
  int i, ret = 0;
  for (i = 0; i < len;) {
    ret = write(fd, buf + i, len - i);
    if (ret < 0) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      return -1;
    } else {
      i += ret;
    }
  }
  return i;
}


x86 assembly (clang, -O0):
<write_holder>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b5 <write_holder+0xa5>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a7 <write_holder+0x97>
mov    0x2e77(%rip),%rax
mov    (%rax),%rax
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%rax
je     1196 <write_holder+0x86>
mov    0x2e5d(%rip),%rax
mov    (%rax),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
jne    119b <write_holder+0x8b>
jmp    1130 <write_holder+0x20>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <write_holder+0xab>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1130 <write_holder+0x20>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_holder>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edx,%edx
jle    1198 <write_holder+0x88>
mov    %edi,%r13d
xor    %r12d,%r12d
mov    0x2eb1(%rip),%r14
mov    0x2ea2(%rip),%r15
mov    %edx,0xc(%rsp)
mov    %edx,%ebp
mov    %rsi,0x10(%rsp)
mov    %rsi,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    1177 <write_holder+0x67>
mov    (%r14),%rax
cmp    (%r15),%rax
je     1150 <write_holder+0x40>
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%rax
je     1150 <write_holder+0x40>
jmp    1190 <write_holder+0x80>
add    %eax,%r12d
movslq %r12d,%rax
mov    0x10(%rsp),%rcx
lea    (%rcx,%rax,1),%rbx
mov    0xc(%rsp),%ebp
sub    %eax,%ebp
jg     1150 <write_holder+0x40>
jmp    119b <write_holder+0x8b>
mov    $0xffffffff,%r12d
jmp    119b <write_holder+0x8b>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @write_holder(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
store i32 0, i32* %8
br label %10
10: ; preds = %40, %34, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %41
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = sub nsw i32 %20, %21
%23 = call i32 @write(i32 %15, i8* %19, i32 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %14
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i64, i64* @errno
%32 = load i64, i64* @EAGAIN
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30, %26
br label %10
35: ; preds = %30
store i32 -1, i32* %4
br label %43
36: ; preds = %14
%37 = load i32, i32* %9
%38 = load i32, i32* %8
%39 = add nsw i32 %38, %37
store i32 %39, i32* %8
br label %40
40: ; preds = %36
br label %10
41: ; preds = %10
%42 = load i32, i32* %8
store i32 %42, i32* %4
br label %43
43: ; preds = %41, %35
%44 = load i32, i32* %4
ret i32 %44
}

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