#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*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ pthread_mutex_destroy(int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(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 main(void) {
  pthread_mutex_t mutex;
  pthread_mutexattr_t mta;
  if (pthread_mutexattr_init(&mta) != 0) {
    perror("Error at pthread_mutexattr_init()\n");
    return 2;
  }
  if (pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK) != 0) {
    printf("Test FAILED: Error setting the attribute 'type'\n");
    return 1;
  }
  if (pthread_mutex_init(&mutex, &mta) != 0) {
    perror("Error initializing the mutex.\n");
    return 2;
  }
  if (pthread_mutex_lock(&mutex) != 0) {
    perror("Error locking the mutex first time around.\n");
    return 2;
  }
  if (pthread_mutex_lock(&mutex) == 0) {
    perror("Test FAILED: Did not return error when locking an already locked "
           "mutex.\n");
    return 1;
  }
  pthread_mutex_unlock(&mutex);
  pthread_mutex_destroy(&mutex);
  if (pthread_mutexattr_destroy(&mta) != 0) {
    perror("Error at pthread_mutex_destroy()\n");
    return 2;
  }
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1080 <pthread_mutexattr_init@plt>
cmp    $0x0,%rax
je     11ca <main+0x3a>
lea    0xe47(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12d3 <main+0x143>
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdi
call   1050 <pthread_mutexattr_settype@plt>
cmp    $0x0,%rax
je     11fe <main+0x6e>
lea    0xe36(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d3 <main+0x143>
lea    -0x8(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1090 <pthread_mutex_init@plt>
cmp    $0x0,%rax
je     122d <main+0x9d>
lea    0xe38(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12d3 <main+0x143>
lea    -0x8(%rbp),%rdi
call   10b0 <pthread_mutex_lock@plt>
cmp    $0x0,%rax
je     1258 <main+0xc8>
lea    0xe2c(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12d3 <main+0x143>
lea    -0x8(%rbp),%rdi
call   10b0 <pthread_mutex_lock@plt>
cmp    $0x0,%rax
jne    1283 <main+0xf3>
lea    0xe2d(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d3 <main+0x143>
lea    -0x8(%rbp),%rdi
call   1060 <pthread_mutex_unlock@plt>
lea    -0x8(%rbp),%rdi
call   1040 <pthread_mutex_destroy@plt>
lea    -0xc(%rbp),%rdi
call   10a0 <pthread_mutexattr_destroy@plt>
cmp    $0x0,%rax
je     12c0 <main+0x130>
lea    0xe39(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12d3 <main+0x143>
lea    0xe43(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
call   1080 <pthread_mutexattr_init@plt>
test   %rax,%rax
jne    123c <main+0xac>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
call   1050 <pthread_mutexattr_settype@plt>
test   %rax,%rax
je     11d6 <main+0x46>
lea    0xf1e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    127b <main+0xeb>
lea    0xc(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1090 <pthread_mutex_init@plt>
test   %rax,%rax
jne    1245 <main+0xb5>
lea    0xc(%rsp),%rdi
call   10b0 <pthread_mutex_lock@plt>
test   %rax,%rax
jne    124e <main+0xbe>
lea    0xc(%rsp),%rdi
call   10b0 <pthread_mutex_lock@plt>
test   %rax,%rax
je     1257 <main+0xc7>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <pthread_mutex_unlock@plt>
mov    %rbx,%rdi
call   1040 <pthread_mutex_destroy@plt>
lea    0x8(%rsp),%rdi
call   10a0 <pthread_mutexattr_destroy@plt>
test   %rax,%rax
jne    126a <main+0xda>
lea    0xea6(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    127b <main+0xeb>
lea    0xdbd(%rip),%rdi
jmp    1271 <main+0xe1>
lea    0xdd7(%rip),%rdi
jmp    1271 <main+0xe1>
lea    0xded(%rip),%rdi
jmp    1271 <main+0xe1>
lea    0xe10(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    127b <main+0xeb>
lea    0xe46(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [35 x i8] c"Error at pthread_mutexattr_init()\0A\00"
@PTHREAD_MUTEX_ERRORCHECK = global i32 0
@.str.1 = [49 x i8] c"Test FAILED: Error setting the attribute 'type'\0A\00"
@.str.2 = [31 x i8] c"Error initializing the mutex.\0A\00"
@.str.3 = [44 x i8] c"Error locking the mutex first time around.\0A\00"
@.str.4 = [73 x i8] c"Test FAILED: Did not return error when locking an already locked mutex.\0A\00"
@.str.5 = [34 x i8] c"Error at pthread_mutex_destroy()\0A\00"
@.str.6 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i64 @pthread_mutexattr_init(i32* %3)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %38
8: ; preds = %0
%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 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %38
14: ; preds = %8
%15 = call i64 @pthread_mutex_init(i32* %2, i32* %3)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %38
19: ; preds = %14
%20 = call i64 @pthread_mutex_lock(i32* %2)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %38
24: ; preds = %19
%25 = call i64 @pthread_mutex_lock(i32* %2)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %38
29: ; preds = %24
%30 = call i32 @pthread_mutex_unlock(i32* %2)
%31 = call i32 @pthread_mutex_destroy(i32* %2)
%32 = call i64 @pthread_mutexattr_destroy(i32* %3)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
store i32 2, i32* %1
br label %38
36: ; preds = %29
%37 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %1
br label %38
38: ; preds = %36, %34, %27, %22, %17, %12, %6
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@.str = [35 x i8] c"Error at pthread_mutexattr_init()\0A\00"
@PTHREAD_MUTEX_ERRORCHECK = global i32 0
@.str.2 = [31 x i8] c"Error initializing the mutex.\0A\00"
@.str.3 = [44 x i8] c"Error locking the mutex first time around.\0A\00"
@.str.4 = [73 x i8] c"Test FAILED: Did not return error when locking an already locked mutex.\0A\00"
@.str.5 = [34 x i8] c"Error at pthread_mutex_destroy()\0A\00"
@str = [12 x i8] c"Test PASSED\00"
@str.7 = [48 x i8] c"Test FAILED: Error setting the attribute 'type'\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = call i64 @pthread_mutexattr_init(i32* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %39
9: ; preds = %0
%10 = load i32, i32* @PTHREAD_MUTEX_ERRORCHECK
%11 = call i64 @pthread_mutexattr_settype(i32* %2, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.7, i64 0, i64 0))
br label %39
15: ; preds = %9
%16 = call i64 @pthread_mutex_init(i32* %1, i32* %2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %39
20: ; preds = %15
%21 = call i64 @pthread_mutex_lock(i32* %1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
br label %39
25: ; preds = %20
%26 = call i64 @pthread_mutex_lock(i32* %1)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.4, i64 0, i64 0))
br label %39
30: ; preds = %25
%31 = call i32 @pthread_mutex_unlock(i32* %1)
%32 = call i32 @pthread_mutex_destroy(i32* %1)
%33 = call i64 @pthread_mutexattr_destroy(i32* %2)
%34 = icmp eq i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
br label %39
37: ; preds = %30
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %39
39: ; preds = %37, %35, %28, %23, %18, %13, %7
%40 = phi i32 [ 2, %7 ], [ 1, %13 ], [ 2, %18 ], [ 2, %23 ], [ 1, %28 ], [ 2, %35 ], [ 0, %37 ]
ret i32 %40
}