#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 */

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int write(int, char *, int);
int write_all(int fd, void *buf, int size) {
  int written = 0;
  int ret;
  while (written < size) {
    ret = write(fd, (char *)buf + written, size - written);
    if (ret == -1 && errno == EINTR)
      continue;
    if (ret <= 0) {
      return 0;
    }
    written += ret;
  }
  return 1;
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119e <write_all+0x8e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    117a <write_all+0x6a>
mov    0x2e7e(%rip),%rax
mov    (%rax),%rax
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%rax
jne    117a <write_all+0x6a>
jmp    1129 <write_all+0x19>
cmpl   $0x0,-0x1c(%rbp)
jg     1190 <write_all+0x80>
movl   $0x0,-0x4(%rbp)
jmp    11a5 <write_all+0x95>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <write_all+0x19>
movl   $0x1,-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
sub    $0x18,%rsp
mov    %edi,%r12d
xor    %r13d,%r13d
mov    0x2eb5(%rip),%r14
mov    0x2ea6(%rip),%r15
mov    %edx,0xc(%rsp)
mov    %rsi,0x10(%rsp)
mov    %edx,%ebx
sub    %r13d,%ebx
jle    1191 <write_all+0x81>
movslq %r13d,%rbp
add    %rsi,%rbp
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1170 <write_all+0x60>
mov    (%r14),%rax
cmp    (%r15),%rax
je     1150 <write_all+0x40>
jmp    1180 <write_all+0x70>
nopl   0x0(%rax)
add    %eax,%r13d
test   %eax,%eax
mov    0xc(%rsp),%edx
mov    0x10(%rsp),%rsi
jg     113b <write_all+0x2b>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%eax
jmp    1182 <write_all+0x72>

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

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