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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int write(int, char *, size_t);
int write_all(int fd, char *data, size_t n) {
  ssize_t wrote;
  while (n > 0)
    if (wrote = write(fd, data, n), wrote >= 0)
      data += wrote, n -= (size_t)wrote;
    else if (errno != EINTR)
      return -1;
  return 0;
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11a0 <write_all+0x90>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1170 <write_all+0x60>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    119b <write_all+0x8b>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%rax
je     1196 <write_all+0x86>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a7 <write_all+0x97>
jmp    119b <write_all+0x8b>
jmp    1123 <write_all+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdx,%rdx
je     116f <write_all+0x5f>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
mov    0x2ead(%rip),%r12
mov    0x2e9e(%rip),%r13
jmp    114d <write_all+0x3d>
nopl   0x0(%rax)
mov    %eax,%eax
add    %rax,%rbp
sub    %rax,%rbx
test   %rbx,%rbx
je     116f <write_all+0x5f>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %eax,%eax
jns    1140 <write_all+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1148 <write_all+0x38>
mov    $0xffffffff,%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
define i32 @write_all(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
br label %9
9: ; preds = %34, %3
%10 = load i64, i64* %7
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %35
12: ; preds = %9
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = load i64, i64* %7
%16 = call i32 @write(i32 %13, i8* %14, i64 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp sge i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %12
%20 = load i32, i32* %8
%21 = load i8*, i8** %6
%22 = sext i32 %20 to i64
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8* %23, i8** %6
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = load i64, i64* %7
%27 = sub i64 %26, %25
store i64 %27, i64* %7
br label %34
28: ; preds = %12
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 -1, i32* %4
br label %36
33: ; preds = %28
br label %34
34: ; preds = %33, %19
br label %9
35: ; preds = %9
store i32 0, i32* %4
br label %36
36: ; preds = %35, %32
%37 = load i32, i32* %4
ret i32 %37
}

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