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

/* Variables and functions */
#define ETIMEDOUT 128
int __pthread_mutex_timedlock(int /*<<< orphan*/ *, struct timespec const *);
int thrd_error;
int thrd_success;
int thrd_timedout;
int mtx_timedlock(mtx_t *restrict m, const struct timespec *restrict ts) {
  int ret = __pthread_mutex_timedlock((pthread_mutex_t *)m, ts);
  switch (ret) {
  default:
    return thrd_error;
  case 0:
    return thrd_success;
  case ETIMEDOUT:
    return thrd_timedout;
  }
}


x86 assembly (clang, -O0):
<mtx_timedlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <__pthread_mutex_timedlock@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
test   %eax,%eax
je     1167 <mtx_timedlock+0x57>
jmp    1143 <mtx_timedlock+0x33>
mov    -0x20(%rbp),%eax
sub    $0x80,%eax
je     1178 <mtx_timedlock+0x68>
jmp    1156 <mtx_timedlock+0x46>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1184 <mtx_timedlock+0x74>
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1184 <mtx_timedlock+0x74>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mtx_timedlock>:
push   %rax
call   1030 <__pthread_mutex_timedlock@plt>
cmp    $0x80,%eax
jne    112a <mtx_timedlock+0x1a>
mov    0x2eb4(%rip),%rcx
test   %eax,%eax
je     1135 <mtx_timedlock+0x25>
jmp    113c <mtx_timedlock+0x2c>
mov    0x2e9f(%rip),%rcx
test   %eax,%eax
jne    113c <mtx_timedlock+0x2c>
mov    0x2eac(%rip),%rcx
mov    (%rcx),%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@thrd_error = global i32 0
@thrd_success = global i32 0
@thrd_timedout = global i32 0
define i32 @mtx_timedlock(i32* noalias %0, %struct.timespec* noalias %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca %struct.timespec*
%6 = alloca i32
store i32* %0, i32** %4
store %struct.timespec* %1, %struct.timespec** %5
%7 = load i32*, i32** %4
%8 = load %struct.timespec*, %struct.timespec** %5
%9 = call i32 @__pthread_mutex_timedlock(i32* %7, %struct.timespec* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
switch i32 %10, label %11 [
i32 0, label %13
i32 128, label %15
]
11: ; preds = %2
%12 = load i32, i32* @thrd_error
store i32 %12, i32* %3
br label %17
13: ; preds = %2
%14 = load i32, i32* @thrd_success
store i32 %14, i32* %3
br label %17
15: ; preds = %2
%16 = load i32, i32* @thrd_timedout
store i32 %16, i32* %3
br label %17
17: ; preds = %15, %13, %11
%18 = load i32, i32* %3
ret i32 %18
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@thrd_error = global i32 0
@thrd_success = global i32 0
@thrd_timedout = global i32 0
define i32 @mtx_timedlock(i32* noalias %0, %struct.timespec* noalias %1) {
%3 = tail call i32 @__pthread_mutex_timedlock(i32* %0, %struct.timespec* %1)
%4 = icmp eq i32 %3, 128
%5 = icmp eq i32 %3, 0
%6 = load i32, i32* @thrd_success
%7 = load i32, i32* @thrd_timedout
%8 = load i32, i32* @thrd_error
%9 = select i1 %4, i32 %7, i32 %8
%10 = select i1 %5, i32 %6, i32 %9
ret i32 %10
}