#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 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_wait(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int pthread_cond_wait(pthread_cond_t *__restrict cond,
                      pthread_mutex_t *__restrict mutex) {
  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_wait(cond=%p, mutex=%p): "
                  "you called pthread_cond_wait 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_wait(cond, mutex);
  lksmith_cond_postwait(cnd);
  return ret;
}


x86 assembly (clang, -O0):
<pthread_cond_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <lksmith_check_locked@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    1189 <pthread_cond_wait+0x39>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <pthread_cond_wait+0xbe>
cmpl   $0xffffffff,-0x24(%rbp)
jne    11c1 <pthread_cond_wait+0x71>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe55(%rip),%rsi
call   1040 <lksmith_error@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <pthread_cond_wait+0xbe>
jmp    11c6 <pthread_cond_wait+0x76>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <lksmith_cond_prewait@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11ef <pthread_cond_wait+0x9f>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <pthread_cond_wait+0xbe>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <r_pthread_cond_wait@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <lksmith_cond_postwait@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pthread_cond_wait>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
movq   $0x0,0x8(%rsp)
mov    %rsi,%rdi
call   1060 <lksmith_check_locked@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1180 <pthread_cond_wait+0x30>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
cmp    $0xffffffff,%ebp
je     11b4 <pthread_cond_wait+0x64>
lea    0x8(%rsp),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <lksmith_cond_prewait@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1175 <pthread_cond_wait+0x25>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <r_pthread_cond_wait@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rdi
call   1070 <lksmith_cond_postwait@plt>
jmp    1175 <pthread_cond_wait+0x25>
mov    0x2e1d(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xe3b(%rip),%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1040 <lksmith_error@plt>
mov    0x0(%rbp),%ebp
jmp    1175 <pthread_cond_wait+0x25>

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

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