#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__ EPERM;
int /*<<< orphan*/ printf(char *);
scalar_t__ pthread_mutex_lock(int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_unlock(int /*<<< orphan*/ *);
int test_debug_double_unlock(pthread_mutex_t *mutex) {
  printf("test_debug_double_unlock()\n");
  if (pthread_mutex_lock(mutex)) {
    printf("pthread_mutex_lock() ERROR\n");
    return (-1);
  }
  if (pthread_mutex_unlock(mutex)) {
    printf("pthread_mutex_unlock() ERROR\n");
    return (-1);
  }
  if (pthread_mutex_unlock(mutex) != EPERM) {
    printf("double unlock error not detected ERROR\n");
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<test_debug_double_unlock>:
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_unlock+0x43>
lea    0xeba(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <test_debug_double_unlock+0xa6>
mov    -0x10(%rbp),%rdi
call   1040 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
je     119e <test_debug_double_unlock+0x6e>
lea    0xeab(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <test_debug_double_unlock+0xa6>
mov    -0x10(%rbp),%rdi
call   1040 <pthread_mutex_unlock@plt>
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
je     11cf <test_debug_double_unlock+0x9f>
lea    0xe98(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <test_debug_double_unlock+0xa6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_debug_double_unlock>:
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_unlock+0x26>
lea    0xf0b(%rip),%rdi
jmp    118c <test_debug_double_unlock+0x5c>
mov    %rbx,%rdi
call   1040 <pthread_mutex_unlock@plt>
test   %rax,%rax
je     116c <test_debug_double_unlock+0x3c>
lea    0xed8(%rip),%rdi
jmp    118c <test_debug_double_unlock+0x5c>
mov    %rbx,%rdi
call   1040 <pthread_mutex_unlock@plt>
mov    %rax,%rcx
mov    0x2e5a(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
je     1196 <test_debug_double_unlock+0x66>
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

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

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