#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;
double now();
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
int main(void) {
  double t0, t1;
  pthread_mutex_t lock1 = PTHREAD_MUTEX_INITIALIZER;
  int volatile lock2 = 0;
  long count;
  const long ITERATIONS = 1000000;
  t0 = now();
  for (count = ITERATIONS; count > 0; count--) {
    pthread_mutex_lock(&lock1);
    pthread_mutex_unlock(&lock1);
  }
  t1 = now() - t0;
  printf("pthread_mutex_lock/unlock:  %.5g us/op\n",
         (t1 * 1000000.0) / ITERATIONS);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movq   $0xf4240,-0x30(%rbp)
mov    $0x0,%al
call   1050 <now@plt>
movsd  %xmm0,-0x10(%rbp)
movq   $0xf4240,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jle    11ac <main+0x6c>
lea    -0x1c(%rbp),%rdi
call   1060 <pthread_mutex_lock@plt>
lea    -0x1c(%rbp),%rdi
call   1040 <pthread_mutex_unlock@plt>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    117e <main+0x3e>
mov    $0x0,%al
call   1050 <now@plt>
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xe3b(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  0xe2e(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xe2b(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
movl   $0x0,0x14(%rsp)
xor    %eax,%eax
call   1050 <now@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0xf4241,%ebx
lea    0x4(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
call   1060 <pthread_mutex_lock@plt>
mov    %r14,%rdi
call   1040 <pthread_mutex_unlock@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1180 <main+0x40>
xor    %eax,%eax
call   1050 <now@plt>
subsd  0x8(%rsp),%xmm0
movsd  0xe51(%rip),%xmm1
mulsd  %xmm1,%xmm0
divsd  %xmm1,%xmm0
lea    0xe4a(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@PTHREAD_MUTEX_INITIALIZER = global i32 0
@.str = [40 x i8] c"pthread_mutex_lock/unlock: %.5g us/op\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca double
%3 = alloca double
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store i32 0, i32* %1
%8 = load i32, i32* @PTHREAD_MUTEX_INITIALIZER
store i32 %8, i32* %4
store volatile i32 0, i32* %5
store i64 1000000, i64* %7
%9 = call double (...) @now()
store double %9, double* %2
store i64 1000000, i64* %6
br label %10
10: ; preds = %16, %0
%11 = load i64, i64* %6
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = call i32 @pthread_mutex_lock(i32* %4)
%15 = call i32 @pthread_mutex_unlock(i32* %4)
br label %16
16: ; preds = %13
%17 = load i64, i64* %6
%18 = add nsw i64 %17, -1
store i64 %18, i64* %6
br label %10
19: ; preds = %10
%20 = call double (...) @now()
%21 = load double, double* %2
%22 = fsub double %20, %21
store double %22, double* %3
%23 = load double, double* %3
%24 = fmul double %23, 1.000000e+06
%25 = fdiv double %24, 1.000000e+06
%26 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), double %25)
ret i32 0
}

LLVM IR (-O2):
@PTHREAD_MUTEX_INITIALIZER = global i32 0
@.str = [40 x i8] c"pthread_mutex_lock/unlock: %.5g us/op\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = load i32, i32* @PTHREAD_MUTEX_INITIALIZER
store i32 %4, i32* %1
%5 = bitcast i32* %2 to i8*
store volatile i32 0, i32* %2
%6 = tail call double (...) @now()
br label %7
7: ; preds = %0, %7
%8 = phi i64 [ 1000000, %0 ], [ %11, %7 ]
%9 = call i32 @pthread_mutex_lock(i32* %1)
%10 = call i32 @pthread_mutex_unlock(i32* %1)
%11 = add nsw i64 %8, -1
%12 = icmp ugt i64 %8, 1
br i1 %12, label %7, label %13
13: ; preds = %7
%14 = call double (...) @now()
%15 = fsub double %14, %6
%16 = fmul double %15, 1.000000e+06
%17 = fdiv double %16, 1.000000e+06
%18 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), double %17)
ret i32 0
}