#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 struct timeval fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(unsigned int, struct timeval *);
int /*<<< orphan*/ FD_SET(unsigned int, struct timeval *);
int /*<<< orphan*/ FD_ZERO(struct timeval *);
int /*<<< orphan*/ g_memset(struct timeval *, int /*<<< orphan*/, int);
int select(int, struct timeval *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct timeval *);
int g_tcp_select(int sck1, int sck2) {
  fd_set rfds;
  struct timeval time;
  int max = 0;
  int rv = 0;
  g_memset(&rfds, 0, sizeof(fd_set));
  g_memset(&time, 0, sizeof(struct timeval));
  time.tv_sec = 0;
  time.tv_usec = 0;
  FD_ZERO(&rfds);
  if (sck1 > 0) {
    FD_SET(((unsigned int)sck1), &rfds);
  }
  if (sck2 > 0) {
    FD_SET(((unsigned int)sck2), &rfds);
  }
  max = sck1;
  if (sck2 > max) {
    max = sck2;
  }
  rv = select(max + 1, &rfds, 0, 0, &time);
  if (rv > 0) {
    rv = 0;
    if (FD_ISSET(((unsigned int)sck1), &rfds)) {
      rv = rv | 1;
    }
    if (FD_ISSET(((unsigned int)sck2), &rfds)) {
      rv = rv | 2;
    }
  } else {
    rv = 0;
  }
  return rv;
}


x86 assembly (clang, -O0):
<g_tcp_select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <g_memset@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <g_memset@plt>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <FD_ZERO@plt>
cmpl   $0x0,-0x4(%rbp)
jle    11bb <g_tcp_select+0x6b>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <FD_SET@plt>
cmpl   $0x0,-0x8(%rbp)
jle    11d1 <g_tcp_select+0x81>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jle    11e9 <g_tcp_select+0x99>
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
add    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %ecx,%ecx
lea    -0x28(%rbp),%r8
mov    %ecx,%edx
call   1060 <select@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jle    1257 <g_tcp_select+0x107>
movl   $0x0,-0x30(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1233 <g_tcp_select+0xe3>
mov    -0x30(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1252 <g_tcp_select+0x102>
mov    -0x30(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x30(%rbp)
jmp    125e <g_tcp_select+0x10e>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<g_tcp_select>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <g_memset@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <g_memset@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
test   %ebp,%ebp
jle    119e <g_tcp_select+0x4e>
mov    %rsp,%rsi
mov    %ebp,%edi
call   1070 <FD_SET@plt>
test   %r14d,%r14d
jle    11ae <g_tcp_select+0x5e>
mov    %rsp,%rsi
mov    %r14d,%edi
call   1070 <FD_SET@plt>
cmp    %ebp,%r14d
mov    %ebp,%edi
cmovg  %r14d,%edi
add    $0x1,%edi
mov    %rsp,%rsi
xor    %ebx,%ebx
lea    0x10(%rsp),%r8
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
jle    11fb <g_tcp_select+0xab>
mov    %rsp,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
lea    0x2(%rbp),%ebx
test   %rax,%rax
cmove  %ebp,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @g_tcp_select(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %7
store i32 0, i32* %8
%9 = call i32 @g_memset(%struct.timeval* %5, i32 0, i32 16)
%10 = call i32 @g_memset(%struct.timeval* %6, i32 0, i32 16)
%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 = call i32 @FD_ZERO(%struct.timeval* %5)
%14 = load i32, i32* %3
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* %3
%18 = call i32 @FD_SET(i32 %17, %struct.timeval* %5)
br label %19
19: ; preds = %16, %2
%20 = load i32, i32* %4
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* %4
%24 = call i32 @FD_SET(i32 %23, %struct.timeval* %5)
br label %25
25: ; preds = %22, %19
%26 = load i32, i32* %3
store i32 %26, i32* %7
%27 = load i32, i32* %4
%28 = load i32, i32* %7
%29 = icmp sgt i32 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = load i32, i32* %4
store i32 %31, i32* %7
br label %32
32: ; preds = %30, %25
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
%35 = call i32 @select(i32 %34, %struct.timeval* %5, i32 0, i32 0, %struct.timeval* %6)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %53
38: ; preds = %32
store i32 0, i32* %8
%39 = load i32, i32* %3
%40 = call i64 @FD_ISSET(i32 %39, %struct.timeval* %5)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* %8
%44 = or i32 %43, 1
store i32 %44, i32* %8
br label %45
45: ; preds = %42, %38
%46 = load i32, i32* %4
%47 = call i64 @FD_ISSET(i32 %46, %struct.timeval* %5)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i32, i32* %8
%51 = or i32 %50, 2
store i32 %51, i32* %8
br label %52
52: ; preds = %49, %45
br label %54
53: ; preds = %32
store i32 0, i32* %8
br label %54
54: ; preds = %53, %52
%55 = load i32, i32* %8
ret i32 %55
}

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