#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_spinlock_t;

/* Variables and functions */
int /*<<< orphan*/ lksmith_destroy(void const *);
int /*<<< orphan*/ lksmith_error(int, char *, int /*<<< orphan*/ *,
                                 int /*<<< orphan*/, int);
int lksmith_optional_init(void const *, int /*<<< orphan*/, int /*<<< orphan*/);
int r_pthread_spin_init(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ terror(int);
int pthread_spin_init(pthread_spinlock_t *lock, int pshared) {
  int ret;
  ret = lksmith_optional_init((const void *)lock, 0, 0);
  if (ret)
    return ret;
  ret = r_pthread_spin_init(lock, pshared);
  if (ret) {
    lksmith_error(ret,
                  "pthread_spin_init(mutex=%p): "
                  "failed with error %s (%d)",
                  lock, terror(ret), ret);
    lksmith_destroy((const void *)lock);
    return ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<pthread_spin_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1040 <lksmith_optional_init@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1184 <pthread_spin_init+0x34>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <pthread_spin_init+0x97>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <r_pthread_spin_init@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11e0 <pthread_spin_init+0x90>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <terror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %eax,%ecx
mov    -0x18(%rbp),%r8d
lea    0xe39(%rip),%rsi
call   1030 <lksmith_error@plt>
mov    -0x10(%rbp),%rdi
call   1050 <lksmith_destroy@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <pthread_spin_init+0x97>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pthread_spin_init>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %esi,%esi
xor    %edx,%edx
call   1040 <lksmith_optional_init@plt>
test   %eax,%eax
jne    11a1 <pthread_spin_init+0x51>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <r_pthread_spin_init@plt>
mov    %eax,%ebp
mov    $0x0,%eax
test   %ebp,%ebp
je     11a1 <pthread_spin_init+0x51>
mov    %ebp,%edi
call   1060 <terror@plt>
lea    0xe78(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
mov    %ebp,%r8d
call   1030 <lksmith_error@plt>
mov    %rbx,%rdi
call   1050 <lksmith_destroy@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [55 x i8] c"pthread_spin_init(mutex=%p): failed with error %s (%d)\00"
define i32 @pthread_spin_init(i32* %0, i32 %1) {
%3 = bitcast i32* %0 to i8*
%4 = tail call i32 @lksmith_optional_init(i8* %3, i32 0, i32 0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %2
%7 = tail call i32 @r_pthread_spin_init(i32* %0, i32 %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = tail call i32 @terror(i32 %7)
%11 = tail call i32 @lksmith_error(i32 %7, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32* %0, i32 %10, i32 %7)
%12 = tail call i32 @lksmith_destroy(i8* %3)
br label %13
13: ; preds = %6, %2, %9
%14 = phi i32 [ %7, %9 ], [ %4, %2 ], [ 0, %6 ]
ret i32 %14
}