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

/* Variables and functions */
scalar_t__ EDEADLK;
int /*<<< orphan*/ printf(char *);
scalar_t__ pthread_mutex_lock(int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_unlock(int /*<<< orphan*/ *);
int test_debug_double_lock(pthread_mutex_t *mutex) {
  printf("test_debug_double_lock()\n");
  if (pthread_mutex_lock(mutex)) {
    printf("pthread_mutex_lock() ERROR\n");
    return (-1);
  }
  if (pthread_mutex_lock(mutex) != EDEADLK) {
    printf("double lock error not detected ERROR\n");
    return (-1);
  }
  if (pthread_mutex_unlock(mutex)) {
    printf("pthread_mutex_unlock() ERROR\n");
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<test_debug_double_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xebd(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <pthread_mutex_lock@plt>
cmp    $0x0,%rax
je     1173 <test_debug_double_lock+0x43>
lea    0xeb8(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <test_debug_double_lock+0xa6>
mov    -0x10(%rbp),%rdi
call   1050 <pthread_mutex_lock@plt>
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%rax
je     11a4 <test_debug_double_lock+0x74>
lea    0xea3(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <test_debug_double_lock+0xa6>
mov    -0x10(%rbp),%rdi
call   1040 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
je     11cf <test_debug_double_lock+0x9f>
lea    0xe9e(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <test_debug_double_lock+0xa6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_debug_double_lock>:
push   %rbx
mov    %rdi,%rbx
lea    0xec5(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <pthread_mutex_lock@plt>
test   %rax,%rax
je     1156 <test_debug_double_lock+0x26>
lea    0xf07(%rip),%rdi
jmp    1187 <test_debug_double_lock+0x57>
mov    %rbx,%rdi
call   1050 <pthread_mutex_lock@plt>
mov    0x2e83(%rip),%rcx
cmp    (%rcx),%rax
jne    1180 <test_debug_double_lock+0x50>
mov    %rbx,%rdi
call   1040 <pthread_mutex_unlock@plt>
test   %rax,%rax
je     1193 <test_debug_double_lock+0x63>
lea    0xe9b(%rip),%rdi
jmp    1187 <test_debug_double_lock+0x57>
lea    0xeaf(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [26 x i8] c"test_debug_double_lock()\0A\00"
@.str.1 = [28 x i8] c"pthread_mutex_lock() ERROR\0A\00"
@EDEADLK = global i64 0
@.str.2 = [38 x i8] c"double lock error not detected ERROR\0A\00"
@.str.3 = [30 x i8] c"pthread_mutex_unlock() ERROR\0A\00"
define i32 @test_debug_double_lock(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
store i32* %0, i32** %3
%4 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%5 = load i32*, i32** %3
%6 = call i64 @pthread_mutex_lock(i32* %5)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %24
10: ; preds = %1
%11 = load i32*, i32** %3
%12 = call i64 @pthread_mutex_lock(i32* %11)
%13 = load i64, i64* @EDEADLK
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %24
17: ; preds = %10
%18 = load i32*, i32** %3
%19 = call i64 @pthread_mutex_unlock(i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %24
23: ; preds = %17
store i32 0, i32* %2
br label %24
24: ; preds = %23, %21, %15, %8
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@EDEADLK = global i64 0
@str = [25 x i8] c"test_debug_double_lock()\00"
@str.4 = [29 x i8] c"pthread_mutex_unlock() ERROR\00"
@str.5 = [37 x i8] c"double lock error not detected ERROR\00"
@str.6 = [27 x i8] c"pthread_mutex_lock() ERROR\00"
define i32 @test_debug_double_lock(i32* %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%3 = tail call i64 @pthread_mutex_lock(i32* %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %12
5: ; preds = %1
%6 = tail call i64 @pthread_mutex_lock(i32* %0)
%7 = load i64, i64* @EDEADLK
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = tail call i64 @pthread_mutex_unlock(i32* %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9, %5, %1
%13 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0), %1 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @str.5, i64 0, i64 0), %5 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @str.4, i64 0, i64 0), %9 ]
%14 = tail call i32 @puts(i8* dereferenceable(1) %13)
br label %15
15: ; preds = %12, %9
%16 = phi i32 [ 0, %9 ], [ -1, %12 ]
ret i32 %16
}