#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 size_t ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
size_t write(int, char const *, size_t);
ssize_t writeall(int fd, const void *buf, size_t nbyte) {
  ssize_t nwritten = 0, n;
  do {
    if ((n = write(fd, &((const char *)buf)[nwritten], nbyte - nwritten)) ==
        -1) {
      if (errno == EINTR)
        continue;
      else
        return -1;
    }
    nwritten += n;
  } while (nwritten < nbyte);
  return nwritten;
}


x86 assembly (clang, -O0):
<writeall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    117d <writeall+0x6d>
mov    0x2e88(%rip),%rax
mov    (%rax),%rax
mov    0x2e76(%rip),%rcx
cmp    (%rcx),%rax
jne    1170 <writeall+0x60>
jmp    1189 <writeall+0x79>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    119f <writeall+0x8f>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     112b <writeall+0x1b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeall>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
mov    0x2eb4(%rip),%r12
mov    0x2ea5(%rip),%r13
jmp    1148 <writeall+0x38>
cs nopw 0x0(%rax,%rax,1)
nop
add    %rax,%rbx
cmp    %r14,%rbx
jae    1170 <writeall+0x60>
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
jne    1140 <writeall+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1143 <writeall+0x33>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
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 i64 @writeall(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%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 = %30, %3
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i64, i64* %8
%14 = getelementptr inbounds i8, i8* %12, i64 %13
%15 = load i64, i64* %7
%16 = load i64, i64* %8
%17 = sub i64 %15, %16
%18 = call i64 @write(i32 %11, i8* %14, i64 %17)
store i64 %18, i64* %9
%19 = icmp eq i64 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %10
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20
br label %30
25: ; preds = %20
store i64 -1, i64* %4
br label %36
26: ; preds = %10
%27 = load i64, i64* %9
%28 = load i64, i64* %8
%29 = add i64 %28, %27
store i64 %29, i64* %8
br label %30
30: ; preds = %26, %24
%31 = load i64, i64* %8
%32 = load i64, i64* %7
%33 = icmp ult i64 %31, %32
br i1 %33, label %10, label %34
34: ; preds = %30
%35 = load i64, i64* %8
store i64 %35, i64* %4
br label %36
36: ; preds = %34, %25
%37 = load i64, i64* %4
ret i64 %37
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i64 @writeall(i32 %0, i8* nocapture %1, i64 %2) {
br label %4
4: ; preds = %16, %3
%5 = phi i64 [ 0, %3 ], [ %17, %16 ]
%6 = getelementptr inbounds i8, i8* %1, i64 %5
%7 = sub i64 %2, %5
%8 = tail call i64 @write(i32 %0, i8* %6, i64 %7)
%9 = icmp eq i64 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %4
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %16, label %19
14: ; preds = %4
%15 = add i64 %8, %5
br label %16
16: ; preds = %10, %14
%17 = phi i64 [ %5, %10 ], [ %15, %14 ]
%18 = icmp ult i64 %17, %2
br i1 %18, label %4, label %19
19: ; preds = %16, %10
%20 = phi i64 [ -1, %10 ], [ %17, %16 ]
ret i64 %20
}