#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 unix_write(int fd, const char *buf, int len) {
  int result = 0;
  while (len > 0) {
    int len2 = write(fd, buf, len);
    if (len2 < 0) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      return -1;
    }
    result += len2;
    len -= len2;
    buf += len2;
  }
  return result;
}


x86 assembly (clang, -O0):
<unix_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11be <unix_write+0xae>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1194 <unix_write+0x84>
mov    0x2e8a(%rip),%rax
mov    (%rax),%rax
mov    0x2e78(%rip),%rcx
cmp    (%rcx),%rax
je     1183 <unix_write+0x73>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    1188 <unix_write+0x78>
jmp    1129 <unix_write+0x19>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c4 <unix_write+0xb4>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1129 <unix_write+0x19>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    117d <unix_write+0x6d>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
xor    %r14d,%r14d
mov    0x2eaf(%rip),%r12
mov    0x2ea0(%rip),%r13
jmp    114e <unix_write+0x3e>
nopw   0x0(%rax,%rax,1)
add    %eax,%r14d
sub    %eax,%ebp
mov    %eax,%eax
add    %rax,%rbx
test   %ebp,%ebp
jle    1180 <unix_write+0x70>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    1140 <unix_write+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     114a <unix_write+0x3a>
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%rax
je     114a <unix_write+0x3a>
mov    $0xffffffff,%r14d
jmp    1180 <unix_write+0x70>
xor    %r14d,%r14d
mov    %r14d,%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
@EAGAIN = global i64 0
define i32 @unix_write(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 = %30, %28, %3
%11 = load i32, i32* %7
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %41
13: ; preds = %10
%14 = load i32, i32* %5
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = call i32 @write(i32 %14, i8* %15, i32 %16)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %13
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = load i64, i64* @EAGAIN
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24, %20
br label %10
29: ; preds = %24
store i32 -1, i32* %4
br label %43
30: ; preds = %13
%31 = load i32, i32* %9
%32 = load i32, i32* %8
%33 = add nsw i32 %32, %31
store i32 %33, i32* %8
%34 = load i32, i32* %9
%35 = load i32, i32* %7
%36 = sub nsw i32 %35, %34
store i32 %36, i32* %7
%37 = load i32, i32* %9
%38 = load i8*, i8** %6
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8* %40, i8** %6
br label %10
41: ; preds = %10
%42 = load i32, i32* %8
store i32 %42, i32* %4
br label %43
43: ; preds = %41, %29
%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 @unix_write(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %28
5: ; preds = %3, %23
%6 = phi i32 [ %26, %23 ], [ 0, %3 ]
%7 = phi i32 [ %25, %23 ], [ %2, %3 ]
%8 = phi i8* [ %24, %23 ], [ %1, %3 ]
%9 = tail call i32 @write(i32 %0, i8* %8, i32 %7)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %5
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
%15 = load i64, i64* @EAGAIN
%16 = icmp eq i64 %12, %15
%17 = select i1 %14, i1 true, i1 %16
br i1 %17, label %23, label %28
18: ; preds = %5
%19 = add nsw i32 %9, %6
%20 = sub nsw i32 %7, %9
%21 = zext i32 %9 to i64
%22 = getelementptr inbounds i8, i8* %8, i64 %21
br label %23
23: ; preds = %11, %18
%24 = phi i8* [ %22, %18 ], [ %8, %11 ]
%25 = phi i32 [ %20, %18 ], [ %7, %11 ]
%26 = phi i32 [ %19, %18 ], [ %6, %11 ]
%27 = icmp sgt i32 %25, 0
br i1 %27, label %5, label %28
28: ; preds = %23, %11, %3
%29 = phi i32 [ 0, %3 ], [ -1, %11 ], [ %26, %23 ]
ret i32 %29
}