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

/* Variables and functions */
int /*<<< orphan*/ bzero(struct timeval *, int);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_cond_timedwait(int /*<<< orphan*/ *,
                                          int /*<<< orphan*/ *,
                                          struct timeval *);
int /*<<< orphan*/ pthread_cond_wait(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int EventWait(void *Handle, int Time, void *mutex) {
  if (Time > 0) {
    struct timespec time;
    struct timeval date;
    int64_t datams;
    bzero(&time, sizeof(struct timespec));
    bzero(&date, sizeof(struct timeval));
    gettimeofday(&date, NULL);
    datams = (((int64_t)date.tv_sec) * 1000000 + date.tv_usec) / 1000 + Time;
    time.tv_sec = datams / 1000;
    time.tv_nsec = datams % 1000 * 1000000;
    pthread_cond_timedwait((pthread_cond_t *)Handle, (pthread_mutex_t *)mutex,
                           &time);
  } else {
    pthread_cond_wait((pthread_cond_t *)Handle, (pthread_mutex_t *)mutex);
  }
  return 1;
}


x86 assembly (clang, -O0):
<EventWait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11d6 <EventWait+0x96>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
imul   $0xf4240,-0x38(%rbp),%eax
add    -0x34(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
add    -0xc(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0xf4240,%edx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1050 <pthread_cond_timedwait@plt>
jmp    11e3 <EventWait+0xa3>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <pthread_cond_wait@plt>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<EventWait>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rdi,%r12
test   %esi,%esi
jle    11e7 <EventWait+0xa7>
mov    %esi,%ebp
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
imul   $0xf4240,(%rsp),%eax
add    0x4(%rsp),%eax
cltq
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
add    %ebp,%eax
cltq
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x10(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0xf4240,%eax,%eax
mov    %eax,0x18(%rsp)
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <pthread_cond_timedwait@plt>
jmp    11f2 <EventWait+0xb2>
mov    %r12,%rdi
mov    %r14,%rsi
call   1030 <pthread_cond_wait@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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