#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*/ handle_error(char *);
scalar_t__ pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ pthread_mutexattr_destroy(int /*<<< orphan*/ *);
scalar_t__ pthread_mutexattr_init(int /*<<< orphan*/ *);
scalar_t__ pthread_mutexattr_settype(int /*<<< orphan*/ *, int /*<<< orphan*/);
int new_mutex(pthread_mutex_t *mtx) {
  pthread_mutexattr_t attr;
  if (pthread_mutexattr_init(&attr) != 0) {
    handle_error("Failed to create mutex attribute");
  }
  if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0) {
    handle_error("Failed to set mutex type");
  }
  if (pthread_mutex_init(mtx, &attr) != 0) {
    handle_error("Failed to initialise mutex.");
  }
  if (pthread_mutexattr_destroy(&attr) != 0) {
    handle_error("Failed to delete mutex attribute.");
  }
  return 0;
}


x86 assembly (clang, -O0):
<new_mutex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <pthread_mutexattr_init@plt>
cmp    $0x0,%rax
je     117b <new_mutex+0x2b>
lea    0xe8a(%rip),%rdi
call   1070 <handle_error@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdi
call   1030 <pthread_mutexattr_settype@plt>
cmp    $0x0,%rax
je     11a3 <new_mutex+0x53>
lea    0xe83(%rip),%rdi
call   1070 <handle_error@plt>
mov    -0x8(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1050 <pthread_mutex_init@plt>
cmp    $0x0,%rax
je     11c6 <new_mutex+0x76>
lea    0xe79(%rip),%rdi
call   1070 <handle_error@plt>
lea    -0xc(%rbp),%rdi
call   1060 <pthread_mutexattr_destroy@plt>
cmp    $0x0,%rax
je     11e5 <new_mutex+0x95>
lea    0xe76(%rip),%rdi
call   1070 <handle_error@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_mutex>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xc(%rsp),%rdi
call   1040 <pthread_mutexattr_init@plt>
test   %rax,%rax
je     1173 <new_mutex+0x23>
lea    0xe92(%rip),%rdi
call   1070 <handle_error@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdi
call   1030 <pthread_mutexattr_settype@plt>
test   %rax,%rax
je     1197 <new_mutex+0x47>
lea    0xe8f(%rip),%rdi
call   1070 <handle_error@plt>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <pthread_mutex_init@plt>
test   %rax,%rax
je     11b5 <new_mutex+0x65>
lea    0xe8a(%rip),%rdi
call   1070 <handle_error@plt>
lea    0xc(%rsp),%rdi
call   1060 <pthread_mutexattr_destroy@plt>
test   %rax,%rax
je     11d0 <new_mutex+0x80>
lea    0xe8b(%rip),%rdi
call   1070 <handle_error@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [33 x i8] c"Failed to create mutex attribute\00"
@PTHREAD_MUTEX_ERRORCHECK = global i32 0
@.str.1 = [25 x i8] c"Failed to set mutex type\00"
@.str.2 = [28 x i8] c"Failed to initialise mutex.\00"
@.str.3 = [34 x i8] c"Failed to delete mutex attribute.\00"
define i32 @new_mutex(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
store i32* %0, i32** %2
%4 = call i64 @pthread_mutexattr_init(i32* %3)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = call i32 @handle_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = load i32, i32* @PTHREAD_MUTEX_ERRORCHECK
%10 = call i64 @pthread_mutexattr_settype(i32* %3, i32 %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @handle_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = load i32*, i32** %2
%16 = call i64 @pthread_mutex_init(i32* %15, i32* %3)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @handle_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = call i64 @pthread_mutexattr_destroy(i32* %3)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @handle_error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
ret i32 0
}

LLVM IR (-O2):
@.str = [33 x i8] c"Failed to create mutex attribute\00"
@PTHREAD_MUTEX_ERRORCHECK = global i32 0
@.str.1 = [25 x i8] c"Failed to set mutex type\00"
@.str.2 = [28 x i8] c"Failed to initialise mutex.\00"
@.str.3 = [34 x i8] c"Failed to delete mutex attribute.\00"
define i32 @new_mutex(i32* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i64 @pthread_mutexattr_init(i32* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = call i32 @handle_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = load i32, i32* @PTHREAD_MUTEX_ERRORCHECK
%10 = call i64 @pthread_mutexattr_settype(i32* %2, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = call i32 @handle_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = call i64 @pthread_mutex_init(i32* %0, i32* %2)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = call i32 @handle_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = call i64 @pthread_mutexattr_destroy(i32* %2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = call i32 @handle_error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
ret i32 0
}