#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 timespec {
  unsigned int tv_nsec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ pthread_mutex_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ hybris_check_android_shared_mutex(unsigned int);
int /*<<< orphan*/ *my_static_init_mutex(int /*<<< orphan*/ *);
int pthread_mutex_timedlock(int /*<<< orphan*/ *, struct timespec *);
int my_pthread_mutex_lock_timeout_np(pthread_mutex_t *__mutex,
                                     unsigned __msecs) {
  struct timespec tv;
  pthread_mutex_t *realmutex;
  unsigned int value = (*(unsigned int *)__mutex);
  if (hybris_check_android_shared_mutex(value)) {
    while (0) {
    };
    return 0;
  }
  realmutex = (pthread_mutex_t *)value;
  if (value <= 0xFFFF) {
    realmutex = my_static_init_mutex(__mutex);
  }
  clock_gettime(CLOCK_REALTIME, &tv);
  tv.tv_sec += __msecs / 1000;
  tv.tv_nsec += (__msecs % 1000) * 1000000;
  if (tv.tv_nsec >= 1000000000) {
    tv.tv_sec++;
    tv.tv_nsec -= 1000000000;
  }
  return pthread_mutex_timedlock(realmutex, &tv);
}


x86 assembly (clang, -O0):
<my_pthread_mutex_lock_timeout_np>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1030 <hybris_check_android_shared_mutex@plt>
cmp    $0x0,%rax
je     118f <my_pthread_mutex_lock_timeout_np+0x4f>
jmp    116f <my_pthread_mutex_lock_timeout_np+0x2f>
xor    %eax,%eax
test   $0x1,%al
jne    117e <my_pthread_mutex_lock_timeout_np+0x3e>
jmp    1183 <my_pthread_mutex_lock_timeout_np+0x43>
jmp    116f <my_pthread_mutex_lock_timeout_np+0x2f>
movl   $0x0,-0x4(%rbp)
jmp    1224 <my_pthread_mutex_lock_timeout_np+0xe4>
mov    -0x34(%rbp),%eax
mov    %rax,-0x30(%rbp)
cmpl   $0xffff,-0x34(%rbp)
ja     11b0 <my_pthread_mutex_lock_timeout_np+0x70>
mov    -0x10(%rbp),%rdi
call   1050 <my_static_init_mutex@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%eax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
imul   $0xf4240,%edx,%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x3b9aca00,-0x28(%rbp)
jb     1214 <my_pthread_mutex_lock_timeout_np+0xd4>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
sub    $0x3b9aca00,%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <pthread_mutex_timedlock@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_pthread_mutex_lock_timeout_np>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    (%rdi),%ebx
mov    %ebx,%edi
call   1030 <hybris_check_android_shared_mutex@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11db <my_pthread_mutex_lock_timeout_np+0x9b>
cmp    $0xffff,%ebx
ja     1174 <my_pthread_mutex_lock_timeout_np+0x34>
mov    %rbp,%rdi
call   1050 <my_static_init_mutex@plt>
mov    %rax,%rbx
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1040 <clock_gettime@plt>
mov    %r14d,%eax
imul   $0x10624dd3,%rax,%rcx
shr    $0x26,%rcx
mov    0x8(%rsp),%rax
add    %rcx,%rax
mov    %rax,0x8(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%r14d
imul   $0xf4240,%r14d,%ecx
add    (%rsp),%ecx
mov    %ecx,(%rsp)
cmp    $0x3b9aca00,%ecx
jb     11d0 <my_pthread_mutex_lock_timeout_np+0x90>
add    $0x1,%rax
mov    %rax,0x8(%rsp)
add    $0xc4653600,%ecx
mov    %ecx,(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <pthread_mutex_timedlock@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@CLOCK_REALTIME = global i32 0
define i32 @my_pthread_mutex_lock_timeout_np(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca %struct.timespec
%7 = alloca i32*
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%9 = load i32*, i32** %4
%10 = load i32, i32* %9
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = call i64 @hybris_check_android_shared_mutex(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
br label %15
15: ; preds = %16, %14
br i1 false, label %16, label %17
16: ; preds = %15
br label %15
17: ; preds = %15
store i32 0, i32* %3
br label %55
18: ; preds = %2
%19 = load i32, i32* %8
%20 = zext i32 %19 to i64
%21 = inttoptr i64 %20 to i32*
store i32* %21, i32** %7
%22 = load i32, i32* %8
%23 = icmp ule i32 %22, 65535
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i32*, i32** %4
%26 = call i32* @my_static_init_mutex(i32* %25)
store i32* %26, i32** %7
br label %27
27: ; preds = %24, %18
%28 = load i32, i32* @CLOCK_REALTIME
%29 = call i32 @clock_gettime(i32 %28, %struct.timespec* %6)
%30 = load i32, i32* %5
%31 = udiv i32 %30, 1000
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%34 = load i64, i64* %33
%35 = add nsw i64 %34, %32
store i64 %35, i64* %33
%36 = load i32, i32* %5
%37 = urem i32 %36, 1000
%38 = mul i32 %37, 1000000
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%40 = load i32, i32* %39
%41 = add i32 %40, %38
store i32 %41, i32* %39
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%43 = load i32, i32* %42
%44 = icmp uge i32 %43, 1000000000
br i1 %44, label %45, label %52
45: ; preds = %27
%46 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%47 = load i64, i64* %46
%48 = add nsw i64 %47, 1
store i64 %48, i64* %46
%49 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%50 = load i32, i32* %49
%51 = sub i32 %50, 1000000000
store i32 %51, i32* %49
br label %52
52: ; preds = %45, %27
%53 = load i32*, i32** %7
%54 = call i32 @pthread_mutex_timedlock(i32* %53, %struct.timespec* %6)
store i32 %54, i32* %3
br label %55
55: ; preds = %52, %17
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@CLOCK_REALTIME = global i32 0
define i32 @my_pthread_mutex_lock_timeout_np(i32* %0, i32 %1) {
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %3 to i8*
%5 = load i32, i32* %0
%6 = tail call i64 @hybris_check_android_shared_mutex(i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %34
8: ; preds = %2
%9 = zext i32 %5 to i64
%10 = inttoptr i64 %9 to i32*
%11 = icmp ult i32 %5, 65536
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32* @my_static_init_mutex(i32* %0)
br label %14
14: ; preds = %12, %8
%15 = phi i32* [ %13, %12 ], [ %10, %8 ]
%16 = load i32, i32* @CLOCK_REALTIME
%17 = call i32 @clock_gettime(i32 %16, %struct.timespec* %3)
%18 = udiv i32 %1, 1000
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%21 = load i64, i64* %20
%22 = add nsw i64 %21, %19
store i64 %22, i64* %20
%23 = urem i32 %1, 1000
%24 = mul nuw nsw i32 %23, 1000000
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%26 = load i32, i32* %25
%27 = add i32 %26, %24
store i32 %27, i32* %25
%28 = icmp ugt i32 %27, 999999999
br i1 %28, label %29, label %32
29: ; preds = %14
%30 = add nsw i64 %22, 1
store i64 %30, i64* %20
%31 = add i32 %27, -1000000000
store i32 %31, i32* %25
br label %32
32: ; preds = %29, %14
%33 = call i32 @pthread_mutex_timedlock(i32* %15, %struct.timespec* %3)
br label %34
34: ; preds = %2, %32
%35 = phi i32 [ %33, %32 ], [ 0, %2 ]
ret i32 %35
}