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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int EBUSY;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ __NR_gettid;
int /*<<< orphan*/ assert(int);
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getpid();
int pthread_mutex_trylock(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
scalar_t__ syscall(int /*<<< orphan*/);
void timed_mutex_lock(pthread_mutex_t *mutex) {
  int i;
  struct timespec start, now;
  i = clock_gettime(CLOCK_MONOTONIC, &start);
  assert(i == 0);
  do {
    i = pthread_mutex_trylock(mutex);
    if (i == 0)
      return;
    assert(i == EBUSY);
    i = clock_gettime(CLOCK_MONOTONIC, &now);
    assert(i == 0);
    assert(now.tv_sec >= start.tv_sec);
  } while (now.tv_sec - start.tv_sec <
           (syscall(__NR_gettid) == getpid() ? 10 : 15));
  fprintf(stderr, "Timed out waiting for internal lock!\n");
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<timed_mutex_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1050 <pthread_mutex_trylock@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11be <timed_mutex_lock+0x4e>
add    $0x30,%rsp
pop    %rbp
ret
mov    -0xc(%rbp),%eax
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%eax
cmp    -0x10(%rbp),%eax
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%eax
sub    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1080 <syscall@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdi
mov    -0x1c(%rbp),%eax
mov    $0xf,%ecx
mov    $0xa,%edx
cmp    %rdi,%rsi
cmove  %edx,%ecx
cmp    %ecx,%eax
jl     11a2 <timed_mutex_lock+0x32>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xd9c(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<timed_mutex_lock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r15
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   1030 <clock_gettime@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    0x2e18(%rip),%r13
lea    0x8(%rsp),%r14
mov    0x2e2c(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
call   1050 <pthread_mutex_trylock@plt>
test   %eax,%eax
je     1262 <timed_mutex_lock+0xf2>
xor    %edi,%edi
cmp    0x0(%r13),%eax
sete   %dil
call   1060 <assert@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
call   1030 <clock_gettime@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    0x8(%rsp),%eax
xor    %edi,%edi
cmp    0x10(%rsp),%eax
setge  %dil
call   1060 <assert@plt>
mov    0x8(%rsp),%r12d
sub    0x10(%rsp),%r12d
mov    0x0(%rbp),%edi
call   1080 <syscall@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
xor    %ecx,%ecx
cmp    %rax,%rbx
setne  %cl
lea    (%rcx,%rcx,4),%eax
add    $0xa,%eax
cmp    %eax,%r12d
jl     11c0 <timed_mutex_lock+0x50>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@EBUSY = global i32 0
@__NR_gettid = global i32 0
@stderr = global i32 0
@.str = [38 x i8] c"Timed out waiting for internal lock!\0A\00"
@EXIT_FAILURE = global i32 0
define void @timed_mutex_lock(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
store i32* %0, i32** %2
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %4)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, 0
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
br label %12
12: ; preds = %37, %1
%13 = load i32*, i32** %2
%14 = call i32 @pthread_mutex_trylock(i32* %13)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
ret void
18: ; preds = %12
%19 = load i32, i32* %3
%20 = load i32, i32* @EBUSY
%21 = icmp eq i32 %19, %20
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i32, i32* @CLOCK_MONOTONIC
%25 = call i32 @clock_gettime(i32 %24, %struct.timespec* %5)
store i32 %25, i32* %3
%26 = load i32, i32* %3
%27 = icmp eq i32 %26, 0
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%33 = load i32, i32* %32
%34 = icmp sge i32 %31, %33
%35 = zext i1 %34 to i32
%36 = call i32 @assert(i32 %35)
br label %37
37: ; preds = %18
%38 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%41 = load i32, i32* %40
%42 = sub nsw i32 %39, %41
%43 = load i32, i32* @__NR_gettid
%44 = call i64 @syscall(i32 %43)
%45 = call i64 (...) @getpid()
%46 = icmp eq i64 %44, %45
%47 = zext i1 %46 to i64
%48 = select i1 %46, i32 10, i32 15
%49 = icmp slt i32 %42, %48
br i1 %49, label %12, label %50
50: ; preds = %37
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@EBUSY = global i32 0
@__NR_gettid = global i32 0
@stderr = global i32 0
@.str = [38 x i8] c"Timed out waiting for internal lock!\0A\00"
@EXIT_FAILURE = global i32 0
define void @timed_mutex_lock(i32* %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %2)
%8 = icmp eq i32 %7, 0
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
br label %13
13: ; preds = %17, %1
%14 = call i32 @pthread_mutex_trylock(i32* %0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13
ret void
17: ; preds = %13
%18 = load i32, i32* @EBUSY
%19 = icmp eq i32 %14, %18
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i32, i32* @CLOCK_MONOTONIC
%23 = call i32 @clock_gettime(i32 %22, %struct.timespec* %3)
%24 = icmp eq i32 %23, 0
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = load i32, i32* %11
%28 = load i32, i32* %12
%29 = icmp sge i32 %27, %28
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i32, i32* %11
%33 = load i32, i32* %12
%34 = sub nsw i32 %32, %33
%35 = load i32, i32* @__NR_gettid
%36 = call i64 @syscall(i32 %35)
%37 = call i64 (...) @getpid()
%38 = icmp eq i64 %36, %37
%39 = select i1 %38, i32 10, i32 15
%40 = icmp slt i32 %34, %39
br i1 %40, label %13, label %41
41: ; preds = %17
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
}