#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 {
  long tv_sec;
  long tv_usec;
};
typedef int 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*/ gettext(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ set_error(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
int write(int, char *, size_t);
ssize_t mywrite(int fd, char *wherefrom, size_t len, double timeout) {
  ssize_t cnt = 0;
  while (len > 0) {
    ssize_t rc;
    struct timeval to;
    fd_set wfds;
    FD_ZERO(&wfds);
    FD_SET(fd, &wfds);
    to.tv_sec = (long)(timeout / 1000.0);
    to.tv_usec = (long)(timeout * 1000.0) % 1000000;
    rc = select(fd + 1, NULL, &wfds, NULL, &to);
    if (rc == 0)
      return -2;
    else if (rc == -1) {
      if (errno == EAGAIN)
        continue;
      if (errno == EINTR)
        return -3;
      set_error(gettext("mywrite::select failed: %s"), strerror(errno));
      return -1;
    }
    rc = write(fd, wherefrom, len);
    if (rc == -1) {
      if (errno == EAGAIN)
        continue;
      if (errno == EINTR)
        return -3;
      set_error(gettext("mywrite::write failed: %s"), strerror(errno));
      return -1;
    } else if (rc == 0)
      break;
    else {
      wherefrom += rc;
      len -= rc;
      cnt += rc;
    }
  }
  return cnt;
}


x86 assembly (clang, -O0):
<mywrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    137c <mywrite+0x20c>
lea    -0x3c(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x3c(%rbp),%rsi
call   1090 <FD_SET@plt>
movsd  -0x20(%rbp),%xmm0
movsd  0xe44(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x38(%rbp)
movsd  0xe2f(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
cvttsd2si %xmm0,%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x30(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x3c(%rbp),%rdx
lea    -0x38(%rbp),%r8
mov    %rcx,%rsi
call   1070 <select@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    121c <mywrite+0xac>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1382 <mywrite+0x212>
cmpl   $0xffffffff,-0x28(%rbp)
jne    129f <mywrite+0x12f>
mov    0x2db3(%rip),%rax
mov    (%rax),%rax
mov    0x2d91(%rip),%rcx
cmp    (%rcx),%rax
jne    1245 <mywrite+0xd5>
jmp    118f <mywrite+0x1f>
mov    0x2d94(%rip),%rax
mov    (%rax),%rax
mov    0x2d82(%rip),%rcx
cmp    (%rcx),%rax
jne    126b <mywrite+0xfb>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1382 <mywrite+0x212>
lea    0xd96(%rip),%rdi
call   1040 <gettext@plt>
mov    %eax,-0x40(%rbp)
mov    0x2d5f(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %eax,%esi
call   1050 <set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1382 <mywrite+0x212>
jmp    12a4 <mywrite+0x134>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    133a <mywrite+0x1ca>
mov    0x2d18(%rip),%rax
mov    (%rax),%rax
mov    0x2cf6(%rip),%rcx
cmp    (%rcx),%rax
jne    12e0 <mywrite+0x170>
jmp    118f <mywrite+0x1f>
mov    0x2cf9(%rip),%rax
mov    (%rax),%rax
mov    0x2ce7(%rip),%rcx
cmp    (%rcx),%rax
jne    1306 <mywrite+0x196>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1382 <mywrite+0x212>
lea    0xd16(%rip),%rdi
call   1040 <gettext@plt>
mov    %eax,-0x44(%rbp)
mov    0x2cc4(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %eax,%esi
call   1050 <set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1382 <mywrite+0x212>
cmpl   $0x0,-0x28(%rbp)
jne    1349 <mywrite+0x1d9>
jmp    137c <mywrite+0x20c>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movslq -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1377 <mywrite+0x207>
jmp    118f <mywrite+0x1f>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mywrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,0x8(%rsp)
test   %rdx,%rdx
je     12b4 <mywrite+0x144>
mov    %rdx,%rbx
mov    %edi,%r15d
movsd  0xe66(%rip),%xmm1
movapd %xmm0,%xmm2
divsd  %xmm1,%xmm2
cvttsd2si %xmm2,%r14
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%r13
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mov    %r13,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x12,%rdx
add    %rax,%rdx
imul   $0xf4240,%rdx,%rax
sub    %rax,%r13
lea    0x1(%r15),%ebp
xor    %eax,%eax
mov    %rax,(%rsp)
lea    0x14(%rsp),%r12
jmp    1213 <mywrite+0xa3>
nopl   0x0(%rax)
mov    0x2de9(%rip),%rax
mov    (%rax),%rax
mov    0x2dc7(%rip),%rcx
cmp    (%rcx),%rax
jne    12c3 <mywrite+0x153>
test   %rbx,%rbx
je     1303 <mywrite+0x193>
mov    %r12,%rdi
call   1060 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r12,%rsi
call   1090 <FD_SET@plt>
mov    %r14,0x18(%rsp)
mov    %r13,0x20(%rsp)
mov    %ebp,%edi
xor    %esi,%esi
mov    %r12,%rdx
xor    %ecx,%ecx
lea    0x18(%rsp),%r8
call   1070 <select@plt>
cmp    $0xffffffff,%eax
je     11f0 <mywrite+0x80>
test   %eax,%eax
je     12bc <mywrite+0x14c>
mov    %r15d,%edi
mov    0x8(%rsp),%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1280 <mywrite+0x110>
test   %eax,%eax
je     1303 <mywrite+0x193>
movslq %eax,%rcx
add    %rcx,0x8(%rsp)
sub    %rcx,%rbx
mov    (%rsp),%rcx
add    %eax,%ecx
mov    %rcx,(%rsp)
jmp    120a <mywrite+0x9a>
mov    0x2d59(%rip),%rax
mov    (%rax),%rax
mov    0x2d37(%rip),%rcx
cmp    (%rcx),%rax
je     120a <mywrite+0x9a>
mov    0x2d37(%rip),%rcx
mov    $0xfffffffd,%edx
cmp    (%rcx),%rax
je     12ff <mywrite+0x18f>
lea    0xd71(%rip),%rdi
jmp    12db <mywrite+0x16b>
xor    %eax,%eax
mov    %rax,(%rsp)
jmp    1303 <mywrite+0x193>
mov    $0xfffffffe,%edx
jmp    12ff <mywrite+0x18f>
mov    0x2d0e(%rip),%rcx
mov    $0xfffffffd,%edx
cmp    (%rcx),%rax
je     12ff <mywrite+0x18f>
lea    0xd2d(%rip),%rdi
call   1040 <gettext@plt>
mov    %eax,%ebx
mov    0x2cf7(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1050 <set_error@plt>
mov    $0xffffffff,%edx
mov    %rdx,(%rsp)
mov    (%rsp),%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str = [27 x i8] c"mywrite::select failed: %s\00"
@.str.1 = [26 x i8] c"mywrite::write failed: %s\00"
define i32 @mywrite(i32 %0, i8* %1, i64 %2, double %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca double
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.timeval
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store double %3, double* %9
store i32 0, i32* %10
br label %14
14: ; preds = %94, %66, %43, %4
%15 = load i64, i64* %8
%16 = icmp ugt i64 %15, 0
br i1 %16, label %17, label %95
17: ; preds = %14
%18 = call i32 @FD_ZERO(i32* %13)
%19 = load i32, i32* %6
%20 = call i32 @FD_SET(i32 %19, i32* %13)
%21 = load double, double* %9
%22 = fdiv double %21, 1.000000e+03
%23 = fptosi double %22 to i64
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i64 %23, i64* %24
%25 = load double, double* %9
%26 = fmul double %25, 1.000000e+03
%27 = fptosi double %26 to i64
%28 = srem i64 %27, 1000000
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 %28, i64* %29
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
%32 = call i32 @select(i32 %31, i32* null, i32* %13, i32* null, %struct.timeval* %12)
store i32 %32, i32* %11
%33 = load i32, i32* %11
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %17
store i32 -2, i32* %5
br label %97
36: ; preds = %17
%37 = load i32, i32* %11
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %54
39: ; preds = %36
%40 = load i64, i64* @errno
%41 = load i64, i64* @EAGAIN
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %39
br label %14
44: ; preds = %39
%45 = load i64, i64* @errno
%46 = load i64, i64* @EINTR
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %44
store i32 -3, i32* %5
br label %97
49: ; preds = %44
%50 = call i32 @gettext(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%51 = load i64, i64* @errno
%52 = call i32 @strerror(i64 %51)
%53 = call i32 @set_error(i32 %50, i32 %52)
store i32 -1, i32* %5
br label %97
54: ; preds = %36
br label %55
55: ; preds = %54
%56 = load i32, i32* %6
%57 = load i8*, i8** %7
%58 = load i64, i64* %8
%59 = call i32 @write(i32 %56, i8* %57, i64 %58)
store i32 %59, i32* %11
%60 = load i32, i32* %11
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %77
62: ; preds = %55
%63 = load i64, i64* @errno
%64 = load i64, i64* @EAGAIN
%65 = icmp eq i64 %63, %64
br i1 %65, label %66, label %67
66: ; preds = %62
br label %14
67: ; preds = %62
%68 = load i64, i64* @errno
%69 = load i64, i64* @EINTR
%70 = icmp eq i64 %68, %69
br i1 %70, label %71, label %72
71: ; preds = %67
store i32 -3, i32* %5
br label %97
72: ; preds = %67
%73 = call i32 @gettext(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%74 = load i64, i64* @errno
%75 = call i32 @strerror(i64 %74)
%76 = call i32 @set_error(i32 %73, i32 %75)
store i32 -1, i32* %5
br label %97
77: ; preds = %55
%78 = load i32, i32* %11
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %81
80: ; preds = %77
br label %95
81: ; preds = %77
%82 = load i32, i32* %11
%83 = load i8*, i8** %7
%84 = sext i32 %82 to i64
%85 = getelementptr inbounds i8, i8* %83, i64 %84
store i8* %85, i8** %7
%86 = load i32, i32* %11
%87 = sext i32 %86 to i64
%88 = load i64, i64* %8
%89 = sub i64 %88, %87
store i64 %89, i64* %8
%90 = load i32, i32* %11
%91 = load i32, i32* %10
%92 = add nsw i32 %91, %90
store i32 %92, i32* %10
br label %93
93: ; preds = %81
br label %94
94: ; preds = %93
br label %14
95: ; preds = %80, %14
%96 = load i32, i32* %10
store i32 %96, i32* %5
br label %97
97: ; preds = %95, %72, %71, %49, %48, %35
%98 = load i32, i32* %5
ret i32 %98
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str = [27 x i8] c"mywrite::select failed: %s\00"
@.str.1 = [26 x i8] c"mywrite::write failed: %s\00"
define i32 @mywrite(i32 %0, i8* nocapture %1, i64 %2, double %3) {
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = icmp eq i64 %2, 0
br i1 %7, label %61, label %8
8: ; preds = %4
%9 = bitcast %struct.timeval* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = fdiv double %3, 1.000000e+03
%12 = fptosi double %11 to i64
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%14 = fmul double %3, 1.000000e+03
%15 = fptosi double %14 to i64
%16 = srem i64 %15, 1000000
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%18 = add nsw i32 %0, 1
br label %19
19: ; preds = %8, %56
%20 = phi i32 [ 0, %8 ], [ %59, %56 ]
%21 = phi i64 [ %2, %8 ], [ %58, %56 ]
%22 = phi i8* [ %1, %8 ], [ %57, %56 ]
%23 = call i32 @FD_ZERO(i32* %6)
%24 = call i32 @FD_SET(i32 %0, i32* %6)
store i64 %12, i64* %13
store i64 %16, i64* %17
%25 = call i32 @select(i32 %18, i32* null, i32* %6, i32* null, %struct.timeval* %5)
switch i32 %25, label %33 [
i32 0, label %54
i32 -1, label %26
]
26: ; preds = %19
%27 = load i64, i64* @errno
%28 = load i64, i64* @EAGAIN
%29 = icmp eq i64 %27, %28
br i1 %29, label %56, label %30
30: ; preds = %26
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %27, %31
br i1 %32, label %54, label %48
33: ; preds = %19
%34 = call i32 @write(i32 %0, i8* %22, i64 %21)
switch i32 %34, label %43 [
i32 -1, label %36
i32 0, label %35
]
35: ; preds = %33
br label %61
36: ; preds = %33
%37 = load i64, i64* @errno
%38 = load i64, i64* @EAGAIN
%39 = icmp eq i64 %37, %38
br i1 %39, label %56, label %40
40: ; preds = %36
%41 = load i64, i64* @EINTR
%42 = icmp eq i64 %37, %41
br i1 %42, label %54, label %48
43: ; preds = %33
%44 = sext i32 %34 to i64
%45 = getelementptr inbounds i8, i8* %22, i64 %44
%46 = sub i64 %21, %44
%47 = add nsw i32 %34, %20
br label %56
48: ; preds = %40, %30
%49 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), %30 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), %40 ]
%50 = call i32 @gettext(i8* %49)
%51 = load i64, i64* @errno
%52 = call i32 @strerror(i64 %51)
%53 = call i32 @set_error(i32 %50, i32 %52)
br label %54
54: ; preds = %19, %48, %30, %40
%55 = phi i32 [ -3, %40 ], [ -3, %30 ], [ -1, %48 ], [ -2, %19 ]
br label %61
56: ; preds = %36, %26, %43
%57 = phi i8* [ %45, %43 ], [ %22, %26 ], [ %22, %36 ]
%58 = phi i64 [ %46, %43 ], [ %21, %26 ], [ %21, %36 ]
%59 = phi i32 [ %47, %43 ], [ %20, %26 ], [ %20, %36 ]
%60 = icmp eq i64 %58, 0
br i1 %60, label %61, label %19
61: ; preds = %56, %4, %35, %54
%62 = phi i32 [ %55, %54 ], [ %20, %35 ], [ 0, %4 ], [ %59, %56 ]
ret i32 %62
}