#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 */
#define EAGAIN 129
#define EINTR 128
int /*<<< orphan*/ MSG_NOSIGNAL;
int 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 {
      switch (errno) {
      case EAGAIN:
        return totalnum;
      case EINTR:
        continue;
      default:
        return -1;
      }
    }
  }
  return totalnum;
}


x86 assembly (clang, -O0):
<fnet_send_safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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    11ea <fnet_send_safe+0xda>
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    0x2e79(%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    11f0 <fnet_send_safe+0xe0>
jmp    11e5 <fnet_send_safe+0xd5>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x80,%eax
je     11d4 <fnet_send_safe+0xc4>
jmp    11b6 <fnet_send_safe+0xa6>
mov    -0x24(%rbp),%eax
sub    $0x81,%eax
jne    11d9 <fnet_send_safe+0xc9>
jmp    11c9 <fnet_send_safe+0xb9>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f0 <fnet_send_safe+0xe0>
jmp    1131 <fnet_send_safe+0x21>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <fnet_send_safe+0xe0>
jmp    1131 <fnet_send_safe+0x21>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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    %r12d,%r12d
mov    0x2ead(%rip),%r15
mov    0x2eae(%rip),%r14
mov    %rdx,0x10(%rsp)
mov    %rsi,0x8(%rsp)
movslq %r12d,%rbx
mov    %rdx,%rbp
sub    %rbx,%rbp
jbe    11a4 <fnet_send_safe+0x94>
add    %rsi,%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r15),%ecx
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <send@plt>
cmp    $0xffffffff,%eax
jne    1180 <fnet_send_safe+0x70>
mov    (%r14),%eax
cmp    $0x80,%eax
je     1150 <fnet_send_safe+0x40>
jmp    1197 <fnet_send_safe+0x87>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    %eax,%r12d
movslq %r12d,%rax
mov    0x10(%rsp),%rdx
cmp    %rdx,%rax
mov    0x8(%rsp),%rsi
jne    113c <fnet_send_safe+0x2c>
jmp    11a4 <fnet_send_safe+0x94>
cmp    $0x81,%eax
je     11a4 <fnet_send_safe+0x94>
mov    $0xffffffff,%r12d
mov    %r12d,%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 i32 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 = %46, %44, %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 %47
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 %49
39: ; preds = %29
br label %46
40: ; preds = %15
%41 = load i32, i32* @errno
switch i32 %41, label %45 [
i32 129, label %42
i32 128, label %44
]
42: ; preds = %40
%43 = load i32, i32* %8
store i32 %43, i32* %4
br label %49
44: ; preds = %40
br label %10
45: ; preds = %40
store i32 -1, i32* %4
br label %49
46: ; preds = %39
br label %10
47: ; preds = %10
%48 = load i32, i32* %8
store i32 %48, i32* %4
br label %49
49: ; preds = %47, %45, %42, %37
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@MSG_NOSIGNAL = global i32 0
@errno = global i32 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 %21
10: ; preds = %4, %18
%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 %21, label %4
18: ; preds = %10
%19 = load i32, i32* @errno
switch i32 %19, label %21 [
i32 129, label %20
i32 128, label %10
]
20: ; preds = %18
br label %21
21: ; preds = %4, %14, %18, %20
%22 = phi i32 [ %5, %20 ], [ -1, %18 ], [ %5, %4 ], [ %15, %14 ]
ret i32 %22
}