#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 = -99;
  fd_set fds;
  struct timeval tv;
  if (fd < 0)
    return (fd);
  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    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0xffffff9d,-0x20(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1171 <timeoutWrite+0x31>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1221 <timeoutWrite+0xe1>
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0xc(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x24(%rbp),%rdx
lea    -0x38(%rbp),%r8
mov    %rcx,%rsi
call   1050 <select@plt>
cmp    $0x0,%rax
je     121b <timeoutWrite+0xdb>
jmp    11bd <timeoutWrite+0x7d>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1216 <timeoutWrite+0xd6>
mov    0x2e01(%rip),%rax
mov    (%rax),%rax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%rax
je     120c <timeoutWrite+0xcc>
mov    0x2dcf(%rip),%rax
mov    (%rax),%rax
mov    0x2ddd(%rip),%rcx
cmp    (%rcx),%rax
jne    1211 <timeoutWrite+0xd1>
jmp    11bd <timeoutWrite+0x7d>
jmp    1216 <timeoutWrite+0xd6>
jmp    121b <timeoutWrite+0xdb>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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    %esi,%ebx
test   %esi,%esi
js     11d2 <timeoutWrite+0x92>
mov    %ecx,%r14d
mov    %rdx,%rbp
mov    %edi,%r15d
lea    0x4(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   1060 <FD_SET@plt>
mov    %r15d,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x1(%rbx),%edi
lea    0x8(%rsp),%r8
xor    %esi,%esi
mov    %r12,%rdx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %rax,%rax
je     11d6 <timeoutWrite+0x96>
mov    0x2e3e(%rip),%r15
mov    0x2e2f(%rip),%r12
mov    0x2e18(%rip),%r13
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    11db <timeoutWrite+0x9b>
mov    (%r15),%rcx
cmp    (%r12),%rcx
je     11b0 <timeoutWrite+0x70>
cmp    %rcx,0x0(%r13)
je     11b0 <timeoutWrite+0x70>
jmp    11db <timeoutWrite+0x9b>
mov    %ebx,%eax
jmp    11db <timeoutWrite+0x9b>
mov    $0xffffff9d,%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 i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.timeval
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 -99, i32* %10
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = load i32, i32* %7
store i32 %16, i32* %5
br label %48
17: ; preds = %4
%18 = call i32 @FD_ZERO(i32* %11)
%19 = load i32, i32* %7
%20 = call i32 @FD_SET(i32 %19, i32* %11)
%21 = load i32, i32* %6
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 0, i64* %23
%24 = load i32, i32* %7
%25 = add nsw i32 %24, 1
%26 = call i64 @select(i32 %25, i32* null, i32* %11, i32* null, %struct.timeval* %12)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %46
28: ; preds = %17
br label %29
29: ; preds = %43, %28
%30 = load i32, i32* %7
%31 = load i8*, i8** %8
%32 = load i32, i32* %9
%33 = call i32 @write(i32 %30, i8* %31, i32 %32)
store i32 %33, i32* %10
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %45
35: ; preds = %29
%36 = load i64, i64* @errno
%37 = load i64, i64* @EINTR
%38 = icmp eq i64 %36, %37
br i1 %38, label %43, label %39
39: ; preds = %35
%40 = load i64, i64* @EAGAIN
%41 = load i64, i64* @errno
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %39, %35
br label %29
44: ; preds = %39
br label %45
45: ; preds = %44, %29
br label %46
46: ; preds = %45, %17
%47 = load i32, i32* %10
store i32 %47, i32* %5
br label %48
48: ; preds = %46, %15
%49 = load i32, i32* %5
ret i32 %49
}

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