#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*/ uint8_t;
typedef int uint32_t;
struct pollfd {
  int fd;
  int events;
  int revents;
};
typedef scalar_t__ int32_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ ETIMEDOUT;
int POLLERR;
int POLLHUP;
int POLLOUT;
scalar_t__ errno;
double monotonic_seconds();
scalar_t__ poll(struct pollfd *, int, int);
scalar_t__ write(int, int /*<<< orphan*/ *, int);
int32_t streamtowrite(int sock, const void *buff, uint32_t leng,
                      uint32_t msecto) {
  uint32_t sent = 0;
  int32_t i;
  struct pollfd pfd;
  double s, c;
  uint32_t msecpassed;
  s = 0.0;
  pfd.fd = sock;
  pfd.events = POLLOUT;
  pfd.revents = 0;
  while (1) {
    i = write(sock, ((uint8_t *)buff) + sent, leng - sent);
    if (i == 0) {
      return 0;
    }
    if (i > 0) {
      sent += i;
    } else if ((errno != EAGAIN)) {
      return -1;
    }
    if (sent >= leng) {
      break;
    }
    if (s == 0.0) {
      s = monotonic_seconds();
      msecpassed = 0;
    } else {
      c = monotonic_seconds();
      msecpassed = (c - s) * 1000.0;
      if (msecpassed >= msecto) {
        errno = ETIMEDOUT;
        return -1;
      }
    }
    pfd.revents = 0;
    if (poll(&pfd, 1, msecto - msecpassed) < 0) {
      if (errno != EINTR) {
        return -1;
      } else {
        continue;
      }
    }
    if (pfd.revents & (POLLHUP | POLLERR)) {
      return -1;
    }
    if ((pfd.revents & POLLOUT) == 0) {
      errno = ETIMEDOUT;
      return -1;
    }
  }
  return sent;
}


