#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__ EBADF;
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 is_closed(int fd) {
  struct timeval tv;
  fd_set readset;
  FD_ZERO(&readset);
  FD_SET(fd, &readset);
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  if (select(fd + 1, &readset, NULL, NULL, &tv) < 0) {
    if (errno == EBADF) {
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<is_closed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x1c(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1050 <FD_SET@plt>
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
cmp    $0x0,%rax
jge    11af <is_closed+0x7f>
mov    0x2e55(%rip),%rax
mov    (%rax),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
jne    11aa <is_closed+0x7a>
movl   $0x1,-0x4(%rbp)
jmp    11b6 <is_closed+0x86>
jmp    11af <is_closed+0x7f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_closed>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <FD_SET@plt>
add    $0x1,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%r8
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
test   %rax,%rax
jns    118e <is_closed+0x5e>
mov    0x2e66(%rip),%rax
mov    (%rax),%rcx
mov    0x2e54(%rip),%rdx
mov    $0x1,%eax
cmp    (%rdx),%rcx
je     1190 <is_closed+0x60>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

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