#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 EINVAL;
int /*<<< orphan*/ PTHREAD_MUTEX_ERRORCHECK;
scalar_t__ is_compatible_with_errcheck(int);
int /*<<< orphan*/ lksmith_error(int, char *, int, int /*<<< orphan*/);
int pthread_mutex_init_errcheck(int /*<<< orphan*/ *);
int pthread_mutexattr_gettype(int /*<<< orphan*/ *, int *);
int pthread_mutexattr_settype(int /*<<< orphan*/ *, int /*<<< orphan*/);
int r_pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ terror(int);
int pthread_mutex_real_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr,
                            int *recursive) {
  int ret, ty = 0;
  *recursive = 0;
  if (!attr) {
    return pthread_mutex_init_errcheck(mutex);
  }
  ret = pthread_mutexattr_gettype(attr, &ty);
  if (ret == EINVAL) {
    lksmith_error(ret,
                  "pthread_mutexattr_gettype failed "
                  "with error code %d: %s\n",
                  ret, terror(ret));
    return ret;
  }
  if (is_compatible_with_errcheck(ty)) {
    ret = pthread_mutexattr_settype(attr, PTHREAD_MUTEX_ERRORCHECK);
    if (ret) {
      lksmith_error(ret,
                    "pthread_mutexattr_settype failed "
                    "with error code %d: %s\n",
                    ret, terror(ret));
      return ret;
    }
  } else {
    *recursive = 1;
  }
  ret = r_pthread_mutex_init(mutex, attr);
  if (ret) {
    lksmith_error(ret,
                  "pthread_mutex_init failed "
                  "with error code %d: %s\n",
                  ret, terror(ret));
    return ret;
  }
  return ret;
}


