#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__ ECONNRESET;
scalar_t__ EINTR;
scalar_t__ EPIPE;
scalar_t__ errno;
int write(int, char *, int);
int write_fill(int fd, const unsigned char *buf, int len) {
  int i;
  unsigned int done = 0;
  do {
    if ((i = write(fd, (char *)(buf + done), len - done)) < 0) {
      if (errno == EPIPE || errno == ECONNRESET) {
        return -2;
      }
      if (errno != EINTR) {
        return -1;
      }
      i = 0;
    }
    done += i;
  } while (done < len);
  return len;
}


x86 assembly (clang, -O0):
<write_fill>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%eax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11ba <write_fill+0xaa>
mov    0x2e7c(%rip),%rax
mov    (%rax),%rax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%rax
je     1181 <write_fill+0x71>
mov    0x2e62(%rip),%rax
mov    (%rax),%rax
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%rax
jne    118d <write_fill+0x7d>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11d5 <write_fill+0xc5>
mov    0x2e3c(%rip),%rax
mov    (%rax),%rax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
je     11b3 <write_fill+0xa3>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <write_fill+0xc5>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jb     1129 <write_fill+0x19>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_fill>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2eb4(%rip),%r12
mov    0x2e95(%rip),%r13
jmp    1146 <write_fill+0x36>
cs nopw 0x0(%rax,%rax,1)
nop
add    %eax,%ebp
cmp    %ebx,%ebp
jae    118b <write_fill+0x7b>
mov    %ebp,%esi
add    %r14,%rsi
mov    %ebx,%edx
sub    %ebp,%edx
mov    %r15d,%edi
call   1030 <write@plt>
test   %eax,%eax
jns    1140 <write_fill+0x30>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rcx
mov    $0xfffffffe,%eax
mov    0x2e67(%rip),%rdx
cmp    (%rdx),%rcx
je     118d <write_fill+0x7d>
cmp    (%r12),%rcx
je     118d <write_fill+0x7d>
xor    %eax,%eax
cmp    0x0(%r13),%rcx
je     1140 <write_fill+0x30>
mov    $0xffffffff,%eax
jmp    118d <write_fill+0x7d>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EPIPE = global i64 0
@ECONNRESET = global i64 0
@EINTR = global i64 0
define i32 @write_fill(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
br label %10
10: ; preds = %40, %3
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i32, i32* %9
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i32, i32* %7
%17 = load i32, i32* %9
%18 = sub i32 %16, %17
%19 = call i32 @write(i32 %11, i8* %15, i32 %18)
store i32 %19, i32* %8
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %36
21: ; preds = %10
%22 = load i64, i64* @errno
%23 = load i64, i64* @EPIPE
%24 = icmp eq i64 %22, %23
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i64, i64* @errno
%27 = load i64, i64* @ECONNRESET
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25, %21
store i32 -2, i32* %4
br label %46
30: ; preds = %25
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINTR
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 -1, i32* %4
br label %46
35: ; preds = %30
store i32 0, i32* %8
br label %36
36: ; preds = %35, %10
%37 = load i32, i32* %8
%38 = load i32, i32* %9
%39 = add i32 %38, %37
store i32 %39, i32* %9
br label %40
40: ; preds = %36
%41 = load i32, i32* %9
%42 = load i32, i32* %7
%43 = icmp ult i32 %41, %42
br i1 %43, label %10, label %44
44: ; preds = %40
%45 = load i32, i32* %7
store i32 %45, i32* %4
br label %46
46: ; preds = %44, %34, %29
%47 = load i32, i32* %4
ret i32 %47
}

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