#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 */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ easy_select_w(int, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int send(int, char *, size_t, int);
int /*<<< orphan*/ tp_suspend();
int tp_send(int sockfd, char *buf, size_t nbytes, int flags) {
  int bytes_sent = 0;
  while (bytes_sent < nbytes) {
    int bytes = send(sockfd, buf, nbytes, flags);
    tp_suspend();
    if (bytes < 0) {
      if (errno == EAGAIN || errno == EWOULDBLOCK)
        easy_select_w(sockfd, 0);
      else {
        perror("tp_send");
        return -1;
      }
    } else
      bytes_sent += bytes;
  }
}


x86 assembly (clang, -O0):
<tp_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1200 <tp_send+0xc0>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1040 <send@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <tp_suspend@plt>
cmpl   $0x0,-0x24(%rbp)
jge    11f2 <tp_send+0xb2>
mov    0x2e47(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
je     11c6 <tp_send+0x86>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
jne    11d5 <tp_send+0x95>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1050 <easy_select_w@plt>
jmp    11ed <tp_send+0xad>
lea    0xe24(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <tp_send+0xc0>
jmp    11fb <tp_send+0xbb>
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    115d <tp_send+0x1d>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tp_send>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11d3 <tp_send+0x93>
mov    %ecx,%r14d
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r12d
xor    %r13d,%r13d
jmp    117b <tp_send+0x3b>
cs nopw 0x0(%rax,%rax,1)
nop
add    %ebp,%r13d
movslq %r13d,%rax
cmp    %rbx,%rax
jae    11d3 <tp_send+0x93>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1040 <send@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <tp_suspend@plt>
test   %ebp,%ebp
jns    1170 <tp_send+0x30>
mov    0x2e40(%rip),%rax
mov    (%rax),%rax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
je     11bb <tp_send+0x7b>
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%rax
jne    11c7 <tp_send+0x87>
mov    %r12d,%edi
xor    %esi,%esi
call   1050 <easy_select_w@plt>
jmp    1173 <tp_send+0x33>
lea    0xe32(%rip),%rdi
call   1060 <perror@plt>
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
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [8 x i8] c"tp_send\00"
define i32 @tp_send(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
store i32 0, i32* %10
br label %12
12: ; preds = %44, %4
%13 = load i32, i32* %10
%14 = sext i32 %13 to i64
%15 = load i64, i64* %8
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %45
17: ; preds = %12
%18 = load i32, i32* %6
%19 = load i8*, i8** %7
%20 = load i64, i64* %8
%21 = load i32, i32* %9
%22 = call i32 @send(i32 %18, i8* %19, i64 %20, i32 %21)
store i32 %22, i32* %11
%23 = call i32 (...) @tp_suspend()
%24 = load i32, i32* %11
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %40
26: ; preds = %17
%27 = load i64, i64* @errno
%28 = load i64, i64* @EAGAIN
%29 = icmp eq i64 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i64, i64* @errno
%32 = load i64, i64* @EWOULDBLOCK
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %30, %26
%35 = load i32, i32* %6
%36 = call i32 @easy_select_w(i32 %35, i32 0)
br label %39
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %45
39: ; preds = %34
br label %44
40: ; preds = %17
%41 = load i32, i32* %11
%42 = load i32, i32* %10
%43 = add nsw i32 %42, %41
store i32 %43, i32* %10
br label %44
44: ; preds = %40, %39
br label %12
45: ; preds = %37, %12
%46 = load i32, i32* %5
ret i32 %46
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [8 x i8] c"tp_send\00"
define i32 @tp_send(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %28, label %6
6: ; preds = %4, %24
%7 = phi i32 [ %25, %24 ], [ 0, %4 ]
%8 = tail call i32 @send(i32 %0, i8* %1, i64 %2, i32 %3)
%9 = tail call i32 (...) @tp_suspend()
%10 = icmp slt i32 %8, 0
br i1 %10, label %11, label %22
11: ; preds = %6
%12 = load i64, i64* @errno
%13 = load i64, i64* @EAGAIN
%14 = icmp eq i64 %12, %13
%15 = load i64, i64* @EWOULDBLOCK
%16 = icmp eq i64 %12, %15
%17 = select i1 %14, i1 true, i1 %16
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = tail call i32 @easy_select_w(i32 %0, i32 0)
br label %24
20: ; preds = %11
%21 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %28
22: ; preds = %6
%23 = add nsw i32 %8, %7
br label %24
24: ; preds = %18, %22
%25 = phi i32 [ %7, %18 ], [ %23, %22 ]
%26 = sext i32 %25 to i64
%27 = icmp ult i64 %26, %2
br i1 %27, label %6, label %28
28: ; preds = %24, %4, %20
ret i32 -1
}