#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 O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ semaphore_unlink(char *);
int spinlock_open(int, char *, int, int, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
int spinlock_init(int idx) {
  int flag_sem = O_CREAT | O_RDWR;
  int init_value = 1;
  char sem_name[32];
  if (idx < 0 || idx >= 5)
    return -1;
  sprintf(sem_name, "%s%d", "/tmp/Semaphore", idx);
  semaphore_unlink(sem_name);
  if (spinlock_open(idx, sem_name, flag_sem,
                    (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
                    init_value) == -1) {
    perror("semaphore_create fail");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<spinlock_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
mov    0x2e85(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     117b <spinlock_init+0x3b>
cmpl   $0x5,-0x8(%rbp)
jl     1187 <spinlock_init+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <spinlock_init+0xc9>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
lea    0xe6b(%rip),%rsi
lea    0xe69(%rip),%rdx
call   1060 <sprintf@plt>
lea    -0x30(%rbp),%rdi
call   1040 <semaphore_unlink@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    0x2dfd(%rip),%rax
mov    (%rax),%ecx
mov    0x2e14(%rip),%rax
or     (%rax),%ecx
mov    0x2dfb(%rip),%rax
or     (%rax),%ecx
mov    0x2dea(%rip),%rax
or     (%rax),%ecx
mov    -0x10(%rbp),%r8d
call   1030 <spinlock_open@plt>
cmp    $0xffffffff,%eax
jne    1202 <spinlock_init+0xc2>
lea    0xe23(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <spinlock_init+0xc9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spinlock_init>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    $0xffffffff,%eax
cmp    $0x4,%edi
ja     11c8 <spinlock_init+0x88>
mov    %edi,%ebx
mov    0x2e9d(%rip),%rax
mov    (%rax),%ebp
mov    0x2e7c(%rip),%rax
or     (%rax),%ebp
lea    0xe93(%rip),%rsi
lea    0xe91(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebx,%ecx
call   1060 <sprintf@plt>
mov    %r14,%rdi
call   1040 <semaphore_unlink@plt>
mov    0x2e28(%rip),%rax
mov    0x2e41(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e26(%rip),%rax
or     (%rax),%ecx
mov    0x2e15(%rip),%rax
or     (%rax),%ecx
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
mov    $0x1,%r8d
call   1030 <spinlock_open@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     11d1 <spinlock_init+0x91>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe3c(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    11c8 <spinlock_init+0x88>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str = [5 x i8] c"%s%d\00"
@.str.1 = [15 x i8] c"/tmp/Semaphore\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [22 x i8] c"semaphore_create fail\00"
define i32 @spinlock_init(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [32 x i8]
store i32 %0, i32* %3
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_RDWR
%9 = or i32 %7, %8
store i32 %9, i32* %4
store i32 1, i32* %5
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = load i32, i32* %3
%14 = icmp sge i32 %13, 5
br i1 %14, label %15, label %16
15: ; preds = %12, %1
store i32 -1, i32* %2
br label %38
16: ; preds = %12
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%18 = load i32, i32* %3
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%21 = call i32 @semaphore_unlink(i8* %20)
%22 = load i32, i32* %3
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%24 = load i32, i32* %4
%25 = load i32, i32* @S_IRUSR
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRGRP
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IROTH
%31 = or i32 %29, %30
%32 = load i32, i32* %5
%33 = call i32 @spinlock_open(i32 %22, i8* %23, i32 %24, i32 %31, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %16
%36 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %38
37: ; preds = %16
store i32 0, i32* %2
br label %38
38: ; preds = %37, %35, %15
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str = [5 x i8] c"%s%d\00"
@.str.1 = [15 x i8] c"/tmp/Semaphore\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [22 x i8] c"semaphore_create fail\00"
define i32 @spinlock_init(i32 %0) {
%2 = alloca [32 x i8]
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%6 = icmp ugt i32 %0, 4
br i1 %6, label %22, label %7
7: ; preds = %1
%8 = or i32 %4, %3
%9 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%10 = call i32 @semaphore_unlink(i8* %5)
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = load i32, i32* @S_IRGRP
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IROTH
%17 = or i32 %15, %16
%18 = call i32 @spinlock_open(i32 %0, i8* %5, i32 %8, i32 %17, i32 1)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %7
%21 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %7, %1, %20
%23 = phi i32 [ -1, %20 ], [ -1, %1 ], [ 0, %7 ]
ret i32 %23
}