#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__ EINTR;
scalar_t__ errno;
scalar_t__ write(int, unsigned char *, size_t);
int write_all(int fd, unsigned char *data, size_t len) {
  size_t written = 0;
  ssize_t bytes;
  while (written < len) {
    do {
      bytes = write(fd, data + written, len - written);
    } while (bytes < 0 && EINTR == errno);
    if (bytes < 0)
      return -1;
    written += bytes;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_all>:
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
jae    11b7 <write_all+0xa7>
jmp    113e <write_all+0x2e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x29(%rbp)
jge    1184 <write_all+0x74>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    113e <write_all+0x2e>
cmpq   $0x0,-0x28(%rbp)
jge    11a6 <write_all+0x96>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <write_all+0xae>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    112b <write_all+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
test   %rdx,%rdx
je     118d <write_all+0x7d>
mov    %rdx,%rbp
mov    %edi,%r12d
xor    %r13d,%r13d
mov    0x2ea0(%rip),%r14
mov    0x2ea1(%rip),%r15
mov    %rdx,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x10(%rsp),%rax
lea    (%rax,%r13,1),%rbx
sub    %r13,%rbp
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %rax,%rax
jns    117b <write_all+0x6b>
mov    (%r15),%rcx
cmp    %rcx,(%r14)
je     1160 <write_all+0x50>
test   %rax,%rax
js     1191 <write_all+0x81>
add    %rax,%r13
mov    0x8(%rsp),%rbp
cmp    %rbp,%r13
jb     1150 <write_all+0x40>
xor    %eax,%eax
jmp    1196 <write_all+0x86>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINTR = global i64 0
@errno = 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 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 = %37, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %41
14: ; preds = %10
br label %15
15: ; preds = %31, %14
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i64, i64* %8
%19 = getelementptr inbounds i8, i8* %17, i64 %18
%20 = load i64, i64* %7
%21 = load i64, i64* %8
%22 = sub i64 %20, %21
%23 = call i64 @write(i32 %16, i8* %19, i64 %22)
store i64 %23, i64* %9
br label %24
24: ; preds = %15
%25 = load i64, i64* %9
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i64, i64* @EINTR
%29 = load i64, i64* @errno
%30 = icmp eq i64 %28, %29
br label %31
31: ; preds = %27, %24
%32 = phi i1 [ false, %24 ], [ %30, %27 ]
br i1 %32, label %15, label %33
33: ; preds = %31
%34 = load i64, i64* %9
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 -1, i32* %4
br label %42
37: ; preds = %33
%38 = load i64, i64* %9
%39 = load i64, i64* %8
%40 = add i64 %39, %38
store i64 %40, i64* %8
br label %10
41: ; preds = %10
store i32 0, i32* %4
br label %42
42: ; preds = %41, %36
%43 = load i32, i32* %4
ret i32 %43
}

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