#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 {
  long tv_sec;
  long tv_usec;
};
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 simple_socket_select(int socket, int directions, long timeout_millis) {
  struct timeval timeout;
  fd_set fd;
  timeout.tv_sec = timeout_millis / 1000;
  timeout.tv_usec = (timeout_millis % 1000) * 1000;
  FD_ZERO(&fd);
  FD_SET(socket, &fd);
  return select(socket + 1, ((directions & 1) ? &fd : NULL),
                ((directions & 2) ? &fd : NULL),
                ((directions & 4) ? &fd : NULL), &timeout);
}


x86 assembly (clang, -O0):
<simple_socket_select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
imul   $0x3e8,%rdx,%rax
mov    %rax,-0x18(%rbp)
lea    -0x24(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1050 <FD_SET@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11a7 <simple_socket_select+0x77>
lea    -0x24(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11b2 <simple_socket_select+0x82>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    11b2 <simple_socket_select+0x82>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11d6 <simple_socket_select+0xa6>
lea    -0x24(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11e1 <simple_socket_select+0xb1>
xor    %eax,%eax
mov    %rax,-0x40(%rbp)
jmp    11e1 <simple_socket_select+0xb1>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     1205 <simple_socket_select+0xd5>
lea    -0x24(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1210 <simple_socket_select+0xe0>
xor    %eax,%eax
mov    %rax,-0x50(%rbp)
jmp    1210 <simple_socket_select+0xe0>
mov    -0x48(%rbp),%rdx
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%edi
mov    -0x50(%rbp),%rcx
lea    -0x20(%rbp),%r8
call   1040 <select@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<simple_socket_select>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%rcx
mov    %esi,%r14d
mov    %edi,%ebx
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
mov    %rcx,%rax
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
mov    %rdx,0x10(%rsp)
imul   $0x3e8,%rdx,%rax
sub    %rax,%rcx
imul   $0x3e8,%rcx,%rax
mov    %rax,0x18(%rsp)
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <FD_SET@plt>
add    $0x1,%ebx
xor    %eax,%eax
test   $0x1,%r14b
mov    %rbp,%rsi
cmove  %rax,%rsi
test   $0x2,%r14b
mov    %rbp,%rdx
cmove  %rax,%rdx
test   $0x4,%r14b
cmove  %rax,%rbp
lea    0x10(%rsp),%r8
mov    %ebx,%edi
mov    %rbp,%rcx
call   1040 <select@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @simple_socket_select(i32 %0, i32 %1, i64 %2) {
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = bitcast %struct.timeval* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = sdiv i64 %2, 1000
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i64 %8, i64* %9
%10 = srem i64 %2, 1000
%11 = mul nsw i64 %10, 1000
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 %11, i64* %12
%13 = call i32 @FD_ZERO(i32* %5)
%14 = call i32 @FD_SET(i32 %0, i32* %5)
%15 = add nsw i32 %0, 1
%16 = and i32 %1, 1
%17 = icmp eq i32 %16, 0
%18 = select i1 %17, i32* null, i32* %5
%19 = and i32 %1, 2
%20 = icmp eq i32 %19, 0
%21 = select i1 %20, i32* null, i32* %5
%22 = and i32 %1, 4
%23 = icmp eq i32 %22, 0
%24 = select i1 %23, i32* null, i32* %5
%25 = call i32 @select(i32 %15, i32* %18, i32* %21, i32* %24, %struct.timeval* %4)
ret i32 %25
}