#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;

/* Variables and functions */
int /*<<< orphan*/ lksmith_postlock(int /*<<< orphan*/ *, int);
int lksmith_prelock(int /*<<< orphan*/ *, int);
int r_pthread_mutex_timedlock(int /*<<< orphan*/ *, struct timespec const *);
int pthread_mutex_timedlock(pthread_mutex_t *__restrict mutex,
                            __const struct timespec *__restrict ts) {
  int ret = lksmith_prelock(mutex, 1);
  if (ret)
    return ret;
  ret = r_pthread_mutex_timedlock(mutex, ts);
  lksmith_postlock(mutex, ret);
  return ret;
}


x86 assembly (clang, -O0):
<pthread_mutex_timedlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1030 <lksmith_prelock@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1166 <pthread_mutex_timedlock+0x36>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1188 <pthread_mutex_timedlock+0x58>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <r_pthread_mutex_timedlock@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <lksmith_postlock@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pthread_mutex_timedlock>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x1,%esi
call   1030 <lksmith_prelock@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1161 <pthread_mutex_timedlock+0x31>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <r_pthread_mutex_timedlock@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <lksmith_postlock@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
define i32 @pthread_mutex_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 = call i32 @lksmith_prelock(i32* %7, i32 1)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i32, i32* %6
store i32 %12, i32* %3
br label %21
13: ; preds = %2
%14 = load i32*, i32** %4
%15 = load %struct.timespec*, %struct.timespec** %5
%16 = call i32 @r_pthread_mutex_timedlock(i32* %14, %struct.timespec* %15)
store i32 %16, i32* %6
%17 = load i32*, i32** %4
%18 = load i32, i32* %6
%19 = call i32 @lksmith_postlock(i32* %17, i32 %18)
%20 = load i32, i32* %6
store i32 %20, i32* %3
br label %21
21: ; preds = %13, %11
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
define i32 @pthread_mutex_timedlock(i32* noalias %0, %struct.timespec* noalias %1) {
%3 = tail call i32 @lksmith_prelock(i32* %0, i32 1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @r_pthread_mutex_timedlock(i32* %0, %struct.timespec* %1)
%7 = tail call i32 @lksmith_postlock(i32* %0, i32 %6)
br label %8
8: ; preds = %2, %5
%9 = phi i32 [ %6, %5 ], [ %3, %2 ]
ret i32 %9
}