#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;
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int, char *, scalar_t__);
ssize_t write_all(int fd, char *buf, ssize_t nr) {
  ssize_t r, w = 0;
  do {
    r = write(fd, buf + w, nr - w);
    if (r < 0 && errno != EINTR) {
      perror("write");
      return -1;
    }
    w += r;
  } while (w < nr);
  return nr;
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1195 <write_all+0x75>
mov    0x2e77(%rip),%rax
mov    (%rax),%rax
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%rax
je     1195 <write_all+0x75>
lea    0xe7d(%rip),%rdi
call   1040 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11b7 <write_all+0x97>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jl     113b <write_all+0x1b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2ea3(%rip),%r12
mov    0x2e94(%rip),%r13
jmp    1158 <write_all+0x38>
cs nopw 0x0(%rax,%rax,1)
add    %rax,%rbp
cmp    %rbx,%rbp
jge    118c <write_all+0x6c>
lea    (%r14,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
test   %rax,%rax
jns    1150 <write_all+0x30>
mov    (%r12),%rcx
cmp    0x0(%r13),%rcx
je     1150 <write_all+0x30>
lea    0xe80(%rip),%rdi
call   1040 <perror@plt>
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
@.str = [6 x i8] c"write\00"
define i64 @write_all(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* %9
br label %10
10: ; preds = %31, %3
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i64, i64* %9
%14 = getelementptr inbounds i8, i8* %12, i64 %13
%15 = load i64, i64* %7
%16 = load i64, i64* %9
%17 = sub nsw i64 %15, %16
%18 = call i64 @write(i32 %11, i8* %14, i64 %17)
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %10
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %4
br label %37
27: ; preds = %21, %10
%28 = load i64, i64* %8
%29 = load i64, i64* %9
%30 = add nsw i64 %29, %28
store i64 %30, i64* %9
br label %31
31: ; preds = %27
%32 = load i64, i64* %9
%33 = load i64, i64* %7
%34 = icmp slt i64 %32, %33
br i1 %34, label %10, label %35
35: ; preds = %31
%36 = load i64, i64* %7
store i64 %36, i64* %4
br label %37
37: ; preds = %35, %25
%38 = load i64, i64* %4
ret i64 %38
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [6 x i8] c"write\00"
define i64 @write_all(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 nsw i64 %2, %5
%8 = tail call i64 @write(i32 %0, i8* %6, i64 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %16
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 %14
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %19
16: ; preds = %10, %4
%17 = add nsw i64 %8, %5
%18 = icmp slt i64 %17, %2
br i1 %18, label %4, label %19
19: ; preds = %16, %14
%20 = phi i64 [ -1, %14 ], [ %2, %16 ]
ret i64 %20
}