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

/* Variables and functions */
int IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ SETVAL;
int /*<<< orphan*/ perror(char *);
scalar_t__ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/, int);
int semget(int /*<<< orphan*/, int, int);
int sem_init(key_t key, int value) {
  int semid = semget(key, 1, IPC_EXCL | IPC_CREAT | 0666);
  if (semid < 0) {
    semid = semget(key, 1, IPC_CREAT | 0666);
    if (semid < 0) {
      perror("Semget()");
      return -1;
    }
    return semid;
  }
  if (semctl(semid, 0, SETVAL, value) < 0) {
    perror("Semctl()");
    return -1;
  }
  return semid;
}


x86 assembly (clang, -O0):
<sem_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea8(%rip),%rax
mov    (%rax),%edx
mov    0x2e87(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11bc <sem_init+0x8c>
mov    -0x8(%rbp),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11b1 <sem_init+0x81>
lea    0xe60(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <sem_init+0xca>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fa <sem_init+0xca>
mov    -0x10(%rbp),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
mov    -0xc(%rbp),%ecx
xor    %esi,%esi
call   1050 <semctl@plt>
cmp    $0x0,%rax
jge    11f4 <sem_init+0xc4>
lea    0xe26(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <sem_init+0xca>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sem_init>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%ebx
mov    0x2ead(%rip),%rax
mov    (%rax),%edx
mov    0x2e8c(%rip),%r15
or     (%r15),%edx
or     $0x1b6,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
test   %eax,%eax
js     1188 <sem_init+0x58>
mov    %eax,%ebp
mov    0x2e64(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
mov    %r14d,%ecx
call   1050 <semctl@plt>
test   %rax,%rax
jns    11a2 <sem_init+0x72>
lea    0xe83(%rip),%rdi
jmp    11b6 <sem_init+0x86>
mov    $0x1b6,%edx
or     (%r15),%edx
mov    %ebx,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11af <sem_init+0x7f>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4a(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11a2 <sem_init+0x72>

LLVM IR (-O0):
@IPC_EXCL = global i32 0
@IPC_CREAT = global i32 0
@.str = [9 x i8] c"Semget()\00"
@SETVAL = global i32 0
@.str.1 = [9 x i8] c"Semctl()\00"
define i32 @sem_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 = load i32, i32* @IPC_EXCL
%9 = load i32, i32* @IPC_CREAT
%10 = or i32 %8, %9
%11 = or i32 %10, 438
%12 = call i32 @semget(i32 %7, i32 1, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %26
15: ; preds = %2
%16 = load i32, i32* %4
%17 = load i32, i32* @IPC_CREAT
%18 = or i32 %17, 438
%19 = call i32 @semget(i32 %16, i32 1, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %36
24: ; preds = %15
%25 = load i32, i32* %6
store i32 %25, i32* %3
br label %36
26: ; preds = %2
%27 = load i32, i32* %6
%28 = load i32, i32* @SETVAL
%29 = load i32, i32* %5
%30 = call i64 @semctl(i32 %27, i32 0, i32 %28, i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %36
34: ; preds = %26
%35 = load i32, i32* %6
store i32 %35, i32* %3
br label %36
36: ; preds = %34, %32, %24, %22
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@IPC_EXCL = global i32 0
@IPC_CREAT = global i32 0
@.str = [9 x i8] c"Semget()\00"
@SETVAL = global i32 0
@.str.1 = [9 x i8] c"Semctl()\00"
define i32 @sem_init(i32 %0, i32 %1) {
%3 = load i32, i32* @IPC_EXCL
%4 = load i32, i32* @IPC_CREAT
%5 = or i32 %3, %4
%6 = or i32 %5, 438
%7 = tail call i32 @semget(i32 %0, i32 1, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i32, i32* @IPC_CREAT
%11 = or i32 %10, 438
%12 = tail call i32 @semget(i32 %0, i32 1, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %18, label %21
14: ; preds = %2
%15 = load i32, i32* @SETVAL
%16 = tail call i64 @semctl(i32 %7, i32 0, i32 %15, i32 %1)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14, %9
%19 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %9 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %14 ]
%20 = tail call i32 @perror(i8* %19)
br label %21
21: ; preds = %18, %14, %9
%22 = phi i32 [ %12, %9 ], [ %7, %14 ], [ -1, %18 ]
ret i32 %22
}