#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 /*<<< orphan*/ printf(char *, int);
int write(int, void const *, size_t);
ssize_t write_all(int fd, const void *buf, size_t count) {
  printf("write all %d\n", fd);
  while (write(fd, buf, count) == -1) {
    if (errno == EINTR) {
      continue;
    } else {
      return -1;
    }
  }
  return count;
}


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)
mov    -0x8(%rbp),%esi
lea    0xec3(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1186 <write_all+0x66>
mov    0x2e7e(%rip),%rax
mov    (%rax),%rax
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%rax
jne    117a <write_all+0x5a>
jmp    1142 <write_all+0x22>
movl   $0xffffffff,-0x4(%rbp)
jmp    118d <write_all+0x6d>
mov    -0x18(%rbp),%rax
mov    %eax,-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   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xec9(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    0x2e9b(%rip),%r15
mov    0x2e8c(%rip),%r12
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1171 <write_all+0x51>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1150 <write_all+0x30>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"write all %d\0A\00"
@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
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%8 = load i32, i32* %5
%9 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %20, %3
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i64, i64* %7
%14 = call i32 @write(i32 %11, i8* %12, i64 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %10
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16
br label %10
21: ; preds = %16
store i32 -1, i32* %4
br label %25
22: ; preds = %10
%23 = load i64, i64* %7
%24 = trunc i64 %23 to i32
store i32 %24, i32* %4
br label %25
25: ; preds = %22, %21
%26 = load i32, i32* %4
ret i32 %26
}

LLVM IR (-O2):
@.str = [14 x i8] c"write all %d\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @write_all(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %5
5: ; preds = %8, %3
%6 = tail call i32 @write(i32 %0, i8* %1, i64 %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %5
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
br i1 %11, label %5, label %14
12: ; preds = %5
%13 = trunc i64 %2 to i32
br label %14
14: ; preds = %8, %12
%15 = phi i32 [ %13, %12 ], [ -1, %8 ]
ret i32 %15
}