#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 {
  unsigned int tv_sec;
  unsigned int tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int send(int, char *, int, int /*<<< orphan*/);
int cr_senddata(int fd, unsigned int msecs, char *buf, int size) {
  fd_set fds;
  struct timeval tv;
  int rc, sent = 0;
  tv.tv_sec = msecs / 1000;
  tv.tv_usec = (msecs % 1000) * 1000;
  while (sent < size) {
    FD_ZERO(&fds);
    FD_SET(fd, &fds);
    rc = select(fd + 1, NULL, &fds, NULL, &tv);
    if (rc > 0) {
      rc = send(fd, buf + sent, size - sent, 0);
      if (rc < 0)
        return -1;
      sent += rc;
    } else if (rc == 0)
      break;
    else
      return -1;
  }
  return sent;
}


x86 assembly (clang, -O0):
<cr_senddata>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    122a <cr_senddata+0xea>
lea    -0x20(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%r8
mov    %rcx,%rsi
call   1050 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    120a <cr_senddata+0xca>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x30(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
sub    -0x30(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11fc <cr_senddata+0xbc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <cr_senddata+0xf0>
mov    -0x2c(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    1225 <cr_senddata+0xe5>
cmpl   $0x0,-0x2c(%rbp)
jne    1219 <cr_senddata+0xd9>
jmp    122a <cr_senddata+0xea>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <cr_senddata+0xf0>
jmp    1180 <cr_senddata+0x40>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cr_senddata>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
mov    %eax,0x8(%rsp)
imul   $0x3e8,%eax,%eax
sub    %eax,%esi
imul   $0x3e8,%esi,%eax
mov    %eax,0xc(%rsp)
test   %ecx,%ecx
jle    11dc <cr_senddata+0x9c>
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %edi,%ebx
lea    0x1(%rbx),%r15d
xor    %r13d,%r13d
lea    0x14(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   1060 <FD_SET@plt>
mov    %r15d,%edi
xor    %esi,%esi
mov    %r12,%rdx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
call   1050 <select@plt>
test   %eax,%eax
jle    11e1 <cr_senddata+0xa1>
movslq %r13d,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %ebx,%edi
xor    %ecx,%ecx
call   1030 <send@plt>
test   %eax,%eax
js     11ed <cr_senddata+0xad>
add    %eax,%r13d
cmp    %ebp,%r13d
jl     1190 <cr_senddata+0x50>
jmp    11e8 <cr_senddata+0xa8>
xor    %r13d,%r13d
jmp    11e8 <cr_senddata+0xa8>
mov    $0xffffffff,%eax
jne    11f2 <cr_senddata+0xb2>
mov    %r13d,%eax
jmp    11f2 <cr_senddata+0xb2>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @cr_senddata(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.timeval
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %13
%14 = load i32, i32* %7
%15 = udiv i32 %14, 1000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* %7
%18 = urem i32 %17, 1000
%19 = mul i32 %18, 1000
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i32 %19, i32* %20
br label %21
21: ; preds = %56, %4
%22 = load i32, i32* %13
%23 = load i32, i32* %9
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %57
25: ; preds = %21
%26 = call i32 @FD_ZERO(i32* %10)
%27 = load i32, i32* %6
%28 = call i32 @FD_SET(i32 %27, i32* %10)
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
%31 = call i32 @select(i32 %30, i32* null, i32* %10, i32* null, %struct.timeval* %11)
store i32 %31, i32* %12
%32 = load i32, i32* %12
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %51
34: ; preds = %25
%35 = load i32, i32* %6
%36 = load i8*, i8** %8
%37 = load i32, i32* %13
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i32, i32* %9
%41 = load i32, i32* %13
%42 = sub nsw i32 %40, %41
%43 = call i32 @send(i32 %35, i8* %39, i32 %42, i32 0)
store i32 %43, i32* %12
%44 = load i32, i32* %12
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %34
store i32 -1, i32* %5
br label %59
47: ; preds = %34
%48 = load i32, i32* %12
%49 = load i32, i32* %13
%50 = add nsw i32 %49, %48
store i32 %50, i32* %13
br label %56
51: ; preds = %25
%52 = load i32, i32* %12
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %51
br label %57
55: ; preds = %51
store i32 -1, i32* %5
br label %59
56: ; preds = %47
br label %21
57: ; preds = %54, %21
%58 = load i32, i32* %13
store i32 %58, i32* %5
br label %59
59: ; preds = %57, %55, %46
%60 = load i32, i32* %5
ret i32 %60
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @cr_senddata(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = udiv i32 %1, 1000
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %9, i32* %10
%11 = urem i32 %1, 1000
%12 = mul nuw nsw i32 %11, 1000
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i32 %12, i32* %13
%14 = icmp sgt i32 %3, 0
br i1 %14, label %15, label %34
15: ; preds = %4
%16 = add nsw i32 %0, 1
br label %19
17: ; preds = %25
%18 = icmp slt i32 %31, %3
br i1 %18, label %19, label %34
19: ; preds = %15, %17
%20 = phi i32 [ 0, %15 ], [ %31, %17 ]
%21 = call i32 @FD_ZERO(i32* %5)
%22 = call i32 @FD_SET(i32 %0, i32* %5)
%23 = call i32 @select(i32 %16, i32* null, i32* %5, i32* null, %struct.timeval* %6)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %19
%26 = sext i32 %20 to i64
%27 = getelementptr inbounds i8, i8* %2, i64 %26
%28 = sub nsw i32 %3, %20
%29 = call i32 @send(i32 %0, i8* %27, i32 %28, i32 0)
%30 = icmp slt i32 %29, 0
%31 = add nsw i32 %29, %20
br i1 %30, label %36, label %17
32: ; preds = %19
%33 = icmp eq i32 %23, 0
br i1 %33, label %34, label %36
34: ; preds = %17, %4, %32
%35 = phi i32 [ %20, %32 ], [ 0, %4 ], [ %31, %17 ]
br label %36
36: ; preds = %25, %32, %34
%37 = phi i32 [ %35, %34 ], [ -1, %32 ], [ -1, %25 ]
ret i32 %37
}