#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;
};
struct lksmith_cond {
  int dummy;
};
typedef int /*<<< orphan*/ pthread_mutex_t;
typedef int /*<<< orphan*/ pthread_cond_t;

/* Variables and functions */
int EPERM;
int lksmith_check_locked(void const *);
int /*<<< orphan*/ lksmith_cond_postwait(struct lksmith_cond *);
int lksmith_cond_prewait(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                         struct lksmith_cond **);
int /*<<< orphan*/ lksmith_error(int, char *, int /*<<< orphan*/ *,
                                 int /*<<< orphan*/ *);
int r_pthread_cond_timedwait(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                             struct timespec const *);
int pthread_cond_timedwait(pthread_cond_t *__restrict cond,
                           pthread_mutex_t *__restrict mutex,
                           const struct timespec *__restrict abstime) {
  struct lksmith_cond *cnd = NULL;
  int ret = lksmith_check_locked((const void *)mutex);
  if (ret > 0) {
    return ret;
  } else if (ret == -1) {
    lksmith_error(EPERM,
                  "pthread_cond_timedwait(cond=%p, "
                  "mutex=%p): you called pthread_cond_timedwait on "
                  "a mutex that you do not currently hold.  Please "
                  "fix this serious error in your program.\n",
                  cond, mutex);
    return EPERM;
  }
  ret = lksmith_cond_prewait(cond, mutex, &cnd);
  if (ret)
    return ret;
  ret = r_pthread_cond_timedwait(cond, mutex, abstime);
  lksmith_cond_postwait(cnd);
  return ret;
}


x86 assembly (clang, -O0):
<pthread_cond_timedwait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <lksmith_check_locked@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    118d <pthread_cond_timedwait+0x3d>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1216 <pthread_cond_timedwait+0xc6>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11c5 <pthread_cond_timedwait+0x75>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe51(%rip),%rsi
call   1050 <lksmith_error@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1216 <pthread_cond_timedwait+0xc6>
jmp    11ca <pthread_cond_timedwait+0x7a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1030 <lksmith_cond_prewait@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11f3 <pthread_cond_timedwait+0xa3>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1216 <pthread_cond_timedwait+0xc6>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <r_pthread_cond_timedwait@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <lksmith_cond_postwait@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pthread_cond_timedwait>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
movq   $0x0,(%rsp)
mov    %rsi,%rdi
call   1060 <lksmith_check_locked@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1183 <pthread_cond_timedwait+0x33>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0xffffffff,%ebp
je     11b7 <pthread_cond_timedwait+0x67>
mov    %rsp,%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <lksmith_cond_prewait@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1176 <pthread_cond_timedwait+0x26>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <r_pthread_cond_timedwait@plt>
mov    %eax,%ebp
mov    (%rsp),%rdi
call   1070 <lksmith_cond_postwait@plt>
jmp    1176 <pthread_cond_timedwait+0x26>
mov    0x2e1a(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe38(%rip),%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1050 <lksmith_error@plt>
mov    0x0(%rbp),%ebp
jmp    1176 <pthread_cond_timedwait+0x26>

LLVM IR (-O0):
%struct.timespec = type { i32 }
%struct.lksmith_cond = type { i32 }
@EPERM = global i32 0
@.str = [169 x i8] c"pthread_cond_timedwait(cond=%p, mutex=%p): you called pthread_cond_timedwait on a mutex that you do not currently hold. Please fix this serious error in your program.\0A\00"
define i32 @pthread_cond_timedwait(i32* noalias %0, i32* noalias %1, %struct.timespec* noalias %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca %struct.timespec*
%8 = alloca %struct.lksmith_cond*
%9 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store %struct.timespec* %2, %struct.timespec** %7
store %struct.lksmith_cond* null, %struct.lksmith_cond** %8
%10 = load i32*, i32** %6
%11 = bitcast i32* %10 to i8*
%12 = call i32 @lksmith_check_locked(i8* %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i32, i32* %9
store i32 %16, i32* %4
br label %43
17: ; preds = %3
%18 = load i32, i32* %9
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i32, i32* @EPERM
%22 = load i32*, i32** %5
%23 = load i32*, i32** %6
%24 = call i32 @lksmith_error(i32 %21, i8* getelementptr inbounds ([169 x i8], [169 x i8]* @.str, i64 0, i64 0), i32* %22, i32* %23)
%25 = load i32, i32* @EPERM
store i32 %25, i32* %4
br label %43
26: ; preds = %17
br label %27
27: ; preds = %26
%28 = load i32*, i32** %5
%29 = load i32*, i32** %6
%30 = call i32 @lksmith_cond_prewait(i32* %28, i32* %29, %struct.lksmith_cond** %8)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = load i32, i32* %9
store i32 %34, i32* %4
br label %43
35: ; preds = %27
%36 = load i32*, i32** %5
%37 = load i32*, i32** %6
%38 = load %struct.timespec*, %struct.timespec** %7
%39 = call i32 @r_pthread_cond_timedwait(i32* %36, i32* %37, %struct.timespec* %38)
store i32 %39, i32* %9
%40 = load %struct.lksmith_cond*, %struct.lksmith_cond** %8
%41 = call i32 @lksmith_cond_postwait(%struct.lksmith_cond* %40)
%42 = load i32, i32* %9
store i32 %42, i32* %4
br label %43
43: ; preds = %35, %33, %20, %15
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
%struct.lksmith_cond = type { i32 }
@EPERM = global i32 0
@.str = [169 x i8] c"pthread_cond_timedwait(cond=%p, mutex=%p): you called pthread_cond_timedwait on a mutex that you do not currently hold. Please fix this serious error in your program.\0A\00"
define i32 @pthread_cond_timedwait(i32* noalias %0, i32* noalias %1, %struct.timespec* noalias %2) {
%4 = alloca %struct.lksmith_cond*
%5 = bitcast %struct.lksmith_cond** %4 to i8*
store %struct.lksmith_cond* null, %struct.lksmith_cond** %4
%6 = bitcast i32* %1 to i8*
%7 = tail call i32 @lksmith_check_locked(i8* %6)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %22, label %9
9: ; preds = %3
%10 = icmp eq i32 %7, -1
br i1 %10, label %11, label %15
11: ; preds = %9
%12 = load i32, i32* @EPERM
%13 = tail call i32 @lksmith_error(i32 %12, i8* getelementptr inbounds ([169 x i8], [169 x i8]* @.str, i64 0, i64 0), i32* %0, i32* %1)
%14 = load i32, i32* @EPERM
br label %22
15: ; preds = %9
%16 = call i32 @lksmith_cond_prewait(i32* %0, i32* %1, %struct.lksmith_cond** %4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = call i32 @r_pthread_cond_timedwait(i32* %0, i32* %1, %struct.timespec* %2)
%20 = load %struct.lksmith_cond*, %struct.lksmith_cond** %4
%21 = call i32 @lksmith_cond_postwait(%struct.lksmith_cond* %20)
br label %22
22: ; preds = %15, %3, %18, %11
%23 = phi i32 [ %14, %11 ], [ %19, %18 ], [ %7, %3 ], [ %16, %15 ]
ret i32 %23
}