#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);
  r = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  r = pthread_mutex_init(&mx1, &attr);
  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)
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdi
call   1030 <pthread_mutexattr_settype@plt>
mov    %eax,-0xc(%rbp)
lea    -0x4(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1080 <pthread_mutex_init@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%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    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%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    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%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    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%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    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%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    0x2d2e(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%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    0x2cfc(%rip),%rax
mov    (%rax),%edi
lea    0xd81(%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>
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1030 <pthread_mutexattr_settype@plt>
lea    0x4(%rsp),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <pthread_mutex_init@plt>
mov    0x2e31(%rip),%rbx
mov    (%rbx),%edi
lea    0xe50(%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    0xe3d(%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    0xe2a(%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    0xe17(%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    0xe06(%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    0xdf5(%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    0xde4(%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* @PTHREAD_MUTEX_RECURSIVE
%6 = call i32 @pthread_mutexattr_settype(i32* %2, i32 %5)
store i32 %6, i32* %3
%7 = call i32 @pthread_mutex_init(i32* %1, i32* %2)
store i32 %7, i32* %3
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @pthread_mutex_lock(i32* %1)
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 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @pthread_mutex_lock(i32* %1)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, 0
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @pthread_mutex_lock(i32* %1)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, 0
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @pthread_mutex_unlock(i32* %1)
store i32 %31, i32* %3
%32 = load i32, i32* %3
%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.4, i64 0, i64 0))
%38 = call i32 @pthread_mutex_unlock(i32* %1)
store i32 %38, i32* %3
%39 = load i32, i32* %3
%40 = icmp eq i32 %39, 0
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%45 = call i32 @pthread_mutex_unlock(i32* %1)
store i32 %45, i32* %3
%46 = load i32, i32* %3
%47 = icmp eq i32 %46, 0
%48 = zext i1 %47 to i32
%49 = call i32 @assert(i32 %48)
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%52 = call i32 @pthread_mutex_unlock(i32* %1)
store i32 %52, 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 = load i32, i32* @PTHREAD_MUTEX_RECURSIVE
%7 = call i32 @pthread_mutexattr_settype(i32* %2, i32 %6)
%8 = call i32 @pthread_mutex_init(i32* %1, i32* %2)
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @pthread_mutex_lock(i32* %1)
%12 = icmp eq i32 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @pthread_mutex_lock(i32* %1)
%18 = icmp eq i32 %17, 0
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @pthread_mutex_lock(i32* %1)
%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 ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @pthread_mutex_unlock(i32* %1)
%30 = icmp eq i32 %29, 0
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%35 = call i32 @pthread_mutex_unlock(i32* %1)
%36 = icmp eq i32 %35, 0
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 @pthread_mutex_unlock(i32* %1)
%42 = icmp eq i32 %41, 0
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
%47 = call i32 @pthread_mutex_unlock(i32* %1)
ret void
}