#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, unsigned char *, int);
int fullWrite(int fd, unsigned char *buf, int len) {
  int total;
  int thistime;
  for (total = 0; total < len;) {
    thistime = write(fd, buf + total, len - total);
    if (thistime < 0) {
      if (EINTR == errno || EAGAIN == errno)
        continue;
      return thistime;
    }
    total += thistime;
  }
  return total;
}


x86 assembly (clang, -O0):
<fullWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ad <fullWrite+0x9d>
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    119f <fullWrite+0x8f>
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    0x2e74(%rip),%rcx
cmp    (%rcx),%rax
je     118f <fullWrite+0x7f>
mov    0x2e4c(%rip),%rax
mov    (%rax),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
jne    1194 <fullWrite+0x84>
jmp    1129 <fullWrite+0x19>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b3 <fullWrite+0xa3>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <fullWrite+0x19>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fullWrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edx,%edx
jle    1195 <fullWrite+0x85>
mov    %edi,%r13d
xor    %r12d,%r12d
mov    0x2ea9(%rip),%r14
mov    0x2eaa(%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 <fullWrite+0x67>
mov    (%r15),%rcx
cmp    %rcx,(%r14)
je     1150 <fullWrite+0x40>
mov    0x2e58(%rip),%rdx
cmp    %rcx,(%rdx)
je     1150 <fullWrite+0x40>
jmp    1190 <fullWrite+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 <fullWrite+0x40>
jmp    1198 <fullWrite+0x88>
mov    %eax,%r12d
jmp    1198 <fullWrite+0x88>
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):
@EINTR = global i64 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @fullWrite(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* %8
br label %10
10: ; preds = %37, %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 %37
26: ; preds = %14
%27 = load i64, i64* @EINTR
%28 = load i64, i64* @errno
%29 = icmp eq i64 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i64, i64* @EAGAIN
%32 = load i64, i64* @errno
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30, %26
br label %10
35: ; preds = %30
%36 = load i32, i32* %9
store i32 %36, i32* %4
br label %43
37: ; preds = %14
%38 = load i32, i32* %9
%39 = load i32, i32* %8
%40 = add nsw i32 %39, %38
store i32 %40, i32* %8
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):
@EINTR = global i64 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @fullWrite(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* @EINTR
%14 = load i64, i64* @errno
%15 = icmp eq i64 %13, %14
%16 = load i64, i64* @EAGAIN
%17 = icmp eq i64 %16, %14
%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 ], [ %10, %12 ], [ %20, %19 ]
ret i32 %26
}