#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
scalar_t__ write(int, char const *, scalar_t__);
int write_exact(int fd, const char *buf, ssize_t len) {
  ssize_t written = 0;
  while (written < len) {
    ssize_t n = write(fd, buf, len - written);
    if (n < 0) {
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
        continue;
      }
      return -1;
    } else if (n == 0) {
      return 0;
    } else {
      buf += n;
      written += n;
    }
  }
  return written;
}


x86 assembly (clang, -O0):
<write_exact>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    11da <write_exact+0xca>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11a1 <write_exact+0x91>
mov    0x2e7d(%rip),%rax
mov    (%rax),%rax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%rax
je     1190 <write_exact+0x80>
mov    0x2e63(%rip),%rax
mov    (%rax),%rax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%rax
jne    1195 <write_exact+0x85>
jmp    112b <write_exact+0x1b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <write_exact+0xd1>
cmpq   $0x0,-0x28(%rbp)
jne    11b8 <write_exact+0xa8>
movl   $0x0,-0x4(%rbp)
jmp    11e1 <write_exact+0xd1>
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11d5 <write_exact+0xc5>
jmp    112b <write_exact+0x1b>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_exact>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
jle    117f <write_exact+0x6f>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
mov    0x2eaf(%rip),%r12
mov    0x2e90(%rip),%r13
jmp    114d <write_exact+0x3d>
nopw   0x0(%rax,%rax,1)
je     117f <write_exact+0x6f>
add    %rax,%r15
add    %rax,%rbx
cmp    %r14,%rbx
jge    1181 <write_exact+0x71>
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
mov    %r15,%rsi
call   1030 <write@plt>
test   %rax,%rax
jns    1140 <write_exact+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1148 <write_exact+0x38>
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%rax
je     1148 <write_exact+0x38>
mov    $0xffffffff,%ebx
jmp    1181 <write_exact+0x71>
xor    %ebx,%ebx
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
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @write_exact(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %10
10: ; preds = %45, %31, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp slt i64 %11, %12
br i1 %13, label %14, label %46
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = load i64, i64* %8
%19 = sub nsw i64 %17, %18
%20 = call i64 @write(i32 %15, i8* %16, i64 %19)
store i64 %20, i64* %9
%21 = load i64, i64* %9
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %14
%24 = load i64, i64* @errno
%25 = load i64, i64* @EAGAIN
%26 = icmp eq i64 %24, %25
br i1 %26, label %31, label %27
27: ; preds = %23
%28 = load i64, i64* @errno
%29 = load i64, i64* @EWOULDBLOCK
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27, %23
br label %10
32: ; preds = %27
store i32 -1, i32* %4
br label %49
33: ; preds = %14
%34 = load i64, i64* %9
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 0, i32* %4
br label %49
37: ; preds = %33
%38 = load i64, i64* %9
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i64 %38
store i8* %40, i8** %6
%41 = load i64, i64* %9
%42 = load i64, i64* %8
%43 = add nsw i64 %42, %41
store i64 %43, i64* %8
br label %44
44: ; preds = %37
br label %45
45: ; preds = %44
br label %10
46: ; preds = %10
%47 = load i64, i64* %8
%48 = trunc i64 %47 to i32
store i32 %48, i32* %4
br label %49
49: ; preds = %46, %36, %32
%50 = load i32, i32* %4
ret i32 %50
}

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