#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 */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ perror(char *);
int write(int, char const *, size_t);
void write_all(int fd, void *buf, size_t size) {
  size_t left = size;
  const char *p = buf;
  while (1) {
    if (left == 0)
      break;
    ssize_t n = write(fd, p, left);
    switch (n) {
    case -1:
      if (errno == EINTR)
        continue;
      else {
        perror("write");
        return;
      }
    default:
      p += n;
      left -= n;
    }
  }
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1153 <write_all+0x33>
jmp    11ca <write_all+0xaa>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
sub    $0xffffffff,%eax
jne    11a5 <write_all+0x85>
jmp    1177 <write_all+0x57>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    0x2e51(%rip),%rcx
cmp    (%rcx),%eax
jne    1194 <write_all+0x74>
jmp    1143 <write_all+0x23>
lea    0xe65(%rip),%rdi
call   1040 <perror@plt>
jmp    11ca <write_all+0xaa>
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
movslq -0x2c(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1143 <write_all+0x23>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     118c <write_all+0x6c>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2ea4(%rip),%r15
mov    0x2e95(%rip),%r12
jmp    115d <write_all+0x3d>
cs nopw 0x0(%rax,%rax,1)
nop
cltq
add    %rax,%r14
sub    %rax,%rbx
test   %rbx,%rbx
je     118c <write_all+0x6c>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1150 <write_all+0x30>
mov    (%r15),%eax
cmp    (%r12),%eax
je     1158 <write_all+0x38>
lea    0xe81(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <perror@plt>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EINTR = global i32 0
@.str = [6 x i8] c"write\00"
define void @write_all(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i64, i64* %6
store i64 %10, i64* %7
%11 = load i8*, i8** %5
store i8* %11, i8** %8
br label %12
12: ; preds = %3, %26, %38
%13 = load i64, i64* %7
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12
br label %39
16: ; preds = %12
%17 = load i32, i32* %4
%18 = load i8*, i8** %8
%19 = load i64, i64* %7
%20 = call i32 @write(i32 %17, i8* %18, i64 %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
switch i32 %21, label %29 [
i32 -1, label %22
]
22: ; preds = %16
%23 = load i32, i32* @errno
%24 = load i32, i32* @EINTR
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
br label %12
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %39
29: ; preds = %16
%30 = load i32, i32* %9
%31 = load i8*, i8** %8
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8* %33, i8** %8
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = load i64, i64* %7
%37 = sub i64 %36, %35
store i64 %37, i64* %7
br label %38
38: ; preds = %29
br label %12
39: ; preds = %27, %15
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@EINTR = global i32 0
@.str = [6 x i8] c"write\00"
define void @write_all(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %24, label %5
5: ; preds = %3, %20
%6 = phi i64 [ %22, %20 ], [ %2, %3 ]
%7 = phi i8* [ %21, %20 ], [ %1, %3 ]
%8 = tail call i32 @write(i32 %0, i8* %7, i64 %6)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %5
%11 = load i32, i32* @errno
%12 = load i32, i32* @EINTR
%13 = icmp eq i32 %11, %12
br i1 %13, label %20, 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 %24
16: ; preds = %5
%17 = sext i32 %8 to i64
%18 = getelementptr inbounds i8, i8* %7, i64 %17
%19 = sub i64 %6, %17
br label %20
20: ; preds = %10, %16
%21 = phi i8* [ %18, %16 ], [ %7, %10 ]
%22 = phi i64 [ %19, %16 ], [ %6, %10 ]
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %5
24: ; preds = %20, %3, %14
ret void
}