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

/* Variables and functions */
int /*<<< orphan*/ PTHREAD_MUTEX_RECURSIVE;
scalar_t__ pthread_mutex_destroy(int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_lock(int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_unlock(int /*<<< orphan*/ *);
scalar_t__ pthread_mutexattr_settype(int /*<<< orphan*/ *, int /*<<< orphan*/);
int test_recursive_locks(pthread_mutex_t *mutex, pthread_mutexattr_t *attr) {
  int result = 1;
  if (pthread_mutexattr_settype(attr, PTHREAD_MUTEX_RECURSIVE) == 0 &&
      pthread_mutex_init(mutex, attr) == 0 && pthread_mutex_lock(mutex) == 0 &&
      pthread_mutex_lock(mutex) == 0 && pthread_mutex_lock(mutex) == 0 &&
      pthread_mutex_unlock(mutex) == 0 && pthread_mutex_unlock(mutex) == 0 &&
      pthread_mutex_unlock(mutex) == 0 && pthread_mutex_destroy(mutex) == 0)
    result = 0;
  return result;
}


x86 assembly (clang, -O0):
<test_recursive_locks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
call   1040 <pthread_mutexattr_settype@plt>
cmp    $0x0,%rax
jne    1226 <test_recursive_locks+0xd6>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <pthread_mutex_init@plt>
cmp    $0x0,%rax
jne    1226 <test_recursive_locks+0xd6>
mov    -0x8(%rbp),%rdi
call   1070 <pthread_mutex_lock@plt>
cmp    $0x0,%rax
jne    1226 <test_recursive_locks+0xd6>
mov    -0x8(%rbp),%rdi
call   1070 <pthread_mutex_lock@plt>
cmp    $0x0,%rax
jne    1226 <test_recursive_locks+0xd6>
mov    -0x8(%rbp),%rdi
call   1070 <pthread_mutex_lock@plt>
cmp    $0x0,%rax
jne    1226 <test_recursive_locks+0xd6>
mov    -0x8(%rbp),%rdi
call   1050 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
jne    1226 <test_recursive_locks+0xd6>
mov    -0x8(%rbp),%rdi
call   1050 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
jne    1226 <test_recursive_locks+0xd6>
mov    -0x8(%rbp),%rdi
call   1050 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
jne    1226 <test_recursive_locks+0xd6>
mov    -0x8(%rbp),%rdi
call   1030 <pthread_mutex_destroy@plt>
cmp    $0x0,%rax
jne    1226 <test_recursive_locks+0xd6>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_recursive_locks>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e97(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <pthread_mutexattr_settype@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    11e4 <test_recursive_locks+0x94>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <pthread_mutex_init@plt>
test   %rax,%rax
jne    11e4 <test_recursive_locks+0x94>
mov    %r14,%rdi
call   1070 <pthread_mutex_lock@plt>
test   %rax,%rax
jne    11e4 <test_recursive_locks+0x94>
mov    %r14,%rdi
call   1070 <pthread_mutex_lock@plt>
test   %rax,%rax
jne    11e4 <test_recursive_locks+0x94>
mov    %r14,%rdi
call   1070 <pthread_mutex_lock@plt>
test   %rax,%rax
jne    11e4 <test_recursive_locks+0x94>
mov    %r14,%rdi
call   1050 <pthread_mutex_unlock@plt>
test   %rax,%rax
jne    11e4 <test_recursive_locks+0x94>
mov    %r14,%rdi
call   1050 <pthread_mutex_unlock@plt>
test   %rax,%rax
jne    11e4 <test_recursive_locks+0x94>
mov    %r14,%rdi
call   1050 <pthread_mutex_unlock@plt>
test   %rax,%rax
jne    11e4 <test_recursive_locks+0x94>
mov    %r14,%rdi
call   1030 <pthread_mutex_destroy@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PTHREAD_MUTEX_RECURSIVE = global i32 0
define i32 @test_recursive_locks(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32 1, i32* %5
%6 = load i32*, i32** %4
%7 = load i32, i32* @PTHREAD_MUTEX_RECURSIVE
%8 = call i64 @pthread_mutexattr_settype(i32* %6, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %44
10: ; preds = %2
%11 = load i32*, i32** %3
%12 = load i32*, i32** %4
%13 = call i64 @pthread_mutex_init(i32* %11, i32* %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %44
15: ; preds = %10
%16 = load i32*, i32** %3
%17 = call i64 @pthread_mutex_lock(i32* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %44
19: ; preds = %15
%20 = load i32*, i32** %3
%21 = call i64 @pthread_mutex_lock(i32* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %44
23: ; preds = %19
%24 = load i32*, i32** %3
%25 = call i64 @pthread_mutex_lock(i32* %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %44
27: ; preds = %23
%28 = load i32*, i32** %3
%29 = call i64 @pthread_mutex_unlock(i32* %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %44
31: ; preds = %27
%32 = load i32*, i32** %3
%33 = call i64 @pthread_mutex_unlock(i32* %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %31
%36 = load i32*, i32** %3
%37 = call i64 @pthread_mutex_unlock(i32* %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %35
%40 = load i32*, i32** %3
%41 = call i64 @pthread_mutex_destroy(i32* %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %39
store i32 0, i32* %5
br label %44
44: ; preds = %43, %39, %35, %31, %27, %23, %19, %15, %10, %2
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
@PTHREAD_MUTEX_RECURSIVE = global i32 0
define i32 @test_recursive_locks(i32* %0, i32* %1) {
%3 = load i32, i32* @PTHREAD_MUTEX_RECURSIVE
%4 = tail call i64 @pthread_mutexattr_settype(i32* %1, i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %31
6: ; preds = %2
%7 = tail call i64 @pthread_mutex_init(i32* %0, i32* %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %31
9: ; preds = %6
%10 = tail call i64 @pthread_mutex_lock(i32* %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %31
12: ; preds = %9
%13 = tail call i64 @pthread_mutex_lock(i32* %0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %31
15: ; preds = %12
%16 = tail call i64 @pthread_mutex_lock(i32* %0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %15
%19 = tail call i64 @pthread_mutex_unlock(i32* %0)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %31
21: ; preds = %18
%22 = tail call i64 @pthread_mutex_unlock(i32* %0)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = tail call i64 @pthread_mutex_unlock(i32* %0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = tail call i64 @pthread_mutex_destroy(i32* %0)
%29 = icmp ne i64 %28, 0
%30 = zext i1 %29 to i32
br label %31
31: ; preds = %27, %24, %21, %18, %15, %12, %9, %6, %2
%32 = phi i32 [ 1, %24 ], [ 1, %21 ], [ 1, %18 ], [ 1, %15 ], [ 1, %12 ], [ 1, %9 ], [ 1, %6 ], [ 1, %2 ], [ %30, %27 ]
ret i32 %32
}