#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_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct timeval *, int /*<<< orphan*/, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int write(int, void const *, int);
int select_and_write(int fd, const void *buf, int size) {
  fd_set write_fds;
  struct timeval tv;
  FD_ZERO(&write_fds);
  FD_SET(fd, &write_fds);
  memset(&tv, 0, sizeof(struct timeval));
  tv.tv_usec = 10000;
  if (select(fd + 1, NULL, &write_fds, NULL, &tv) > 0 &&
      FD_ISSET(fd, &write_fds)) {
    return write(fd, buf, size);
  }
  return -1;
}


x86 assembly (clang, -O0):
<select_and_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <FD_SET@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movl   $0x2710,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%r8
mov    %rcx,%rsi
call   1070 <select@plt>
cmp    $0x0,%rax
jle    11ef <select_and_write+0x8f>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11ef <select_and_write+0x8f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x4(%rbp)
jmp    11f6 <select_and_write+0x96>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<select_and_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
movl   $0x2710,0x8(%rsp)
lea    0x1(%rbp),%edi
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
mov    %r12,%r8
call   1070 <select@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jle    11e3 <select_and_write+0x83>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11e3 <select_and_write+0x83>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
define i32 @select_and_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = call i32 @FD_ZERO(i32* %8)
%11 = load i32, i32* %5
%12 = call i32 @FD_SET(i32 %11, i32* %8)
%13 = call i32 @memset(%struct.timeval* %9, i32 0, i32 4)
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 10000, i32* %14
%15 = load i32, i32* %5
%16 = add nsw i32 %15, 1
%17 = call i64 @select(i32 %16, i32* null, i32* %8, i32* null, %struct.timeval* %9)
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %3
%20 = load i32, i32* %5
%21 = call i64 @FD_ISSET(i32 %20, i32* %8)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i32, i32* %5
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = call i32 @write(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %4
br label %29
28: ; preds = %19, %3
store i32 -1, i32* %4
br label %29
29: ; preds = %28, %23
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
define i32 @select_and_write(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = call i32 @FD_ZERO(i32* %4)
%9 = call i32 @FD_SET(i32 %0, i32* %4)
%10 = call i32 @memset(%struct.timeval* %5, i32 0, i32 4)
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 10000, i32* %11
%12 = add nsw i32 %0, 1
%13 = call i64 @select(i32 %12, i32* null, i32* %4, i32* null, %struct.timeval* %5)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = call i64 @FD_ISSET(i32 %0, i32* %4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = call i32 @write(i32 %0, i8* %1, i32 %2)
br label %20
20: ; preds = %3, %15, %18
%21 = phi i32 [ %19, %18 ], [ -1, %15 ], [ -1, %3 ]
ret i32 %21
}