#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;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *, int /*<<< orphan*/,
           struct timeval *);
int tcp_wait_for_socket(int fd, int len, int action) {
  int ret;
  fd_set fdset;
  struct timeval timeval;
  FD_ZERO(&fdset);
  FD_SET(fd, &fdset);
  timeval.tv_sec = len;
  timeval.tv_usec = 0;
  if (action == 1)
    ret = select(fd + 1, &fdset, 0, 0, &timeval);
  else
    ret = select(fd + 1, 0, &fdset, 0, &timeval);
  if (ret == -1)
    perror("TCP_WAIT_FOR_SOCKET (SELECT)");
  return (ret);
}


x86 assembly (clang, -O0):
<tcp_wait_for_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    -0x14(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    119f <tcp_wait_for_socket+0x5f>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
xor    %ecx,%ecx
lea    -0x28(%rbp),%r8
call   1040 <select@plt>
mov    %eax,-0x10(%rbp)
jmp    11bb <tcp_wait_for_socket+0x7b>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x14(%rbp),%rdx
xor    %ecx,%ecx
lea    -0x28(%rbp),%r8
call   1040 <select@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11d1 <tcp_wait_for_socket+0x91>
lea    0xe34(%rip),%rdi
call   1050 <perror@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_wait_for_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1060 <FD_SET@plt>
mov    %ebp,0x8(%rsp)
movq   $0x0,0x10(%rsp)
add    $0x1,%ebx
cmp    $0x1,%r14d
jne    118e <tcp_wait_for_socket+0x4e>
lea    0x4(%rsp),%rsi
lea    0x8(%rsp),%r8
mov    %ebx,%edi
xor    %edx,%edx
jmp    119c <tcp_wait_for_socket+0x5c>
lea    0x4(%rsp),%rdx
lea    0x8(%rsp),%r8
mov    %ebx,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1040 <select@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11b7 <tcp_wait_for_socket+0x77>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
jmp    11aa <tcp_wait_for_socket+0x6a>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [29 x i8] c"TCP_WAIT_FOR_SOCKET (SELECT)\00"
define i32 @tcp_wait_for_socket(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = call i32 @FD_ZERO(i32* %8)
%11 = load i32, i32* %4
%12 = call i32 @FD_SET(i32 %11, i32* %8)
%13 = load i32, i32* %5
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %15
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %22
18: ; preds = %3
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 1
%21 = call i32 @select(i32 %20, i32* %8, i32* null, i32 0, %struct.timeval* %9)
store i32 %21, i32* %7
br label %26
22: ; preds = %3
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
%25 = call i32 @select(i32 %24, i32* null, i32* %8, i32 0, %struct.timeval* %9)
store i32 %25, i32* %7
br label %26
26: ; preds = %22, %18
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %29, %26
%32 = load i32, i32* %7
ret i32 %32
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [29 x i8] c"TCP_WAIT_FOR_SOCKET (SELECT)\00"
define i32 @tcp_wait_for_socket(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = call i32 @FD_ZERO(i32* %4)
%9 = call i32 @FD_SET(i32 %0, i32* %4)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 %1, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %11
%12 = icmp eq i32 %2, 1
%13 = add nsw i32 %0, 1
br i1 %12, label %14, label %16
14: ; preds = %3
%15 = call i32 @select(i32 %13, i32* %4, i32* null, i32 0, %struct.timeval* %5)
br label %18
16: ; preds = %3
%17 = call i32 @select(i32 %13, i32* null, i32* %4, i32 0, %struct.timeval* %5)
br label %18
18: ; preds = %16, %14
%19 = phi i32 [ %15, %14 ], [ %17, %16 ]
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
ret i32 %19
}