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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int send(int, char *, size_t, int /*<<< orphan*/);
ssize_t write_with_select(int sock, void *buf, size_t len, int tout) {
  size_t sent = 0;
  fd_set writefd;
  struct timeval timeout;
  while (sent < len) {
    FD_ZERO(&writefd);
    FD_SET(sock, &writefd);
    timeout.tv_sec = tout;
    timeout.tv_usec = 0;
    if (select(sock + 1, NULL, &writefd, NULL, &timeout) <= 0)
      return -1;
    ssize_t howmany;
    if ((howmany = send(sock, ((char *)buf) + sent, len - sent, 0)) <= 0)
      return -1;
    sent += howmany;
  }
  return sent;
}


x86 assembly (clang, -O0):
<write_with_select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1202 <write_with_select+0xc2>
lea    -0x2c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x2c(%rbp),%rdx
lea    -0x40(%rbp),%r8
mov    %rcx,%rsi
call   1050 <select@plt>
cmp    $0x0,%rax
jg     11bf <write_with_select+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <write_with_select+0xc9>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
jg     11f1 <write_with_select+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <write_with_select+0xc9>
movslq -0x44(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    115e <write_with_select+0x1e>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_with_select>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,0x4(%rsp)
mov    %rdx,%rbx
mov    %rsi,0x8(%rsp)
mov    %edi,%ebp
lea    0x1(%rbp),%r12d
xor    %r15d,%r15d
lea    0x14(%rsp),%r13
mov    $0xffffffff,%r14d
xchg   %ax,%ax
cmp    %rbx,%r15
jae    11d4 <write_with_select+0x94>
mov    %r13,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r13,%rsi
call   1060 <FD_SET@plt>
mov    0x4(%rsp),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    %r12d,%edi
xor    %esi,%esi
mov    %r13,%rdx
xor    %ecx,%ecx
lea    0x18(%rsp),%r8
call   1050 <select@plt>
test   %rax,%rax
jle    11d7 <write_with_select+0x97>
mov    0x8(%rsp),%rax
lea    (%rax,%r15,1),%rsi
mov    %rbx,%rdx
sub    %r15,%rdx
mov    %ebp,%edi
xor    %ecx,%ecx
call   1030 <send@plt>
cltq
add    %rax,%r15
test   %eax,%eax
jg     1170 <write_with_select+0x30>
jmp    11d7 <write_with_select+0x97>
mov    %r15d,%r14d
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i32 @write_with_select(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
%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 i32 %3, i32* %9
store i64 0, i64* %10
br label %14
14: ; preds = %41, %4
%15 = load i64, i64* %10
%16 = load i64, i64* %8
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %46
18: ; preds = %14
%19 = call i32 @FD_ZERO(i32* %11)
%20 = load i32, i32* %6
%21 = call i32 @FD_SET(i32 %20, i32* %11)
%22 = load i32, i32* %9
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 0, i64* %24
%25 = load i32, i32* %6
%26 = add nsw i32 %25, 1
%27 = call i64 @select(i32 %26, i32* null, i32* %11, i32* null, %struct.timeval* %12)
%28 = icmp sle i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %18
store i32 -1, i32* %5
br label %49
30: ; preds = %18
%31 = load i32, i32* %6
%32 = load i8*, i8** %7
%33 = load i64, i64* %10
%34 = getelementptr inbounds i8, i8* %32, i64 %33
%35 = load i64, i64* %8
%36 = load i64, i64* %10
%37 = sub i64 %35, %36
%38 = call i32 @send(i32 %31, i8* %34, i64 %37, i32 0)
store i32 %38, i32* %13
%39 = icmp sle i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %30
store i32 -1, i32* %5
br label %49
41: ; preds = %30
%42 = load i32, i32* %13
%43 = sext i32 %42 to i64
%44 = load i64, i64* %10
%45 = add i64 %44, %43
store i64 %45, i64* %10
br label %14
46: ; preds = %14
%47 = load i64, i64* %10
%48 = trunc i64 %47 to i32
store i32 %48, i32* %5
br label %49
49: ; preds = %46, %40, %29
%50 = load i32, i32* %5
ret i32 %50
}

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