#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 {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
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;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int sock_check_opened(int socket) {
  fd_set readfds, writefds, exceptfds;
  int retval;
  struct timeval timeout;
  for (;;) {
    FD_ZERO(&readfds);
    FD_ZERO(&writefds);
    FD_ZERO(&exceptfds);
    FD_SET(socket, &exceptfds);
    timeout.tv_sec = 0;
    timeout.tv_usec = 0;
    retval = select(socket + 1, &readfds, &writefds, &exceptfds, &timeout);
    if (retval == -1 && (errno != EAGAIN && errno != EINTR))
      return 0;
    if (retval != -1)
      return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sock_check_opened>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1030 <FD_ZERO@plt>
lea    -0x10(%rbp),%rdi
call   1030 <FD_ZERO@plt>
lea    -0x14(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1050 <FD_SET@plt>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
lea    -0x14(%rbp),%rcx
lea    -0x28(%rbp),%r8
call   1040 <select@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11da <sock_check_opened+0xaa>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%rax
je     11da <sock_check_opened+0xaa>
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%rax
je     11da <sock_check_opened+0xaa>
movl   $0x0,-0x4(%rbp)
jmp    11f5 <sock_check_opened+0xc5>
cmpl   $0xffffffff,-0x18(%rbp)
je     11f0 <sock_check_opened+0xc0>
movl   $0x1,-0x4(%rbp)
jmp    11f5 <sock_check_opened+0xc5>
jmp    113b <sock_check_opened+0xb>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_check_opened>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    0x1(%rbx),%r12d
lea    0xc(%rsp),%r14
lea    0x8(%rsp),%r15
lea    0x4(%rsp),%rbp
lea    0x10(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    %r15,%rdi
call   1030 <FD_ZERO@plt>
mov    %rbp,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <FD_SET@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %rbp,%rcx
mov    %r13,%r8
call   1040 <select@plt>
cmp    $0xffffffff,%eax
jne    11c9 <sock_check_opened+0x99>
mov    0x2e36(%rip),%rax
mov    (%rax),%rax
mov    0x2e14(%rip),%rcx
cmp    (%rcx),%rax
je     1160 <sock_check_opened+0x30>
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
je     1160 <sock_check_opened+0x30>
xor    %eax,%eax
jmp    11ce <sock_check_opened+0x9e>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
define i32 @sock_check_opened(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast %struct.timeval* %5 to i8*
%10 = add nsw i32 %0, 1
%11 = bitcast %struct.timeval* %5 to i8*
br label %12
12: ; preds = %19, %1
%13 = call i32 @FD_ZERO(i32* %2)
%14 = call i32 @FD_ZERO(i32* %3)
%15 = call i32 @FD_ZERO(i32* %4)
%16 = call i32 @FD_SET(i32 %0, i32* %4)
%17 = call i32 @select(i32 %10, i32* %2, i32* %3, i32* %4, %struct.timeval* %5)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %26
19: ; preds = %12
%20 = load i64, i64* @errno
%21 = load i64, i64* @EAGAIN
%22 = icmp eq i64 %20, %21
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %20, %23
%25 = select i1 %22, i1 true, i1 %24
br i1 %25, label %12, label %26
26: ; preds = %12, %19
%27 = phi i32 [ 0, %19 ], [ 1, %12 ]
ret i32 %27
}