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

/* Variables and functions */
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
void timed_wait(long sec, long usec, long early_usec) {
  fd_set mt;
  struct timeval timeout;
  int rc;
  FD_ZERO(&mt);
  timeout.tv_sec = sec;
  timeout.tv_usec = usec;
  do {
    rc = select(0, &mt, &mt, &mt, &timeout);
    if (!timeout.tv_sec && timeout.tv_usec < early_usec)
      return;
  } while (rc < 0 && timeout.tv_sec && timeout.tv_usec);
}


x86 assembly (clang, -O0):
<timed_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
xor    %edi,%edi
lea    -0x1c(%rbp),%rcx
lea    -0x30(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0x34(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1183 <timed_wait+0x63>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1183 <timed_wait+0x63>
jmp    11bd <timed_wait+0x9d>
jmp    1188 <timed_wait+0x68>
xor    %eax,%eax
cmpl   $0x0,-0x34(%rbp)
mov    %al,-0x35(%rbp)
jge    11b2 <timed_wait+0x92>
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x35(%rbp)
je     11b2 <timed_wait+0x92>
cmpq   $0x0,-0x28(%rbp)
setne  %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    114d <timed_wait+0x2d>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timed_wait>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <FD_ZERO@plt>
mov    %r12,(%rsp)
mov    %r15,0x8(%rsp)
mov    %rsp,%r15
nopl   (%rax)
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbx,%rdx
mov    %rbx,%rcx
mov    %r15,%r8
call   1040 <select@plt>
mov    (%rsp),%rdx
mov    0x8(%rsp),%rcx
test   %rdx,%rdx
jne    1176 <timed_wait+0x56>
cmp    %r14,%rcx
jl     1184 <timed_wait+0x64>
test   %eax,%eax
jns    1184 <timed_wait+0x64>
test   %rdx,%rdx
je     1184 <timed_wait+0x64>
test   %rcx,%rcx
jne    1150 <timed_wait+0x30>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define void @timed_wait(i64 %0, i64 %1, i64 %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i32
store i64 %0, i64* %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%10 = call i32 @FD_ZERO(i32* %7)
%11 = load i64, i64* %4
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i64 %11, i64* %12
%13 = load i64, i64* %5
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 %13, i64* %14
br label %15
15: ; preds = %38, %3
%16 = call i32 @select(i32 0, i32* %7, i32* %7, i32* %7, %struct.timeval* %8)
store i32 %16, i32* %9
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%18 = load i64, i64* %17
%19 = icmp ne i64 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %15
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%22 = load i64, i64* %21
%23 = load i64, i64* %6
%24 = icmp slt i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %20
br label %40
26: ; preds = %20, %15
br label %27
27: ; preds = %26
%28 = load i32, i32* %9
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %38
30: ; preds = %27
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%32 = load i64, i64* %31
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%36 = load i64, i64* %35
%37 = icmp ne i64 %36, 0
br label %38
38: ; preds = %34, %30, %27
%39 = phi i1 [ false, %30 ], [ false, %27 ], [ %37, %34 ]
br i1 %39, label %15, label %40
40: ; preds = %25, %38
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define void @timed_wait(i64 %0, i64 %1, i64 %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 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i64 %0, i64* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 %1, i64* %10
br label %11
11: ; preds = %18, %3
%12 = call i32 @select(i32 0, i32* %4, i32* %4, i32* %4, %struct.timeval* %5)
%13 = load i64, i64* %9
%14 = icmp eq i64 %13, 0
%15 = load i64, i64* %10
%16 = icmp slt i64 %15, %2
%17 = select i1 %14, i1 %16, i1 false
br i1 %17, label %24, label %18
18: ; preds = %11
%19 = icmp slt i32 %12, 0
%20 = icmp ne i64 %13, 0
%21 = select i1 %19, i1 %20, i1 false
%22 = icmp ne i64 %15, 0
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %11, label %24
24: ; preds = %18, %11
ret void
}