#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 *);
scalar_t__ pthread_mutex_destroy(int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_lock(int /*<<< orphan*/ *);
scalar_t__ 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 at pthread_mutex_lock().\n");
    return 2;
  }
  if (pthread_mutex_unlock(&mutex) != 0) {
    perror("Error at pthread_mutex_unlock().\n");
    return 2;
  }
  if (pthread_mutex_unlock(&mutex) == 0) {
    perror("Test FAILED: Did not return error when unlocking an already "
           "unlocked mutex.\n");
    return 1;
  }
  if (pthread_mutex_destroy(&mutex)) {
    perror("Error at pthread_mutex_destroy().\n");
    return 2;
  }
  if (pthread_mutexattr_destroy(&mta)) {
    perror("Error at pthread_mutexattr_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    1317 <main+0x187>
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    1317 <main+0x187>
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    1317 <main+0x187>
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    1317 <main+0x187>
lea    -0x8(%rbp),%rdi
call   1060 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
je     1283 <main+0xf3>
lea    0xe21(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1317 <main+0x187>
lea    -0x8(%rbp),%rdi
call   1060 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
jne    12ae <main+0x11e>
lea    0xe18(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1317 <main+0x187>
lea    -0x8(%rbp),%rdi
call   1040 <pthread_mutex_destroy@plt>
cmp    $0x0,%rax
je     12d9 <main+0x149>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1317 <main+0x187>
lea    -0xc(%rbp),%rdi
call   10a0 <pthread_mutexattr_destroy@plt>
cmp    $0x0,%rax
je     1304 <main+0x174>
lea    0xe32(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1317 <main+0x187>
lea    0xe41(%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   %rax
lea    0x4(%rsp),%rdi
call   1080 <pthread_mutexattr_init@plt>
test   %rax,%rax
jne    1234 <main+0xa4>
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdi
call   1050 <pthread_mutexattr_settype@plt>
test   %rax,%rax
je     11cf <main+0x3f>
lea    0xf64(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rcx
ret
mov    %rsp,%rdi
lea    0x4(%rsp),%rsi
call   1090 <pthread_mutex_init@plt>
test   %rax,%rax
jne    123d <main+0xad>
mov    %rsp,%rdi
call   10b0 <pthread_mutex_lock@plt>
test   %rax,%rax
jne    1246 <main+0xb6>
mov    %rsp,%rdi
call   1060 <pthread_mutex_unlock@plt>
test   %rax,%rax
jne    124f <main+0xbf>
mov    %rsp,%rdi
call   1060 <pthread_mutex_unlock@plt>
test   %rax,%rax
je     1258 <main+0xc8>
mov    %rsp,%rdi
call   1040 <pthread_mutex_destroy@plt>
test   %rax,%rax
jne    1269 <main+0xd9>
lea    0x4(%rsp),%rdi
call   10a0 <pthread_mutexattr_destroy@plt>
test   %rax,%rax
jne    1272 <main+0xe2>
lea    0xef0(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xdc5(%rip),%rdi
jmp    1279 <main+0xe9>
lea    0xddf(%rip),%rdi
jmp    1279 <main+0xe9>
lea    0xdf5(%rip),%rdi
jmp    1279 <main+0xe9>
lea    0xe0c(%rip),%rdi
jmp    1279 <main+0xe9>
lea    0xe25(%rip),%rdi
call   1070 <perror@plt>
jmp    11c8 <main+0x38>
lea    0xe61(%rip),%rdi
jmp    1279 <main+0xe9>
lea    0xe7b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%eax
pop    %rcx
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 = [32 x i8] c"Error at pthread_mutex_lock().\0A\00"
@.str.4 = [34 x i8] c"Error at pthread_mutex_unlock().\0A\00"
@.str.5 = [77 x i8] c"Test FAILED: Did not return error when unlocking an already unlocked mutex.\0A\00"
@.str.6 = [35 x i8] c"Error at pthread_mutex_destroy().\0A\00"
@.str.7 = [39 x i8] c"Error at pthread_mutexattr_destroy().\0A\00"
@.str.8 = [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 %46
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 %46
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 %46
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 ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %46
24: ; preds = %19
%25 = call i64 @pthread_mutex_unlock(i32* %2)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %1
br label %46
29: ; preds = %24
%30 = call i64 @pthread_mutex_unlock(i32* %2)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %46
34: ; preds = %29
%35 = call i64 @pthread_mutex_destroy(i32* %2)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
store i32 2, i32* %1
br label %46
39: ; preds = %34
%40 = call i64 @pthread_mutexattr_destroy(i32* %3)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
store i32 2, i32* %1
br label %46
44: ; preds = %39
%45 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %1
br label %46
46: ; preds = %44, %42, %37, %32, %27, %22, %17, %12, %6
%47 = load i32, i32* %1
ret i32 %47
}

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 = [32 x i8] c"Error at pthread_mutex_lock().\0A\00"
@.str.4 = [34 x i8] c"Error at pthread_mutex_unlock().\0A\00"
@.str.5 = [77 x i8] c"Test FAILED: Did not return error when unlocking an already unlocked mutex.\0A\00"
@.str.6 = [35 x i8] c"Error at pthread_mutex_destroy().\0A\00"
@.str.7 = [39 x i8] c"Error at pthread_mutexattr_destroy().\0A\00"
@str = [12 x i8] c"Test PASSED\00"
@str.9 = [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 %47
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.9, i64 0, i64 0))
br label %47
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 %47
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 ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %47
25: ; preds = %20
%26 = call i64 @pthread_mutex_unlock(i32* %1)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
br label %47
30: ; preds = %25
%31 = call i64 @pthread_mutex_unlock(i32* %1)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.5, i64 0, i64 0))
br label %47
35: ; preds = %30
%36 = call i64 @pthread_mutex_destroy(i32* %1)
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
br label %47
40: ; preds = %35
%41 = call i64 @pthread_mutexattr_destroy(i32* %2)
%42 = icmp eq i64 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %40
%44 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
br label %47
45: ; preds = %40
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %47
47: ; preds = %45, %43, %38, %33, %28, %23, %18, %13, %7
%48 = phi i32 [ 2, %7 ], [ 1, %13 ], [ 2, %18 ], [ 2, %23 ], [ 2, %28 ], [ 1, %33 ], [ 2, %38 ], [ 2, %43 ], [ 0, %45 ]
ret i32 %48
}