#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 *);
bool serial_select(int sock, int mode) {
  fd_set set;
  struct timeval timeout;
  FD_ZERO(&set);
  FD_SET(sock, &set);
  timeout.tv_sec = 0;
  timeout.tv_usec = 0;
  int rv =
      select(sock + 1, ((mode == 0) ? &set : NULL), ((mode == 1) ? &set : NULL),
             ((mode == 2) ? &set : NULL), &timeout);
  return rv > 0;
}


x86 assembly (clang, -O0):
<serial_select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1050 <FD_SET@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1183 <serial_select+0x53>
lea    -0xc(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    118e <serial_select+0x5e>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    118e <serial_select+0x5e>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11ad <serial_select+0x7d>
lea    -0xc(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11b8 <serial_select+0x88>
xor    %eax,%eax
mov    %rax,-0x40(%rbp)
jmp    11b8 <serial_select+0x88>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11d7 <serial_select+0xa7>
lea    -0xc(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    11e2 <serial_select+0xb2>
xor    %eax,%eax
mov    %rax,-0x50(%rbp)
jmp    11e2 <serial_select+0xb2>
mov    -0x48(%rbp),%rdx
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%edi
mov    -0x50(%rbp),%rcx
lea    -0x20(%rbp),%r8
call   1040 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_select>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <FD_SET@plt>
add    $0x1,%ebp
xor    %ecx,%ecx
test   %ebx,%ebx
mov    $0x0,%esi
cmove  %r14,%rsi
cmp    $0x1,%ebx
mov    $0x0,%edx
cmove  %r14,%rdx
cmp    $0x2,%ebx
cmove  %r14,%rcx
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%r8
mov    %ebp,%edi
call   1040 <select@plt>
xor    %ecx,%ecx
test   %eax,%eax
setg   %cl
mov    %ecx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @serial_select(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = call i32 @FD_ZERO(i32* %3)
%8 = call i32 @FD_SET(i32 %0, i32* %3)
%9 = add nsw i32 %0, 1
%10 = icmp eq i32 %1, 0
%11 = select i1 %10, i32* %3, i32* null
%12 = icmp eq i32 %1, 1
%13 = select i1 %12, i32* %3, i32* null
%14 = icmp eq i32 %1, 2
%15 = select i1 %14, i32* %3, i32* null
%16 = bitcast %struct.timeval* %4 to i8*
%17 = call i32 @select(i32 %9, i32* %11, i32* %13, i32* %15, %struct.timeval* %4)
%18 = icmp sgt i32 %17, 0
%19 = zext i1 %18 to i32
ret i32 %19
}