#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 */
#define EACCES 132
#define EIDRM 131
#define EINVAL 130
#define EPERM 129
#define ERANGE 128
int /*<<< orphan*/ IPC_RMID;
int errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/,
                  int /*<<< orphan*/);
int semdest(int semid) {
  if (semctl(semid, 0, IPC_RMID, 0) < 0) {
    switch (errno) {
    case EPERM:
      printf("EUID does not match the owners/creators UID!\n");
      break;
    case EACCES:
      printf("Insufficient permissions!\n");
      break;
    case EIDRM:
      printf("Id has been removed removed!\n");
      break;
    case ERANGE:
      printf("[SEMCTL] Attempt to set value outside of range\n");
      break;
    case EINVAL:
      printf("False semaphor id: %d\n", semid);
      break;
    default:
      perror("[SEMCTL]");
      break;
    }
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<semdest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e93(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   1050 <semctl@plt>
cmp    $0x0,%rax
jge    1203 <semdest+0xd3>
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x10(%rbp)
sub    $0x4,%eax
ja     11eb <semdest+0xbb>
mov    -0x10(%rbp),%rax
lea    0xe80(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f7 <semdest+0xc7>
lea    0xe9f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f7 <semdest+0xc7>
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f7 <semdest+0xc7>
lea    0xeb2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f7 <semdest+0xc7>
mov    -0x8(%rbp),%esi
lea    0xecc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f7 <semdest+0xc7>
lea    0xed0(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <semdest+0xda>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<semdest>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e8c(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
xor    %esi,%esi
xor    %ecx,%ecx
call   1060 <semctl@plt>
test   %rax,%rax
jns    11bd <semdest+0x7d>
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
cmp    $0x4,%eax
ja     11c6 <semdest+0x86>
lea    0xe8a(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xeae(%rip),%rdi
jmp    11b3 <semdest+0x73>
lea    0xf0b(%rip),%rdi
jmp    11b3 <semdest+0x73>
lea    0xe7c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b8 <semdest+0x78>
lea    0xeb9(%rip),%rdi
jmp    11b3 <semdest+0x73>
lea    0xecd(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe5e(%rip),%rdi
call   1050 <perror@plt>
jmp    11b8 <semdest+0x78>

LLVM IR (-O0):
@IPC_RMID = global i32 0
@errno = global i32 0
@.str = [46 x i8] c"EUID does not match the owners/creators UID!\0A\00"
@.str.1 = [27 x i8] c"Insufficient permissions!\0A\00"
@.str.2 = [30 x i8] c"Id has been removed removed!\0A\00"
@.str.3 = [48 x i8] c"[SEMCTL] Attempt to set value outside of range\0A\00"
@.str.4 = [23 x i8] c"False semaphor id: %d\0A\00"
@.str.5 = [9 x i8] c"[SEMCTL]\00"
define i32 @semdest(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = load i32, i32* @IPC_RMID
%6 = call i64 @semctl(i32 %4, i32 0, i32 %5, i32 0)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %24
8: ; preds = %1
%9 = load i32, i32* @errno
switch i32 %9, label %21 [
i32 129, label %10
i32 132, label %12
i32 131, label %14
i32 128, label %16
i32 130, label %18
]
10: ; preds = %8
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %23
12: ; preds = %8
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %23
14: ; preds = %8
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %23
16: ; preds = %8
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
br label %23
18: ; preds = %8
%19 = load i32, i32* %3
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %19)
br label %23
21: ; preds = %8
%22 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %23
23: ; preds = %21, %18, %16, %14, %12, %10
store i32 -1, i32* %2
br label %25
24: ; preds = %1
store i32 0, i32* %2
br label %25
25: ; preds = %24, %23
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@IPC_RMID = global i32 0
@errno = global i32 0
@.str.4 = [23 x i8] c"False semaphor id: %d\0A\00"
@.str.5 = [9 x i8] c"[SEMCTL]\00"
@str = [47 x i8] c"[SEMCTL] Attempt to set value outside of range\00"
@str.6 = [29 x i8] c"Id has been removed removed!\00"
@str.7 = [26 x i8] c"Insufficient permissions!\00"
@str.8 = [45 x i8] c"EUID does not match the owners/creators UID!\00"
define i32 @semdest(i32 %0) {
%2 = load i32, i32* @IPC_RMID
%3 = tail call i64 @semctl(i32 %0, i32 0, i32 %2, i32 0)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %19
5: ; preds = %1
%6 = load i32, i32* @errno
switch i32 %6, label %17 [
i32 129, label %7
i32 132, label %9
i32 131, label %11
i32 128, label %13
i32 130, label %15
]
7: ; preds = %5
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.8, i64 0, i64 0))
br label %19
9: ; preds = %5
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
br label %19
11: ; preds = %5
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.6, i64 0, i64 0))
br label %19
13: ; preds = %5
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
br label %19
15: ; preds = %5
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %0)
br label %19
17: ; preds = %5
%18 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %19
19: ; preds = %1, %7, %9, %11, %13, %15, %17
%20 = phi i32 [ -1, %17 ], [ -1, %15 ], [ -1, %13 ], [ -1, %11 ], [ -1, %9 ], [ -1, %7 ], [ 0, %1 ]
ret i32 %20
}