#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 {
  unsigned int tv_sec;
  int tv_usec;
};
struct timespec {
  unsigned int tv_sec;
  int tv_nsec;
};
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 /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
int pwait_event_timeout(pthread_cond_t *cond, pthread_mutex_t *mtx,
                        unsigned int sec) {
  int ret;
  struct timeval tp;
  struct timespec ts;
  pthread_mutex_lock(mtx);
  gettimeofday(&tp, NULL);
  ts.tv_sec = tp.tv_sec;
  ts.tv_nsec = tp.tv_usec * 1000;
  ts.tv_sec += sec;
  ret = pthread_cond_timedwait(cond, mtx, &ts);
  pthread_mutex_unlock(mtx);
  return ret;
}


x86 assembly (clang, -O0):
<pwait_event_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <pthread_mutex_lock@plt>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
imul   $0x3e8,-0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1050 <pthread_cond_timedwait@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <pthread_mutex_unlock@plt>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pwait_event_timeout>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1060 <pthread_mutex_lock@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
imul   $0x3e8,0xc(%rsp),%eax
mov    %eax,0x4(%rsp)
add    0x8(%rsp),%ebx
mov    %ebx,(%rsp)
mov    %rsp,%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <pthread_cond_timedwait@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <pthread_mutex_unlock@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.timespec = type { i32, i32 }
define i32 @pwait_event_timeout(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca %struct.timespec
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%10 = load i32*, i32** %5
%11 = call i32 @pthread_mutex_lock(i32* %10)
%12 = call i32 @gettimeofday(%struct.timeval* %8, i32* null)
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%17 = load i32, i32* %16
%18 = mul nsw i32 %17, 1000
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* %6
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%22 = load i32, i32* %21
%23 = add i32 %22, %20
store i32 %23, i32* %21
%24 = load i32*, i32** %4
%25 = load i32*, i32** %5
%26 = call i32 @pthread_cond_timedwait(i32* %24, i32* %25, %struct.timespec* %9)
store i32 %26, i32* %7
%27 = load i32*, i32** %5
%28 = call i32 @pthread_mutex_unlock(i32* %27)
%29 = load i32, i32* %7
ret i32 %29
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.timespec = type { i32, i32 }
define i32 @pwait_event_timeout(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 = tail call i32 @pthread_mutex_lock(i32* %1)
%9 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%14 = load i32, i32* %13
%15 = mul nsw i32 %14, 1000
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
store i32 %15, i32* %16
%17 = add i32 %11, %2
store i32 %17, i32* %12
%18 = call i32 @pthread_cond_timedwait(i32* %0, i32* %1, %struct.timespec* %5)
%19 = call i32 @pthread_mutex_unlock(i32* %1)
ret i32 %18
}