#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 *);
scalar_t__ pthread_mutex_trylock(int /*<<< orphan*/ *const);
scalar_t__ pthread_mutex_unlock(int /*<<< orphan*/ *const);
int /*<<< orphan*/ stderr;
void lock_twice(pthread_mutex_t *const p) {
  if (pthread_mutex_trylock(p) != 0)
    fprintf(stderr, "first lock call failed !\n");
  if (pthread_mutex_trylock(p) != 0)
    fprintf(stderr, "second lock call failed !\n");
  if (pthread_mutex_unlock(p) != 0)
    fprintf(stderr, "first unlock call failed !\n");
  if (pthread_mutex_unlock(p) != 0)
    fprintf(stderr, "second unlock call failed !\n");
}


x86 assembly (clang, -O0):
<lock_twice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <pthread_mutex_trylock@plt>
cmp    $0x0,%rax
je     1164 <lock_twice+0x34>
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
lea    0xea1(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <pthread_mutex_trylock@plt>
cmp    $0x0,%rax
je     118c <lock_twice+0x5c>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe93(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1050 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
je     11b4 <lock_twice+0x84>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1050 <pthread_mutex_unlock@plt>
cmp    $0x0,%rax
je     11dc <lock_twice+0xac>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe7a(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_twice>:
push   %rbx
mov    %rdi,%rbx
call   1030 <pthread_mutex_trylock@plt>
test   %rax,%rax
je     1153 <lock_twice+0x23>
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
lea    0xeb2(%rip),%rsi
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <pthread_mutex_trylock@plt>
test   %rax,%rax
je     1175 <lock_twice+0x45>
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
lea    0xeaa(%rip),%rsi
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <pthread_mutex_unlock@plt>
test   %rax,%rax
je     1197 <lock_twice+0x67>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xea3(%rip),%rsi
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <pthread_mutex_unlock@plt>
test   %rax,%rax
je     11ba <lock_twice+0x8a>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe9d(%rip),%rsi
pop    %rbx
jmp    1040 <fprintf@plt>
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"first lock call failed !\0A\00"
@.str.1 = [27 x i8] c"second lock call failed !\0A\00"
@.str.2 = [28 x i8] c"first unlock call failed !\0A\00"
@.str.3 = [29 x i8] c"second unlock call failed !\0A\00"
define void @lock_twice(i32* %0) {
%2 = alloca i32*
store i32* %0, i32** %2
%3 = load i32*, i32** %2
%4 = call i64 @pthread_mutex_trylock(i32* %3)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %6, %1
%10 = load i32*, i32** %2
%11 = call i64 @pthread_mutex_trylock(i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %13, %9
%17 = load i32*, i32** %2
%18 = call i64 @pthread_mutex_unlock(i32* %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %20, %16
%24 = load i32*, i32** %2
%25 = call i64 @pthread_mutex_unlock(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %27, %23
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"first lock call failed !\0A\00"
@.str.1 = [27 x i8] c"second lock call failed !\0A\00"
@.str.2 = [28 x i8] c"first unlock call failed !\0A\00"
@.str.3 = [29 x i8] c"second unlock call failed !\0A\00"
define void @lock_twice(i32* %0) {
%2 = tail call i64 @pthread_mutex_trylock(i32* %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %4, %1
%8 = tail call i64 @pthread_mutex_trylock(i32* %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %10, %7
%14 = tail call i64 @pthread_mutex_unlock(i32* %0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %16, %13
%20 = tail call i64 @pthread_mutex_unlock(i32* %0)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %22, %19
ret void
}