#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
scalar_t__ write(int, char const *, size_t);
int write_all(int fd, const void *buf, size_t len) {
  size_t pos = 0;
  while (pos < len) {
    ssize_t sent = write(fd, (const char *)buf + pos, len - pos);
    if (sent < 0) {
      if (errno == EAGAIN || errno == EINTR)
        continue;
      fprintf(stderr,
              "remotethread"
              " WARNING: "
              "write() failed (%s)\n",
              strerror(errno));
      return -1;
    }
    pos += sent;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1203 <write_all+0xd3>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11f2 <write_all+0xc2>
mov    0x2e51(%rip),%rax
mov    (%rax),%rax
mov    0x2e2f(%rip),%rcx
cmp    (%rcx),%rax
je     11b4 <write_all+0x84>
mov    0x2e37(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
jne    11b9 <write_all+0x89>
jmp    114b <write_all+0x1b>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe1f(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <write_all+0xda>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    114b <write_all+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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     11bb <write_all+0x8b>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2e86(%rip),%r12
mov    0x2e67(%rip),%r13
jmp    1168 <write_all+0x38>
nopl   0x0(%rax,%rax,1)
add    %rax,%rbp
cmp    %rbx,%rbp
jae    11bb <write_all+0x8b>
lea    (%r14,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1030 <write@plt>
test   %rax,%rax
jns    1160 <write_all+0x30>
mov    (%r12),%rdi
cmp    0x0(%r13),%rdi
je     1163 <write_all+0x33>
mov    0x2e40(%rip),%rax
cmp    (%rax),%rdi
je     1163 <write_all+0x33>
mov    0x2e44(%rip),%rax
mov    (%rax),%ebx
call   1050 <strerror@plt>
lea    0xe56(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11bd <write_all+0x8d>
xor    %eax,%eax
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
@EINTR = global i64 0
@stderr = global i32 0
@.str = [43 x i8] c"remotethread WARNING: write() failed (%s)\0A\00"
define i32 @write_all(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %10
10: ; preds = %39, %33, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %43
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %8
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i64, i64* %7
%20 = load i64, i64* %8
%21 = sub i64 %19, %20
%22 = call i64 @write(i32 %15, i8* %18, i64 %21)
store i64 %22, i64* %9
%23 = load i64, i64* %9
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %39
25: ; preds = %14
%26 = load i64, i64* @errno
%27 = load i64, i64* @EAGAIN
%28 = icmp eq i64 %26, %27
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29, %25
br label %10
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = load i64, i64* @errno
%37 = call i8* @strerror(i64 %36)
%38 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %37)
store i32 -1, i32* %4
br label %44
39: ; preds = %14
%40 = load i64, i64* %9
%41 = load i64, i64* %8
%42 = add i64 %41, %40
store i64 %42, i64* %8
br label %10
43: ; preds = %10
store i32 0, i32* %4
br label %44
44: ; preds = %43, %34
%45 = load i32, i32* %4
ret i32 %45
}

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