#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 */

/* Variables and functions */
int /*<<< orphan*/ printf(char *);
int TrySpinlock(int id) {
  unsigned long tmp;
  if (id < 0 || id > 63) {
    printf("bad spinlock id\n");
    return -1;
  }
  tmp = ({
    unsigned long __v =
        (*(volatile unsigned long *)((0x02640000 + 0x100 + id * 0x4)));
    __v;
  });
  if (!(tmp & 0x1))
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<TrySpinlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     112f <TrySpinlock+0x1f>
cmpl   $0x3f,-0x8(%rbp)
jle    1147 <TrySpinlock+0x37>
lea    0xeca(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1190 <TrySpinlock+0x80>
mov    -0x8(%rbp),%eax
shl    $0x2,%eax
add    $0x2640100,%eax
cltq
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
jne    1189 <TrySpinlock+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    1190 <TrySpinlock+0x80>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TrySpinlock>:
push   %rax
cmp    $0x40,%edi
jb     1129 <TrySpinlock+0x19>
lea    0xee3(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret
mov    %edi,%eax
mov    0x2640100(,%rax,4),%rax
02
and    $0x1,%eax
add    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [17 x i8] c"bad spinlock id\0A\00"
define i32 @TrySpinlock(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = load i32, i32* %3
%11 = icmp sgt i32 %10, 63
br i1 %11, label %12, label %14
12: ; preds = %9, %1
%13 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %28
14: ; preds = %9
%15 = load i32, i32* %3
%16 = mul nsw i32 %15, 4
%17 = add nsw i32 40108288, %16
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to i64*
%20 = load volatile i64, i64* %19
store i64 %20, i64* %5
%21 = load i64, i64* %5
store i64 %21, i64* %6
%22 = load i64, i64* %6
store i64 %22, i64* %4
%23 = load i64, i64* %4
%24 = and i64 %23, 1
%25 = icmp ne i64 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %14
store i32 -1, i32* %2
br label %28
27: ; preds = %14
store i32 0, i32* %2
br label %28
28: ; preds = %27, %26, %12
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@str = [16 x i8] c"bad spinlock id\00"
define i32 @TrySpinlock(i32 %0) {
%2 = icmp ugt i32 %0, 63
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %14
5: ; preds = %1
%6 = shl nuw nsw i32 %0, 2
%7 = add nuw nsw i32 %6, 40108288
%8 = zext i32 %7 to i64
%9 = inttoptr i64 %8 to i64*
%10 = load volatile i64, i64* %9
%11 = trunc i64 %10 to i32
%12 = and i32 %11, 1
%13 = add nsw i32 %12, -1
br label %14
14: ; preds = %5, %3
%15 = phi i32 [ -1, %3 ], [ %13, %5 ]
ret i32 %15
}