#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 */
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 can_send_to_socket(int sock) {
  fd_set fs;
  struct timeval nowait;
  if (sock == -1) {
    return 0;
  }
  nowait.tv_sec = 0;
  nowait.tv_usec = 0;
  FD_ZERO(&fs);
  FD_SET(sock, &fs);
  return select(sock + 1, NULL, &fs, NULL, &nowait) == 1;
}


x86 assembly (clang, -O0):
<can_send_to_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1151 <can_send_to_socket+0x21>
movl   $0x0,-0x4(%rbp)
jmp    119e <can_send_to_socket+0x6e>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0xc(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1050 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0xc(%rbp),%rdx
lea    -0x20(%rbp),%r8
mov    %rcx,%rsi
call   1040 <select@plt>
cmp    $0x1,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<can_send_to_socket>:
cmp    $0xffffffff,%edi
je     1185 <can_send_to_socket+0x55>
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
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
lea    0x10(%rsp),%r8
mov    %ebx,%edi
xor    %esi,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1040 <select@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
sete   %al
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
ret

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

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