#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;
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;
ssize_t writen_nonblock(int fd, const void *buf, size_t len, int flags) {
  int cnt;
  int rc;
  char *bp;
  fd_set wfds;
  cnt = len;
  bp = (char *)buf;
  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    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    129e <writen_nonblock+0x14e>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    125b <writen_nonblock+0x10b>
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%rax
jne    11bc <writen_nonblock+0x6c>
jmp    1175 <writen_nonblock+0x25>
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
je     11f0 <writen_nonblock+0xa0>
mov    0x2dfb(%rip),%rax
mov    (%rax),%rax
mov    0x2de9(%rip),%rcx
cmp    (%rcx),%rax
jne    124f <writen_nonblock+0xff>
lea    -0x34(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x34(%rbp),%rdx
mov    %r8,%rsi
mov    %r8,%rcx
call   1060 <select@plt>
cmp    $0x0,%rax
jge    124a <writen_nonblock+0xfa>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <writen_nonblock+0x155>
jmp    1175 <writen_nonblock+0x25>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <writen_nonblock+0x155>
cmpl   $0x0,-0x24(%rbp)
jne    1278 <writen_nonblock+0x128>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    12a5 <writen_nonblock+0x155>
jmp    127d <writen_nonblock+0x12d>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    1175 <writen_nonblock+0x25>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%14 = load i64, i64* %8
%15 = trunc i64 %14 to i32
store i32 %15, i32* %10
%16 = load i8*, i8** %7
store i8* %16, i8** %12
br label %17
17: ; preds = %63, %51, %31, %4
%18 = load i32, i32* %10
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %71
20: ; preds = %17
%21 = load i32, i32* %6
%22 = load i8*, i8** %12
%23 = load i32, i32* %10
%24 = call i32 @send(i32 %21, i8* %22, i32 %23, i32 0)
store i32 %24, i32* %11
%25 = load i32, i32* %11
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %53
27: ; preds = %20
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
br label %17
32: ; preds = %27
%33 = load i64, i64* @errno
%34 = load i64, i64* @EAGAIN
%35 = icmp eq i64 %33, %34
br i1 %35, label %40, label %36
36: ; preds = %32
%37 = load i64, i64* @errno
%38 = load i64, i64* @EWOULDBLOCK
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %52
40: ; preds = %36, %32
%41 = call i32 @FD_ZERO(i32* %13)
%42 = load i32, i32* %6
%43 = call i32 @FD_SET(i32 %42, i32* %13)
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
%46 = call i64 @select(i32 %45, i32* null, i32* %13, i32* null, i32* null)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %40
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %74
51: ; preds = %40
br label %17
52: ; preds = %36
store i32 -1, i32* %5
br label %74
53: ; preds = %20
%54 = load i32, i32* %11
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %53
%57 = load i64, i64* %8
%58 = load i32, i32* %10
%59 = sext i32 %58 to i64
%60 = sub i64 %57, %59
%61 = trunc i64 %60 to i32
store i32 %61, i32* %5
br label %74
62: ; preds = %53
br label %63
63: ; preds = %62
%64 = load i32, i32* %11
%65 = load i32, i32* %10
%66 = sub nsw i32 %65, %64
store i32 %66, i32* %10
%67 = load i32, i32* %11
%68 = load i8*, i8** %12
%69 = sext i32 %67 to i64
%70 = getelementptr inbounds i8, i8* %68, i64 %69
store i8* %70, i8** %12
br label %17
71: ; preds = %17
%72 = load i64, i64* %8
%73 = trunc i64 %72 to i32
store i32 %73, i32* %5
br label %74
74: ; preds = %71, %56, %52, %48
%75 = load i32, i32* %5
ret i32 %75
}

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