#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_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_unlock(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_unlock(&mutex) == 0) {
    perror("Test FAILED: Did not return error when unlocking an already "
           "unlocked mutex.\n");
    return 1;
  }
  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   1070 <pthread_mutexattr_init@plt>
cmp    $0x0,%rax
je     119a <main+0x3a>
lea    0xe77(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    123b <main+0xdb>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdi
call   1040 <pthread_mutexattr_settype@plt>
cmp    $0x0,%rax
je     11ce <main+0x6e>
lea    0xe66(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123b <main+0xdb>
lea    -0x8(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1080 <pthread_mutex_init@plt>
cmp    $0x0,%rax
je     11fd <main+0x9d>
lea    0xe68(%rip),%rdi
call   1060 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    123b <main+0xdb>
lea    -0x8(%rbp),%rdi
call   1050 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
jne    1228 <main+0xc8>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    123b <main+0xdb>
lea    0xe91(%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
mov    %rsp,%rdi
call   1070 <pthread_mutexattr_init@plt>
test   %rax,%rax
jne    11c8 <main+0x68>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1040 <pthread_mutexattr_settype@plt>
test   %rax,%rax
je     1197 <main+0x37>
lea    0xf10(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rcx
ret
lea    0x4(%rsp),%rdi
mov    %rsp,%rsi
call   1080 <pthread_mutex_init@plt>
test   %rax,%rax
jne    11d1 <main+0x71>
lea    0x4(%rsp),%rdi
call   1050 <pthread_mutex_unlock@plt>
test   %rax,%rax
je     11e4 <main+0x84>
lea    0xed0(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe31(%rip),%rdi
jmp    11d8 <main+0x78>
lea    0xe4b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x2,%eax
pop    %rcx
ret
lea    0xe57(%rip),%rdi
call   1060 <perror@plt>
jmp    1190 <main+0x30>

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 = [77 x i8] c"Test FAILED: Did not return error when unlocking an already unlocked mutex.\0A\00"
@.str.4 = [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 %26
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 %26
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 %26
19: ; preds = %14
%20 = call i64 @pthread_mutex_unlock(i32* %2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %26
24: ; preds = %19
%25 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %26
26: ; preds = %24, %22, %17, %12, %6
%27 = load i32, i32* %1
ret i32 %27
}

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 = [77 x i8] c"Test FAILED: Did not return error when unlocking an already unlocked mutex.\0A\00"
@str = [12 x i8] c"Test PASSED\00"
@str.5 = [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 %27
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.5, i64 0, i64 0))
br label %27
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 %27
20: ; preds = %15
%21 = call i64 @pthread_mutex_unlock(i32* %1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.3, i64 0, i64 0))
br label %27
25: ; preds = %20
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %27
27: ; preds = %25, %23, %18, %13, %7
%28 = phi i32 [ 2, %7 ], [ 1, %13 ], [ 2, %18 ], [ 1, %23 ], [ 0, %25 ]
ret i32 %28
}