#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 */

/* Variables and functions */
int send(int, char *, int, int /*<<< orphan*/);
int jk_tcp_socket_sendfull(int sd, const unsigned char *b, int len) {
  int sent = 0;
  while (sent < len) {
    int this_time = send(sd, (char *)b + sent, len - sent, 0);
    if (0 == this_time) {
      return -2;
    }
    if (this_time < 0) {
      return -3;
    }
    sent += this_time;
  }
  return sent;
}


x86 assembly (clang, -O0):
<jk_tcp_socket_sendfull>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    118e <jk_tcp_socket_sendfull+0x7e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x1c(%rbp),%eax
jne    116a <jk_tcp_socket_sendfull+0x5a>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1194 <jk_tcp_socket_sendfull+0x84>
cmpl   $0x0,-0x1c(%rbp)
jge    1180 <jk_tcp_socket_sendfull+0x70>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1194 <jk_tcp_socket_sendfull+0x84>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1129 <jk_tcp_socket_sendfull+0x19>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<jk_tcp_socket_sendfull>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1153 <jk_tcp_socket_sendfull+0x43>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r15d,%edi
xor    %ecx,%ecx
call   1030 <send@plt>
test   %eax,%eax
je     1157 <jk_tcp_socket_sendfull+0x47>
js     115e <jk_tcp_socket_sendfull+0x4e>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1130 <jk_tcp_socket_sendfull+0x20>
jmp    1163 <jk_tcp_socket_sendfull+0x53>
xor    %ebx,%ebx
jmp    1163 <jk_tcp_socket_sendfull+0x53>
mov    $0xfffffffe,%ebx
jmp    1163 <jk_tcp_socket_sendfull+0x53>
mov    $0xfffffffd,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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