#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 /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ ETIMEDOUT;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int write(int, char *, int);
int timeoutwrite(int t, int fd, char *buf, int len) {
  fd_set wfds;
  struct timeval tv;
  tv.tv_sec = t;
  tv.tv_usec = 0;
  FD_ZERO(&wfds);
  FD_SET(fd, &wfds);
  if (select(fd + 1, (fd_set *)0, &wfds, (fd_set *)0, &tv) == -1)
    return -1;
  if (FD_ISSET(fd, &wfds))
    return write(fd, buf, len);
  errno = ETIMEDOUT;
  return -1;
}


x86 assembly (clang, -O0):
<timeoutwrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%r8
mov    %rcx,%rsi
call   1060 <select@plt>
cmp    $0xffffffff,%eax
jne    11b7 <timeoutwrite+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <timeoutwrite+0xad>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11e4 <timeoutwrite+0x94>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x4(%rbp)
jmp    11fd <timeoutwrite+0xad>
mov    0x2ded(%rip),%rax
mov    (%rax),%ecx
mov    0x2dec(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeoutwrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %esi,%ebp
mov    %edi,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
lea    0x1(%rbp),%edi
lea    0x8(%rsp),%r8
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1060 <select@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11d8 <timeoutwrite+0x88>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11c6 <timeoutwrite+0x76>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
mov    %eax,%ebx
jmp    11d8 <timeoutwrite+0x88>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    0x2e0a(%rip),%rcx
mov    %eax,(%rcx)
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@ETIMEDOUT = global i32 0
@errno = global i32 0
define i32 @timeoutwrite(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
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i32, i32* %6
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %14
%15 = call i32 @FD_ZERO(i32* %10)
%16 = load i32, i32* %7
%17 = call i32 @FD_SET(i32 %16, i32* %10)
%18 = load i32, i32* %7
%19 = add nsw i32 %18, 1
%20 = call i32 @select(i32 %19, i32* null, i32* %10, i32* null, %struct.timeval* %11)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %4
store i32 -1, i32* %5
br label %34
23: ; preds = %4
%24 = load i32, i32* %7
%25 = call i64 @FD_ISSET(i32 %24, i32* %10)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i32, i32* %7
%29 = load i8*, i8** %8
%30 = load i32, i32* %9
%31 = call i32 @write(i32 %28, i8* %29, i32 %30)
store i32 %31, i32* %5
br label %34
32: ; preds = %23
%33 = load i32, i32* @ETIMEDOUT
store i32 %33, i32* @errno
store i32 -1, i32* %5
br label %34
34: ; preds = %32, %27, %22
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@ETIMEDOUT = global i32 0
@errno = global i32 0
define i32 @timeoutwrite(i32 %0, i32 %1, i8* nocapture %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
store i32 %0, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %10
%11 = call i32 @FD_ZERO(i32* %5)
%12 = call i32 @FD_SET(i32 %1, i32* %5)
%13 = add nsw i32 %1, 1
%14 = call i32 @select(i32 %13, i32* null, i32* %5, i32* null, %struct.timeval* %6)
%15 = icmp eq i32 %14, -1
br i1 %15, label %23, label %16
16: ; preds = %4
%17 = call i64 @FD_ISSET(i32 %1, i32* %5)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @write(i32 %1, i8* %2, i32 %3)
br label %23
21: ; preds = %16
%22 = load i32, i32* @ETIMEDOUT
store i32 %22, i32* @errno
br label %23
23: ; preds = %4, %21, %19
%24 = phi i32 [ %20, %19 ], [ -1, %21 ], [ -1, %4 ]
ret i32 %24
}