#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*/ mtx_t;

/* Variables and functions */
int /*<<< orphan*/ PTHREAD_MUTEX_RECURSIVE;
int mtx_plain;
int mtx_recursive;
int mtx_timed;
scalar_t__ pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutexattr_destroy(int /*<<< orphan*/ *);
scalar_t__ pthread_mutexattr_init(int /*<<< orphan*/ *);
scalar_t__ pthread_mutexattr_settype(int /*<<< orphan*/ *, int /*<<< orphan*/);
int thrd_error;
int thrd_success;
int mtx_init(mtx_t *mtx, int type) {
  if (type == mtx_plain || type == mtx_timed) {
    if (pthread_mutex_init(mtx, NULL) == 0)
      return thrd_success;
    else
      return thrd_error;
  } else if (type == mtx_recursive || type == (mtx_recursive | mtx_timed)) {
    int rc = thrd_error;
    pthread_mutexattr_t attr;
    if (pthread_mutexattr_init(&attr))
      goto cleanup1;
    if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE))
      goto cleanup2;
    if (pthread_mutex_init(mtx, &attr) == 0)
      rc = thrd_success;
  cleanup2:
    pthread_mutexattr_destroy(&attr);
  cleanup1:
    return rc;
  } else {
    return thrd_error;
  }
}


