#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 const *, int);
int full_write(int fd, const void *vbuf, int len) {
  const unsigned char *buf = (const unsigned char *)vbuf;
  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):
<full_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b5 <full_write+0xa5>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x24(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11a7 <full_write+0x97>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%rax
je     1197 <full_write+0x87>
mov    0x2e44(%rip),%rax
mov    (%rax),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
jne    119c <full_write+0x8c>
jmp    1131 <full_write+0x21>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bb <full_write+0xab>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1131 <full_write+0x21>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<full_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edx,%edx
jle    1195 <full_write+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 <full_write+0x67>
mov    (%r15),%rcx
cmp    %rcx,(%r14)
je     1150 <full_write+0x40>
mov    0x2e58(%rip),%rdx
cmp    %rcx,(%rdx)
je     1150 <full_write+0x40>
jmp    1190 <full_write+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 <full_write+0x40>
jmp    1198 <full_write+0x88>
mov    %eax,%r12d
jmp    1198 <full_write+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 @full_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %8
store i32 0, i32* %9
br label %12
12: ; preds = %39, %36, %3
%13 = load i32, i32* %9
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %43
16: ; preds = %12
%17 = load i32, i32* %5
%18 = load i8*, i8** %8
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i32, i32* %7
%23 = load i32, i32* %9
%24 = sub nsw i32 %22, %23
%25 = call i32 @write(i32 %17, i8* %21, i32 %24)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %39
28: ; preds = %16
%29 = load i64, i64* @EINTR
%30 = load i64, i64* @errno
%31 = icmp eq i64 %29, %30
br i1 %31, label %36, label %32
32: ; preds = %28
%33 = load i64, i64* @EAGAIN
%34 = load i64, i64* @errno
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32, %28
br label %12
37: ; preds = %32
%38 = load i32, i32* %10
store i32 %38, i32* %4
br label %45
39: ; preds = %16
%40 = load i32, i32* %10
%41 = load i32, i32* %9
%42 = add nsw i32 %41, %40
store i32 %42, i32* %9
br label %12
43: ; preds = %12
%44 = load i32, i32* %9
store i32 %44, i32* %4
br label %45
45: ; preds = %43, %37
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@EINTR = global i64 0
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @full_write(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
}