#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 /*<<< orphan*/ print_msg(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int _fda_do_select(int fd) {
  struct timeval timeout;
  int retval;
  fd_set rfds;
  timeout.tv_sec = 1;
  timeout.tv_usec = 0;
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  retval = select(fd + 1, &rfds, NULL, NULL, &timeout);
  if (retval < 0) {
    perror("select()\n");
    return retval;
  } else if (retval == 0) {
    print_msg("select () timeout\n");
  } else {
    print_msg("select () data ready\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<_fda_do_select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11bd <_fda_do_select+0x6d>
lea    0xe53(%rip),%rdi
call   1060 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f0 <_fda_do_select+0xa0>
cmpl   $0x0,-0x1c(%rbp)
jne    11d8 <_fda_do_select+0x88>
lea    0xe3c(%rip),%rdi
call   1050 <print_msg@plt>
jmp    11e4 <_fda_do_select+0x94>
lea    0xe3e(%rip),%rdi
call   1050 <print_msg@plt>
jmp    11e9 <_fda_do_select+0x99>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_fda_do_select>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movl   $0x1,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1070 <FD_SET@plt>
add    $0x1,%ebx
lea    0x8(%rsp),%r8
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
test   %eax,%eax
js     11be <_fda_do_select+0x6e>
je     11a6 <_fda_do_select+0x56>
lea    0xe79(%rip),%rdi
jmp    11ad <_fda_do_select+0x5d>
lea    0xe5d(%rip),%rdi
call   1050 <print_msg@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    %eax,%ebx
lea    0xe39(%rip),%rdi
call   1060 <perror@plt>
jmp    11b4 <_fda_do_select+0x64>

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

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