#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__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int select(int, int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/ *,
           struct timeval *);
int network_ok(int fd) {
  int result;
  fd_set efds;
  struct timeval timeout;
  timeout.tv_sec = 0;
  timeout.tv_usec = 0;
  FD_ZERO(&efds);
  FD_SET(fd, &efds);
  result = select(fd + 1, 0, 0, &efds, &timeout);
  if (result < 0 || FD_ISSET(fd, &efds)) {
    return 0;
  } else {
    return 1;
  }
}


x86 assembly (clang, -O0):
<network_ok>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x10(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %edx,%edx
lea    -0x10(%rbp),%rcx
lea    -0x20(%rbp),%r8
mov    %edx,%esi
call   1050 <select@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11aa <network_ok+0x6a>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11b6 <network_ok+0x76>
movl   $0x0,-0x4(%rbp)
jmp    11bd <network_ok+0x7d>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<network_ok>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%rbx),%edi
xor    %ebp,%ebp
lea    0x10(%rsp),%r8
xor    %esi,%esi
xor    %edx,%edx
mov    %r14,%rcx
call   1050 <select@plt>
test   %eax,%eax
js     1198 <network_ok+0x58>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @network_ok(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
store i32 %0, i32* %3
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i64 0, i64* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i64 0, i64* %8
%9 = call i32 @FD_ZERO(i32* %5)
%10 = load i32, i32* %3
%11 = call i32 @FD_SET(i32 %10, i32* %5)
%12 = load i32, i32* %3
%13 = add nsw i32 %12, 1
%14 = call i32 @select(i32 %13, i32 0, i32 0, i32* %5, %struct.timeval* %6)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %1
%18 = load i32, i32* %3
%19 = call i64 @FD_ISSET(i32 %18, i32* %5)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17, %1
store i32 0, i32* %2
br label %23
22: ; preds = %17
store i32 1, i32* %2
br label %23
23: ; preds = %22, %21
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @network_ok(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast %struct.timeval* %3 to i8*
%7 = call i32 @FD_ZERO(i32* %2)
%8 = call i32 @FD_SET(i32 %0, i32* %2)
%9 = add nsw i32 %0, 1
%10 = call i32 @select(i32 %9, i32 0, i32 0, i32* %2, %struct.timeval* %3)
%11 = icmp slt i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %1
%13 = call i64 @FD_ISSET(i32 %0, i32* %2)
%14 = icmp eq i64 %13, 0
%15 = zext i1 %14 to i32
br label %16
16: ; preds = %12, %1
%17 = phi i32 [ 0, %1 ], [ %15, %12 ]
ret i32 %17
}