#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 timezone {
  int dummy;
};
struct timeval {
  scalar_t__ tv_sec;
  int tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, struct timezone *);
int select(int n, fd_set *a, fd_set *b, fd_set *c, struct timeval *timeout) {
  struct timeval time1;
  struct timeval time2;
  struct timezone zone1;
  gettimeofday(&time1, &zone1);
  time1.tv_sec += timeout->tv_sec;
  time1.tv_usec += timeout->tv_usec;
  if (time1.tv_usec >= 1000000) {
    time1.tv_sec += time1.tv_usec / 1000000;
    time1.tv_usec %= 1000000;
  }
  do {
    gettimeofday(&time2, &zone1);
  } while ((time2.tv_sec < time1.tv_sec) ||
           ((time2.tv_usec < time1.tv_usec) && (time2.tv_sec == time1.tv_sec)));
}


x86 assembly (clang, -O0):
<select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x38(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1030 <gettimeofday@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0xf4240,-0x30(%rbp)
jl     1184 <select+0x74>
mov    -0x30(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
cltq
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %edx,-0x30(%rbp)
jmp    1189 <select+0x79>
lea    -0x48(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1030 <gettimeofday@plt>
mov    -0x48(%rbp),%rcx
mov    $0x1,%al
cmp    -0x38(%rbp),%rcx
mov    %al,-0x51(%rbp)
jl     11ce <select+0xbe>
mov    -0x40(%rbp),%ecx
xor    %eax,%eax
cmp    -0x30(%rbp),%ecx
mov    %al,-0x52(%rbp)
jge    11c8 <select+0xb8>
mov    -0x48(%rbp),%rax
cmp    -0x38(%rbp),%rax
sete   %al
mov    %al,-0x52(%rbp)
mov    -0x52(%rbp),%al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    1189 <select+0x79>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<select>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %r8,%r14
lea    0x18(%rsp),%rdi
mov    %rsp,%rsi
call   1030 <gettimeofday@plt>
mov    0x18(%rsp),%rbx
add    (%r14),%rbx
mov    0x20(%rsp),%ebp
add    0x8(%r14),%ebp
cmp    $0xf4240,%ebp
jl     115a <select+0x4a>
mov    %ebp,%eax
imul   $0x431bde83,%rax,%rax
shr    $0x32,%rax
add    %rax,%rbx
imul   $0xf4240,%eax,%eax
sub    %eax,%ebp
lea    0x8(%rsp),%r14
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <gettimeofday@plt>
mov    0x8(%rsp),%rax
cmp    %rbx,%rax
jl     1170 <select+0x60>
cmp    %ebp,0x10(%rsp)
jge    1190 <select+0x80>
cmp    %rbx,%rax
je     1170 <select+0x60>
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i32 }
%struct.timezone = type { i32 }
define i32 @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 i32*
%11 = alloca %struct.timeval*
%12 = alloca %struct.timeval
%13 = alloca %struct.timeval
%14 = alloca %struct.timezone
store i32 %0, i32* %7
store i32* %1, i32** %8
store i32* %2, i32** %9
store i32* %3, i32** %10
store %struct.timeval* %4, %struct.timeval** %11
%15 = call i32 @gettimeofday(%struct.timeval* %12, %struct.timezone* %14)
%16 = load %struct.timeval*, %struct.timeval** %11
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
%18 = load i64, i64* %17
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
%20 = load i64, i64* %19
%21 = add nsw i64 %20, %18
store i64 %21, i64* %19
%22 = load %struct.timeval*, %struct.timeval** %11
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 1
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
%26 = load i32, i32* %25
%27 = add nsw i32 %26, %24
store i32 %27, i32* %25
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
%29 = load i32, i32* %28
%30 = icmp sge i32 %29, 1000000
br i1 %30, label %31, label %42
31: ; preds = %5
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
%33 = load i32, i32* %32
%34 = sdiv i32 %33, 1000000
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
%37 = load i64, i64* %36
%38 = add nsw i64 %37, %35
store i64 %38, i64* %36
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
%40 = load i32, i32* %39
%41 = srem i32 %40, 1000000
store i32 %41, i32* %39
br label %42
42: ; preds = %31, %5
br label %43
43: ; preds = %65, %42
%44 = call i32 @gettimeofday(%struct.timeval* %13, %struct.timezone* %14)
br label %45
45: ; preds = %43
%46 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%47 = load i64, i64* %46
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
%49 = load i64, i64* %48
%50 = icmp slt i64 %47, %49
br i1 %50, label %65, label %51
51: ; preds = %45
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
%53 = load i32, i32* %52
%54 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
%55 = load i32, i32* %54
%56 = icmp slt i32 %53, %55
br i1 %56, label %57, label %63
57: ; preds = %51
%58 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%59 = load i64, i64* %58
%60 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
%61 = load i64, i64* %60
%62 = icmp eq i64 %59, %61
br label %63
63: ; preds = %57, %51
%64 = phi i1 [ false, %51 ], [ %62, %57 ]
br label %65
65: ; preds = %63, %45
%66 = phi i1 [ true, %45 ], [ %64, %63 ]
br i1 %66, label %43, label %67
67: ; preds = %65
%68 = load i32, i32* %6
ret i32 %68
}

LLVM IR (-O2):
%struct.timeval = type { i64, i32 }
%struct.timezone = type { i32 }
define i32 @select(i32 %0, i32* nocapture readnone %1, i32* nocapture readnone %2, i32* nocapture readnone %3, %struct.timeval* nocapture %4) {
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
%8 = alloca %struct.timezone
%9 = bitcast %struct.timeval* %6 to i8*
%10 = bitcast %struct.timeval* %7 to i8*
%11 = bitcast %struct.timezone* %8 to i8*
%12 = call i32 @gettimeofday(%struct.timeval* %6, %struct.timezone* %8)
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%14 = load i64, i64* %13
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
%16 = load i64, i64* %15
%17 = add nsw i64 %16, %14
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
%21 = load i32, i32* %20
%22 = add nsw i32 %21, %19
%23 = icmp sgt i32 %22, 999999
br i1 %23, label %24, label %29
24: ; preds = %5
%25 = udiv i32 %22, 1000000
%26 = zext i32 %25 to i64
%27 = add nsw i64 %17, %26
%28 = urem i32 %22, 1000000
br label %29
29: ; preds = %24, %5
%30 = phi i32 [ %28, %24 ], [ %22, %5 ]
%31 = phi i64 [ %27, %24 ], [ %17, %5 ]
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
br label %34
34: ; preds = %43, %29
%35 = call i32 @gettimeofday(%struct.timeval* %7, %struct.timezone* %8)
%36 = load i64, i64* %32
%37 = icmp slt i64 %36, %31
br i1 %37, label %43, label %38
38: ; preds = %34
%39 = load i32, i32* %33
%40 = icmp slt i32 %39, %30
%41 = icmp eq i64 %36, %31
%42 = select i1 %40, i1 %41, i1 false
br i1 %42, label %43, label %44
43: ; preds = %38, %34
br label %34
44: ; preds = %38
ret i32 undef
}