#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_usec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ error;

/* 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*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int /*<<< orphan*/ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int is_readable(int socketfd) {
  int nRet = -1;
  if (socketfd > 0) {
    fd_set fdread;
    fd_set fderror;
    FD_ZERO(&fdread);
    FD_ZERO(&fderror);
    FD_SET(socketfd, &fdread);
    FD_SET(socketfd, &fderror);
    struct timeval tvSelect;
    tvSelect.tv_sec = 0;
    tvSelect.tv_usec = 1;
    int nSelectRet = select(socketfd + 1, &fdread, NULL, &fderror, &tvSelect);
    if (nSelectRet > 0) {
      if (FD_ISSET(socketfd, &fdread)) {
        int error = 0;
        int errlen = sizeof(error);
        getsockopt(socketfd, SOL_SOCKET, SO_ERROR, &error,
                   (socklen_t *)&errlen);
        if (0 == error) {
          nRet = 1;
        } else {
          printf("is_readable.select some error! error:%d\n", error);
          nRet = -5;
        }
      } else if (FD_ISSET(socketfd, &fderror)) {
        printf("is_readable.select with error fd!\n");
        nRet = -4;
      } else {
        printf("is_readable.select CANNOT understand!\n");
      }
    } else if (nSelectRet < 0) {
      printf("is_readable.select fail!\n");
      nRet = -3;
    } else if (nSelectRet == 0) {
      nRet = 0;
    }
  } else {
    printf("is_readable.select socketfd must be > 0!\n");
  }
  return nRet;
}


