#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 */
struct timeval {
  int dummy;
};
typedef size_t ssize_t;
typedef int off_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EPIPE;
scalar_t__ ETIMEDOUT;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int select_retry(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                 int /*<<< orphan*/ *, struct timeval *);
int write(int, char *, size_t);
ssize_t write_retry(int fd, void *buf, size_t count, struct timeval *timeout) {
  int n, rv = 0;
  ssize_t total = 0, remain = count;
  fd_set wfds, xfds;
  while (total < count) {
    FD_ZERO(&wfds);
    FD_SET(fd, &wfds);
    FD_ZERO(&xfds);
    FD_SET(fd, &xfds);
    rv = select_retry(fd + 1, NULL, &wfds, &xfds, timeout);
    if (rv == -1)
      return -1;
    else if (rv == 0) {
      errno = ETIMEDOUT;
      return -1;
    }
    if (FD_ISSET(fd, &xfds)) {
      errno = EPIPE;
      return -1;
    }
    n = write(fd, (char *)buf + (off_t)total, remain);
    if ((n == 0) && (rv == 1)) {
      errno = EPIPE;
      return -1;
    }
    if (n == -1) {
      if ((errno == EAGAIN) || (errno == EINTR)) {
        continue;
      }
      return -1;
    }
    total += n;
    remain -= n;
  }
  return total;
}


