#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 */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int write(int, char *, int);
int timeoutWrite(int t, int fd, char *b, int l) {
  int rc = -1;
  fd_set fds;
  struct timeval tv;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  tv.tv_sec = t;
  tv.tv_usec = 0;
  if (select(fd + 1, NULL, &fds, NULL, &tv)) {
  again:
    if ((rc = write(fd, b, l)) < 0) {
      if (errno == EINTR || EAGAIN == errno)
        goto again;
    }
  }
  return (rc);
}


x86 assembly (clang, -O0):
<timeoutWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x1c(%rbp),%rdx
lea    -0x30(%rbp),%r8
mov    %rcx,%rsi
call   1050 <select@plt>
cmp    $0x0,%rax
je     1206 <timeoutWrite+0xc6>
jmp    11a8 <timeoutWrite+0x68>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1201 <timeoutWrite+0xc1>
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rax
je     11f7 <timeoutWrite+0xb7>
mov    0x2de4(%rip),%rax
mov    (%rax),%rax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%rax
jne    11fc <timeoutWrite+0xbc>
jmp    11a8 <timeoutWrite+0x68>
jmp    1201 <timeoutWrite+0xc1>
jmp    1206 <timeoutWrite+0xc6>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeoutWrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %edi,%r15d
lea    0x4(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r12,%rsi
call   1060 <FD_SET@plt>
mov    %r15d,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x1(%rbp),%edi
lea    0x8(%rsp),%r8
xor    %esi,%esi
mov    %r12,%rdx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %rax,%rax
je     11d2 <timeoutWrite+0x92>
mov    0x2e42(%rip),%r15
mov    0x2e33(%rip),%r12
mov    0x2e1c(%rip),%r13
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    11d7 <timeoutWrite+0x97>
mov    (%r15),%rcx
cmp    (%r12),%rcx
je     11b0 <timeoutWrite+0x70>
cmp    %rcx,0x0(%r13)
je     11b0 <timeoutWrite+0x70>
jmp    11d7 <timeoutWrite+0x97>
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, i64 }
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @timeoutWrite(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.timeval
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 -1, i32* %9
%12 = call i32 @FD_ZERO(i32* %10)
%13 = load i32, i32* %6
%14 = call i32 @FD_SET(i32 %13, i32* %10)
%15 = load i32, i32* %5
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %17
%18 = load i32, i32* %6
%19 = add nsw i32 %18, 1
%20 = call i64 @select(i32 %19, i32* null, i32* %10, i32* null, %struct.timeval* %11)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %40
22: ; preds = %4
br label %23
23: ; preds = %37, %22
%24 = load i32, i32* %6
%25 = load i8*, i8** %7
%26 = load i32, i32* %8
%27 = call i32 @write(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %39
29: ; preds = %23
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = load i64, i64* @EAGAIN
%35 = load i64, i64* @errno
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33, %29
br label %23
38: ; preds = %33
br label %39
39: ; preds = %38, %23
br label %40
40: ; preds = %39, %4
%41 = load i32, i32* %9
ret i32 %41
}

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