#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 */
typedef int uint32_t;
struct timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};
struct timespec {
  int tv_nsec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ pthread_mutex_t;
typedef int /*<<< orphan*/ pthread_cond_t;

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int pthread_cond_timedwait(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                           struct timespec *);
int hdd_timed_wait(pthread_cond_t *cond, pthread_mutex_t *mutex,
                   uint32_t usecs) {
  struct timeval tv;
  struct timespec ts;
  gettimeofday(&tv, NULL);
  tv.tv_usec += (usecs % 1000000);
  while (tv.tv_usec > 1000000) {
    tv.tv_sec++;
    tv.tv_usec -= 1000000;
  }
  tv.tv_sec += usecs / 1000000;
  ts.tv_nsec = tv.tv_usec * 1000;
  ts.tv_sec = tv.tv_sec;
  return pthread_cond_timedwait(cond, mutex, &ts);
}


x86 assembly (clang, -O0):
<hdd_timed_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x14(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
add    -0x28(%rbp),%edx
mov    %edx,-0x28(%rbp)
cmpl   $0xf4240,-0x28(%rbp)
jle    117a <hdd_timed_wait+0x5a>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
sub    $0xf4240,%eax
mov    %eax,-0x28(%rbp)
jmp    1151 <hdd_timed_wait+0x31>
mov    -0x14(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
cltq
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
imul   $0x3e8,-0x28(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1040 <pthread_cond_timedwait@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hdd_timed_wait>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movslq %ebp,%rax
imul   $0x431bde83,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
imul   $0xf4240,%ecx,%edx
sub    %edx,%eax
add    0x18(%rsp),%eax
mov    0x20(%rsp),%rdx
cmp    $0xf4241,%eax
jl     11aa <hdd_timed_wait+0x8a>
cmp    $0x1e8480,%eax
mov    $0x1e8480,%esi
cmovl  %eax,%esi
neg    %esi
lea    (%rax,%rsi,1),%ebx
add    $0xf423f,%ebx
imul   $0x431bde83,%rbx,%rdi
shr    $0x32,%rdi
imul   $0xf4240,%edi,%ebp
mov    %ebx,%esi
sub    %ebp,%esi
sub    %ebx,%esi
add    %rdi,%rdx
add    $0x1,%rdx
add    %esi,%eax
add    $0xfff0bdc0,%eax
movslq %ecx,%rcx
add    %rdx,%rcx
imul   $0x3e8,%eax,%eax
mov    %eax,0x8(%rsp)
mov    %rcx,0x10(%rsp)
lea    0x8(%rsp),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <pthread_cond_timedwait@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
%struct.timespec = type { i32, i64 }
define i32 @hdd_timed_wait(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca %struct.timespec
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%9 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%10 = load i32, i32* %6
%11 = srem i32 %10, 1000000
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%13 = load i32, i32* %12
%14 = add nsw i32 %13, %11
store i32 %14, i32* %12
br label %15
15: ; preds = %19, %3
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%17 = load i32, i32* %16
%18 = icmp sgt i32 %17, 1000000
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%21 = load i64, i64* %20
%22 = add nsw i64 %21, 1
store i64 %22, i64* %20
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%24 = load i32, i32* %23
%25 = sub nsw i32 %24, 1000000
store i32 %25, i32* %23
br label %15
26: ; preds = %15
%27 = load i32, i32* %6
%28 = sdiv i32 %27, 1000000
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%31 = load i64, i64* %30
%32 = add nsw i64 %31, %29
store i64 %32, i64* %30
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%34 = load i32, i32* %33
%35 = mul nsw i32 %34, 1000
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
store i32 %35, i32* %36
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%38 = load i64, i64* %37
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
store i64 %38, i64* %39
%40 = load i32*, i32** %4
%41 = load i32*, i32** %5
%42 = call i32 @pthread_cond_timedwait(i32* %40, i32* %41, %struct.timespec* %8)
ret i32 %42
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
%struct.timespec = type { i32, i64 }
define i32 @hdd_timed_wait(i32* %0, i32* %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca %struct.timespec
%6 = bitcast %struct.timeval* %4 to i8*
%7 = bitcast %struct.timespec* %5 to i8*
%8 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%9 = srem i32 %2, 1000000
%10 = sdiv i32 %2, 1000000
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%12 = load i32, i32* %11
%13 = add i32 %12, %9
%14 = icmp sgt i32 %13, 1000000
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%16 = load i64, i64* %15
br i1 %14, label %17, label %29
17: ; preds = %3
%18 = add nuw i32 %13, 999999
%19 = call i32 @llvm.smin.i32(i32 %13, i32 2000000)
%20 = sub nuw i32 %18, %19
%21 = udiv i32 %20, 1000000
%22 = zext i32 %21 to i64
%23 = add i64 %16, %22
%24 = urem i32 %20, 1000000
%25 = sub nsw i32 %24, %20
%26 = add i64 %23, 1
%27 = add nsw i32 %13, -1000000
%28 = add nsw i32 %25, %27
br label %29
29: ; preds = %3, %17
%30 = phi i64 [ %26, %17 ], [ %16, %3 ]
%31 = phi i32 [ %28, %17 ], [ %13, %3 ]
%32 = sext i32 %10 to i64
%33 = add nsw i64 %30, %32
%34 = mul nsw i32 %31, 1000
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
store i64 %33, i64* %36
%37 = call i32 @pthread_cond_timedwait(i32* %0, i32* %1, %struct.timespec* %5)
ret i32 %37
}