#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 ENOTSUP;
int /*<<< orphan*/ alarm(int);
int pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_lock(int /*<<< orphan*/ *);
scalar_t__ pthread_mutexattr_destroy(int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
int do_test(void) {
  pthread_mutex_t m;
  int e = pthread_mutex_init(&m, NULL);
  if (NULL != NULL && e == ENOTSUP) {
    puts("cannot support selected type of mutexes");
    e = pthread_mutex_init(&m, NULL);
  }
  if (e != 0) {
    puts("mutex_init failed");
    return 1;
  }
  if (NULL != NULL && pthread_mutexattr_destroy(NULL) != 0) {
    puts("mutexattr_destroy failed");
    return 1;
  }
  if (pthread_mutex_lock(&m) != 0) {
    puts("1st mutex_lock failed");
    return 1;
  }
  alarm(1);
  pthread_mutex_lock(&m);
  puts("2nd mutex_lock returned");
  return 1;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <pthread_mutex_init@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     117a <do_test+0x3a>
lea    0xe97(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    11cb <do_test+0x8b>
lea    -0x8(%rbp),%rdi
call   1060 <pthread_mutex_lock@plt>
cmp    $0x0,%rax
je     11a5 <do_test+0x65>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    11cb <do_test+0x8b>
mov    $0x1,%edi
call   1040 <alarm@plt>
lea    -0x8(%rbp),%rdi
call   1060 <pthread_mutex_lock@plt>
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rax
lea    0x4(%rsp),%rdi
xor    %esi,%esi
call   1050 <pthread_mutex_init@plt>
test   %eax,%eax
je     115a <do_test+0x1a>
lea    0xea8(%rip),%rdi
jmp    118d <do_test+0x4d>
lea    0x4(%rsp),%rdi
call   1060 <pthread_mutex_lock@plt>
test   %rax,%rax
je     1172 <do_test+0x32>
lea    0xea2(%rip),%rdi
jmp    118d <do_test+0x4d>
mov    $0x1,%edi
call   1040 <alarm@plt>
lea    0x4(%rsp),%rdi
call   1060 <pthread_mutex_lock@plt>
lea    0xe9b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [18 x i8] c"mutex_init failed\00"
@.str.1 = [22 x i8] c"1st mutex_lock failed\00"
@.str.2 = [24 x i8] c"2nd mutex_lock returned\00"
@ENOTSUP = global i32 0
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 @pthread_mutex_init(i32* %2, i32* null)
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = icmp ne i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %18
9: ; preds = %0
%10 = call i64 @pthread_mutex_lock(i32* %2)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %18
14: ; preds = %9
%15 = call i32 @alarm(i32 1)
%16 = call i64 @pthread_mutex_lock(i32* %2)
%17 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %18
18: ; preds = %14, %12, %7
%19 = load i32, i32* %1
ret i32 %19
}

LLVM IR (-O2):
@.str = [18 x i8] c"mutex_init failed\00"
@.str.1 = [22 x i8] c"1st mutex_lock failed\00"
@.str.2 = [24 x i8] c"2nd mutex_lock returned\00"
@ENOTSUP = global i32 0
define i32 @do_test() {
%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 %5, label %11
5: ; preds = %0
%6 = call i64 @pthread_mutex_lock(i32* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = call i32 @alarm(i32 1)
%10 = call i64 @pthread_mutex_lock(i32* %1)
br label %11
11: ; preds = %5, %0, %8
%12 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), %8 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), %5 ]
%13 = call i32 @puts(i8* dereferenceable(1) %12)
ret i32 1
}