#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 const *, int);
int erts_run_erl_write_all(int fd, const char *buf, int len) {
  int left = len;
  int written;
  for (;;) {
    do {
      written = write(fd, buf, left);
    } while (written < 0 && errno == EINTR);
    if (written == left) {
      return len;
    }
    if (written < 0) {
      return -1;
    }
    left -= written;
    buf += written;
  }
  return written;
}


x86 assembly (clang, -O0):
<erts_run_erl_write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    112d <erts_run_erl_write_all+0x1d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jge    1168 <erts_run_erl_write_all+0x58>
mov    0x2e8b(%rip),%rax
mov    (%rax),%rax
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    112d <erts_run_erl_write_all+0x1d>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    118a <erts_run_erl_write_all+0x7a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c1 <erts_run_erl_write_all+0xb1>
cmpl   $0x0,-0x1c(%rbp)
jge    11a0 <erts_run_erl_write_all+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <erts_run_erl_write_all+0xb1>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1128 <erts_run_erl_write_all+0x18>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<erts_run_erl_write_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2eb5(%rip),%r12
mov    0x2ea6(%rip),%r13
mov    %edx,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    115b <erts_run_erl_write_all+0x4b>
mov    0x0(%r13),%rcx
cmp    %rcx,(%r12)
je     1140 <erts_run_erl_write_all+0x30>
mov    %ebp,%ecx
sub    %eax,%ecx
je     116e <erts_run_erl_write_all+0x5e>
test   %eax,%eax
js     116e <erts_run_erl_write_all+0x5e>
mov    %eax,%eax
add    %rax,%rbx
mov    %ecx,%ebp
jmp    1140 <erts_run_erl_write_all+0x30>
cmp    %eax,%ebp
mov    $0xffffffff,%eax
cmove  %r14d,%eax
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
define i32 @erts_run_erl_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
%10 = load i32, i32* %7
store i32 %10, i32* %8
br label %11
11: ; preds = %36, %3
br label %12
12: ; preds = %24, %11
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = load i32, i32* %8
%16 = call i32 @write(i32 %13, i8* %14, i32 %15)
store i32 %16, i32* %9
br label %17
17: ; preds = %12
%18 = load i32, i32* %9
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br label %24
24: ; preds = %20, %17
%25 = phi i1 [ false, %17 ], [ %23, %20 ]
br i1 %25, label %12, label %26
26: ; preds = %24
%27 = load i32, i32* %9
%28 = load i32, i32* %8
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i32, i32* %7
store i32 %31, i32* %4
br label %44
32: ; preds = %26
%33 = load i32, i32* %9
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %32
store i32 -1, i32* %4
br label %44
36: ; preds = %32
%37 = load i32, i32* %9
%38 = load i32, i32* %8
%39 = sub nsw i32 %38, %37
store i32 %39, i32* %8
%40 = load i32, i32* %9
%41 = load i8*, i8** %6
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8* %43, i8** %6
br label %11
44: ; preds = %35, %30
%45 = load i32, i32* %4
ret i32 %45
}

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