#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_RECURSIVE;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int pthread_mutex_lock(int /*<<< orphan*/ *);
int pthread_mutex_unlock(int /*<<< orphan*/ *);
int pthread_mutexattr_init(int /*<<< orphan*/ *);
int pthread_mutexattr_settype(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void nearly_main(void) {
  pthread_mutex_t mx1;
  pthread_mutexattr_t attr;
  int r;
  r = pthread_mutexattr_init(&attr);
  assert(r == 0);
  r = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  assert(r == 0);
  r = pthread_mutex_init(&mx1, &attr);
  assert(r == 0);
  fprintf(stderr, "before lock #1\n");
  r = pthread_mutex_lock(&mx1);
  assert(r == 0);
  fprintf(stderr, "before lock #2\n");
  r = pthread_mutex_lock(&mx1);
  assert(r == 0);
  fprintf(stderr, "before lock #3\n");
  r = pthread_mutex_lock(&mx1);
  assert(r == 0);
  fprintf(stderr, "before unlock #1\n");
  r = pthread_mutex_unlock(&mx1);
  assert(r == 0);
  fprintf(stderr, "before unlock #2\n");
  r = pthread_mutex_unlock(&mx1);
  assert(r == 0);
  fprintf(stderr, "before unlock #3\n");
  r = pthread_mutex_unlock(&mx1);
  assert(r == 0);
  fprintf(stderr, "before unlock #4\n");
  r = pthread_mutex_unlock(&mx1);
}


x86 assembly (clang, -O0):
<nearly_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    -0x8(%rbp),%rdi
call   1070 <pthread_mutexattr_init@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdi
call   1030 <pthread_mutexattr_settype@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
lea    -0x4(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1080 <pthread_mutex_init@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
call   1050 <fprintf@plt>
lea    -0x4(%rbp),%rdi
call   1090 <pthread_mutex_lock@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
call   1050 <fprintf@plt>
lea    -0x4(%rbp),%rdi
call   1090 <pthread_mutex_lock@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
call   1050 <fprintf@plt>
lea    -0x4(%rbp),%rdi
call   1090 <pthread_mutex_lock@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
call   1050 <fprintf@plt>
lea    -0x4(%rbp),%rdi
call   1060 <pthread_mutex_unlock@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
call   1050 <fprintf@plt>
lea    -0x4(%rbp),%rdi
call   1060 <pthread_mutex_unlock@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xd6e(%rip),%rsi
call   1050 <fprintf@plt>
lea    -0x4(%rbp),%rdi
call   1060 <pthread_mutex_unlock@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
lea    0xd4e(%rip),%rsi
call   1050 <fprintf@plt>
lea    -0x4(%rbp),%rdi
call   1060 <pthread_mutex_unlock@plt>
mov    %eax,-0xc(%rbp)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nearly_main>:
push   %r14
push   %rbx
push   %rax
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <pthread_mutexattr_init@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1030 <pthread_mutexattr_settype@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
lea    0x4(%rsp),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <pthread_mutex_init@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
mov    0x2e0a(%rip),%rbx
mov    (%rbx),%edi
lea    0xe29(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1090 <pthread_mutex_lock@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
mov    (%rbx),%edi
lea    0xe16(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1090 <pthread_mutex_lock@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
mov    (%rbx),%edi
lea    0xe03(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1090 <pthread_mutex_lock@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
mov    (%rbx),%edi
lea    0xdf0(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1060 <pthread_mutex_unlock@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
mov    (%rbx),%edi
lea    0xddf(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1060 <pthread_mutex_unlock@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
mov    (%rbx),%edi
lea    0xdce(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1060 <pthread_mutex_unlock@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
mov    (%rbx),%edi
lea    0xdbd(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1060 <pthread_mutex_unlock@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@PTHREAD_MUTEX_RECURSIVE = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"before lock\0A\00"
@.str.1 = [16 x i8] c"before lock\0A\00"
@.str.2 = [16 x i8] c"before lock\0A\00"
@.str.3 = [18 x i8] c"before unlock\0A\00"
@.str.4 = [18 x i8] c"before unlock\0A\00"
@.str.5 = [18 x i8] c"before unlock\0A\00"
@.str.6 = [18 x i8] c"before unlock\0A\00"
define void @nearly_main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 @pthread_mutexattr_init(i32* %2)
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = icmp eq i32 %5, 0
%7 = zext i1 %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = load i32, i32* @PTHREAD_MUTEX_RECURSIVE
%10 = call i32 @pthread_mutexattr_settype(i32* %2, i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = call i32 @pthread_mutex_init(i32* %1, i32* %2)
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, 0
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @pthread_mutex_lock(i32* %1)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp eq i32 %23, 0
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @pthread_mutex_lock(i32* %1)
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = icmp eq i32 %30, 0
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @pthread_mutex_lock(i32* %1)
store i32 %36, i32* %3
%37 = load i32, i32* %3
%38 = icmp eq i32 %37, 0
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @pthread_mutex_unlock(i32* %1)
store i32 %43, i32* %3
%44 = load i32, i32* %3
%45 = icmp eq i32 %44, 0
%46 = zext i1 %45 to i32
%47 = call i32 @assert(i32 %46)
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 @pthread_mutex_unlock(i32* %1)
store i32 %50, i32* %3
%51 = load i32, i32* %3
%52 = icmp eq i32 %51, 0
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%57 = call i32 @pthread_mutex_unlock(i32* %1)
store i32 %57, i32* %3
%58 = load i32, i32* %3
%59 = icmp eq i32 %58, 0
%60 = zext i1 %59 to i32
%61 = call i32 @assert(i32 %60)
%62 = load i32, i32* @stderr
%63 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%64 = call i32 @pthread_mutex_unlock(i32* %1)
store i32 %64, i32* %3
ret void
}

LLVM IR (-O2):
@PTHREAD_MUTEX_RECURSIVE = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"before lock\0A\00"
@.str.1 = [16 x i8] c"before lock\0A\00"
@.str.2 = [16 x i8] c"before lock\0A\00"
@.str.3 = [18 x i8] c"before unlock\0A\00"
@.str.4 = [18 x i8] c"before unlock\0A\00"
@.str.5 = [18 x i8] c"before unlock\0A\00"
@.str.6 = [18 x i8] c"before unlock\0A\00"
define void @nearly_main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = call i32 @pthread_mutexattr_init(i32* %2)
%6 = icmp eq i32 %5, 0
%7 = zext i1 %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = load i32, i32* @PTHREAD_MUTEX_RECURSIVE
%10 = call i32 @pthread_mutexattr_settype(i32* %2, i32 %9)
%11 = icmp eq i32 %10, 0
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = call i32 @pthread_mutex_init(i32* %1, i32* %2)
%15 = icmp eq i32 %14, 0
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @pthread_mutex_lock(i32* %1)
%21 = icmp eq i32 %20, 0
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @pthread_mutex_lock(i32* %1)
%27 = icmp eq i32 %26, 0
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @pthread_mutex_lock(i32* %1)
%33 = icmp eq i32 %32, 0
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @pthread_mutex_unlock(i32* %1)
%39 = icmp eq i32 %38, 0
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @pthread_mutex_unlock(i32* %1)
%45 = icmp eq i32 %44, 0
%46 = zext i1 %45 to i32
%47 = call i32 @assert(i32 %46)
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%50 = call i32 @pthread_mutex_unlock(i32* %1)
%51 = icmp eq i32 %50, 0
%52 = zext i1 %51 to i32
%53 = call i32 @assert(i32 %52)
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%56 = call i32 @pthread_mutex_unlock(i32* %1)
ret void
}