#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_ERRORCHECK;
int /*<<< orphan*/ lksmith_error(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ pthread_mutexattr_destroy(int /*<<< orphan*/ *);
int pthread_mutexattr_init(int /*<<< orphan*/ *);
int pthread_mutexattr_settype(int /*<<< orphan*/ *, int /*<<< orphan*/);
int r_pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ terror(int);
int pthread_mutex_init_errcheck(pthread_mutex_t *mutex) {
  int ret;
  pthread_mutexattr_t attr;
  ret = pthread_mutexattr_init(&attr);
  if (ret) {
    lksmith_error(ret,
                  "pthread_mutexattr_init failed "
                  "with error code %d: %s\n",
                  ret, terror(ret));
    goto done;
  }
  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));
    goto done_free_mutexattr;
  }
  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));
    goto done_free_mutexattr;
  }
done_free_mutexattr:
  pthread_mutexattr_destroy(&attr);
done:
  return ret;
}


x86 assembly (clang, -O0):
<pthread_mutex_init_errcheck>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
call   1070 <pthread_mutexattr_init@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11af <pthread_mutex_init_errcheck+0x4f>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <terror@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%edx
mov    %eax,%ecx
lea    0xe5b(%rip),%rsi
call   1030 <lksmith_error@plt>
jmp    1250 <pthread_mutex_init_errcheck+0xf0>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   1040 <pthread_mutexattr_settype@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11fb <pthread_mutex_init_errcheck+0x9b>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <terror@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
lea    0xe45(%rip),%rsi
call   1030 <lksmith_error@plt>
jmp    1247 <pthread_mutex_init_errcheck+0xe7>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <r_pthread_mutex_init@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1242 <pthread_mutex_init_errcheck+0xe2>
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <terror@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%edx
mov    %eax,%ecx
lea    0xe37(%rip),%rsi
call   1030 <lksmith_error@plt>
jmp    1247 <pthread_mutex_init_errcheck+0xe7>
jmp    1247 <pthread_mutex_init_errcheck+0xe7>
lea    -0x10(%rbp),%rdi
call   1080 <pthread_mutexattr_destroy@plt>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pthread_mutex_init_errcheck>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0x4(%rsp),%rdi
call   1070 <pthread_mutexattr_init@plt>
test   %eax,%eax
je     1192 <pthread_mutex_init_errcheck+0x32>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <terror@plt>
lea    0xe7b(%rip),%rsi
mov    %ebx,%edi
mov    %ebx,%edx
mov    %eax,%ecx
call   1030 <lksmith_error@plt>
jmp    11f1 <pthread_mutex_init_errcheck+0x91>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdi
call   1040 <pthread_mutexattr_settype@plt>
test   %eax,%eax
je     11bb <pthread_mutex_init_errcheck+0x5b>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <terror@plt>
lea    0xe7d(%rip),%rsi
jmp    11dc <pthread_mutex_init_errcheck+0x7c>
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
call   1050 <r_pthread_mutex_init@plt>
test   %eax,%eax
je     11fb <pthread_mutex_init_errcheck+0x9b>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <terror@plt>
lea    0xe93(%rip),%rsi
mov    %ebx,%edi
mov    %ebx,%edx
mov    %eax,%ecx
call   1030 <lksmith_error@plt>
lea    0x4(%rsp),%rdi
call   1080 <pthread_mutexattr_destroy@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %ebx,%ebx
jmp    11e7 <pthread_mutex_init_errcheck+0x87>

LLVM IR (-O0):
@.str = [54 x i8] c"pthread_mutexattr_init 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_init_errcheck(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
store i32* %0, i32** %2
%5 = call i32 @pthread_mutexattr_init(i32* %4)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %1
%9 = load i32, i32* %3
%10 = load i32, i32* %3
%11 = load i32, i32* %3
%12 = call i32 @terror(i32 %11)
%13 = call i32 @lksmith_error(i32 %9, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %12)
br label %39
14: ; preds = %1
%15 = load i32, i32* @PTHREAD_MUTEX_ERRORCHECK
%16 = call i32 @pthread_mutexattr_settype(i32* %4, i32 %15)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i32, i32* %3
%21 = load i32, i32* %3
%22 = load i32, i32* %3
%23 = call i32 @terror(i32 %22)
%24 = call i32 @lksmith_error(i32 %20, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %23)
br label %37
25: ; preds = %14
%26 = load i32*, i32** %2
%27 = call i32 @r_pthread_mutex_init(i32* %26, i32* %4)
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %25
%31 = load i32, i32* %3
%32 = load i32, i32* %3
%33 = load i32, i32* %3
%34 = call i32 @terror(i32 %33)
%35 = call i32 @lksmith_error(i32 %31, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %34)
br label %37
36: ; preds = %25
br label %37
37: ; preds = %36, %30, %19
%38 = call i32 @pthread_mutexattr_destroy(i32* %4)
br label %39
39: ; preds = %37, %8
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [54 x i8] c"pthread_mutexattr_init 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_init_errcheck(i32* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @pthread_mutexattr_init(i32* %2)
%5 = icmp eq i32 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = call i32 @terror(i32 %4)
%8 = call i32 @lksmith_error(i32 %4, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %7)
br label %25
9: ; preds = %1
%10 = load i32, i32* @PTHREAD_MUTEX_ERRORCHECK
%11 = call i32 @pthread_mutexattr_settype(i32* %2, i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %9
%14 = call i32 @terror(i32 %11)
%15 = call i32 @lksmith_error(i32 %11, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %14)
br label %22
16: ; preds = %9
%17 = call i32 @r_pthread_mutex_init(i32* %0, i32* %2)
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = call i32 @terror(i32 %17)
%21 = call i32 @lksmith_error(i32 %17, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %20)
br label %22
22: ; preds = %16, %19, %13
%23 = phi i32 [ %11, %13 ], [ %17, %19 ], [ 0, %16 ]
%24 = call i32 @pthread_mutexattr_destroy(i32* %2)
br label %25
25: ; preds = %22, %6
%26 = phi i32 [ %4, %6 ], [ %23, %22 ]
ret i32 %26
}