#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_mutex_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *);
int pthread_mutex_destroy(int /*<<< orphan*/ *);
int pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int pthread_mutex_lock(int /*<<< orphan*/ *);
int pthread_mutex_unlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(void) {
  pthread_mutex_t mutex;
  int rc;
  if ((rc = pthread_mutex_init(&mutex, NULL)) != 0) {
    fprintf(stderr, "Fail to initialize mutex, rc=%d\n", rc);
    printf("Test FAILED\n");
    return 1;
  }
  if ((rc = pthread_mutex_lock(&mutex)) != 0) {
    fprintf(stderr, "Fail to lock the mutex, rc=%d\n", rc);
    printf("Test FAILED\n");
    return 1;
  }
  fprintf(stderr, "Main: hold the mutex for a while\n");
  sleep(1);
  if ((rc = pthread_mutex_unlock(&mutex)) != 0) {
    fprintf(stderr, "Fail to unlock the mutex, rc=%d\n", rc);
    return 2;
  }
  if ((rc = pthread_mutex_destroy(&mutex)) != 0) {
    fprintf(stderr, "Fail to destroy the mutex, rc=%d\n", rc);
    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    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <pthread_mutex_init@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     11ca <main+0x5a>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe55(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    0xe68(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bb <main+0x14b>
lea    -0x8(%rbp),%rdi
call   1090 <pthread_mutex_lock@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     1211 <main+0xa1>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    0xe21(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bb <main+0x14b>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    -0x8(%rbp),%rdi
call   1060 <pthread_mutex_unlock@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     126d <main+0xfd>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12bb <main+0x14b>
lea    -0x8(%rbp),%rdi
call   1040 <pthread_mutex_destroy@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     12a8 <main+0x138>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xdfb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12bb <main+0x14b>
lea    0xe03(%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    0xc(%rsp),%rdi
xor    %esi,%esi
call   1080 <pthread_mutex_init@plt>
test   %eax,%eax
je     1197 <main+0x27>
mov    0x2e54(%rip),%rcx
mov    (%rcx),%edi
lea    0xe6b(%rip),%rsi
jmp    11b5 <main+0x45>
lea    0xc(%rsp),%rdi
call   1090 <pthread_mutex_lock@plt>
mov    0x2e38(%rip),%rbx
mov    (%rbx),%edi
test   %eax,%eax
je     11d5 <main+0x65>
lea    0xe6c(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xeec(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe64(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0xc(%rsp),%rdi
call   1060 <pthread_mutex_unlock@plt>
test   %eax,%eax
je     1206 <main+0x96>
mov    (%rbx),%edi
lea    0xe5e(%rip),%rsi
jmp    121d <main+0xad>
lea    0xc(%rsp),%rdi
call   1040 <pthread_mutex_destroy@plt>
test   %eax,%eax
je     122d <main+0xbd>
mov    (%rbx),%edi
lea    0xe66(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%eax
jmp    11cf <main+0x5f>
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    11cf <main+0x5f>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"Fail to initialize mutex, rc=%d\0A\00"
@.str.1 = [13 x i8] c"Test FAILED\0A\00"
@.str.2 = [31 x i8] c"Fail to lock the mutex, rc=%d\0A\00"
@.str.3 = [34 x i8] c"Main: hold the mutex for a while\0A\00"
@.str.4 = [33 x i8] c"Fail to unlock the mutex, rc=%d\0A\00"
@.str.5 = [34 x i8] c"Fail to destroy the mutex, rc=%d\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 i32 @pthread_mutex_init(i32* %2, i32* null)
store i32 %4, i32* %3
%5 = icmp ne i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = load i32, i32* %3
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %38
11: ; preds = %0
%12 = call i32 @pthread_mutex_lock(i32* %2)
store i32 %12, i32* %3
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = load i32, i32* %3
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %38
19: ; preds = %11
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 @sleep(i32 1)
%23 = call i32 @pthread_mutex_unlock(i32* %2)
store i32 %23, i32* %3
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = load i32, i32* %3
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %27)
store i32 2, i32* %1
br label %38
29: ; preds = %19
%30 = call i32 @pthread_mutex_destroy(i32* %2)
store i32 %30, i32* %3
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = load i32, i32* %3
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %34)
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, %32, %25, %14, %6
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"Fail to initialize mutex, rc=%d\0A\00"
@.str.2 = [31 x i8] c"Fail to lock the mutex, rc=%d\0A\00"
@.str.3 = [34 x i8] c"Main: hold the mutex for a while\0A\00"
@.str.4 = [33 x i8] c"Fail to unlock the mutex, rc=%d\0A\00"
@.str.5 = [34 x i8] c"Fail to destroy the mutex, rc=%d\0A\00"
@str = [12 x i8] c"Test PASSED\00"
@str.8 = [12 x i8] c"Test FAILED\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i32 @pthread_mutex_init(i32* %1, i32* null)
%4 = icmp eq i32 %3, 0
br i1 %4, label %9, label %5
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %3)
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.8, i64 0, i64 0))
br label %32
9: ; preds = %0
%10 = call i32 @pthread_mutex_lock(i32* %1)
%11 = icmp eq i32 %10, 0
%12 = load i32, i32* @stderr
br i1 %11, label %16, label %13
13: ; preds = %9
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.8, i64 0, i64 0))
br label %32
16: ; preds = %9
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%18 = call i32 @sleep(i32 1)
%19 = call i32 @pthread_mutex_unlock(i32* %1)
%20 = icmp eq i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %19)
br label %32
24: ; preds = %16
%25 = call i32 @pthread_mutex_destroy(i32* %1)
%26 = icmp eq i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %25)
br label %32
30: ; preds = %24
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %32
32: ; preds = %30, %27, %21, %13, %5
%33 = phi i32 [ 1, %5 ], [ 1, %13 ], [ 2, %21 ], [ 2, %27 ], [ 0, %30 ]
ret i32 %33
}