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


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    11f0 <tp_send+0xc0>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1040 <send@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11e2 <tp_send+0xb2>
mov    0x2e57(%rip),%rax
mov    (%rax),%rax
mov    0x2e35(%rip),%rcx
cmp    (%rcx),%rax
jne    11b6 <tp_send+0x86>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
je     11ce <tp_send+0x9e>
lea    0xe43(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <tp_send+0xc6>
mov    $0x1,%edi
mov    $0x64,%esi
call   1030 <tp_suspend@plt>
jmp    11eb <tp_send+0xbb>
mov    -0x24(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    114d <tp_send+0x1d>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
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     11ac <tp_send+0x7c>
mov    %ecx,%r14d
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r12d
xor    %esi,%esi
mov    0x2e83(%rip),%r13
xor    %ebp,%ebp
jmp    116a <tp_send+0x3a>
nopl   0x0(%rax)
add    %eax,%ebp
movslq %ebp,%rsi
cmp    %rbx,%rsi
jae    11ae <tp_send+0x7e>
add    %r15,%rsi
mov    %r12d,%edi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1040 <send@plt>
test   %eax,%eax
jns    1160 <tp_send+0x30>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    0x2e38(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <tp_send+0x8f>
cmp    0x0(%r13),%rax
jne    11bf <tp_send+0x8f>
mov    $0x1,%edi
mov    $0x64,%esi
call   1030 <tp_suspend@plt>
jmp    1162 <tp_send+0x32>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3a(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11ae <tp_send+0x7e>

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 i32, i32* %10
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i64, i64* %8
%24 = load i32, i32* %9
%25 = call i32 @send(i32 %18, i8* %22, i64 %23, i32 %24)
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %40
28: ; preds = %17
%29 = load i64, i64* @errno
%30 = load i64, i64* @EAGAIN
%31 = icmp ne i64 %29, %30
br i1 %31, label %36, label %32
32: ; preds = %28
%33 = load i64, i64* @errno
%34 = load i64, i64* @EWOULDBLOCK
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %32, %28
%37 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %47
38: ; preds = %32
%39 = call i32 @tp_suspend(i32 1, i32 100)
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, %38
br label %12
45: ; preds = %12
%46 = load i32, i32* %10
store i32 %46, i32* %5
br label %47
47: ; preds = %45, %36
%48 = load i32, i32* %5
ret i32 %48
}

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 %29, label %6
6: ; preds = %4, %25
%7 = phi i64 [ %27, %25 ], [ 0, %4 ]
%8 = phi i32 [ %26, %25 ], [ 0, %4 ]
%9 = getelementptr inbounds i8, i8* %1, i64 %7
%10 = tail call i32 @send(i32 %0, i8* %9, i64 %2, i32 %3)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %6
%13 = load i64, i64* @errno
%14 = load i64, i64* @EAGAIN
%15 = icmp eq i64 %13, %14
%16 = load i64, i64* @EWOULDBLOCK
%17 = icmp eq i64 %13, %16
%18 = select i1 %15, i1 %17, i1 false
br i1 %18, label %21, label %19
19: ; preds = %12
%20 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %29
21: ; preds = %12
%22 = tail call i32 @tp_suspend(i32 1, i32 100)
br label %25
23: ; preds = %6
%24 = add nsw i32 %10, %8
br label %25
25: ; preds = %23, %21
%26 = phi i32 [ %8, %21 ], [ %24, %23 ]
%27 = sext i32 %26 to i64
%28 = icmp ult i64 %27, %2
br i1 %28, label %6, label %29
29: ; preds = %25, %4, %19
%30 = phi i32 [ -1, %19 ], [ 0, %4 ], [ %26, %25 ]
ret i32 %30
}