#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;
};
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 *);
scalar_t__ time(int /*<<< orphan*/ *);
int ui_cond_timedwait(pthread_cond_t *cnd, pthread_mutex_t *mtx, int timeInMs) {
  struct timeval tv;
  struct timespec ts;
  gettimeofday(&tv, NULL);
  ts.tv_sec = time(NULL) + timeInMs / 1000;
  ts.tv_nsec = tv.tv_usec * 1000 + 1000 * 1000 * (timeInMs % 1000);
  ts.tv_sec += ts.tv_nsec / (1000 * 1000 * 1000);
  ts.tv_nsec %= (1000 * 1000 * 1000);
  return pthread_cond_timedwait(cnd, mtx, &ts);
}


x86 assembly (clang, -O0):
<ui_cond_timedwait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
imul   $0x3e8,-0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    -0x2c(%rbp),%eax
imul   $0xf4240,%edx,%ecx
add    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
cltq
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    %edx,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1050 <pthread_cond_timedwait@plt>
add    $0x40,%rsp
pop    %rbp
ret

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

LLVM IR (-O0):
%struct.timeval = type { i32 }
%struct.timespec = type { i32, i64 }
define i32 @ui_cond_timedwait(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 = call i64 @time(i32* null)
%11 = load i32, i32* %6
%12 = sdiv i32 %11, 1000
%13 = sext i32 %12 to i64
%14 = add nsw i64 %10, %13
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
store i64 %14, i64* %15
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%17 = load i32, i32* %16
%18 = mul nsw i32 %17, 1000
%19 = load i32, i32* %6
%20 = srem i32 %19, 1000
%21 = mul nsw i32 1000000, %20
%22 = add nsw i32 %18, %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%25 = load i32, i32* %24
%26 = sdiv i32 %25, 1000000000
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
%29 = load i64, i64* %28
%30 = add nsw i64 %29, %27
store i64 %30, i64* %28
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%32 = load i32, i32* %31
%33 = srem i32 %32, 1000000000
store i32 %33, i32* %31
%34 = load i32*, i32** %4
%35 = load i32*, i32** %5
%36 = call i32 @pthread_cond_timedwait(i32* %34, i32* %35, %struct.timespec* %8)
ret i32 %36
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
%struct.timespec = type { i32, i64 }
define i32 @ui_cond_timedwait(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 = tail call i64 @time(i32* null)
%10 = sdiv i32 %2, 1000
%11 = sext i32 %10 to i64
%12 = add nsw i64 %9, %11
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%15 = load i32, i32* %14
%16 = mul nsw i32 %15, 1000
%17 = srem i32 %2, 1000
%18 = mul nsw i32 %17, 1000000
%19 = add nsw i32 %16, %18
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%21 = sdiv i32 %19, 1000000000
%22 = sext i32 %21 to i64
%23 = add nsw i64 %12, %22
store i64 %23, i64* %13
%24 = srem i32 %19, 1000000000
store i32 %24, i32* %20
%25 = call i32 @pthread_cond_timedwait(i32* %0, i32* %1, %struct.timespec* %5)
ret i32 %25
}