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

/* Variables and functions */
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int pthread_mutex_trylock(int /*<<< orphan*/ *);
int lock_with_timeout(pthread_mutex_t *mutex, const struct timespec *ts) {
  int ret;
  int left, step;
  left = ts->tv_sec * 1000;
  step = 10;
  do {
    if ((ret = pthread_mutex_trylock(mutex)) != 0) {
      struct timespec dly;
      dly.tv_sec = 0;
      dly.tv_nsec = step * 1000000;
      nanosleep(&dly, NULL);
      left -= step;
    }
  } while (ret != 0 && left > 0);
  return ret;
}


x86 assembly (clang, -O0):
<lock_with_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
imul   $0x3e8,(%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0xa,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <pthread_mutex_trylock@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1182 <lock_with_timeout+0x62>
movl   $0x0,-0x28(%rbp)
imul   $0xf4240,-0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
jmp    1187 <lock_with_timeout+0x67>
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x29(%rbp)
je     11a0 <lock_with_timeout+0x80>
cmpl   $0x0,-0x18(%rbp)
setg   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1144 <lock_with_timeout+0x24>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_with_timeout>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
imul   $0x3e8,(%rsi),%ebx
mov    %rdi,%r15
add    $0xa,%ebx
movabs $0x98968000000000,%r12
lea    0x8(%rsp),%r14
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <pthread_mutex_trylock@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1175 <lock_with_timeout+0x55>
mov    %r12,0x8(%rsp)
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
add    $0xfffffff6,%ebx
cmp    $0xa,%ebx
jg     1150 <lock_with_timeout+0x30>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
define i32 @lock_with_timeout(i32* %0, %struct.timespec* %1) {
%3 = alloca i32*
%4 = alloca %struct.timespec*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timespec
store i32* %0, i32** %3
store %struct.timespec* %1, %struct.timespec** %4
%9 = load %struct.timespec*, %struct.timespec** %4
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = mul nsw i32 %11, 1000
store i32 %12, i32* %6
store i32 10, i32* %7
br label %13
13: ; preds = %33, %2
%14 = load i32*, i32** %3
%15 = call i32 @pthread_mutex_trylock(i32* %14)
store i32 %15, i32* %5
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %13
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
store i32 0, i32* %18
%19 = load i32, i32* %7
%20 = mul nsw i32 %19, 1000000
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
store i32 %20, i32* %21
%22 = call i32 @nanosleep(%struct.timespec* %8, i32* null)
%23 = load i32, i32* %7
%24 = load i32, i32* %6
%25 = sub nsw i32 %24, %23
store i32 %25, i32* %6
br label %26
26: ; preds = %17, %13
br label %27
27: ; preds = %26
%28 = load i32, i32* %5
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* %6
%32 = icmp sgt i32 %31, 0
br label %33
33: ; preds = %30, %27
%34 = phi i1 [ false, %27 ], [ %32, %30 ]
br i1 %34, label %13, label %35
35: ; preds = %33
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define i32 @lock_with_timeout(i32* %0, %struct.timespec* nocapture %1) {
%3 = alloca %struct.timespec
%4 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%5 = load i32, i32* %4
%6 = mul nsw i32 %5, 1000
%7 = bitcast %struct.timespec* %3 to i8*
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
br label %10
10: ; preds = %14, %2
%11 = phi i32 [ %6, %2 ], [ %16, %14 ]
%12 = call i32 @pthread_mutex_trylock(i32* %0)
%13 = icmp eq i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %10
store i32 0, i32* %8
store i32 10000000, i32* %9
%15 = call i32 @nanosleep(%struct.timespec* %3, i32* null)
%16 = add nsw i32 %11, -10
%17 = icmp sgt i32 %11, 10
br i1 %17, label %10, label %18
18: ; preds = %10, %14
ret i32 %12
}