#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 /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int writen_nonblock(int fd, char *bp, size_t len) {
  int cnt;
  int rc;
  fd_set wfds;
  cnt = len;
  while (cnt > 0) {
    rc = send(fd, bp, cnt, 0);
    if (rc < 0) {
      if (errno == EINTR)
        continue;
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
        FD_ZERO(&wfds);
        FD_SET(fd, &wfds);
        if (select(fd + 1, NULL, &wfds, NULL, NULL) < 0) {
          fprintf(stderr, "writen_nonblock select error:%m\n");
          return -1;
        }
        continue;
      }
      return -1;
    } else if (rc == 0)
      return len - cnt;
    cnt -= rc;
    bp += rc;
  }
  return len;
}


x86 assembly (clang, -O0):
<writen_nonblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1293 <writen_nonblock+0x143>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1250 <writen_nonblock+0x100>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
jne    11b1 <writen_nonblock+0x61>
jmp    116a <writen_nonblock+0x1a>
mov    0x2e20(%rip),%rax
mov    (%rax),%rax
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%rax
je     11e5 <writen_nonblock+0x95>
mov    0x2e06(%rip),%rax
mov    (%rax),%rax
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%rax
jne    1244 <writen_nonblock+0xf4>
lea    -0x24(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x24(%rbp),%rdx
mov    %r8,%rsi
mov    %r8,%rcx
call   1060 <select@plt>
cmp    $0x0,%rax
jge    123f <writen_nonblock+0xef>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129a <writen_nonblock+0x14a>
jmp    116a <writen_nonblock+0x1a>
movl   $0xffffffff,-0x4(%rbp)
jmp    129a <writen_nonblock+0x14a>
cmpl   $0x0,-0x20(%rbp)
jne    126d <writen_nonblock+0x11d>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    129a <writen_nonblock+0x14a>
jmp    1272 <writen_nonblock+0x122>
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    116a <writen_nonblock+0x1a>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writen_nonblock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edx,%edx
jle    120c <writen_nonblock+0xbc>
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0x1(%r15),%eax
mov    %eax,0x4(%rsp)
mov    0x2e4d(%rip),%r12
lea    0x14(%rsp),%r13
mov    %rdx,0x8(%rsp)
mov    %edx,%ebp
mov    $0xffffffff,%r14d
jmp    11bb <writen_nonblock+0x6b>
nop
mov    %r13,%rdi
call   1050 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r13,%rsi
call   1070 <FD_SET@plt>
mov    0x4(%rsp),%edi
xor    %esi,%esi
mov    %r13,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <select@plt>
test   %rax,%rax
js     1211 <writen_nonblock+0xc1>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
test   %eax,%eax
jns    11f8 <writen_nonblock+0xa8>
mov    0x2e03(%rip),%rax
mov    (%rax),%rax
cmp    (%r12),%rax
je     11bb <writen_nonblock+0x6b>
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
je     1190 <writen_nonblock+0x40>
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%rax
je     1190 <writen_nonblock+0x40>
jmp    1232 <writen_nonblock+0xe2>
test   %eax,%eax
je     1228 <writen_nonblock+0xd8>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     11bb <writen_nonblock+0x6b>
mov    0x8(%rsp),%rax
jmp    122f <writen_nonblock+0xdf>
mov    %edx,%r14d
jmp    1232 <writen_nonblock+0xe2>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1232 <writen_nonblock+0xe2>
mov    0x8(%rsp),%rax
sub    %ebp,%eax
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str = [33 x i8] c"writen_nonblock select error:%m\0A\00"
define i32 @writen_nonblock(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i64, i64* %7
%12 = trunc i64 %11 to i32
store i32 %12, i32* %8
br label %13
13: ; preds = %59, %47, %27, %3
%14 = load i32, i32* %8
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %67
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %6
%19 = load i32, i32* %8
%20 = call i32 @send(i32 %17, i8* %18, i32 %19, i32 0)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %49
23: ; preds = %16
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
br label %13
28: ; preds = %23
%29 = load i64, i64* @errno
%30 = load i64, i64* @EAGAIN
%31 = icmp eq 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 eq i64 %33, %34
br i1 %35, label %36, label %48
36: ; preds = %32, %28
%37 = call i32 @FD_ZERO(i32* %10)
%38 = load i32, i32* %5
%39 = call i32 @FD_SET(i32 %38, i32* %10)
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
%42 = call i64 @select(i32 %41, i32* null, i32* %10, i32* null, i32* null)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %36
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %70
47: ; preds = %36
br label %13
48: ; preds = %32
store i32 -1, i32* %4
br label %70
49: ; preds = %16
%50 = load i32, i32* %9
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %49
%53 = load i64, i64* %7
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = sub i64 %53, %55
%57 = trunc i64 %56 to i32
store i32 %57, i32* %4
br label %70
58: ; preds = %49
br label %59
59: ; preds = %58
%60 = load i32, i32* %9
%61 = load i32, i32* %8
%62 = sub nsw i32 %61, %60
store i32 %62, i32* %8
%63 = load i32, i32* %9
%64 = load i8*, i8** %6
%65 = sext i32 %63 to i64
%66 = getelementptr inbounds i8, i8* %64, i64 %65
store i8* %66, i8** %6
br label %13
67: ; preds = %13
%68 = load i64, i64* %7
%69 = trunc i64 %68 to i32
store i32 %69, i32* %4
br label %70
70: ; preds = %67, %52, %48, %44
%71 = load i32, i32* %4
ret i32 %71
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str = [33 x i8] c"writen_nonblock select error:%m\0A\00"
define i32 @writen_nonblock(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = trunc i64 %2 to i32
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %46
8: ; preds = %3
%9 = add nsw i32 %0, 1
br label %10
10: ; preds = %8, %41
%11 = phi i32 [ %6, %8 ], [ %42, %41 ]
%12 = phi i8* [ %1, %8 ], [ %44, %41 ]
%13 = call i32 @send(i32 %0, i8* %12, i32 %11, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %36
15: ; preds = %10, %19
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %15, %28
%20 = call i32 @send(i32 %0, i8* %12, i32 %11, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %15, label %36
22: ; preds = %15
%23 = load i64, i64* @EAGAIN
%24 = icmp eq i64 %16, %23
%25 = load i64, i64* @EWOULDBLOCK
%26 = icmp eq i64 %16, %25
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %28, label %46
28: ; preds = %22
%29 = call i32 @FD_ZERO(i32* %4)
%30 = call i32 @FD_SET(i32 %0, i32* %4)
%31 = call i64 @select(i32 %9, i32* null, i32* %4, i32* null, i32* null)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %19
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %46
36: ; preds = %19, %10
%37 = phi i32 [ %13, %10 ], [ %20, %19 ]
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = sub i32 %6, %11
br label %46
41: ; preds = %36
%42 = sub nsw i32 %11, %37
%43 = zext i32 %37 to i64
%44 = getelementptr inbounds i8, i8* %12, i64 %43
%45 = icmp sgt i32 %42, 0
br i1 %45, label %10, label %46
46: ; preds = %41, %22, %3, %39, %33
%47 = phi i32 [ -1, %33 ], [ %40, %39 ], [ %6, %3 ], [ -1, %22 ], [ %6, %41 ]
ret i32 %47
}