#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 */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ MSG_NOSIGNAL;
scalar_t__ errno;
int send(int, char *, size_t, int /*<<< orphan*/);
ssize_t fnet_send_safe(int fd, const void *data, size_t len) {
  ssize_t totalnum = 0;
  ssize_t sendnum = 0;
  while ((size_t)totalnum < len) {
    sendnum =
        send(fd, (char *)data + totalnum, len - (size_t)totalnum, MSG_NOSIGNAL);
    if (sendnum != -1) {
      totalnum += sendnum;
      if ((size_t)totalnum == len) {
        return totalnum;
      }
    } else {
      if (errno == EAGAIN) {
        return totalnum;
      } else if (errno == EINTR || errno == EWOULDBLOCK) {
        continue;
      } else {
        return -1;
      }
    }
  }
  return totalnum;
}


x86 assembly (clang, -O0):
<fnet_send_safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1209 <fnet_send_safe+0xf9>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
movslq -0x1c(%rbp),%rax
sub    %rax,%rdx
mov    0x2e69(%rip),%rax
mov    (%rax),%ecx
call   1030 <send@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
je     119a <fnet_send_safe+0x8a>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1195 <fnet_send_safe+0x85>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120f <fnet_send_safe+0xff>
jmp    1204 <fnet_send_safe+0xf4>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <fnet_send_safe+0xaf>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120f <fnet_send_safe+0xff>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rax
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%rax
je     11f3 <fnet_send_safe+0xe3>
mov    0x2e00(%rip),%rax
mov    (%rax),%rax
mov    0x2dee(%rip),%rcx
cmp    (%rcx),%rax
jne    11f8 <fnet_send_safe+0xe8>
jmp    1131 <fnet_send_safe+0x21>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <fnet_send_safe+0xff>
jmp    1131 <fnet_send_safe+0x21>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fnet_send_safe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r13d
xor    %ecx,%ecx
mov    0x2e9e(%rip),%r15
mov    0x2eaf(%rip),%r14
mov    0x2e80(%rip),%r12
mov    %rdx,0x10(%rsp)
mov    %rsi,0x8(%rsp)
mov    %ecx,0x4(%rsp)
movslq %ecx,%rbx
mov    %rdx,%rbp
sub    %rbx,%rbp
jbe    11ba <fnet_send_safe+0xaa>
add    %rsi,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15),%ecx
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <send@plt>
cmp    $0xffffffff,%eax
jne    11a0 <fnet_send_safe+0x90>
mov    (%r14),%rax
cmp    (%r12),%rax
je     11ba <fnet_send_safe+0xaa>
mov    0x2e4a(%rip),%rcx
cmp    (%rcx),%rax
je     1160 <fnet_send_safe+0x50>
mov    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
je     1160 <fnet_send_safe+0x50>
jmp    11c0 <fnet_send_safe+0xb0>
nopl   0x0(%rax)
mov    0x4(%rsp),%ecx
add    %eax,%ecx
movslq %ecx,%rax
mov    0x10(%rsp),%rdx
cmp    %rdx,%rax
mov    0x8(%rsp),%rsi
jne    1142 <fnet_send_safe+0x32>
jmp    11c5 <fnet_send_safe+0xb5>
mov    0x4(%rsp),%ecx
jmp    11c5 <fnet_send_safe+0xb5>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MSG_NOSIGNAL = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @fnet_send_safe(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
store i32 0, i32* %9
br label %10
10: ; preds = %56, %54, %3
%11 = load i32, i32* %8
%12 = sext i32 %11 to i64
%13 = load i64, i64* %7
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %57
15: ; preds = %10
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i64, i64* %7
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = sub i64 %21, %23
%25 = load i32, i32* @MSG_NOSIGNAL
%26 = call i32 @send(i32 %16, i8* %20, i64 %24, i32 %25)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %40
29: ; preds = %15
%30 = load i32, i32* %9
%31 = load i32, i32* %8
%32 = add nsw i32 %31, %30
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = load i64, i64* %7
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = load i32, i32* %8
store i32 %38, i32* %4
br label %59
39: ; preds = %29
br label %56
40: ; preds = %15
%41 = load i64, i64* @errno
%42 = load i64, i64* @EAGAIN
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = load i32, i32* %8
store i32 %45, i32* %4
br label %59
46: ; preds = %40
%47 = load i64, i64* @errno
%48 = load i64, i64* @EINTR
%49 = icmp eq i64 %47, %48
br i1 %49, label %54, label %50
50: ; preds = %46
%51 = load i64, i64* @errno
%52 = load i64, i64* @EWOULDBLOCK
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %50, %46
br label %10
55: ; preds = %50
store i32 -1, i32* %4
br label %59
56: ; preds = %39
br label %10
57: ; preds = %10
%58 = load i32, i32* %8
store i32 %58, i32* %4
br label %59
59: ; preds = %57, %55, %44, %37
%60 = load i32, i32* %4
ret i32 %60
}

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