#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 131
#define EBUSY 130
#define ENOTRECOVERABLE 129
#define EOWNERDEAD 128
int /*<<< orphan*/ abort();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
char *g_strerr_r(int, char *, int);
int getpid();
int /*<<< orphan*/ pthread_mutex_consistent(int /*<<< orphan*/ *);
int pthread_mutex_trylock(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usleep(int);
int mutex_trylock(pthread_mutex_t *mutex) {
  int r;
  switch (r = pthread_mutex_trylock(mutex)) {
  case 0:
    return 0;
  case EBUSY:
    return EBUSY;
  case EOWNERDEAD:
    fprintf(stderr,
            "ERROR: %d: calling pthread_mutex_consistent [EOWNERDEAD]\n",
            getpid());
    if (0 == pthread_mutex_consistent(mutex)) {
      return mutex_trylock(mutex);
    } else {
      fprintf(stderr, "ERROR: %d: pthread_mutex_consistent failed\n", getpid());
      abort();
    }
  case EAGAIN:
    usleep(10000);
    return mutex_trylock(mutex);
  case ENOTRECOVERABLE:
    fprintf(stderr, "ERROR: %d: pthread_mutex_lock: [ENOTRECOVERABLE]\n",
            getpid());
    abort();
  default:;
    char err_b[1024];
    fprintf(stderr, "ERROR: %d: pthread_mutex_lock: [%d] [%s]\n", getpid(), r,
            g_strerr_r(r, (char *)err_b, sizeof(err_b)));
    abort();
  }
  return r;
}


x86 assembly (clang, -O0):
<mutex_trylock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <pthread_mutex_trylock@plt>
mov    %eax,-0x14(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x424(%rbp)
test   %eax,%eax
je     1208 <mutex_trylock+0x88>
jmp    11b0 <mutex_trylock+0x30>
mov    -0x424(%rbp),%eax
sub    $0x80,%eax
je     1220 <mutex_trylock+0xa0>
jmp    11c6 <mutex_trylock+0x46>
mov    -0x424(%rbp),%eax
sub    $0x81,%eax
je     12c0 <mutex_trylock+0x140>
jmp    11dc <mutex_trylock+0x5c>
mov    -0x424(%rbp),%eax
sub    $0x82,%eax
je     1214 <mutex_trylock+0x94>
jmp    11f2 <mutex_trylock+0x72>
mov    -0x424(%rbp),%eax
sub    $0x83,%eax
je     12a5 <mutex_trylock+0x125>
jmp    12f3 <mutex_trylock+0x173>
movl   $0x0,-0x4(%rbp)
jmp    1356 <mutex_trylock+0x1d6>
movl   $0x82,-0x4(%rbp)
jmp    1356 <mutex_trylock+0x1d6>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x428(%rbp),%edi
mov    %eax,%edx
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <pthread_mutex_consistent@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    1272 <mutex_trylock+0xf2>
mov    -0x10(%rbp),%rdi
call   1060 <mutex_trylock@plt>
mov    %eax,-0x4(%rbp)
jmp    1356 <mutex_trylock+0x1d6>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x42c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x42c(%rbp),%edi
mov    %eax,%edx
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    $0x2710,%edi
call   10a0 <usleep@plt>
mov    -0x10(%rbp),%rdi
call   1060 <mutex_trylock@plt>
mov    %eax,-0x4(%rbp)
jmp    1356 <mutex_trylock+0x1d6>
mov    0x2d19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x430(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x430(%rbp),%edi
mov    %eax,%edx
lea    0xd81(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    0x2ce6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x43c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x438(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x434(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <g_strerr_r@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%edx
mov    -0x434(%rbp),%ecx
mov    %rax,%r8
lea    0xd50(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mutex_trylock>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%r15
lea    0xe77(%rip),%rbp
mov    0x2e50(%rip),%r12
lea    0xe79(%rip),%r14
jmp    11aa <mutex_trylock+0x3a>
nopl   0x0(%rax)
mov    $0x2710,%edi
call   1090 <usleep@plt>
mov    %r15,%rdi
call   1050 <pthread_mutex_trylock@plt>
mov    %eax,%ebx
lea    -0x80(%rbx),%eax
cmp    $0x3,%eax
ja     11ff <mutex_trylock+0x8f>
movslq 0x0(%rbp,%rax,4),%rax
add    %rbp,%rax
jmp    *%rax
mov    (%r12),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r15,%rdi
call   1070 <pthread_mutex_consistent@plt>
test   %eax,%eax
je     11aa <mutex_trylock+0x3a>
mov    (%r12),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe4d(%rip),%rsi
jmp    1227 <mutex_trylock+0xb7>
test   %ebx,%ebx
jne    1239 <mutex_trylock+0xc9>
mov    %ebx,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r12),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe4f(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    (%r12),%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x400,%edx
call   1060 <g_strerr_r@plt>
lea    0xe4c(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1080 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

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

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