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

/* Variables and functions */
#define EACCES 133
#define EEXIST 132
#define EINVAL 131
#define ENOENT 130
#define ENOMEM 129
#define ENOSPC 128
int errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int semget(int, int /*<<< orphan*/, int /*<<< orphan*/);
int takesem(key_t semkey) {
  int semid;
  if ((semid = semget(semkey, 0, 0)) < 0) {
    switch (errno) {
    case EACCES:
      printf("Insufficient permissions for existing semaphor\n");
      break;
    case EEXIST:
      printf("Semaphor with key %d exists!\n", semkey);
      break;
    case EINVAL:
      printf("[SEMGET] invalid parameter\n");
      break;
    case ENOENT:
      printf("No semaphor could be found\n");
      break;
    case ENOMEM:
      printf("System has not enough memory left!\n");
      break;
    case ENOSPC:
      printf("Systemlimit for semaphores has exceeded!\n");
      break;
    default:
      perror("[SEMGET]");
      break;
    }
  }
  return semid;
}


x86 assembly (clang, -O0):
<takesem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <semget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1208 <takesem+0xd8>
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x10(%rbp)
sub    $0x5,%eax
ja     11f7 <takesem+0xc7>
mov    -0x10(%rbp),%rax
lea    0xe87(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1203 <takesem+0xd3>
mov    -0x4(%rbp),%esi
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1203 <takesem+0xd3>
lea    0xeb4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1203 <takesem+0xd3>
lea    0xebd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1203 <takesem+0xd3>
lea    0xec6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1203 <takesem+0xd3>
lea    0xed7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1203 <takesem+0xd3>
lea    0xeee(%rip),%rdi
call   1050 <perror@plt>
jmp    1208 <takesem+0xd8>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<takesem>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %esi,%esi
xor    %edx,%edx
call   1030 <semget@plt>
mov    %eax,%ebx
test   %eax,%eax
js     115d <takesem+0x1d>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
cmp    $0x5,%eax
ja     11d9 <takesem+0x99>
lea    0xe8b(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xeba(%rip),%rdi
call   1040 <puts@plt>
jmp    1154 <takesem+0x14>
lea    0xed5(%rip),%rdi
call   1040 <puts@plt>
jmp    1154 <takesem+0x14>
lea    0xeea(%rip),%rdi
call   1040 <puts@plt>
jmp    1154 <takesem+0x14>
lea    0xef7(%rip),%rdi
call   1040 <puts@plt>
jmp    1154 <takesem+0x14>
lea    0xe5b(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1154 <takesem+0x14>
lea    0xef2(%rip),%rdi
call   1040 <puts@plt>
jmp    1154 <takesem+0x14>
lea    0xe56(%rip),%rdi
call   1060 <perror@plt>
jmp    1154 <takesem+0x14>

LLVM IR (-O0):
@errno = global i32 0
@.str = [48 x i8] c"Insufficient permissions for existing semaphor\0A\00"
@.str.1 = [30 x i8] c"Semaphor with key %d exists!\0A\00"
@.str.2 = [28 x i8] c"[SEMGET] invalid parameter\0A\00"
@.str.3 = [28 x i8] c"No semaphor could be found\0A\00"
@.str.4 = [36 x i8] c"System has not enough memory left!\0A\00"
@.str.5 = [42 x i8] c"Systemlimit for semaphores has exceeded!\0A\00"
@.str.6 = [9 x i8] c"[SEMGET]\00"
define i32 @takesem(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i32 @semget(i32 %4, i32 0, i32 0)
store i32 %5, i32* %3
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %25
7: ; preds = %1
%8 = load i32, i32* @errno
switch i32 %8, label %22 [
i32 133, label %9
i32 132, label %11
i32 131, label %14
i32 130, label %16
i32 129, label %18
i32 128, label %20
]
9: ; preds = %7
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
br label %24
11: ; preds = %7
%12 = load i32, i32* %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %24
14: ; preds = %7
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %24
16: ; preds = %7
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %24
18: ; preds = %7
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %24
20: ; preds = %7
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0))
br label %24
22: ; preds = %7
%23 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %24
24: ; preds = %22, %20, %18, %16, %14, %11, %9
br label %25
25: ; preds = %24, %1
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@errno = global i32 0
@.str.1 = [30 x i8] c"Semaphor with key %d exists!\0A\00"
@.str.6 = [9 x i8] c"[SEMGET]\00"
@str = [41 x i8] c"Systemlimit for semaphores has exceeded!\00"
@str.7 = [35 x i8] c"System has not enough memory left!\00"
@str.8 = [27 x i8] c"No semaphor could be found\00"
@str.9 = [27 x i8] c"[SEMGET] invalid parameter\00"
@str.10 = [47 x i8] c"Insufficient permissions for existing semaphor\00"
define i32 @takesem(i32 %0) {
%2 = tail call i32 @semget(i32 %0, i32 0, i32 0)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %20
4: ; preds = %1
%5 = load i32, i32* @errno
switch i32 %5, label %18 [
i32 133, label %6
i32 132, label %8
i32 131, label %10
i32 130, label %12
i32 129, label %14
i32 128, label %16
]
6: ; preds = %4
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.10, i64 0, i64 0))
br label %20
8: ; preds = %4
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %20
10: ; preds = %4
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.9, i64 0, i64 0))
br label %20
12: ; preds = %4
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
br label %20
14: ; preds = %4
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.7, i64 0, i64 0))
br label %20
16: ; preds = %4
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
br label %20
18: ; preds = %4
%19 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %20
20: ; preds = %6, %8, %10, %12, %14, %16, %18, %1
ret i32 %2
}