#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 {
  int tv_sec;
  int tv_usec;
};
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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/,
                  int /*<<< orphan*/, struct timeval *);
int /*<<< orphan*/ stderr;
int socketIsReadable(int sock_desc, int ms_timeout) {
  fd_set socketReadSet;
  FD_ZERO(&socketReadSet);
  FD_SET(sock_desc, &socketReadSet);
  struct timeval timeout;
  if (ms_timeout > 0) {
    timeout.tv_sec = ms_timeout / 1000;
    timeout.tv_usec = (ms_timeout % 1000) * 1000;
  } else {
    timeout.tv_sec = 0;
    timeout.tv_usec = 0;
  }
  if (select(sock_desc + 1, &socketReadSet, 0, 0, &timeout) < 0) {
    fprintf(stderr, "Socket Error on select()");
    return 0;
  }
  return (FD_ISSET(sock_desc, &socketReadSet) != 0);
}


x86 assembly (clang, -O0):
<socketIsReadable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1070 <FD_SET@plt>
cmpl   $0x0,-0xc(%rbp)
jle    11a4 <socketIsReadable+0x54>
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x14(%rbp)
jmp    11b2 <socketIsReadable+0x62>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x10(%rbp),%rsi
xor    %ecx,%ecx
lea    -0x18(%rbp),%r8
mov    %ecx,%edx
call   1060 <select@plt>
cmp    $0x0,%rax
jge    11f4 <socketIsReadable+0xa4>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    120f <socketIsReadable+0xbf>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
setne  %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):
<socketIsReadable>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
test   %ebp,%ebp
jle    1196 <socketIsReadable+0x46>
mov    %ebp,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%ecx
sub    %ecx,%ebp
imul   $0x3e8,%ebp,%ecx
jmp    119a <socketIsReadable+0x4a>
xor    %eax,%eax
xor    %ecx,%ecx
mov    %eax,0x8(%rsp)
mov    %ecx,0xc(%rsp)
lea    0x1(%r14),%edi
lea    0x4(%rsp),%rsi
xor    %ebp,%ebp
lea    0x8(%rsp),%r8
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %rax,%rax
js     11d8 <socketIsReadable+0x88>
lea    0x4(%rsp),%rsi
mov    %r14d,%edi
call   1030 <FD_ISSET@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
jmp    11ed <socketIsReadable+0x9d>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
call   1040 <fprintf@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@stderr = global i32 0
@.str = [25 x i8] c"Socket Error on select()\00"
define i32 @socketIsReadable(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timeval
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = call i32 @FD_ZERO(i32* %6)
%9 = load i32, i32* %4
%10 = call i32 @FD_SET(i32 %9, i32* %6)
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %2
%14 = load i32, i32* %5
%15 = sdiv i32 %14, 1000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* %5
%18 = srem i32 %17, 1000
%19 = mul nsw i32 %18, 1000
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i32 %19, i32* %20
br label %24
21: ; preds = %2
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 0, i32* %22
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i32 0, i32* %23
br label %24
24: ; preds = %21, %13
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
%27 = call i64 @select(i32 %26, i32* %6, i32 0, i32 0, %struct.timeval* %7)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %37
32: ; preds = %24
%33 = load i32, i32* %4
%34 = call i64 @FD_ISSET(i32 %33, i32* %6)
%35 = icmp ne i64 %34, 0
%36 = zext i1 %35 to i32
store i32 %36, i32* %3
br label %37
37: ; preds = %32, %29
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@stderr = global i32 0
@.str = [25 x i8] c"Socket Error on select()\00"
define i32 @socketIsReadable(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %3 to i8*
%6 = call i32 @FD_ZERO(i32* %3)
%7 = call i32 @FD_SET(i32 %0, i32* %3)
%8 = bitcast %struct.timeval* %4 to i8*
%9 = icmp sgt i32 %1, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = udiv i32 %1, 1000
%12 = urem i32 %1, 1000
%13 = mul nuw nsw i32 %12, 1000
br label %14
14: ; preds = %2, %10
%15 = phi i32 [ %11, %10 ], [ 0, %2 ]
%16 = phi i32 [ %13, %10 ], [ 0, %2 ]
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %15, i32* %17
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i32 %16, i32* %18
%19 = add nsw i32 %0, 1
%20 = call i64 @select(i32 %19, i32* %3, i32 0, i32 0, %struct.timeval* %4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %29
25: ; preds = %14
%26 = call i64 @FD_ISSET(i32 %0, i32* %3)
%27 = icmp ne i64 %26, 0
%28 = zext i1 %27 to i32
br label %29
29: ; preds = %25, %22
%30 = phi i32 [ 0, %22 ], [ %28, %25 ]
ret i32 %30
}