#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;
int pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutexattr_destroy(int /*<<< orphan*/ *);
int pthread_mutexattr_init(int /*<<< orphan*/ *);
int pthread_mutexattr_settype(int /*<<< orphan*/ *, int /*<<< orphan*/);
int usbi_mutex_init_recursive(pthread_mutex_t *mutex,
                              pthread_mutexattr_t *attr) {
  int err;
  pthread_mutexattr_t stack_attr;
  if (!attr) {
    attr = &stack_attr;
    err = pthread_mutexattr_init(&stack_attr);
    if (err != 0)
      return err;
  }
  err = pthread_mutexattr_settype(attr, PTHREAD_MUTEX_RECURSIVE);
  if (err != 0)
    goto finish;
  err = pthread_mutex_init(mutex, attr);
finish:
  if (attr == &stack_attr)
    pthread_mutexattr_destroy(&stack_attr);
  return err;
}


x86 assembly (clang, -O0):
<usbi_mutex_init_recursive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1189 <usbi_mutex_init_recursive+0x49>
lea    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <pthread_mutexattr_init@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1184 <usbi_mutex_init_recursive+0x44>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11da <usbi_mutex_init_recursive+0x9a>
jmp    1189 <usbi_mutex_init_recursive+0x49>
mov    -0x18(%rbp),%rdi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
call   1030 <pthread_mutexattr_settype@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11ad <usbi_mutex_init_recursive+0x6d>
jmp    11bd <usbi_mutex_init_recursive+0x7d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <pthread_mutex_init@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rax
cmp    %rax,-0x18(%rbp)
jne    11d4 <usbi_mutex_init_recursive+0x94>
lea    -0x20(%rbp),%rdi
call   1060 <pthread_mutexattr_destroy@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usbi_mutex_init_recursive>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
test   %rsi,%rsi
je     118d <usbi_mutex_init_recursive+0x4d>
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1030 <pthread_mutexattr_settype@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1177 <usbi_mutex_init_recursive+0x37>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <pthread_mutex_init@plt>
mov    %eax,%ebp
lea    0xc(%rsp),%rax
cmp    %rax,%rbx
jne    11a0 <usbi_mutex_init_recursive+0x60>
lea    0xc(%rsp),%rdi
call   1060 <pthread_mutexattr_destroy@plt>
jmp    11a0 <usbi_mutex_init_recursive+0x60>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <pthread_mutexattr_init@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1153 <usbi_mutex_init_recursive+0x13>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PTHREAD_MUTEX_RECURSIVE = global i32 0
define i32 @usbi_mutex_init_recursive(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %17, label %10
10: ; preds = %2
store i32* %7, i32** %5
%11 = call i32 @pthread_mutexattr_init(i32* %7)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = load i32, i32* %6
store i32 %15, i32* %3
br label %35
16: ; preds = %10
br label %17
17: ; preds = %16, %2
%18 = load i32*, i32** %5
%19 = load i32, i32* @PTHREAD_MUTEX_RECURSIVE
%20 = call i32 @pthread_mutexattr_settype(i32* %18, i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %17
br label %28
24: ; preds = %17
%25 = load i32*, i32** %4
%26 = load i32*, i32** %5
%27 = call i32 @pthread_mutex_init(i32* %25, i32* %26)
store i32 %27, i32* %6
br label %28
28: ; preds = %24, %23
%29 = load i32*, i32** %5
%30 = icmp eq i32* %29, %7
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @pthread_mutexattr_destroy(i32* %7)
br label %33
33: ; preds = %31, %28
%34 = load i32, i32* %6
store i32 %34, i32* %3
br label %35
35: ; preds = %33, %14
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@PTHREAD_MUTEX_RECURSIVE = global i32 0
define i32 @usbi_mutex_init_recursive(i32* %0, i32* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32* %1, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = call i32 @pthread_mutexattr_init(i32* %3)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %6, %2
%10 = phi i32* [ %1, %2 ], [ %3, %6 ]
%11 = load i32, i32* @PTHREAD_MUTEX_RECURSIVE
%12 = call i32 @pthread_mutexattr_settype(i32* %10, i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @pthread_mutex_init(i32* %0, i32* %10)
br label %16
16: ; preds = %9, %14
%17 = phi i32 [ %12, %9 ], [ %15, %14 ]
%18 = icmp eq i32* %10, %3
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @pthread_mutexattr_destroy(i32* %3)
br label %21
21: ; preds = %16, %19, %6
%22 = phi i32 [ %7, %6 ], [ %17, %19 ], [ %17, %16 ]
ret i32 %22
}