#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*/ PTHREAD_MUTEX_INITIALIZER;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ printf(char *, int const);
int pthread_mutex_lock(int /*<<< orphan*/ *);
int pthread_mutex_unlock(int /*<<< orphan*/ *);
int main(void) {
  const int n = 100000;
  int i, r;
  pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
  printf("begin %d lock--unlocks\n", n);
  for (i = 0; i < n; i++) {
    r = pthread_mutex_lock(&mx);
    r |= pthread_mutex_unlock(&mx);
    assert(r == 0);
  }
  printf("done  %d lock--unlocks\n", n);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x186a0,-0x8(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    0xe97(%rip),%rdi
mov    $0x186a0,%esi
call   1030 <printf@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x186a0,-0xc(%rbp)
jge    11c1 <main+0x81>
lea    -0x14(%rbp),%rdi
call   1060 <pthread_mutex_lock@plt>
mov    %eax,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1050 <pthread_mutex_unlock@plt>
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    117a <main+0x3a>
lea    0xe50(%rip),%rdi
mov    $0x186a0,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0xea4(%rip),%rdi
mov    $0x186a0,%ebx
mov    $0x186a0,%esi
call   1030 <printf@plt>
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1060 <pthread_mutex_lock@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1050 <pthread_mutex_unlock@plt>
xor    %edi,%edi
or     %ebp,%eax
sete   %dil
call   1040 <assert@plt>
add    $0xffffffff,%ebx
jne    1170 <main+0x30>
lea    0xe7d(%rip),%rdi
mov    $0x186a0,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PTHREAD_MUTEX_INITIALIZER = global i32 0
@.str = [24 x i8] c"begin %d lock--unlocks\0A\00"
@.str.1 = [24 x i8] c"done %d lock--unlocks\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 100000, i32* %2
%6 = load i32, i32* @PTHREAD_MUTEX_INITIALIZER
store i32 %6, i32* %5
%7 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 100000)
store i32 0, i32* %3
br label %8
8: ; preds = %20, %0
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 100000
br i1 %10, label %11, label %23
11: ; preds = %8
%12 = call i32 @pthread_mutex_lock(i32* %5)
store i32 %12, i32* %4
%13 = call i32 @pthread_mutex_unlock(i32* %5)
%14 = load i32, i32* %4
%15 = or i32 %14, %13
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 0
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
br label %20
20: ; preds = %11
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %8
23: ; preds = %8
%24 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 100000)
ret i32 0
}

LLVM IR (-O2):
@PTHREAD_MUTEX_INITIALIZER = global i32 0
@.str = [24 x i8] c"begin %d lock--unlocks\0A\00"
@.str.1 = [24 x i8] c"done %d lock--unlocks\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @PTHREAD_MUTEX_INITIALIZER
store i32 %3, i32* %1
%4 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 100000)
br label %5
5: ; preds = %0, %5
%6 = phi i32 [ 0, %0 ], [ %13, %5 ]
%7 = call i32 @pthread_mutex_lock(i32* %1)
%8 = call i32 @pthread_mutex_unlock(i32* %1)
%9 = or i32 %8, %7
%10 = icmp eq i32 %9, 0
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = add nuw nsw i32 %6, 1
%14 = icmp eq i32 %13, 100000
br i1 %14, label %15, label %5
15: ; preds = %5
%16 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 100000)
ret i32 0
}