x86 assembly (clang, -O0):
<streamtowrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
sub    -0x24(%rbp),%edx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a6 <streamtowrite+0x76>
movq   $0x0,-0x8(%rbp)
jmp    134d <streamtowrite+0x21d>
cmpq   $0x0,-0x30(%rbp)
jle    11c4 <streamtowrite+0x94>
mov    -0x30(%rbp),%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
jmp    11f0 <streamtowrite+0xc0>
mov    0x2dfd(%rip),%rax
mov    (%rax),%rax
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
je     11eb <streamtowrite+0xbb>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    134d <streamtowrite+0x21d>
jmp    11f0 <streamtowrite+0xc0>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     1201 <streamtowrite+0xd1>
jmp    1345 <streamtowrite+0x215>
movsd  -0x48(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1231 <streamtowrite+0x101>
jp     1231 <streamtowrite+0x101>
mov    $0x0,%al
call   1050 <monotonic_seconds@plt>
movsd  %xmm0,-0x48(%rbp)
movl   $0x0,-0x54(%rbp)
jmp    128c <streamtowrite+0x15c>
mov    $0x0,%al
call   1050 <monotonic_seconds@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
subsd  -0x48(%rbp),%xmm0
movsd  0xdb1(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x20(%rbp),%eax
jl     1287 <streamtowrite+0x157>
mov    0x2d53(%rip),%rax
mov    (%rax),%rcx
mov    0x2d51(%rip),%rax
mov    %rcx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    134d <streamtowrite+0x21d>
jmp    128c <streamtowrite+0x15c>
movl   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%edx
sub    -0x54(%rbp),%edx
lea    -0x40(%rbp),%rdi
mov    $0x1,%esi
call   1040 <poll@plt>
cmp    $0x0,%rax
jge    12dd <streamtowrite+0x1ad>
mov    0x2d10(%rip),%rax
mov    (%rax),%rax
mov    0x2cf6(%rip),%rcx
cmp    (%rcx),%rax
je     12d8 <streamtowrite+0x1a8>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    134d <streamtowrite+0x21d>
jmp    116d <streamtowrite+0x3d>
mov    -0x38(%rbp),%eax
mov    0x2cf9(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2ce8(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     130a <streamtowrite+0x1da>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    134d <streamtowrite+0x21d>
mov    -0x38(%rbp),%eax
mov    0x2ce4(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1340 <streamtowrite+0x210>
mov    0x2c9a(%rip),%rax
mov    (%rax),%rcx
mov    0x2c98(%rip),%rax
mov    %rcx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    134d <streamtowrite+0x21d>
jmp    116d <streamtowrite+0x3d>
movslq -0x24(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<streamtowrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%ebx
mov    %edi,0x8(%rsp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
movl   $0x0,0x10(%rsp)
call   1030 <write@plt>
test   %rax,%rax
je     129e <streamtowrite+0x16e>
xorpd  %xmm0,%xmm0
xor    %r13d,%r13d
jmp    11bb <streamtowrite+0x8b>
nopl   0x0(%rax,%rax,1)
mov    0x2e41(%rip),%rax
mov    (%rax),%rax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
jne    12bb <streamtowrite+0x18b>
movslq %r13d,%rax
lea    (%r14,%rax,4),%rsi
mov    %ebp,%edx
sub    %r13d,%edx
mov    %ebx,%edi
call   1030 <write@plt>
movsd  (%rsp),%xmm0
test   %rax,%rax
je     129e <streamtowrite+0x16e>
test   %rax,%rax
jle    11d0 <streamtowrite+0xa0>
add    %eax,%r13d
jmp    11ea <streamtowrite+0xba>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2df1(%rip),%rax
mov    (%rax),%rax
mov    0x2dc7(%rip),%rcx
cmp    (%rcx),%rax
jne    12bb <streamtowrite+0x18b>
cmp    %ebp,%r13d
jge    12a2 <streamtowrite+0x172>
xor    %r12d,%r12d
xor    %eax,%eax
movsd  %xmm0,(%rsp)
call   1050 <monotonic_seconds@plt>
movsd  (%rsp),%xmm1
ucomisd 0xdf9(%rip),%xmm1
jne    1213 <streamtowrite+0xe3>
jnp    122d <streamtowrite+0xfd>
subsd  %xmm1,%xmm0
mulsd  0xde1(%rip),%xmm0
cvttsd2si %xmm0,%r12d
movapd %xmm1,%xmm0
cmp    %r15d,%r12d
jge    12a7 <streamtowrite+0x177>
movsd  %xmm0,(%rsp)
movl   $0x0,0x10(%rsp)
mov    %r15d,%edx
sub    %r12d,%edx
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
call   1040 <poll@plt>
test   %rax,%rax
js     1180 <streamtowrite+0x50>
mov    0x10(%rsp),%ecx
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
mov    0x2d74(%rip),%rax
or     (%rax),%edx
mov    $0xffffffffffffffff,%rax
test   %ecx,%edx
jne    12c2 <streamtowrite+0x192>
mov    0x2d78(%rip),%rdx
test   %ecx,(%rdx)
jne    119a <streamtowrite+0x6a>
mov    0x2d31(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2d2f(%rip),%rdx
mov    %rcx,(%rdx)
jmp    12c2 <streamtowrite+0x192>
xor    %eax,%eax
jmp    12c2 <streamtowrite+0x192>
movslq %r13d,%rax
jmp    12c2 <streamtowrite+0x192>
mov    0x2d12(%rip),%rax
mov    (%rax),%rax
mov    0x2d10(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffffffffffff,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLOUT = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@ETIMEDOUT = global i64 0
@EINTR = global i64 0
@POLLHUP = global i32 0
@POLLERR = global i32 0
define i64 @streamtowrite(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca %struct.pollfd
%13 = alloca double
%14 = alloca double
%15 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %10
store double 0.000000e+00, double* %13
%16 = load i32, i32* %6
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @POLLOUT
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 2
store i32 0, i32* %20
br label %21
21: ; preds = %4, %85, %103
%22 = load i32, i32* %6
%23 = load i8*, i8** %7
%24 = bitcast i8* %23 to i32*
%25 = load i32, i32* %10
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %8
%29 = load i32, i32* %10
%30 = sub nsw i32 %28, %29
%31 = call i64 @write(i32 %22, i32* %27, i32 %30)
store i64 %31, i64* %11
%32 = load i64, i64* %11
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %21
store i64 0, i64* %5
br label %107
35: ; preds = %21
%36 = load i64, i64* %11
%37 = icmp sgt i64 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %35
%39 = load i64, i64* %11
%40 = load i32, i32* %10
%41 = sext i32 %40 to i64
%42 = add nsw i64 %41, %39
%43 = trunc i64 %42 to i32
store i32 %43, i32* %10
br label %50
44: ; preds = %35
%45 = load i64, i64* @errno
%46 = load i64, i64* @EAGAIN
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %44
store i64 -1, i64* %5
br label %107
49: ; preds = %44
br label %50
50: ; preds = %49, %38
%51 = load i32, i32* %10
%52 = load i32, i32* %8
%53 = icmp sge i32 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %50
br label %104
55: ; preds = %50
%56 = load double, double* %13
%57 = fcmp oeq double %56, 0.000000e+00
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call double (...) @monotonic_seconds()
store double %59, double* %13
store i32 0, i32* %15
br label %73
60: ; preds = %55
%61 = call double (...) @monotonic_seconds()
store double %61, double* %14
%62 = load double, double* %14
%63 = load double, double* %13
%64 = fsub double %62, %63
%65 = fmul double %64, 1.000000e+03
%66 = fptosi double %65 to i32
store i32 %66, i32* %15
%67 = load i32, i32* %15
%68 = load i32, i32* %9
%69 = icmp sge i32 %67, %68
br i1 %69, label %70, label %72
70: ; preds = %60
%71 = load i64, i64* @ETIMEDOUT
store i64 %71, i64* @errno
store i64 -1, i64* %5
br label %107
72: ; preds = %60
br label %73
73: ; preds = %72, %58
%74 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 2
store i32 0, i32* %74
%75 = load i32, i32* %9
%76 = load i32, i32* %15
%77 = sub nsw i32 %75, %76
%78 = call i64 @poll(%struct.pollfd* %12, i32 1, i32 %77)
%79 = icmp slt i64 %78, 0
br i1 %79, label %80, label %86
80: ; preds = %73
%81 = load i64, i64* @errno
%82 = load i64, i64* @EINTR
%83 = icmp ne i64 %81, %82
br i1 %83, label %84, label %85
84: ; preds = %80
store i64 -1, i64* %5
br label %107
85: ; preds = %80
br label %21
86: ; preds = %73
%87 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 2
%88 = load i32, i32* %87
%89 = load i32, i32* @POLLHUP
%90 = load i32, i32* @POLLERR
%91 = or i32 %89, %90
%92 = and i32 %88, %91
%93 = icmp ne i32 %92, 0
br i1 %93, label %94, label %95
94: ; preds = %86
store i64 -1, i64* %5
br label %107
95: ; preds = %86
%96 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 2
%97 = load i32, i32* %96
%98 = load i32, i32* @POLLOUT
%99 = and i32 %97, %98
%100 = icmp eq i32 %99, 0
br i1 %100, label %101, label %103
101: ; preds = %95
%102 = load i64, i64* @ETIMEDOUT
store i64 %102, i64* @errno
store i64 -1, i64* %5
br label %107
103: ; preds = %95
br label %21
104: ; preds = %54
%105 = load i32, i32* %10
%106 = sext i32 %105 to i64
store i64 %106, i64* %5
br label %107
107: ; preds = %104, %101, %94, %84, %70, %48, %34
%108 = load i64, i64* %5
ret i64 %108
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLOUT = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@ETIMEDOUT = global i64 0
@EINTR = global i64 0
@POLLHUP = global i32 0
@POLLERR = global i32 0
define i64 @streamtowrite(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = alloca %struct.pollfd
%6 = bitcast %struct.pollfd* %5 to i8*
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
store i32 %0, i32* %7
%8 = load i32, i32* @POLLOUT
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 2
store i32 0, i32* %10
%11 = bitcast i8* %1 to i32*
%12 = call i64 @write(i32 %0, i32* %11, i32 %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %70, label %14
14: ; preds = %4, %49
%15 = phi i64 [ %53, %49 ], [ %12, %4 ]
%16 = phi double [ %40, %49 ], [ 0.000000e+00, %4 ]
%17 = phi i32 [ %27, %49 ], [ 0, %4 ]
%18 = icmp sgt i64 %15, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = trunc i64 %15 to i32
%21 = add i32 %17, %20
br label %26
22: ; preds = %14
%23 = load i64, i64* @errno
%24 = load i64, i64* @EAGAIN
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %70
26: ; preds = %22, %19
%27 = phi i32 [ %21, %19 ], [ %17, %22 ]
%28 = icmp slt i32 %27, %2
br i1 %28, label %29, label %68
29: ; preds = %26
%30 = fcmp oeq double %16, 0.000000e+00
%31 = call double (...) @monotonic_seconds()
br i1 %30, label %39, label %32
32: ; preds = %29
%33 = fsub double %31, %16
%34 = fmul double %33, 1.000000e+03
%35 = fptosi double %34 to i32
%36 = icmp slt i32 %35, %3
br i1 %36, label %39, label %37
37: ; preds = %32
%38 = load i64, i64* @ETIMEDOUT
store i64 %38, i64* @errno
br label %70
39: ; preds = %29, %32
%40 = phi double [ %16, %32 ], [ %31, %29 ]
%41 = phi i32 [ %35, %32 ], [ 0, %29 ]
store i32 0, i32* %10
%42 = sub nsw i32 %3, %41
%43 = call i64 @poll(%struct.pollfd* %5, i32 1, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %39
%46 = load i64, i64* @errno
%47 = load i64, i64* @EINTR
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %70
49: ; preds = %45, %62
%50 = sext i32 %27 to i64
%51 = getelementptr inbounds i32, i32* %11, i64 %50
%52 = sub nsw i32 %2, %27
%53 = call i64 @write(i32 %0, i32* %51, i32 %52)
%54 = icmp eq i64 %53, 0
br i1 %54, label %70, label %14
55: ; preds = %39
%56 = load i32, i32* %10
%57 = load i32, i32* @POLLHUP
%58 = load i32, i32* @POLLERR
%59 = or i32 %58, %57
%60 = and i32 %59, %56
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %70
62: ; preds = %55
%63 = load i32, i32* @POLLOUT
%64 = and i32 %63, %56
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %49
66: ; preds = %62
%67 = load i64, i64* @ETIMEDOUT
store i64 %67, i64* @errno
br label %70
68: ; preds = %26
%69 = sext i32 %27 to i64
br label %70
70: ; preds = %49, %22, %45, %55, %4, %68, %66, %37
%71 = phi i64 [ %69, %68 ], [ -1, %66 ], [ -1, %37 ], [ 0, %4 ], [ 0, %49 ], [ -1, %22 ], [ -1, %45 ], [ -1, %55 ]
ret i64 %71
}