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

/* Variables and functions */
scalar_t__ errno;
int send(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int send_all(int sock, uint8_t *buf, int len) {
  errno = 0;
  int sent = 0;
  while (sent < len) {
    errno = 0;
    int ret = send(sock, buf + sent, len - sent, 0);
    if (ret < 0)
      return ret;
    if (ret == 0)
      return -1;
    sent += ret;
  }
  return sent;
}


x86 assembly (clang, -O0):
<send_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2eb7(%rip),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ac <send_all+0x9c>
mov    0x2e96(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1188 <send_all+0x78>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b2 <send_all+0xa2>
cmpl   $0x0,-0x1c(%rbp)
jne    119e <send_all+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b2 <send_all+0xa2>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1137 <send_all+0x27>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_all>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2ec1(%rip),%r12
movq   $0x0,(%r12)
test   %edx,%edx
jle    116b <send_all+0x5b>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movq   $0x0,(%r12)
movslq %ebx,%rax
lea    (%r14,%rax,4),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
call   1030 <send@plt>
test   %eax,%eax
js     116f <send_all+0x5f>
je     1173 <send_all+0x63>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1140 <send_all+0x30>
jmp    1178 <send_all+0x68>
xor    %ebx,%ebx
jmp    1178 <send_all+0x68>
mov    %eax,%ebx
jmp    1178 <send_all+0x68>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
define i32 @send_all(i32 %0, i32* %1, i32 %2) {
store i64 0, i64* @errno
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %17
5: ; preds = %3, %14
%6 = phi i32 [ %15, %14 ], [ 0, %3 ]
store i64 0, i64* @errno
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i32, i32* %1, i64 %7
%9 = sub nsw i32 %2, %6
%10 = tail call i32 @send(i32 %0, i32* %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %5
%13 = icmp eq i32 %10, 0
br i1 %13, label %17, label %14
14: ; preds = %12
%15 = add nsw i32 %10, %6
%16 = icmp slt i32 %15, %2
br i1 %16, label %5, label %17
17: ; preds = %14, %5, %12, %3
%18 = phi i32 [ 0, %3 ], [ -1, %12 ], [ %10, %5 ], [ %15, %14 ]
ret i32 %18
}