#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 /*<<< orphan*/ perror_wrapper(char *);
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) {
      perror_wrapper("write");
      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    11ba <write_all+0x9a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    118a <write_all+0x6a>
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <write_all+0x6a>
jmp    1139 <write_all+0x19>
cmpl   $0x0,-0x1c(%rbp)
jg     11ac <write_all+0x8c>
lea    0xe65(%rip),%rdi
call   1030 <perror_wrapper@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c1 <write_all+0xa1>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1139 <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    $0x1,%eax
test   %edx,%edx
jle    11ad <write_all+0x8d>
mov    %edi,%r13d
xor    %r14d,%r14d
mov    0x2e9c(%rip),%r15
mov    0x2e8d(%rip),%r12
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %edx,0xc(%rsp)
mov    %rsi,0x10(%rsp)
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    117d <write_all+0x5d>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1160 <write_all+0x40>
jmp    119f <write_all+0x7f>
test   %eax,%eax
jle    119f <write_all+0x7f>
add    %eax,%r14d
movslq %r14d,%rax
mov    0x10(%rsp),%rsi
lea    (%rsi,%rax,1),%rbp
mov    0xc(%rsp),%ebx
sub    %eax,%ebx
jg     1160 <write_all+0x40>
mov    $0x1,%eax
jmp    11ad <write_all+0x8d>
lea    0xe5a(%rip),%rdi
call   1030 <perror_wrapper@plt>
xor    %eax,%eax
add    $0x18,%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 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 = %36, %30, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %40
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 %36
34: ; preds = %31
%35 = call i32 @perror_wrapper(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %41
36: ; preds = %31
%37 = load i32, i32* %9
%38 = load i32, i32* %8
%39 = add nsw i32 %38, %37
store i32 %39, i32* %8
br label %10
40: ; preds = %10
store i32 1, i32* %4
br label %41
41: ; preds = %40, %34
%42 = load i32, i32* %4
ret i32 %42
}

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