#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__ EINTR;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int);
int send(int, char *, int, int /*<<< orphan*/);
int bsend(int s, void *buf, int len) {
  int oldfl, ret, comp = len;
  oldfl = fcntl(s, F_GETFL, 0);
  if (oldfl & O_NONBLOCK)
    fcntl(s, F_SETFL, oldfl & (~O_NONBLOCK));
  while (comp) {
  bsend_restart:
    if ((ret = send(s, (char *)buf, comp, 0)) < 0) {
      if (errno == EINTR)
        goto bsend_restart;
      return (-1);
    }
    comp -= ret;
    buf = (char *)buf + ret;
  }
  return (len - comp);
}


x86 assembly (clang, -O0):
<bsend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2e90(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1185 <bsend+0x65>
mov    -0x8(%rbp),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
call   1030 <fcntl@plt>
jmp    118a <bsend+0x6a>
cmpl   $0x0,-0x20(%rbp)
je     1200 <bsend+0xe0>
jmp    1199 <bsend+0x79>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11e1 <bsend+0xc1>
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
jne    11d5 <bsend+0xb5>
jmp    1199 <bsend+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <bsend+0xe9>
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    118a <bsend+0x6a>
mov    -0x14(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bsend>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r12d
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2e9a(%rip),%rcx
mov    (%rcx),%ecx
test   %eax,%ecx
je     116b <bsend+0x4b>
mov    0x2e7d(%rip),%rdx
mov    (%rdx),%esi
not    %ecx
and    %ecx,%eax
mov    %r12d,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
test   %r14d,%r14d
je     11c2 <bsend+0xa2>
mov    0x2e59(%rip),%r15
mov    0x2e42(%rip),%r13
mov    %r14d,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
test   %eax,%eax
jns    11ae <bsend+0x8e>
mov    (%r15),%rax
cmp    0x0(%r13),%rax
je     1190 <bsend+0x70>
jmp    11bc <bsend+0x9c>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jne    1190 <bsend+0x70>
mov    %r14d,%r15d
jmp    11c2 <bsend+0xa2>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @bsend(i32 %0, i8* %1, i32 %2) {
%4 = load i32, i32* @F_GETFL
%5 = tail call i32 @fcntl(i32 %0, i32 %4, i32 0)
%6 = load i32, i32* @O_NONBLOCK
%7 = and i32 %6, %5
%8 = icmp eq i32 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %3
%10 = load i32, i32* @F_SETFL
%11 = xor i32 %6, -1
%12 = and i32 %5, %11
%13 = tail call i32 @fcntl(i32 %0, i32 %10, i32 %12)
br label %14
14: ; preds = %9, %3
%15 = icmp eq i32 %2, 0
br i1 %15, label %31, label %16
16: ; preds = %14, %26
%17 = phi i32 [ %27, %26 ], [ %2, %14 ]
%18 = phi i8* [ %29, %26 ], [ %1, %14 ]
br label %19
19: ; preds = %16, %22
%20 = tail call i32 @send(i32 %0, i8* %18, i32 %17, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %19, label %31
26: ; preds = %19
%27 = sub nsw i32 %17, %20
%28 = zext i32 %20 to i64
%29 = getelementptr inbounds i8, i8* %18, i64 %28
%30 = icmp eq i32 %27, 0
br i1 %30, label %31, label %16
31: ; preds = %26, %22, %14
%32 = phi i32 [ 0, %14 ], [ -1, %22 ], [ %2, %26 ]
ret i32 %32
}