#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 IPC_CREAT;
int IPC_EXCL;
int IPC_PRIVATE;
int /*<<< orphan*/ SETVAL;
int /*<<< orphan*/ printf(char *);
int semctl(int, int /*<<< orphan*/, int /*<<< orphan*/, int);
int semget(int, int, int);
int /*<<< orphan*/ umask(int /*<<< orphan*/);
int init_semaphore(int SEMKEY) {
  int semid;
  semid = semget(SEMKEY, 0, IPC_PRIVATE);
  if (semid < 0) {
    umask(0);
    semid = semget(SEMKEY, 1, IPC_CREAT | IPC_EXCL | 0666);
    if (semid < 0) {
      printf("Fehler beim Anlegen der Semaphore...\n");
      return -1;
    } else {
      printf("Semaphore angelegt...\n");
      if (semctl(semid, 0, SETVAL, (int)1) == -1) {
        return -1;
      }
    }
    return semid;
  }
}


x86 assembly (clang, -O0):
<init_semaphore>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e8b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <semget@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1200 <init_semaphore+0xc0>
xor    %edi,%edi
call   1050 <umask@plt>
mov    -0x8(%rbp),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
mov    0x2e6b(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11bc <init_semaphore+0x7c>
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <init_semaphore+0xc0>
lea    0xe63(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    $0x1,%ecx
call   1060 <semctl@plt>
cmp    $0xffffffff,%eax
jne    11f5 <init_semaphore+0xb5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <init_semaphore+0xc0>
jmp    11fa <init_semaphore+0xba>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_semaphore>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2e94(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <semget@plt>
test   %eax,%eax
jns    11c6 <init_semaphore+0x86>
xor    %edi,%edi
call   1050 <umask@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%edx
mov    0x2e80(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    %ebp,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
test   %eax,%eax
js     11b5 <init_semaphore+0x75>
mov    %eax,%ebx
lea    0xe6f(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
mov    $0x1,%ecx
call   1060 <semctl@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
jmp    11c6 <init_semaphore+0x86>
lea    0xe5a(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@.str = [38 x i8] c"Fehler beim Anlegen der Semaphore...\0A\00"
@.str.1 = [23 x i8] c"Semaphore angelegt...\0A\00"
@SETVAL = global i32 0
define i32 @init_semaphore(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = load i32, i32* @IPC_PRIVATE
%7 = call i32 @semget(i32 %5, i32 0, i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %32
10: ; preds = %1
%11 = call i32 @umask(i32 0)
%12 = load i32, i32* %3
%13 = load i32, i32* @IPC_CREAT
%14 = load i32, i32* @IPC_EXCL
%15 = or i32 %13, %14
%16 = or i32 %15, 438
%17 = call i32 @semget(i32 %12, i32 1, i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %32
22: ; preds = %10
%23 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* %4
%25 = load i32, i32* @SETVAL
%26 = call i32 @semctl(i32 %24, i32 0, i32 %25, i32 1)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %22
store i32 -1, i32* %2
br label %32
29: ; preds = %22
br label %30
30: ; preds = %29
%31 = load i32, i32* %4
store i32 %31, i32* %2
br label %32
32: ; preds = %20, %28, %30, %1
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@SETVAL = global i32 0
@str = [22 x i8] c"Semaphore angelegt...\00"
@str.2 = [37 x i8] c"Fehler beim Anlegen der Semaphore...\00"
define i32 @init_semaphore(i32 %0) {
%2 = load i32, i32* @IPC_PRIVATE
%3 = tail call i32 @semget(i32 %0, i32 0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %21
5: ; preds = %1
%6 = tail call i32 @umask(i32 0)
%7 = load i32, i32* @IPC_CREAT
%8 = load i32, i32* @IPC_EXCL
%9 = or i32 %7, %8
%10 = or i32 %9, 438
%11 = tail call i32 @semget(i32 %0, i32 1, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %5
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.2, i64 0, i64 0))
br label %21
15: ; preds = %5
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%17 = load i32, i32* @SETVAL
%18 = tail call i32 @semctl(i32 %11, i32 0, i32 %17, i32 1)
%19 = icmp eq i32 %18, -1
%20 = select i1 %19, i32 -1, i32 %11
br label %21
21: ; preds = %1, %15, %13
%22 = phi i32 [ -1, %13 ], [ %20, %15 ], [ undef, %1 ]
ret i32 %22
}