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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
char *strerror(scalar_t__);
int write(int, char *, int);
int timeout_write(int fd1, char ch) {
  fd_set out_set;
  struct timeval timeout;
  int res;
  FD_ZERO(&out_set);
  FD_SET(fd1, &out_set);
  timeout.tv_sec = 0;
  timeout.tv_usec = 250;
  res = select(fd1 + 1, NULL, &out_set, NULL, &timeout);
  if (res < 0)
    printf("ERROR: %s\n", strerror(errno));
  if (res <= 0)
    return 0;
retry:
  res = write(fd1, &ch, 1);
  if (res < 0)
    printf("ERROR: %s\n", strerror(errno));
  if (res < 0) {
    if (errno == EINTR)
      goto retry;
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<timeout_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
lea    -0x10(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <FD_SET@plt>
movq   $0x0,-0x18(%rbp)
movl   $0xfa,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%r8
mov    %rcx,%rsi
call   1060 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11da <timeout_write+0x7a>
mov    0x2e1d(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe2b(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x24(%rbp)
jg     11f0 <timeout_write+0x90>
movl   $0x0,-0x4(%rbp)
jmp    126d <timeout_write+0x10d>
jmp    11f5 <timeout_write+0x95>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1231 <timeout_write+0xd1>
mov    0x2dc6(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xdd4(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x24(%rbp)
jge    1266 <timeout_write+0x106>
mov    0x2d9e(%rip),%rax
mov    (%rax),%rax
mov    0x2d8c(%rip),%rcx
cmp    (%rcx),%rax
jne    125a <timeout_write+0xfa>
jmp    11f5 <timeout_write+0x95>
movl   $0x1,-0x4(%rbp)
jmp    126d <timeout_write+0x10d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeout_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r13d
mov    %sil,0x3(%rsp)
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1050 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %r15,%rsi
call   1080 <FD_SET@plt>
movq   $0x0,0x10(%rsp)
movl   $0xfa,0x8(%rsp)
lea    0x1(%r13),%edi
xor    %r14d,%r14d
lea    0x8(%rsp),%r8
xor    %esi,%esi
mov    %r15,%rdx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
js     1218 <timeout_write+0xb8>
je     1236 <timeout_write+0xd6>
lea    0x3(%rsp),%r15
mov    0x2e17(%rip),%rbx
lea    0xe30(%rip),%r12
mov    0x2e01(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    1236 <timeout_write+0xd6>
mov    (%rbx),%rdi
call   1070 <strerror@plt>
mov    %r12,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    (%rbx),%rax
cmp    0x0(%rbp),%rax
je     11e0 <timeout_write+0x80>
mov    $0x1,%r14d
jmp    1236 <timeout_write+0xd6>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
lea    0xdd2(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %r14d,%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 }
@.str = [11 x i8] c"ERROR: %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @timeout_write(i32 %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i32
store i32 %0, i32* %4
store i8 %1, i8* %5
%9 = call i32 @FD_ZERO(i32* %6)
%10 = load i32, i32* %4
%11 = call i32 @FD_SET(i32 %10, i32* %6)
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i64 0, i64* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 250, i32* %13
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 1
%16 = call i32 @select(i32 %15, i32* null, i32* %6, i32* null, %struct.timeval* %7)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = load i64, i64* @errno
%21 = call i8* @strerror(i64 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %19, %2
%24 = load i32, i32* %8
%25 = icmp sle i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 0, i32* %3
br label %47
27: ; preds = %23
br label %28
28: ; preds = %44, %27
%29 = load i32, i32* %4
%30 = call i32 @write(i32 %29, i8* %5, i32 1)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = load i64, i64* @errno
%35 = call i8* @strerror(i64 %34)
%36 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %33, %28
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %37
%41 = load i64, i64* @errno
%42 = load i64, i64* @EINTR
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40
br label %28
45: ; preds = %40
store i32 1, i32* %3
br label %47
46: ; preds = %37
store i32 0, i32* %3
br label %47
47: ; preds = %46, %45, %26
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [11 x i8] c"ERROR: %s\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @timeout_write(i32 %0, i8 signext %1) {
%3 = alloca i8
%4 = alloca i32
%5 = alloca %struct.timeval
store i8 %1, i8* %3
%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 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 250, i32* %11
%12 = add nsw i32 %0, 1
%13 = call i32 @select(i32 %12, i32* null, i32* %4, i32* null, %struct.timeval* %5)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i64, i64* @errno
%17 = call i8* @strerror(i64 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %31
19: ; preds = %2
%20 = icmp eq i32 %13, 0
br i1 %20, label %31, label %21
21: ; preds = %19, %24
%22 = call i32 @write(i32 %0, i8* %3, i32 1)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = call i8* @strerror(i64 %25)
%27 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br i1 %30, label %21, label %31
31: ; preds = %21, %24, %15, %19
%32 = phi i32 [ 0, %19 ], [ 0, %15 ], [ 0, %21 ], [ 1, %24 ]
ret i32 %32
}