#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_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 wait_timeout(pthread_mutex_t *grcba_lock, pthread_cond_t *grcba_cv,
                 int timeout_ms) {
  int retval;
  struct timespec timeout;
  struct timeval now;
  gettimeofday(&now, 0);
  timeout.tv_sec = now.tv_sec + timeout_ms / 1000;
  timeout.tv_nsec = now.tv_usec * 1000 + (timeout_ms % 1000 * 1000000);
  if (timeout.tv_nsec >= 1000000000) {
    timeout.tv_sec++;
    timeout.tv_nsec -= 1000000000;
  }
  retval = pthread_cond_timedwait(grcba_cv, grcba_lock, &timeout);
  return retval;
}


x86 assembly (clang, -O0):
<wait_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
imul   $0x3e8,-0x38(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    -0x3c(%rbp),%eax
imul   $0xf4240,%edx,%ecx
add    %ecx,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x3b9aca00,-0x28(%rbp)
jl     11a8 <wait_timeout+0x88>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
sub    $0x3b9aca00,%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <pthread_cond_timedwait@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_timeout>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movslq %ebp,%rcx
imul   $0x10624dd3,%rcx,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
movslq %eax,%rdx
mov    0x18(%rsp),%rax
add    %rdx,%rax
mov    %rax,0x8(%rsp)
imul   $0x3e8,0x10(%rsp),%esi
imul   $0x3e8,%edx,%edx
sub    %edx,%ecx
imul   $0xf4240,%ecx,%ecx
add    %esi,%ecx
mov    %ecx,(%rsp)
cmp    $0x3b9aca00,%ecx
jl     1198 <wait_timeout+0x78>
add    $0x1,%rax
mov    %rax,0x8(%rsp)
add    $0xc4653600,%ecx
mov    %ecx,(%rsp)
mov    %rsp,%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <pthread_cond_timedwait@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
%struct.timeval = type { i32, i64 }
define i32 @wait_timeout(i32* %0, i32* %1, i32 %2) {
%4 = alloca %struct.timespec
%5 = alloca %struct.timeval
%6 = bitcast %struct.timespec* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = call i32 @gettimeofday(%struct.timeval* %5, i32 0)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%10 = load i64, i64* %9
%11 = sdiv i32 %2, 1000
%12 = sext i32 %11 to i64
%13 = add nsw i64 %10, %12
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
store i64 %13, i64* %14
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%16 = load i32, i32* %15
%17 = mul nsw i32 %16, 1000
%18 = srem i32 %2, 1000
%19 = mul nsw i32 %18, 1000000
%20 = add nsw i32 %17, %19
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
store i32 %20, i32* %21
%22 = icmp sgt i32 %20, 999999999
br i1 %22, label %23, label %26
23: ; preds = %3
%24 = add nsw i64 %13, 1
store i64 %24, i64* %14
%25 = add nsw i32 %20, -1000000000
store i32 %25, i32* %21
br label %26
26: ; preds = %23, %3
%27 = call i32 @pthread_cond_timedwait(i32* %1, i32* %0, %struct.timespec* %4)
ret i32 %27
}