x86 assembly (clang, -O0):
<pthread_mutex_real_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
cmpq   $0x0,-0x18(%rbp)
jne    11b1 <pthread_mutex_real_init+0x41>
mov    -0x10(%rbp),%rdi
call   1060 <pthread_mutex_init_errcheck@plt>
mov    %eax,-0x4(%rbp)
jmp    12cc <pthread_mutex_real_init+0x15c>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <pthread_mutexattr_gettype@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e15(%rip),%rcx
cmp    (%rcx),%eax
jne    1206 <pthread_mutex_real_init+0x96>
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
call   1090 <terror@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%edx
mov    %eax,%ecx
lea    0xe0a(%rip),%rsi
call   1040 <lksmith_error@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12cc <pthread_mutex_real_init+0x15c>
mov    -0x28(%rbp),%edi
call   1070 <is_compatible_with_errcheck@plt>
cmp    $0x0,%rax
je     126f <pthread_mutex_real_init+0xff>
mov    -0x18(%rbp),%rdi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
call   1050 <pthread_mutexattr_settype@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     126a <pthread_mutex_real_init+0xfa>
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%edi
call   1090 <terror@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%edx
mov    %eax,%ecx
lea    0xddf(%rip),%rsi
call   1040 <lksmith_error@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12cc <pthread_mutex_real_init+0x15c>
jmp    1279 <pthread_mutex_real_init+0x109>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <r_pthread_mutex_init@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12c6 <pthread_mutex_real_init+0x156>
mov    -0x24(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x24(%rbp),%edi
call   1090 <terror@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%edx
mov    %eax,%ecx
lea    0xdbc(%rip),%rsi
call   1040 <lksmith_error@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12cc <pthread_mutex_real_init+0x15c>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pthread_mutex_real_init>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
movl   $0x0,0x4(%rsp)
movl   $0x0,(%rdx)
test   %rsi,%rsi
je     11be <pthread_mutex_real_init+0x4e>
mov    %rdx,%r12
mov    %rsi,%r14
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
call   1030 <pthread_mutexattr_gettype@plt>
mov    %eax,%ebx
mov    0x2e36(%rip),%rax
cmp    (%rax),%ebx
jne    11d1 <pthread_mutex_real_init+0x61>
mov    %ebx,%edi
call   1090 <terror@plt>
lea    0xe44(%rip),%rsi
jmp    122d <pthread_mutex_real_init+0xbd>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1060 <pthread_mutex_init_errcheck@plt>
mov    0x4(%rsp),%edi
call   1070 <is_compatible_with_errcheck@plt>
test   %rax,%rax
je     1206 <pthread_mutex_real_init+0x96>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1050 <pthread_mutexattr_settype@plt>
test   %eax,%eax
je     120e <pthread_mutex_real_init+0x9e>
mov    %eax,%ebx
mov    %eax,%edi
call   1090 <terror@plt>
lea    0xe35(%rip),%rsi
jmp    122d <pthread_mutex_real_init+0xbd>
movl   $0x1,(%r12)
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <r_pthread_mutex_init@plt>
test   %eax,%eax
je     1246 <pthread_mutex_real_init+0xd6>
mov    %eax,%ebx
mov    %eax,%edi
call   1090 <terror@plt>
lea    0xe45(%rip),%rsi
mov    %ebx,%edi
mov    %ebx,%edx
mov    %eax,%ecx
call   1040 <lksmith_error@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %ebx,%ebx
jmp    1238 <pthread_mutex_real_init+0xc8>

LLVM IR (-O0):
@EINVAL = global i32 0
@.str = [57 x i8] c"pthread_mutexattr_gettype failed with error code %d: %s\0A\00"
@PTHREAD_MUTEX_ERRORCHECK = global i32 0
@.str.1 = [57 x i8] c"pthread_mutexattr_settype failed with error code %d: %s\0A\00"
@.str.2 = [50 x i8] c"pthread_mutex_init failed with error code %d: %s\0A\00"
define i32 @pthread_mutex_real_init(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32 0, i32* %9
%10 = load i32*, i32** %7
store i32 0, i32* %10
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = load i32*, i32** %5
%15 = call i32 @pthread_mutex_init_errcheck(i32* %14)
store i32 %15, i32* %4
br label %64
16: ; preds = %3
%17 = load i32*, i32** %6
%18 = call i32 @pthread_mutexattr_gettype(i32* %17, i32* %9)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = load i32, i32* @EINVAL
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %29
22: ; preds = %16
%23 = load i32, i32* %8
%24 = load i32, i32* %8
%25 = load i32, i32* %8
%26 = call i32 @terror(i32 %25)
%27 = call i32 @lksmith_error(i32 %23, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %26)
%28 = load i32, i32* %8
store i32 %28, i32* %4
br label %64
29: ; preds = %16
%30 = load i32, i32* %9
%31 = call i64 @is_compatible_with_errcheck(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %29
%34 = load i32*, i32** %6
%35 = load i32, i32* @PTHREAD_MUTEX_ERRORCHECK
%36 = call i32 @pthread_mutexattr_settype(i32* %34, i32 %35)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %33
%40 = load i32, i32* %8
%41 = load i32, i32* %8
%42 = load i32, i32* %8
%43 = call i32 @terror(i32 %42)
%44 = call i32 @lksmith_error(i32 %40, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i32 %41, i32 %43)
%45 = load i32, i32* %8
store i32 %45, i32* %4
br label %64
46: ; preds = %33
br label %49
47: ; preds = %29
%48 = load i32*, i32** %7
store i32 1, i32* %48
br label %49
49: ; preds = %47, %46
%50 = load i32*, i32** %5
%51 = load i32*, i32** %6
%52 = call i32 @r_pthread_mutex_init(i32* %50, i32* %51)
store i32 %52, i32* %8
%53 = load i32, i32* %8
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %62
55: ; preds = %49
%56 = load i32, i32* %8
%57 = load i32, i32* %8
%58 = load i32, i32* %8
%59 = call i32 @terror(i32 %58)
%60 = call i32 @lksmith_error(i32 %56, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 %57, i32 %59)
%61 = load i32, i32* %8
store i32 %61, i32* %4
br label %64
62: ; preds = %49
%63 = load i32, i32* %8
store i32 %63, i32* %4
br label %64
64: ; preds = %62, %55, %39, %22, %13
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
@EINVAL = global i32 0
@.str = [57 x i8] c"pthread_mutexattr_gettype failed with error code %d: %s\0A\00"
@PTHREAD_MUTEX_ERRORCHECK = global i32 0
@.str.1 = [57 x i8] c"pthread_mutexattr_settype failed with error code %d: %s\0A\00"
@.str.2 = [50 x i8] c"pthread_mutex_init failed with error code %d: %s\0A\00"
define i32 @pthread_mutex_real_init(i32* %0, i32* %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
store i32 0, i32* %4
store i32 0, i32* %2
%6 = icmp eq i32* %1, null
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @pthread_mutex_init_errcheck(i32* %0)
br label %34
9: ; preds = %3
%10 = call i32 @pthread_mutexattr_gettype(i32* %1, i32* %4)
%11 = load i32, i32* @EINVAL
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = call i32 @terror(i32 %10)
%15 = call i32 @lksmith_error(i32 %10, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %14)
br label %34
16: ; preds = %9
%17 = load i32, i32* %4
%18 = call i64 @is_compatible_with_errcheck(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %16
%21 = load i32, i32* @PTHREAD_MUTEX_ERRORCHECK
%22 = call i32 @pthread_mutexattr_settype(i32* %1, i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = call i32 @terror(i32 %22)
%26 = call i32 @lksmith_error(i32 %22, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %25)
br label %34
27: ; preds = %16
store i32 1, i32* %2
br label %28
28: ; preds = %20, %27
%29 = call i32 @r_pthread_mutex_init(i32* %0, i32* %1)
%30 = icmp eq i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = call i32 @terror(i32 %29)
%33 = call i32 @lksmith_error(i32 %29, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 %29, i32 %32)
br label %34
34: ; preds = %28, %31, %24, %13, %7
%35 = phi i32 [ %10, %13 ], [ %22, %24 ], [ %29, %31 ], [ %8, %7 ], [ 0, %28 ]
ret i32 %35
}