#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;
typedef int /*<<< orphan*/ err_b;

/* Variables and functions */
#define EAGAIN 130
#define ENOTRECOVERABLE 129
#define EOWNERDEAD 128
int /*<<< orphan*/ SYS_gettid;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int, ...);
int /*<<< orphan*/ g_strerr_r(int, char *, int);
scalar_t__ getpid();
int /*<<< orphan*/ pthread_mutex_consistent(int /*<<< orphan*/ *);
int pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ syscall(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
void mutex_lock(pthread_mutex_t *mutex) {
  int r;
  switch (r = pthread_mutex_lock(mutex)) {
  case 0:
    return;
  case EOWNERDEAD:
    fprintf(stderr,
            "WARNING: %ld: calling pthread_mutex_consistent [EOWNERDEAD]\n",
            syscall(SYS_gettid));
    if (0 == pthread_mutex_consistent(mutex)) {
      mutex_lock(mutex);
    } else {
      fprintf(stderr, "ERROR: %ld: pthread_mutex_consistent failed\n",
              syscall(SYS_gettid));
      abort();
    }
    return;
  case EAGAIN:
    usleep(10000);
    mutex_lock(mutex);
    return;
  case ENOTRECOVERABLE:
    fprintf(stderr, "ERROR: %u: pthread_mutex_lock: [ENOTRECOVERABLE]\n",
            (unsigned int)getpid());
    abort();
    return;
  default:;
    char err_b[1024];
    fprintf(stderr, "ERROR: %ld: pthread_mutex_lock: [%d] [%s]\n",
            syscall(SYS_gettid), r,
            g_strerr_r(r, (char *)err_b, sizeof(err_b)));
    abort();
  }
}


x86 assembly (clang, -O0):
<mutex_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <pthread_mutex_lock@plt>
mov    %eax,-0xc(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x414(%rbp)
test   %eax,%eax
je     1202 <mutex_lock+0x72>
jmp    11c0 <mutex_lock+0x30>
mov    -0x414(%rbp),%eax
sub    $0x80,%eax
je     1207 <mutex_lock+0x77>
jmp    11d6 <mutex_lock+0x46>
mov    -0x414(%rbp),%eax
sub    $0x81,%eax
je     12b4 <mutex_lock+0x124>
jmp    11ec <mutex_lock+0x5c>
mov    -0x414(%rbp),%eax
sub    $0x82,%eax
je     129c <mutex_lock+0x10c>
jmp    12e7 <mutex_lock+0x157>
jmp    1351 <mutex_lock+0x1c1>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x418(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1090 <syscall@plt>
mov    -0x418(%rbp),%edi
mov    %eax,%edx
lea    0xdcd(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1070 <pthread_mutex_consistent@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    125d <mutex_lock+0xcd>
mov    -0x8(%rbp),%rdi
call   1030 <mutex_lock@plt>
jmp    1297 <mutex_lock+0x107>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x41c(%rbp)
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   1090 <syscall@plt>
mov    -0x41c(%rbp),%edi
mov    %eax,%edx
lea    0xdb4(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
jmp    1351 <mutex_lock+0x1c1>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    -0x8(%rbp),%rdi
call   1030 <mutex_lock@plt>
jmp    1351 <mutex_lock+0x1c1>
mov    0x2d1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x420(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x420(%rbp),%edi
mov    %eax,%edx
lea    0xd91(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
mov    0x2cea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x42c(%rbp)
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
call   1090 <syscall@plt>
mov    %eax,-0x428(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x424(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1060 <g_strerr_r@plt>
mov    -0x42c(%rbp),%edi
mov    -0x428(%rbp),%edx
mov    -0x424(%rbp),%ecx
mov    %eax,%r8d
lea    0xd59(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mutex_lock>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%rbx
mov    0x2e3f(%rip),%r15
mov    0x2e48(%rip),%r12
lea    0xe59(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <pthread_mutex_lock@plt>
mov    %eax,%ebp
cmp    $0x80,%eax
jg     11f0 <mutex_lock+0x70>
jne    1204 <mutex_lock+0x84>
mov    (%r15),%ebp
mov    (%r12),%edi
call   1080 <syscall@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1060 <pthread_mutex_consistent@plt>
test   %eax,%eax
je     11b0 <mutex_lock+0x30>
jmp    1273 <mutex_lock+0xf3>
xchg   %ax,%ax
cmp    $0x82,%ebp
jne    1218 <mutex_lock+0x98>
mov    $0x2710,%edi
call   10a0 <usleep@plt>
jmp    11b0 <mutex_lock+0x30>
test   %ebp,%ebp
jne    1237 <mutex_lock+0xb7>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0x81,%ebp
jne    1237 <mutex_lock+0xb7>
mov    (%r15),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe39(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
jmp    128a <mutex_lock+0x10a>
mov    (%r15),%r14d
mov    (%r12),%edi
call   1080 <syscall@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1050 <g_strerr_r@plt>
lea    0xe41(%rip),%rsi
mov    %r14d,%edi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    (%r15),%ebx
mov    (%r12),%edi
call   1080 <syscall@plt>
lea    0xdb7(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [61 x i8] c"WARNING: %ld: calling pthread_mutex_consistent [EOWNERDEAD]\0A\00"
@SYS_gettid = global i32 0
@.str.1 = [45 x i8] c"ERROR: %ld: pthread_mutex_consistent failed\0A\00"
@.str.2 = [50 x i8] c"ERROR: %u: pthread_mutex_lock: [ENOTRECOVERABLE]\0A\00"
@.str.3 = [43 x i8] c"ERROR: %ld: pthread_mutex_lock: [%d] [%s]\0A\00"
define void @mutex_lock(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32* %0, i32** %2
%5 = load i32*, i32** %2
%6 = call i32 @pthread_mutex_lock(i32* %5)
store i32 %6, i32* %3
switch i32 %6, label %34 [
i32 0, label %7
i32 128, label %8
i32 130, label %25
i32 129, label %28
]
7: ; preds = %1
br label %44
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i32, i32* @SYS_gettid
%11 = call i32 @syscall(i32 %10)
%12 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32*, i32** %2
%14 = call i32 @pthread_mutex_consistent(i32* %13)
%15 = icmp eq i32 0, %14
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = load i32*, i32** %2
br label %24
18: ; preds = %8
%19 = load i32, i32* @stderr
%20 = load i32, i32* @SYS_gettid
%21 = call i32 @syscall(i32 %20)
%22 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call i32 (...) @abort()
unreachable
24: ; preds = %16
br label %44
25: ; preds = %1
%26 = call i32 @usleep(i32 10000)
%27 = load i32*, i32** %2
br label %44
28: ; preds = %1
%29 = load i32, i32* @stderr
%30 = call i64 (...) @getpid()
%31 = trunc i64 %30 to i32
%32 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = call i32 (...) @abort()
unreachable
34: ; preds = %1
%35 = load i32, i32* @stderr
%36 = load i32, i32* @SYS_gettid
%37 = call i32 @syscall(i32 %36)
%38 = load i32, i32* %3
%39 = load i32, i32* %3
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%41 = call i32 @g_strerr_r(i32 %39, i8* %40, i32 1024)
%42 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %37, i32 %38, i32 %41)
%43 = call i32 (...) @abort()
unreachable
44: ; preds = %25, %24, %7
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [61 x i8] c"WARNING: %ld: calling pthread_mutex_consistent [EOWNERDEAD]\0A\00"
@SYS_gettid = global i32 0
@.str.1 = [45 x i8] c"ERROR: %ld: pthread_mutex_consistent failed\0A\00"
@.str.2 = [50 x i8] c"ERROR: %u: pthread_mutex_lock: [ENOTRECOVERABLE]\0A\00"
@.str.3 = [43 x i8] c"ERROR: %ld: pthread_mutex_lock: [%d] [%s]\0A\00"
define void @mutex_lock(i32* %0) {
%2 = alloca [1024 x i8]
br label %3
3: ; preds = %20, %1
%4 = tail call i32 @pthread_mutex_lock(i32* %0)
switch i32 %4, label %27 [
i32 0, label %35
i32 128, label %5
i32 130, label %18
i32 129, label %21
]
5: ; preds = %3
%6 = load i32, i32* @stderr
%7 = load i32, i32* @SYS_gettid
%8 = tail call i32 @syscall(i32 %7)
%9 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = tail call i32 @pthread_mutex_consistent(i32* %0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %5
%13 = load i32, i32* @stderr
%14 = load i32, i32* @SYS_gettid
%15 = tail call i32 @syscall(i32 %14)
%16 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = tail call i32 (...) @abort()
unreachable
18: ; preds = %3
%19 = tail call i32 @usleep(i32 10000)
br label %20
20: ; preds = %18, %5
br label %3
21: ; preds = %3
%22 = load i32, i32* @stderr
%23 = tail call i64 (...) @getpid()
%24 = trunc i64 %23 to i32
%25 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = tail call i32 (...) @abort()
unreachable
27: ; preds = %3
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%29 = load i32, i32* @stderr
%30 = load i32, i32* @SYS_gettid
%31 = tail call i32 @syscall(i32 %30)
%32 = call i32 @g_strerr_r(i32 %4, i8* %28, i32 1024)
%33 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %4, i32 %32)
%34 = call i32 (...) @abort()
unreachable
35: ; preds = %3
ret void
}