#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 EAGAIN;
int EOWNERDEAD;
int pthread_mutex_consistent(int /*<<< orphan*/ *);
int pthread_mutex_lock(int /*<<< orphan*/ *);
int take_mutex(pthread_mutex_t *mutex) {
  int ret_code;
  do {
    ret_code = pthread_mutex_lock(mutex);
  } while (ret_code == EAGAIN);
  if (ret_code == EOWNERDEAD) {
    ret_code = pthread_mutex_consistent(mutex);
    if (ret_code != 0) {
      return ret_code;
    }
  } else if (ret_code != 0) {
    return ret_code;
  }
  return 0;
}


x86 assembly (clang, -O0):
<take_mutex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <pthread_mutex_lock@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e8e(%rip),%rcx
cmp    (%rcx),%eax
je     112c <take_mutex+0xc>
mov    -0x14(%rbp),%eax
mov    0x2e8c(%rip),%rcx
cmp    (%rcx),%eax
jne    1182 <take_mutex+0x62>
mov    -0x10(%rbp),%rdi
call   1030 <pthread_mutex_consistent@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     117d <take_mutex+0x5d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a3 <take_mutex+0x83>
jmp    119c <take_mutex+0x7c>
cmpl   $0x0,-0x14(%rbp)
je     1197 <take_mutex+0x77>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a3 <take_mutex+0x83>
jmp    119c <take_mutex+0x7c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<take_mutex>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2ea2(%rip),%r14
xchg   %ax,%ax
mov    %rbx,%rdi
call   1040 <pthread_mutex_lock@plt>
cmp    (%r14),%eax
je     1130 <take_mutex+0x10>
mov    0x2e9c(%rip),%rcx
cmp    (%rcx),%eax
jne    1150 <take_mutex+0x30>
mov    %rbx,%rdi
call   1030 <pthread_mutex_consistent@plt>
test   %eax,%eax
jne    1156 <take_mutex+0x36>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@EAGAIN = global i32 0
@EOWNERDEAD = global i32 0
define i32 @take_mutex(i32* %0) {
br label %2
2: ; preds = %2, %1
%3 = tail call i32 @pthread_mutex_lock(i32* %0)
%4 = load i32, i32* @EAGAIN
%5 = icmp eq i32 %3, %4
br i1 %5, label %2, label %6
6: ; preds = %2
%7 = load i32, i32* @EOWNERDEAD
%8 = icmp eq i32 %3, %7
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i32 @pthread_mutex_consistent(i32* %0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %15
12: ; preds = %6
%13 = icmp eq i32 %3, 0
br i1 %13, label %14, label %15
14: ; preds = %12, %9
br label %15
15: ; preds = %12, %9, %14
%16 = phi i32 [ 0, %14 ], [ %10, %9 ], [ %3, %12 ]
ret i32 %16
}