#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 scalar_t__ uint64_t;
typedef int /*<<< orphan*/ pthread_mutex_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
scalar_t__ clock_gettime_us(int /*<<< orphan*/);
int pthread_mutex_lock(int /*<<< orphan*/ *);
int mutex_lock(pthread_mutex_t *mutex, uint64_t *max_blocked_us) {
  int rc;
  uint64_t start_us;
  uint64_t blocked_us;
  if (max_blocked_us != NULL)
    start_us = clock_gettime_us(CLOCK_MONOTONIC);
  rc = pthread_mutex_lock(mutex);
  if (rc == 0 && max_blocked_us != NULL) {
    blocked_us = clock_gettime_us(CLOCK_MONOTONIC) - start_us;
    if (*max_blocked_us < blocked_us)
      *max_blocked_us = blocked_us;
  }
  return rc;
}


x86 assembly (clang, -O0):
<mutex_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     114d <mutex_lock+0x2d>
mov    0x2ea6(%rip),%rax
mov    (%rax),%edi
call   1030 <clock_gettime_us@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <pthread_mutex_lock@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11a5 <mutex_lock+0x85>
cmpq   $0x0,-0x10(%rbp)
je     11a5 <mutex_lock+0x85>
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
call   1030 <clock_gettime_us@plt>
sub    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
cmp    -0x28(%rbp),%rax
jge    11a0 <mutex_lock+0x80>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    11a5 <mutex_lock+0x85>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mutex_lock>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %rsi,%rsi
je     1177 <mutex_lock+0x57>
mov    %rsi,%r15
mov    0x2eae(%rip),%r12
mov    (%r12),%edi
call   1030 <clock_gettime_us@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <pthread_mutex_lock@plt>
test   %eax,%eax
jne    116b <mutex_lock+0x4b>
mov    (%r12),%edi
call   1030 <clock_gettime_us@plt>
mov    %rax,%rcx
sub    %r14,%rcx
xor    %eax,%eax
cmp    %rcx,(%r15)
jge    116b <mutex_lock+0x4b>
mov    %rcx,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <pthread_mutex_lock@plt>

LLVM IR (-O0):
@CLOCK_MONOTONIC = global i32 0
define i32 @mutex_lock(i32* %0, i64* %1) {
%3 = alloca i32*
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store i32* %0, i32** %3
store i64* %1, i64** %4
%8 = load i64*, i64** %4
%9 = icmp ne i64* %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @CLOCK_MONOTONIC
%12 = call i64 @clock_gettime_us(i32 %11)
store i64 %12, i64* %6
br label %13
13: ; preds = %10, %2
%14 = load i32*, i32** %3
%15 = call i32 @pthread_mutex_lock(i32* %14)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %34
18: ; preds = %13
%19 = load i64*, i64** %4
%20 = icmp ne i64* %19, null
br i1 %20, label %21, label %34
21: ; preds = %18
%22 = load i32, i32* @CLOCK_MONOTONIC
%23 = call i64 @clock_gettime_us(i32 %22)
%24 = load i64, i64* %6
%25 = sub nsw i64 %23, %24
store i64 %25, i64* %7
%26 = load i64*, i64** %4
%27 = load i64, i64* %26
%28 = load i64, i64* %7
%29 = icmp slt i64 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %21
%31 = load i64, i64* %7
%32 = load i64*, i64** %4
store i64 %31, i64* %32
br label %33
33: ; preds = %30, %21
br label %34
34: ; preds = %33, %18, %13
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
@CLOCK_MONOTONIC = global i32 0
define i32 @mutex_lock(i32* %0, i64* %1) {
%3 = icmp eq i64* %1, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @pthread_mutex_lock(i32* %0)
br label %18
6: ; preds = %2
%7 = load i32, i32* @CLOCK_MONOTONIC
%8 = tail call i64 @clock_gettime_us(i32 %7)
%9 = tail call i32 @pthread_mutex_lock(i32* %0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %6
%12 = load i32, i32* @CLOCK_MONOTONIC
%13 = tail call i64 @clock_gettime_us(i32 %12)
%14 = sub nsw i64 %13, %8
%15 = load i64, i64* %1
%16 = icmp slt i64 %15, %14
br i1 %16, label %17, label %18
17: ; preds = %11
store i64 %14, i64* %1
br label %18
18: ; preds = %4, %11, %17, %6
%19 = phi i32 [ %5, %4 ], [ 0, %11 ], [ 0, %17 ], [ %9, %6 ]
ret i32 %19
}