x86 assembly (clang, -O0):
<mtx_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%eax
je     1173 <mtx_init+0x33>
mov    -0x14(%rbp),%eax
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%eax
jne    11ac <mtx_init+0x6c>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <pthread_mutex_init@plt>
cmp    $0x0,%rax
jne    119b <mtx_init+0x5b>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1268 <mtx_init+0x128>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1268 <mtx_init+0x128>
mov    -0x14(%rbp),%eax
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%eax
je     11db <mtx_init+0x9b>
mov    -0x14(%rbp),%eax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2dff(%rip),%rdx
or     (%rdx),%ecx
cmp    %ecx,%eax
jne    125c <mtx_init+0x11c>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <pthread_mutexattr_init@plt>
cmp    $0x0,%rax
je     11ff <mtx_init+0xbf>
jmp    1251 <mtx_init+0x111>
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdi
call   1030 <pthread_mutexattr_settype@plt>
cmp    $0x0,%rax
je     1220 <mtx_init+0xe0>
jmp    1248 <mtx_init+0x108>
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1050 <pthread_mutex_init@plt>
cmp    $0x0,%rax
jne    1243 <mtx_init+0x103>
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    1248 <mtx_init+0x108>
lea    -0x1c(%rbp),%rdi
call   1060 <pthread_mutexattr_destroy@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1268 <mtx_init+0x128>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mtx_init>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e63(%rip),%rax
cmp    %esi,(%rax)
je     115e <mtx_init+0x1e>
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
cmp    %esi,%eax
jne    1183 <mtx_init+0x43>
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <pthread_mutex_init@plt>
test   %rax,%rax
je     1178 <mtx_init+0x38>
mov    0x2e4c(%rip),%rax
mov    (%rax),%ebp
jmp    11eb <mtx_init+0xab>
mov    0x2e61(%rip),%rax
mov    (%rax),%ebp
jmp    11eb <mtx_init+0xab>
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e2d(%rip),%rdx
mov    (%rdx),%ebp
cmp    %esi,%ecx
je     119f <mtx_init+0x5f>
or     %eax,%ecx
cmp    %esi,%ecx
jne    11eb <mtx_init+0xab>
lea    0x4(%rsp),%rdi
call   1040 <pthread_mutexattr_init@plt>
test   %rax,%rax
jne    11eb <mtx_init+0xab>
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdi
call   1030 <pthread_mutexattr_settype@plt>
test   %rax,%rax
jne    11e1 <mtx_init+0xa1>
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <pthread_mutex_init@plt>
test   %rax,%rax
jne    11e1 <mtx_init+0xa1>
mov    0x2e01(%rip),%rax
mov    (%rax),%ebp
lea    0x4(%rsp),%rdi
call   1060 <pthread_mutexattr_destroy@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@mtx_plain = global i32 0
@mtx_timed = global i32 0
@thrd_success = global i32 0
@thrd_error = global i32 0
@mtx_recursive = global i32 0
@PTHREAD_MUTEX_RECURSIVE = global i32 0
define i32 @mtx_init(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 = load i32, i32* @mtx_plain
%10 = icmp eq i32 %8, %9
br i1 %10, label %15, label %11
11: ; preds = %2
%12 = load i32, i32* %5
%13 = load i32, i32* @mtx_timed
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %23
15: ; preds = %11, %2
%16 = load i32*, i32** %4
%17 = call i64 @pthread_mutex_init(i32* %16, i32* null)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i32, i32* @thrd_success
store i32 %20, i32* %3
br label %56
21: ; preds = %15
%22 = load i32, i32* @thrd_error
store i32 %22, i32* %3
br label %56
23: ; preds = %11
%24 = load i32, i32* %5
%25 = load i32, i32* @mtx_recursive
%26 = icmp eq i32 %24, %25
br i1 %26, label %33, label %27
27: ; preds = %23
%28 = load i32, i32* %5
%29 = load i32, i32* @mtx_recursive
%30 = load i32, i32* @mtx_timed
%31 = or i32 %29, %30
%32 = icmp eq i32 %28, %31
br i1 %32, label %33, label %54
33: ; preds = %27, %23
%34 = load i32, i32* @thrd_error
store i32 %34, i32* %6
%35 = call i64 @pthread_mutexattr_init(i32* %7)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33
br label %52
38: ; preds = %33
%39 = load i32, i32* @PTHREAD_MUTEX_RECURSIVE
%40 = call i64 @pthread_mutexattr_settype(i32* %7, i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38
br label %50
43: ; preds = %38
%44 = load i32*, i32** %4
%45 = call i64 @pthread_mutex_init(i32* %44, i32* %7)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = load i32, i32* @thrd_success
store i32 %48, i32* %6
br label %49
49: ; preds = %47, %43
br label %50
50: ; preds = %49, %42
%51 = call i32 @pthread_mutexattr_destroy(i32* %7)
br label %52
52: ; preds = %50, %37
%53 = load i32, i32* %6
store i32 %53, i32* %3
br label %56
54: ; preds = %27
%55 = load i32, i32* @thrd_error
store i32 %55, i32* %3
br label %56
56: ; preds = %54, %52, %21, %19
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@mtx_plain = global i32 0
@mtx_timed = global i32 0
@thrd_success = global i32 0
@thrd_error = global i32 0
@mtx_recursive = global i32 0
@PTHREAD_MUTEX_RECURSIVE = global i32 0
define i32 @mtx_init(i32* %0, i32 %1) {
%3 = alloca i32
%4 = load i32, i32* @mtx_plain
%5 = icmp eq i32 %4, %1
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i32, i32* @mtx_timed
%8 = icmp eq i32 %7, %1
br i1 %8, label %9, label %16
9: ; preds = %6, %2
%10 = tail call i64 @pthread_mutex_init(i32* %0, i32* null)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = load i32, i32* @thrd_success
br label %41
14: ; preds = %9
%15 = load i32, i32* @thrd_error
br label %41
16: ; preds = %6
%17 = load i32, i32* @mtx_recursive
%18 = icmp eq i32 %17, %1
%19 = or i32 %17, %7
%20 = icmp eq i32 %19, %1
%21 = select i1 %18, i1 true, i1 %20
%22 = load i32, i32* @thrd_error
br i1 %21, label %23, label %41
23: ; preds = %16
%24 = bitcast i32* %3 to i8*
%25 = call i64 @pthread_mutexattr_init(i32* %3)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %39
27: ; preds = %23
%28 = load i32, i32* @PTHREAD_MUTEX_RECURSIVE
%29 = call i64 @pthread_mutexattr_settype(i32* %3, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = call i64 @pthread_mutex_init(i32* %0, i32* %3)
%33 = icmp eq i64 %32, 0
%34 = load i32, i32* @thrd_success
%35 = select i1 %33, i32 %34, i32 %22
br label %36
36: ; preds = %31, %27
%37 = phi i32 [ %22, %27 ], [ %35, %31 ]
%38 = call i32 @pthread_mutexattr_destroy(i32* %3)
br label %39
39: ; preds = %23, %36
%40 = phi i32 [ %22, %23 ], [ %37, %36 ]
br label %41
41: ; preds = %16, %39, %14, %12
%42 = phi i32 [ %13, %12 ], [ %15, %14 ], [ %40, %39 ], [ %22, %16 ]
ret i32 %42
}