#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__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
size_t write(int, void const *, size_t);
ssize_t writeall(int fd, const void *data, size_t n) {
  size_t wr, res;
  wr = 0;
  while (n > wr) {
    res = write(fd, data + wr, n - wr);
    if (res == -1) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      return wr;
    }
    if (res == 0)
      return wr;
    wr += res;
  }
  return wr;
}


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    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jbe    11cf <writeall+0xbf>
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)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    11a6 <writeall+0x96>
mov    0x2e79(%rip),%rax
mov    (%rax),%rax
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%rax
je     1194 <writeall+0x84>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
jne    1199 <writeall+0x89>
jmp    112b <writeall+0x1b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d7 <writeall+0xc7>
cmpq   $0x0,-0x30(%rbp)
jne    11be <writeall+0xae>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d7 <writeall+0xc7>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    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
sub    $0x18,%rsp
test   %rdx,%rdx
je     1197 <writeall+0x87>
mov    %edi,%r13d
xor    %r12d,%r12d
mov    0x2eb0(%rip),%r15
mov    0x2ea1(%rip),%r14
mov    %rdx,0x10(%rsp)
mov    %rdx,%rbx
mov    %rsi,0x8(%rsp)
mov    %rsi,%rbp
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    $0xffffffffffffffff,%rax
jne    117a <writeall+0x6a>
mov    (%r15),%rax
cmp    (%r14),%rax
je     1150 <writeall+0x40>
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
je     1150 <writeall+0x40>
jmp    119a <writeall+0x8a>
test   %rax,%rax
je     119a <writeall+0x8a>
add    %rax,%r12
mov    0x8(%rsp),%rax
lea    (%rax,%r12,1),%rbp
mov    0x10(%rsp),%rbx
sub    %r12,%rbx
ja     1150 <writeall+0x40>
jmp    119a <writeall+0x8a>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x18,%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 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 = %41, %33, %3
%11 = load i64, i64* %7
%12 = load i64, i64* %8
%13 = icmp ugt i64 %11, %12
br i1 %13, label %14, label %45
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %8
%18 = getelementptr i8, i8* %16, i64 %17
%19 = load i64, i64* %7
%20 = load i64, i64* %8
%21 = sub i64 %19, %20
%22 = call i64 @write(i32 %15, i8* %18, i64 %21)
store i64 %22, i64* %9
%23 = load i64, i64* %9
%24 = icmp eq i64 %23, -1
br i1 %24, label %25, label %36
25: ; preds = %14
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i64, i64* @errno
%31 = load i64, i64* @EAGAIN
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29, %25
br label %10
34: ; preds = %29
%35 = load i64, i64* %8
store i64 %35, i64* %4
br label %47
36: ; preds = %14
%37 = load i64, i64* %9
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = load i64, i64* %8
store i64 %40, i64* %4
br label %47
41: ; preds = %36
%42 = load i64, i64* %9
%43 = load i64, i64* %8
%44 = add i64 %43, %42
store i64 %44, i64* %8
br label %10
45: ; preds = %10
%46 = load i64, i64* %8
store i64 %46, i64* %4
br label %47
47: ; preds = %45, %39, %34
%48 = load i64, i64* %4
ret i64 %48
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i64 @writeall(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %23, label %5
5: ; preds = %3, %18
%6 = phi i64 [ %22, %18 ], [ %2, %3 ]
%7 = phi i8* [ %21, %18 ], [ %1, %3 ]
%8 = phi i64 [ %19, %18 ], [ 0, %3 ]
br label %9
9: ; preds = %5, %11
%10 = tail call i64 @write(i32 %0, i8* %7, i64 %6)
switch i64 %10, label %18 [
i64 -1, label %11
i64 0, label %23
]
11: ; preds = %9
%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 %9, label %23
18: ; preds = %9
%19 = add i64 %10, %8
%20 = icmp ult i64 %19, %2
%21 = getelementptr i8, i8* %1, i64 %19
%22 = sub i64 %2, %19
br i1 %20, label %5, label %23
23: ; preds = %18, %11, %9, %3
%24 = phi i64 [ 0, %3 ], [ %8, %9 ], [ %8, %11 ], [ %19, %18 ]
ret i64 %24
}