#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 */
int /*<<< orphan*/ MSG_NOSIGNAL;
int errno;
int send(int, char const *, size_t, int /*<<< orphan*/);
ssize_t tcp_send_bytes(int sock, const char *buf, size_t len) {
  size_t bsent;
  ssize_t rv;
  bsent = 0;
  while (bsent < len) {
    rv = send(sock, buf + bsent, len - bsent, MSG_NOSIGNAL);
    if (rv == -1)
      return -errno;
    bsent += rv;
  }
  return bsent;
}


x86 assembly (clang, -O0):
<tcp_send_bytes>:
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    118b <tcp_send_bytes+0x7b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
mov    0x2e85(%rip),%rax
mov    (%rax),%ecx
call   1030 <send@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    117a <tcp_send_bytes+0x6a>
xor    %eax,%eax
mov    0x2e70(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1192 <tcp_send_bytes+0x82>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    112b <tcp_send_bytes+0x1b>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_send_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     1156 <tcp_send_bytes+0x46>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
mov    0x2eaa(%rip),%r12
xchg   %ax,%ax
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    (%r12),%ecx
mov    %ebp,%edi
call   1030 <send@plt>
cmp    $0xffffffff,%eax
je     115a <tcp_send_bytes+0x4a>
cltq
add    %rax,%rbx
cmp    %r14,%rbx
jb     1130 <tcp_send_bytes+0x20>
jmp    1165 <tcp_send_bytes+0x55>
xor    %ebx,%ebx
jmp    1165 <tcp_send_bytes+0x55>
mov    0x2e7f(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MSG_NOSIGNAL = global i32 0
@errno = global i32 0
define i32 @tcp_send_bytes(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
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 = %29, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %34
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 = load i32, i32* @MSG_NOSIGNAL
%23 = call i32 @send(i32 %15, i8* %18, i64 %21, i32 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %14
%27 = load i32, i32* @errno
%28 = sub nsw i32 0, %27
store i32 %28, i32* %4
br label %37
29: ; preds = %14
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = load i64, i64* %8
%33 = add i64 %32, %31
store i64 %33, i64* %8
br label %10
34: ; preds = %10
%35 = load i64, i64* %8
%36 = trunc i64 %35 to i32
store i32 %36, i32* %4
br label %37
37: ; preds = %34, %26
%38 = load i32, i32* %4
ret i32 %38
}

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