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

/* Variables and functions */
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ sim(double);
int sim_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
               struct timeval *timeout) {
  double t = timeout->tv_sec + timeout->tv_usec * 1e-6;
  for (;;) {
    struct timeval to = {0, 0};
    int r = select(nfds, readfds, writefds, exceptfds, &to);
    if (r != 0 || t <= 0)
      return r;
    sim(0.05);
    t -= 0.05;
  }
}


x86 assembly (clang, -O0):
<sim_select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cvtsi2sdl (%rax),%xmm1
mov    -0x28(%rbp),%rax
cvtsi2sdl 0x4(%rax),%xmm0
movsd  0xe9c(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1040 <select@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    11b4 <sim_select+0x84>
xorps  %xmm0,%xmm0
ucomisd -0x30(%rbp),%xmm0
jb     11bd <sim_select+0x8d>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret
movsd  0xe43(%rip),%xmm0
call   1050 <sim@plt>
movsd  -0x30(%rbp),%xmm0
movsd  0xe31(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1171 <sim_select+0x41>

x86 assembly (clang, -O2):
<sim_select>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%ebp
cvtsi2sdl (%r8),%xmm0
movsd  %xmm0,0x28(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sdl 0x4(%r8),%xmm0
movsd  %xmm0,0x8(%rsp)
xorpd  %xmm0,%xmm0
movapd %xmm0,0x10(%rsp)
lea    0x10(%rsp),%r8
call   1030 <select@plt>
test   %eax,%eax
jne    11fc <sim_select+0xdc>
movsd  0x8(%rsp),%xmm1
mulsd  0xe85(%rip),%xmm1
addsd  0x28(%rsp),%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jae    11fc <sim_select+0xdc>
movapd %xmm1,%xmm0
lea    0x10(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  %xmm0,0x8(%rsp)
movsd  0xe5a(%rip),%xmm0
call   1040 <sim@plt>
movsd  0x8(%rsp),%xmm0
addsd  0xe4f(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
xorpd  %xmm0,%xmm0
movapd %xmm0,0x10(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %r12,%r8
call   1030 <select@plt>
test   %eax,%eax
jne    11fc <sim_select+0xdc>
xorpd  %xmm0,%xmm0
movsd  0x8(%rsp),%xmm1
ucomisd %xmm1,%xmm0
movapd %xmm1,%xmm0
jb     11a0 <sim_select+0x80>
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32, i32, i32 }
define i32 @sim_select(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timeval* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca %struct.timeval*
%11 = alloca double
%12 = alloca %struct.timeval
%13 = alloca i32
store i32 %0, i32* %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store %struct.timeval* %4, %struct.timeval** %10
%14 = load %struct.timeval*, %struct.timeval** %10
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = sitofp i32 %16 to double
%18 = load %struct.timeval*, %struct.timeval** %10
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 1
%20 = load i32, i32* %19
%21 = sitofp i32 %20 to double
%22 = call double @llvm.fmuladd.f64(double %21, double 0x3EB0C6F7A0B5ED8D, double %17)
store double %22, double* %11
br label %23
23: ; preds = %37, %5
%24 = bitcast %struct.timeval* %12 to i8*
%25 = load i32, i32* %6
%26 = load i32*, i32** %7
%27 = load i32*, i32** %8
%28 = load i32*, i32** %9
%29 = call i32 @select(i32 %25, i32* %26, i32* %27, i32* %28, %struct.timeval* %12)
store i32 %29, i32* %13
%30 = load i32, i32* %13
%31 = icmp ne i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %23
%33 = load double, double* %11
%34 = fcmp ole double %33, 0.000000e+00
br i1 %34, label %35, label %37
35: ; preds = %32, %23
%36 = load i32, i32* %13
ret i32 %36
37: ; preds = %32
%38 = call i32 @sim(double 5.000000e-02)
%39 = load double, double* %11
%40 = fsub double %39, 5.000000e-02
store double %40, double* %11
br label %23
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32, i32, i32 }
define i32 @sim_select(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timeval* nocapture %4) {
%6 = alloca %struct.timeval
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%8 = load i32, i32* %7
%9 = sitofp i32 %8 to double
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%11 = load i32, i32* %10
%12 = sitofp i32 %11 to double
%13 = tail call double @llvm.fmuladd.f64(double %12, double 0x3EB0C6F7A0B5ED8D, double %9)
%14 = bitcast %struct.timeval* %6 to i8*
%15 = call i32 @select(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timeval* %6)
%16 = icmp ne i32 %15, 0
%17 = fcmp ole double %13, 0.000000e+00
%18 = select i1 %16, i1 true, i1 %17
br i1 %18, label %27, label %19
19: ; preds = %5, %19
%20 = phi double [ %22, %19 ], [ %13, %5 ]
%21 = call i32 @sim(double 5.000000e-02)
%22 = fadd double %20, -5.000000e-02
%23 = call i32 @select(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timeval* %6)
%24 = icmp ne i32 %23, 0
%25 = fcmp ole double %22, 0.000000e+00
%26 = select i1 %24, i1 true, i1 %25
br i1 %26, label %27, label %19
27: ; preds = %19, %5
%28 = phi i32 [ %15, %5 ], [ %23, %19 ]
ret i32 %28
}