#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 size_t ssize_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
size_t write(int, void const *, size_t);
ssize_t fdwrite(int fd, const void *ptr, size_t len) {
  size_t nleft;
  ssize_t nwritten;
  fd_set waitforwrite;
  unsigned char *byteptr = (unsigned char *)ptr;
  nleft = len;
  while (nleft > 0) {
    if ((nwritten = write(fd, (const void *)byteptr, nleft)) <= 0) {
      if (errno == EINTR) {
        continue;
      }
      if (errno == EAGAIN) {
        FD_ZERO(&waitforwrite);
        FD_SET(fd, &waitforwrite);
        if (select(fd + 1, NULL, &waitforwrite, NULL, NULL) < 0) {
          perror("select");
          len = -1;
          break;
        }
        continue;
      }
      perror("write");
      len = -1;
      break;
    }
    nleft -= nwritten;
    byteptr += nwritten;
  }
  return len;
}


x86 assembly (clang, -O0):
<fdwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    1265 <fdwrite+0x115>
mov    -0x4(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
ja     1245 <fdwrite+0xf5>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
jne    11bb <fdwrite+0x6b>
jmp    1173 <fdwrite+0x23>
mov    0x2e1e(%rip),%rax
mov    (%rax),%rax
mov    0x2dfc(%rip),%rcx
cmp    (%rcx),%rax
jne    122c <fdwrite+0xdc>
lea    -0x2c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x2c(%rbp),%rdx
mov    %r8,%rsi
mov    %r8,%rcx
call   1050 <select@plt>
cmp    $0x0,%rax
jge    1227 <fdwrite+0xd7>
lea    0xdeb(%rip),%rdi
call   1060 <perror@plt>
movq   $0xffffffffffffffff,-0x18(%rbp)
ff
jmp    1265 <fdwrite+0x115>
jmp    1173 <fdwrite+0x23>
lea    0xdd4(%rip),%rdi
call   1060 <perror@plt>
movq   $0xffffffffffffffff,-0x18(%rbp)
ff
jmp    1265 <fdwrite+0x115>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1173 <fdwrite+0x23>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdwrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdx,%rdx
je     11f2 <fdwrite+0xa2>
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0x1(%r15),%r13d
mov    0x2e60(%rip),%r12
lea    0x14(%rsp),%rbp
mov    %rdx,0x8(%rsp)
mov    %rdx,%r14
jmp    1190 <fdwrite+0x40>
add    %rax,%rbx
sub    %rax,%r14
je     11f6 <fdwrite+0xa6>
nop
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
test   %rax,%rax
jne    1187 <fdwrite+0x37>
mov    0x2e36(%rip),%rax
mov    (%rax),%rax
cmp    (%r12),%rax
je     1190 <fdwrite+0x40>
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
jne    11fd <fdwrite+0xad>
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   1070 <FD_SET@plt>
mov    %r13d,%edi
xor    %esi,%esi
mov    %rbp,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <select@plt>
test   %rax,%rax
jns    1190 <fdwrite+0x40>
lea    0xe10(%rip),%rdi
jmp    1204 <fdwrite+0xb4>
xor    %eax,%eax
jmp    1210 <fdwrite+0xc0>
mov    0x8(%rsp),%rax
jmp    1210 <fdwrite+0xc0>
lea    0xe03(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffffffffffff,%rax
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
@.str = [7 x i8] c"select\00"
@.str.1 = [6 x i8] c"write\00"
define i64 @fdwrite(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%11 = load i8*, i8** %5
store i8* %11, i8** %10
%12 = load i64, i64* %6
store i64 %12, i64* %7
br label %13
13: ; preds = %44, %41, %26, %3
%14 = load i64, i64* %7
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %51
16: ; preds = %13
%17 = load i32, i32* %4
%18 = load i8*, i8** %10
%19 = load i64, i64* %7
%20 = call i64 @write(i32 %17, i8* %18, i64 %19)
store i64 %20, i64* %8
%21 = icmp ule i64 %20, 0
br i1 %21, label %22, label %44
22: ; preds = %16
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
br label %13
27: ; preds = %22
%28 = load i64, i64* @errno
%29 = load i64, i64* @EAGAIN
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %42
31: ; preds = %27
%32 = call i32 @FD_ZERO(i32* %9)
%33 = load i32, i32* %4
%34 = call i32 @FD_SET(i32 %33, i32* %9)
%35 = load i32, i32* %4
%36 = add nsw i32 %35, 1
%37 = call i64 @select(i32 %36, i32* null, i32* %9, i32* null, i32* null)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %6
br label %51
41: ; preds = %31
br label %13
42: ; preds = %27
%43 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i64 -1, i64* %6
br label %51
44: ; preds = %16
%45 = load i64, i64* %8
%46 = load i64, i64* %7
%47 = sub i64 %46, %45
store i64 %47, i64* %7
%48 = load i64, i64* %8
%49 = load i8*, i8** %10
%50 = getelementptr inbounds i8, i8* %49, i64 %48
store i8* %50, i8** %10
br label %13
51: ; preds = %42, %39, %13
%52 = load i64, i64* %6
ret i64 %52
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str = [7 x i8] c"select\00"
@.str.1 = [6 x i8] c"write\00"
define i64 @fdwrite(i32 %0, i8* nocapture %1, i64 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp eq i64 %2, 0
br i1 %6, label %37, label %7
7: ; preds = %3
%8 = add nsw i32 %0, 1
br label %9
9: ; preds = %7, %29
%10 = phi i8* [ %1, %7 ], [ %32, %29 ]
%11 = phi i64 [ %2, %7 ], [ %31, %29 ]
%12 = call i64 @write(i32 %0, i8* %10, i64 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %29
14: ; preds = %9, %18
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %14, %24
%19 = call i64 @write(i32 %0, i8* %10, i64 %11)
%20 = icmp eq i64 %19, 0
br i1 %20, label %14, label %29
21: ; preds = %14
%22 = load i64, i64* @EAGAIN
%23 = icmp eq i64 %15, %22
br i1 %23, label %24, label %34
24: ; preds = %21
%25 = call i32 @FD_ZERO(i32* %4)
%26 = call i32 @FD_SET(i32 %0, i32* %4)
%27 = call i64 @select(i32 %8, i32* null, i32* %4, i32* null, i32* null)
%28 = icmp slt i64 %27, 0
br i1 %28, label %34, label %18
29: ; preds = %18, %9
%30 = phi i64 [ %12, %9 ], [ %19, %18 ]
%31 = sub i64 %11, %30
%32 = getelementptr inbounds i8, i8* %10, i64 %30
%33 = icmp eq i64 %31, 0
br i1 %33, label %37, label %9
34: ; preds = %21, %24
%35 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), %24 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %21 ]
%36 = call i32 @perror(i8* %35)
br label %37
37: ; preds = %29, %34, %3
%38 = phi i64 [ 0, %3 ], [ -1, %34 ], [ %2, %29 ]
ret i64 %38
}