x86 assembly (clang, -O0):
<is_readable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    12e3 <is_readable+0x183>
lea    -0xc(%rbp),%rdi
call   1060 <FD_ZERO@plt>
lea    -0x10(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <FD_SET@plt>
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x10(%rbp),%rcx
lea    -0x20(%rbp),%r8
call   1070 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    129f <is_readable+0x13f>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1257 <is_readable+0xf7>
movl   $0x0,-0x28(%rbp)
movl   $0x4,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
lea    -0x2c(%rbp),%r8
call   1050 <getsockopt@plt>
xor    %eax,%eax
cmp    -0x28(%rbp),%eax
jne    123a <is_readable+0xda>
movl   $0x1,-0x8(%rbp)
jmp    1252 <is_readable+0xf2>
mov    -0x28(%rbp),%esi
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xfffffffb,-0x8(%rbp)
jmp    129a <is_readable+0x13a>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1287 <is_readable+0x127>
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xfffffffc,-0x8(%rbp)
jmp    1295 <is_readable+0x135>
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    129a <is_readable+0x13a>
jmp    12de <is_readable+0x17e>
cmpl   $0x0,-0x24(%rbp)
jge    12c3 <is_readable+0x163>
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xfffffffd,-0x8(%rbp)
jmp    12d9 <is_readable+0x179>
cmpl   $0x0,-0x24(%rbp)
jne    12d4 <is_readable+0x174>
movl   $0x0,-0x8(%rbp)
jmp    12d9 <is_readable+0x179>
jmp    12de <is_readable+0x17e>
jmp    12f1 <is_readable+0x191>
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_readable>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %edi,%edi
jle    1242 <is_readable+0xd2>
mov    %edi,%ebx
lea    0x14(%rsp),%r15
mov    %r15,%rdi
call   1070 <FD_ZERO@plt>
lea    0x10(%rsp),%r12
mov    %r12,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1090 <FD_SET@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   1090 <FD_SET@plt>
movq   $0x0,0x20(%rsp)
movl   $0x1,0x18(%rsp)
lea    0x1(%rbx),%edi
xor    %r14d,%r14d
lea    0x18(%rsp),%r8
mov    %r15,%rsi
xor    %edx,%edx
mov    %r12,%rcx
call   1080 <select@plt>
test   %eax,%eax
jle    1256 <is_readable+0xe6>
lea    0x14(%rsp),%rsi
mov    %ebx,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     126c <is_readable+0xfc>
movl   $0x0,0x8(%rsp)
movl   $0x4,0xc(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %ebx,%edi
call   1060 <getsockopt@plt>
mov    0x8(%rsp),%esi
test   %esi,%esi
je     1291 <is_readable+0x121>
lea    0xdcd(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xfffffffb,%r14d
jmp    1297 <is_readable+0x127>
lea    0xde0(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
jmp    1297 <is_readable+0x127>
jns    1297 <is_readable+0x127>
lea    0xdf3(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffd,%r14d
jmp    1297 <is_readable+0x127>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12a6 <is_readable+0x136>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffc,%r14d
jmp    1297 <is_readable+0x127>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdbe(%rip),%rdi
jmp    1249 <is_readable+0xd9>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@.str = [41 x i8] c"is_readable.select some error! error:%d\0A\00"
@.str.1 = [35 x i8] c"is_readable.select with error fd!\0A\00"
@.str.2 = [39 x i8] c"is_readable.select CANNOT understand!\0A\00"
@.str.3 = [26 x i8] c"is_readable.select fail!\0A\00"
@.str.4 = [42 x i8] c"is_readable.select socketfd must be > 0!\0A\00"
define i32 @is_readable(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %2
store i32 -1, i32* %3
%10 = load i32, i32* %2
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %64
12: ; preds = %1
%13 = call i32 @FD_ZERO(i32* %4)
%14 = call i32 @FD_ZERO(i32* %5)
%15 = load i32, i32* %2
%16 = call i32 @FD_SET(i32 %15, i32* %4)
%17 = load i32, i32* %2
%18 = call i32 @FD_SET(i32 %17, i32* %5)
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i64 0, i64* %19
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 1, i32* %20
%21 = load i32, i32* %2
%22 = add nsw i32 %21, 1
%23 = call i32 @select(i32 %22, i32* %4, i32* null, i32* %5, %struct.timeval* %6)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %52
26: ; preds = %12
%27 = load i32, i32* %2
%28 = call i64 @FD_ISSET(i32 %27, i32* %4)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %42
30: ; preds = %26
store i32 0, i32* %8
store i32 4, i32* %9
%31 = load i32, i32* %2
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_ERROR
%34 = call i32 @getsockopt(i32 %31, i32 %32, i32 %33, i32* %8, i32* %9)
%35 = load i32, i32* %8
%36 = icmp eq i32 0, %35
br i1 %36, label %37, label %38
37: ; preds = %30
store i32 1, i32* %3
br label %41
38: ; preds = %30
%39 = load i32, i32* %8
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %39)
store i32 -5, i32* %3
br label %41
41: ; preds = %38, %37
br label %51
42: ; preds = %26
%43 = load i32, i32* %2
%44 = call i64 @FD_ISSET(i32 %43, i32* %5)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 -4, i32* %3
br label %50
48: ; preds = %42
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %48, %46
br label %51
51: ; preds = %50, %41
br label %63
52: ; preds = %12
%53 = load i32, i32* %7
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store i32 -3, i32* %3
br label %62
57: ; preds = %52
%58 = load i32, i32* %7
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %57
store i32 0, i32* %3
br label %61
61: ; preds = %60, %57
br label %62
62: ; preds = %61, %55
br label %63
63: ; preds = %62, %51
br label %66
64: ; preds = %1
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
br label %66
66: ; preds = %64, %63
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@.str = [41 x i8] c"is_readable.select some error! error:%d\0A\00"
@str = [41 x i8] c"is_readable.select socketfd must be > 0!\00"
@str.5 = [25 x i8] c"is_readable.select fail!\00"
@str.6 = [38 x i8] c"is_readable.select CANNOT understand!\00"
@str.7 = [34 x i8] c"is_readable.select with error fd!\00"
define i32 @is_readable(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = alloca i32
%7 = icmp sgt i32 %0, 0
br i1 %7, label %8, label %49
8: ; preds = %1
%9 = bitcast i32* %2 to i8*
%10 = bitcast i32* %3 to i8*
%11 = call i32 @FD_ZERO(i32* %2)
%12 = call i32 @FD_ZERO(i32* %3)
%13 = call i32 @FD_SET(i32 %0, i32* %2)
%14 = call i32 @FD_SET(i32 %0, i32* %3)
%15 = bitcast %struct.timeval* %4 to i8*
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 1, i32* %17
%18 = add nuw nsw i32 %0, 1
%19 = call i32 @select(i32 %18, i32* %2, i32* null, i32* %3, %struct.timeval* %4)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %43
21: ; preds = %8
%22 = call i64 @FD_ISSET(i32 %0, i32* %2)
%23 = icmp eq i64 %22, 0
br i1 %23, label %36, label %24
24: ; preds = %21
%25 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%26 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_ERROR
%29 = call i32 @getsockopt(i32 %0, i32 %27, i32 %28, i32* %5, i32* %6)
%30 = load i32, i32* %5
%31 = icmp eq i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %24
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %30)
br label %34
34: ; preds = %24, %32
%35 = phi i32 [ -5, %32 ], [ 1, %24 ]
br label %47
36: ; preds = %21
%37 = call i64 @FD_ISSET(i32 %0, i32* %3)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.7, i64 0, i64 0))
br label %47
41: ; preds = %36
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.6, i64 0, i64 0))
br label %47
43: ; preds = %8
%44 = icmp slt i32 %19, 0
br i1 %44, label %45, label %47
45: ; preds = %43
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.5, i64 0, i64 0))
br label %47
47: ; preds = %43, %45, %34, %41, %39
%48 = phi i32 [ %35, %34 ], [ -4, %39 ], [ -1, %41 ], [ -3, %45 ], [ 0, %43 ]
br label %51
49: ; preds = %1
%50 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
br label %51
51: ; preds = %49, %47
%52 = phi i32 [ %48, %47 ], [ -1, %49 ]
ret i32 %52
}