x86 assembly (clang, -O0):
<write_retry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1313 <write_retry+0x1c3>
lea    -0x44(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x44(%rbp),%rsi
call   1070 <FD_SET@plt>
lea    -0x48(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
mov    -0x28(%rbp),%r8
xor    %eax,%eax
mov    %eax,%esi
lea    -0x44(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1050 <select_retry@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    11eb <write_retry+0x9b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    131b <write_retry+0x1cb>
cmpl   $0x0,-0x30(%rbp)
jne    1216 <write_retry+0xc6>
mov    0x2dd4(%rip),%rax
mov    (%rax),%rcx
mov    0x2dd2(%rip),%rax
mov    %rcx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    131b <write_retry+0x1cb>
jmp    121b <write_retry+0xcb>
mov    -0xc(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1252 <write_retry+0x102>
mov    0x2da8(%rip),%rax
mov    (%rax),%rcx
mov    0x2d96(%rip),%rax
mov    %rcx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    131b <write_retry+0x1cb>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rax
cltq
add    %rax,%rsi
mov    -0x40(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    12a3 <write_retry+0x153>
cmpl   $0x1,-0x30(%rbp)
jne    12a3 <write_retry+0x153>
mov    0x2d57(%rip),%rax
mov    (%rax),%rcx
mov    0x2d45(%rip),%rax
mov    %rcx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    131b <write_retry+0x1cb>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12f3 <write_retry+0x1a3>
mov    0x2d24(%rip),%rax
mov    (%rax),%rax
mov    0x2cfa(%rip),%rcx
cmp    (%rcx),%rax
je     12e1 <write_retry+0x191>
mov    0x2d0a(%rip),%rax
mov    (%rax),%rax
mov    0x2cf0(%rip),%rcx
cmp    (%rcx),%rax
jne    12e6 <write_retry+0x196>
jmp    117e <write_retry+0x2e>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    131b <write_retry+0x1cb>
movslq -0x2c(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
movslq -0x2c(%rbp),%rcx
mov    -0x40(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
jmp    117e <write_retry+0x2e>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_retry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,0x28(%rsp)
test   %rdx,%rdx
je     127a <write_retry+0x12a>
mov    %edi,%ebx
lea    0x1(%rbx),%eax
mov    %eax,0x4(%rsp)
xor    %eax,%eax
mov    %rax,0x8(%rsp)
lea    0x34(%rsp),%rbp
lea    0x30(%rsp),%r15
mov    %rsi,0x18(%rsp)
mov    %rsi,%r13
mov    %rdx,0x20(%rsp)
mov    %rdx,0x10(%rsp)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1070 <FD_SET@plt>
mov    %r15,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1070 <FD_SET@plt>
mov    0x4(%rsp),%edi
xor    %esi,%esi
mov    %rbp,%rdx
mov    %r15,%rcx
mov    0x28(%rsp),%r8
call   1050 <select_retry@plt>
mov    $0xffffffffffffffff,%r12
cmp    $0xffffffff,%eax
je     129c <write_retry+0x14c>
mov    %eax,%r14d
test   %eax,%eax
je     1288 <write_retry+0x138>
mov    %ebx,%edi
mov    %r15,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
jne    127f <write_retry+0x12f>
mov    %ebx,%edi
mov    %r13,%rsi
mov    0x10(%rsp),%rdx
call   1030 <write@plt>
test   %eax,%eax
jne    121d <write_retry+0xcd>
cmp    $0x1,%r14d
je     127f <write_retry+0x12f>
cmp    $0xffffffff,%eax
jne    124e <write_retry+0xfe>
mov    0x2daf(%rip),%rax
mov    (%rax),%rax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
je     11a0 <write_retry+0x50>
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
je     11a0 <write_retry+0x50>
jmp    129c <write_retry+0x14c>
cltq
mov    0x8(%rsp),%rcx
add    %rax,%rcx
sub    %rax,0x10(%rsp)
movslq %ecx,%r13
add    0x18(%rsp),%r13
mov    %rcx,%r12
mov    %rcx,0x8(%rsp)
cmp    0x20(%rsp),%rcx
jb     11a0 <write_retry+0x50>
jmp    129c <write_retry+0x14c>
xor    %r12d,%r12d
jmp    129c <write_retry+0x14c>
mov    0x2d5a(%rip),%rax
jmp    128f <write_retry+0x13f>
mov    0x2d41(%rip),%rax
mov    (%rax),%rax
mov    0x2d3f(%rip),%rcx
mov    %rax,(%rcx)
mov    %r12,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@ETIMEDOUT = global i64 0
@errno = global i64 0
@EPIPE = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
define i64 @write_retry(i32 %0, i8* %1, i64 %2, %struct.timeval* %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.timeval*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store %struct.timeval* %3, %struct.timeval** %9
store i32 0, i32* %11
store i64 0, i64* %12
%16 = load i64, i64* %8
store i64 %16, i64* %13
br label %17
17: ; preds = %76, %74, %4
%18 = load i64, i64* %12
%19 = load i64, i64* %8
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %85
21: ; preds = %17
%22 = call i32 @FD_ZERO(i32* %14)
%23 = load i32, i32* %6
%24 = call i32 @FD_SET(i32 %23, i32* %14)
%25 = call i32 @FD_ZERO(i32* %15)
%26 = load i32, i32* %6
%27 = call i32 @FD_SET(i32 %26, i32* %15)
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
%30 = load %struct.timeval*, %struct.timeval** %9
%31 = call i32 @select_retry(i32 %29, i32* null, i32* %14, i32* %15, %struct.timeval* %30)
store i32 %31, i32* %11
%32 = load i32, i32* %11
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %35
34: ; preds = %21
store i64 -1, i64* %5
br label %87
35: ; preds = %21
%36 = load i32, i32* %11
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = load i64, i64* @ETIMEDOUT
store i64 %39, i64* @errno
store i64 -1, i64* %5
br label %87
40: ; preds = %35
br label %41
41: ; preds = %40
%42 = load i32, i32* %6
%43 = call i64 @FD_ISSET(i32 %42, i32* %15)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = load i64, i64* @EPIPE
store i64 %46, i64* @errno
store i64 -1, i64* %5
br label %87
47: ; preds = %41
%48 = load i32, i32* %6
%49 = load i8*, i8** %7
%50 = load i64, i64* %12
%51 = trunc i64 %50 to i32
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %49, i64 %52
%54 = load i64, i64* %13
%55 = call i32 @write(i32 %48, i8* %53, i64 %54)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %63
58: ; preds = %47
%59 = load i32, i32* %11
%60 = icmp eq i32 %59, 1
br i1 %60, label %61, label %63
61: ; preds = %58
%62 = load i64, i64* @EPIPE
store i64 %62, i64* @errno
store i64 -1, i64* %5
br label %87
63: ; preds = %58, %47
%64 = load i32, i32* %10
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %76
66: ; preds = %63
%67 = load i64, i64* @errno
%68 = load i64, i64* @EAGAIN
%69 = icmp eq i64 %67, %68
br i1 %69, label %74, label %70
70: ; preds = %66
%71 = load i64, i64* @errno
%72 = load i64, i64* @EINTR
%73 = icmp eq i64 %71, %72
br i1 %73, label %74, label %75
74: ; preds = %70, %66
br label %17
75: ; preds = %70
store i64 -1, i64* %5
br label %87
76: ; preds = %63
%77 = load i32, i32* %10
%78 = sext i32 %77 to i64
%79 = load i64, i64* %12
%80 = add i64 %79, %78
store i64 %80, i64* %12
%81 = load i32, i32* %10
%82 = sext i32 %81 to i64
%83 = load i64, i64* %13
%84 = sub i64 %83, %82
store i64 %84, i64* %13
br label %17
85: ; preds = %17
%86 = load i64, i64* %12
store i64 %86, i64* %5
br label %87
87: ; preds = %85, %75, %61, %45, %38, %34
%88 = load i64, i64* %5
ret i64 %88
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@ETIMEDOUT = global i64 0
@errno = global i64 0
@EPIPE = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
define i64 @write_retry(i32 %0, i8* nocapture %1, i64 %2, %struct.timeval* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = add nsw i32 %0, 1
%10 = icmp eq i64 %2, 0
br i1 %10, label %49, label %11
11: ; preds = %4, %38
%12 = phi i8* [ %45, %38 ], [ %1, %4 ]
%13 = phi i64 [ %41, %38 ], [ %2, %4 ]
%14 = phi i64 [ %40, %38 ], [ 0, %4 ]
br label %15
15: ; preds = %11, %31
%16 = call i32 @FD_ZERO(i32* %5)
%17 = call i32 @FD_SET(i32 %0, i32* %5)
%18 = call i32 @FD_ZERO(i32* %6)
%19 = call i32 @FD_SET(i32 %0, i32* %6)
%20 = call i32 @select_retry(i32 %9, i32* null, i32* %5, i32* %6, %struct.timeval* %3)
switch i32 %20, label %21 [
i32 -1, label %49
i32 0, label %46
]
21: ; preds = %15
%22 = call i64 @FD_ISSET(i32 %0, i32* %6)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %46
24: ; preds = %21
%25 = call i32 @write(i32 %0, i8* %12, i64 %13)
%26 = icmp eq i32 %25, 0
%27 = icmp eq i32 %20, 1
%28 = select i1 %26, i1 %27, i1 false
br i1 %28, label %46, label %29
29: ; preds = %24
%30 = icmp eq i32 %25, -1
br i1 %30, label %31, label %38
31: ; preds = %29
%32 = load i64, i64* @errno
%33 = load i64, i64* @EAGAIN
%34 = icmp eq i64 %32, %33
%35 = load i64, i64* @EINTR
%36 = icmp eq i64 %32, %35
%37 = select i1 %34, i1 true, i1 %36
br i1 %37, label %15, label %49
38: ; preds = %29
%39 = sext i32 %25 to i64
%40 = add i64 %14, %39
%41 = sub i64 %13, %39
%42 = icmp ult i64 %40, %2
%43 = shl i64 %40, 32
%44 = ashr exact i64 %43, 32
%45 = getelementptr inbounds i8, i8* %1, i64 %44
br i1 %42, label %11, label %49
46: ; preds = %24, %21, %15
%47 = phi i64* [ @ETIMEDOUT, %15 ], [ @EPIPE, %21 ], [ @EPIPE, %24 ]
%48 = load i64, i64* %47
store i64 %48, i64* @errno
br label %49
49: ; preds = %38, %15, %31, %4, %46
%50 = phi i64 [ -1, %46 ], [ 0, %4 ], [ -1, %31 ], [ -1, %15 ], [ %40, %38 ]
ret i64 %50
}