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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
int send(int, void const *, size_t, int /*<<< orphan*/);
int writeallfd(int sock, const void *ptr, size_t len) {
  ssize_t res;
  while (len) {
    res = send(sock, ptr, len, 0);
    if (res == -1) {
      ;
      if (errno == EINTR || errno == EAGAIN) {
        ;
        continue;
      }
      return -1;
    }
    len -= res;
    ptr += res;
  }
  return 0;
}


x86 assembly (clang, -O0):
<writeallfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b7 <writeallfd+0xa7>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1192 <writeallfd+0x82>
mov    0x2e8c(%rip),%rax
mov    (%rax),%rax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%rax
je     1181 <writeallfd+0x71>
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%rax
jne    1186 <writeallfd+0x76>
jmp    1123 <writeallfd+0x13>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <writeallfd+0xae>
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1123 <writeallfd+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeallfd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     116f <writeallfd+0x5f>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2eb1(%rip),%r15
mov    0x2ea2(%rip),%r12
mov    0x2e8b(%rip),%r13
nopl   (%rax)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1030 <send@plt>
cmp    $0xffffffff,%eax
jne    1165 <writeallfd+0x55>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1140 <writeallfd+0x30>
cmp    0x0(%r13),%rax
je     1140 <writeallfd+0x30>
jmp    1173 <writeallfd+0x63>
cltq
add    %rax,%r14
sub    %rax,%rbx
jne    1140 <writeallfd+0x30>
xor    %eax,%eax
jmp    1178 <writeallfd+0x68>
mov    $0xffffffff,%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
@EAGAIN = global i64 0
define i32 @writeallfd(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
br label %9
9: ; preds = %29, %27, %3
%10 = load i64, i64* %7
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %38
12: ; preds = %9
%13 = load i32, i32* %5
%14 = load i8*, i8** %6
%15 = load i64, i64* %7
%16 = call i32 @send(i32 %13, i8* %14, i64 %15, i32 0)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %29
19: ; preds = %12
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i64, i64* @errno
%25 = load i64, i64* @EAGAIN
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23, %19
br label %9
28: ; preds = %23
store i32 -1, i32* %4
br label %39
29: ; preds = %12
%30 = load i32, i32* %8
%31 = sext i32 %30 to i64
%32 = load i64, i64* %7
%33 = sub i64 %32, %31
store i64 %33, i64* %7
%34 = load i32, i32* %8
%35 = load i8*, i8** %6
%36 = sext i32 %34 to i64
%37 = getelementptr i8, i8* %35, i64 %36
store i8* %37, i8** %6
br label %9
38: ; preds = %9
store i32 0, i32* %4
br label %39
39: ; preds = %38, %28
%40 = load i32, i32* %4
ret i32 %40
}

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