#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 /*<<< orphan*/ uint8_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ errno;
scalar_t__ write(int const, int /*<<< orphan*/ const *const, size_t const);
ssize_t write_all(const int fd, const void *const buf_ptr, const size_t len) {
  const uint8_t *const buf = buf_ptr;
  size_t offset = 0;
  while (offset < len) {
    const ssize_t rc = write(fd, buf + offset, len - offset);
    if (rc < 0) {
      if (errno == EAGAIN)
        continue;
      return -1;
    }
    if (rc == 0)
      return -1;
    offset += rc;
  }
  return len;
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11c4 <write_all+0xb4>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    119b <write_all+0x8b>
mov    0x2e6a(%rip),%rax
mov    (%rax),%rax
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%rax
jne    118e <write_all+0x7e>
jmp    1133 <write_all+0x23>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11cc <write_all+0xbc>
cmpq   $0x0,-0x38(%rbp)
jne    11b3 <write_all+0xa3>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11cc <write_all+0xbc>
mov    -0x38(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1133 <write_all+0x23>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_all>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1172 <write_all+0x62>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2eae(%rip),%r12
mov    0x2e97(%rip),%r13
jmp    114f <write_all+0x3f>
nopl   0x0(%rax,%rax,1)
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    1176 <write_all+0x66>
cmp    %rbx,%rbp
jae    117d <write_all+0x6d>
lea    (%r14,%rbp,4),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
test   %rax,%rax
js     1140 <write_all+0x30>
je     1176 <write_all+0x66>
add    %rax,%rbp
cmp    %rbx,%rbp
jb     114f <write_all+0x3f>
jmp    117d <write_all+0x6d>
xor    %ebx,%ebx
jmp    117d <write_all+0x6d>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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