#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 */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
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 *);
scalar_t__ timeout_cb(int /*<<< orphan*/);
void timer_run(int fd) {
  fd_set rd;
  struct timeval tv;
  tv.tv_sec = 1;
  tv.tv_usec = 0;
  FD_ZERO(&rd);
  FD_SET(fd, &rd);
  fd_set tmp;
  while (FD_ISSET(fd, &rd)) {
    tmp = rd;
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    int record = select(fd + 1, &tmp, NULL, NULL, &tv);
    if (record == -1) {
      perror("select error\n");
    } else if (record == 0) {
      if (timeout_cb(0))
        return;
    }
  }
  return;
}


x86 assembly (clang, -O0):
<timer_run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x8(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1221 <timer_run+0xc1>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x1,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11f2 <timer_run+0x92>
lea    0xe18(%rip),%rdi
call   1070 <perror@plt>
jmp    121c <timer_run+0xbc>
cmpl   $0x0,-0x20(%rbp)
jne    1217 <timer_run+0xb7>
xor    %edi,%edi
call   1030 <timeout_cb@plt>
cmp    $0x0,%rax
je     1212 <timer_run+0xb2>
jmp    1226 <timer_run+0xc6>
jmp    1217 <timer_run+0xb7>
jmp    121c <timer_run+0xbc>
jmp    118f <timer_run+0x2f>
jmp    1226 <timer_run+0xc6>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timer_run>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1207 <timer_run+0xa7>
lea    0x1(%rbx),%ebp
lea    0x4(%rsp),%r14
lea    0x8(%rsp),%r15
mov    %rsp,%r12
lea    0xe55(%rip),%r13
jmp    11cb <timer_run+0x6b>
nopl   (%rax)
xor    %edi,%edi
call   1030 <timeout_cb@plt>
test   %rax,%rax
jne    1207 <timer_run+0xa7>
mov    %ebx,%edi
mov    %r12,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1207 <timer_run+0xa7>
mov    (%rsp),%eax
mov    %eax,0x4(%rsp)
movl   $0x1,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1060 <select@plt>
test   %eax,%eax
je     11b0 <timer_run+0x50>
cmp    $0xffffffff,%eax
jne    11bc <timer_run+0x5c>
mov    %r13,%rdi
call   1070 <perror@plt>
jmp    11bc <timer_run+0x5c>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [14 x i8] c"select error\0A\00"
define void @timer_run(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%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* %3)
%10 = load i32, i32* %2
%11 = call i32 @FD_SET(i32 %10, i32* %3)
br label %12
12: ; preds = %36, %1
%13 = load i32, i32* %2
%14 = call i64 @FD_ISSET(i32 %13, i32* %3)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %37
16: ; preds = %12
%17 = load i32, i32* %3
store i32 %17, i32* %5
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
store i32 1, i32* %18
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
store i64 0, i64* %19
%20 = load i32, i32* %2
%21 = add nsw i32 %20, 1
%22 = call i32 @select(i32 %21, i32* %5, i32* null, i32* null, %struct.timeval* %4)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %36
27: ; preds = %16
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = call i64 @timeout_cb(i32 0)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
br label %38
34: ; preds = %30
br label %35
35: ; preds = %34, %27
br label %36
36: ; preds = %35, %25
br label %12
37: ; preds = %12
br label %38
38: ; preds = %37, %33
ret void
}

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