#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__ EINTR;
scalar_t__ errno;
int write(int, char *, int);
int write_fill(int fd, unsigned char *buf, int len) {
  int i;
  unsigned int done = 0;
  do {
    if ((i = write(fd, (char *)(buf + done), len - done)) < 0) {
      if (errno != EINTR)
        return (i);
      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    1179 <write_fill+0x69>
mov    0x2e8c(%rip),%rax
mov    (%rax),%rax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%rax
je     1172 <write_fill+0x62>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1194 <write_fill+0x84>
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    0x2ea5(%rip),%r13
jmp    1148 <write_fill+0x38>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,%ecx
add    %ecx,%ebp
cmp    %ebx,%ebp
jae    116b <write_fill+0x5b>
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    (%r12),%rdx
xor    %ecx,%ecx
cmp    0x0(%r13),%rdx
je     1142 <write_fill+0x32>
jmp    116d <write_fill+0x5d>
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
@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 = %32, %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 %28
21: ; preds = %10
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i32, i32* %8
store i32 %26, i32* %4
br label %38
27: ; preds = %21
store i32 0, i32* %8
br label %28
28: ; preds = %27, %10
%29 = load i32, i32* %8
%30 = load i32, i32* %9
%31 = add i32 %30, %29
store i32 %31, i32* %9
br label %32
32: ; preds = %28
%33 = load i32, i32* %9
%34 = load i32, i32* %7
%35 = icmp ult i32 %33, %34
br i1 %35, label %10, label %36
36: ; preds = %32
%37 = load i32, i32* %7
store i32 %37, i32* %4
br label %38
38: ; preds = %36, %25
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @write_fill(i32 %0, i8* nocapture %1, i32 %2) {
br label %4
4: ; preds = %15, %3
%5 = phi i32 [ 0, %3 ], [ %17, %15 ]
%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 %15
11: ; preds = %4
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %19
15: ; preds = %11, %4
%16 = phi i32 [ %9, %4 ], [ 0, %11 ]
%17 = add i32 %16, %5
%18 = icmp ult i32 %17, %2
br i1 %18, label %4, label %19
19: ; preds = %15, %11
%20 = phi i32 [ %9, %11 ], [ %2, %15 ]
ret i32 %20
}