#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 /*<<< orphan*/ IPC_PRIVATE;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int semget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ system(char *);
int main(void) {
  int sem_id;
  int nsems = 1;
  int flags = 0666;
  sem_id = semget(IPC_PRIVATE, nsems, flags);
  if (sem_id < 0) {
    perror("semget");
    exit(1);
  }
  printf("successfully created a semaphore: %d \n", sem_id);
  system("ipcs -s");
  if ((semctl(sem_id, 0, IPC_RMID)) < 0) {
    perror("semctl");
    exit(1);
  } else {
    printf("semaphore removed \n");
    system("ipcs -s ");
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
movl   $0x1b6,-0x10(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1030 <semget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11b4 <main+0x54>
lea    0xe5b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe62(%rip),%rdi
call   1040 <system@plt>
mov    -0x8(%rbp),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <semctl@plt>
cmp    $0x0,%rax
jge    1204 <main+0xa4>
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe38(%rip),%rdi
call   1040 <system@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
mov    $0x1,%esi
mov    $0x1b6,%edx
call   1030 <semget@plt>
test   %eax,%eax
jns    1196 <main+0x26>
lea    0xe6c(%rip),%rdi
jmp    11d2 <main+0x62>
mov    %eax,%ebx
lea    0xe68(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe7f(%rip),%rdi
call   1050 <system@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1080 <semctl@plt>
test   %rax,%rax
jns    11e1 <main+0x71>
lea    0xe64(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe5e(%rip),%rdi
call   1040 <puts@plt>
lea    0xe49(%rip),%rdi
call   1050 <system@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@IPC_PRIVATE = global i32 0
@.str = [7 x i8] c"semget\00"
@.str.1 = [39 x i8] c"successfully created a semaphore: %d \0A\00"
@.str.2 = [8 x i8] c"ipcs -s\00"
@IPC_RMID = global i32 0
@.str.3 = [7 x i8] c"semctl\00"
@.str.4 = [20 x i8] c"semaphore removed \0A\00"
@.str.5 = [9 x i8] c"ipcs -s \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %3
store i32 438, i32* %4
%5 = load i32, i32* @IPC_PRIVATE
%6 = load i32, i32* %3
%7 = load i32, i32* %4
%8 = call i32 @semget(i32 %5, i32 %6, i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* %2
%19 = load i32, i32* @IPC_RMID
%20 = call i64 @semctl(i32 %18, i32 0, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %14
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @system(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %28
28: ; preds = %25
%29 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@IPC_PRIVATE = global i32 0
@.str = [7 x i8] c"semget\00"
@.str.1 = [39 x i8] c"successfully created a semaphore: %d \0A\00"
@.str.2 = [8 x i8] c"ipcs -s\00"
@IPC_RMID = global i32 0
@.str.3 = [7 x i8] c"semctl\00"
@.str.5 = [9 x i8] c"ipcs -s \00"
@str = [19 x i8] c"semaphore removed \00"
define i32 @main() {
%1 = load i32, i32* @IPC_PRIVATE
%2 = tail call i32 @semget(i32 %1, i32 1, i32 438)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%9 = tail call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%10 = load i32, i32* @IPC_RMID
%11 = tail call i64 @semctl(i32 %2, i32 0, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %7
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%18 = tail call i32 @system(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%19 = tail call i32 @exit(i32 0)
unreachable
}