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

/* Variables and functions */
int init_tls();
int /*<<< orphan*/ lksmith_error(int, char *, int /*<<< orphan*/ *,
                                 int /*<<< orphan*/, int);
int lksmith_optional_init(void const *, int, int);
int /*<<< orphan*/ pthread_mutex_destroy(int /*<<< orphan*/ *);
int pthread_mutex_real_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ terror(int);
int pthread_mutex_init(pthread_mutex_t *mutex,
                       const pthread_mutexattr_t *attr) {
  int ret, recursive = 0;
  ret = init_tls();
  if (ret)
    return ret;
  ret = pthread_mutex_real_init(mutex, (pthread_mutexattr_t *)attr, &recursive);
  if (ret) {
    lksmith_error(ret,
                  "pthread_mutex_init(mutex=%p): "
                  "failed with error %s (%d)",
                  mutex, terror(ret), ret);
    return ret;
  }
  ret = lksmith_optional_init((const void *)mutex, recursive, 1);
  if (ret) {
    pthread_mutex_destroy(mutex);
    return ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<pthread_mutex_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1070 <init_tls@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1196 <pthread_mutex_init+0x36>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1227 <pthread_mutex_init+0xc7>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <pthread_mutex_real_init@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11ee <pthread_mutex_init+0x8e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1080 <terror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
mov    -0x1c(%rbp),%r8d
lea    0xe22(%rip),%rsi
call   1030 <lksmith_error@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1227 <pthread_mutex_init+0xc7>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    $0x1,%edx
call   1050 <lksmith_optional_init@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1220 <pthread_mutex_init+0xc0>
mov    -0x10(%rbp),%rdi
call   1040 <pthread_mutex_destroy@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1227 <pthread_mutex_init+0xc7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pthread_mutex_init>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
movl   $0x0,0xc(%rsp)
xor    %eax,%eax
call   1070 <init_tls@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11db <pthread_mutex_init+0x7b>
lea    0xc(%rsp),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <pthread_mutex_real_init@plt>
test   %eax,%eax
je     11b8 <pthread_mutex_init+0x58>
mov    %eax,%ebp
mov    %eax,%edi
call   1080 <terror@plt>
lea    0xe59(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %eax,%ecx
mov    %ebp,%r8d
call   1030 <lksmith_error@plt>
jmp    11db <pthread_mutex_init+0x7b>
mov    0xc(%rsp),%esi
mov    %r14,%rdi
mov    $0x1,%edx
call   1050 <lksmith_optional_init@plt>
test   %eax,%eax
je     11d9 <pthread_mutex_init+0x79>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <pthread_mutex_destroy@plt>
jmp    11db <pthread_mutex_init+0x7b>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [56 x i8] c"pthread_mutex_init(mutex=%p): failed with error %s (%d)\00"
define i32 @pthread_mutex_init(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 0, i32* %7
%8 = call i32 (...) @init_tls()
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i32, i32* %6
store i32 %12, i32* %3
br label %39
13: ; preds = %2
%14 = load i32*, i32** %4
%15 = load i32*, i32** %5
%16 = call i32 @pthread_mutex_real_init(i32* %14, i32* %15, i32* %7)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %13
%20 = load i32, i32* %6
%21 = load i32*, i32** %4
%22 = load i32, i32* %6
%23 = call i32 @terror(i32 %22)
%24 = load i32, i32* %6
%25 = call i32 @lksmith_error(i32 %20, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32* %21, i32 %23, i32 %24)
%26 = load i32, i32* %6
store i32 %26, i32* %3
br label %39
27: ; preds = %13
%28 = load i32*, i32** %4
%29 = bitcast i32* %28 to i8*
%30 = load i32, i32* %7
%31 = call i32 @lksmith_optional_init(i8* %29, i32 %30, i32 1)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %27
%35 = load i32*, i32** %4
%36 = call i32 @pthread_mutex_destroy(i32* %35)
%37 = load i32, i32* %6
store i32 %37, i32* %3
br label %39
38: ; preds = %27
store i32 0, i32* %3
br label %39
39: ; preds = %38, %34, %19, %11
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [56 x i8] c"pthread_mutex_init(mutex=%p): failed with error %s (%d)\00"
define i32 @pthread_mutex_init(i32* %0, i32* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = tail call i32 (...) @init_tls()
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %20
7: ; preds = %2
%8 = call i32 @pthread_mutex_real_init(i32* %0, i32* %1, i32* %3)
%9 = icmp eq i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = call i32 @terror(i32 %8)
%12 = call i32 @lksmith_error(i32 %8, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i32* %0, i32 %11, i32 %8)
br label %20
13: ; preds = %7
%14 = bitcast i32* %0 to i8*
%15 = load i32, i32* %3
%16 = call i32 @lksmith_optional_init(i8* %14, i32 %15, i32 1)
%17 = icmp eq i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = call i32 @pthread_mutex_destroy(i32* %0)
br label %20
20: ; preds = %13, %2, %18, %10
%21 = phi i32 [ %8, %10 ], [ %16, %18 ], [ %5, %2 ], [ 0, %13 ]
ret i32 %21
}