#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 */
struct sembuf {
  int sem_op;
  int /*<<< orphan*/ sem_flg;
  scalar_t__ sem_num;
};

/* Variables and functions */
int /*<<< orphan*/ IPC_NOWAIT;
int /*<<< orphan*/ IPC_PRIVATE;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int semget(int /*<<< orphan*/, int, int);
scalar_t__ semop(int, struct sembuf *, int);
int /*<<< orphan*/ system(char *);
int main(void) {
  int sem_id;
  int nsems = 1;
  int flags = 0666;
  struct sembuf buf;
  sem_id = semget(IPC_PRIVATE, nsems, flags);
  if (sem_id < 0) {
    perror("semget ");
    exit(1);
  }
  printf("successfully created a semaphore : %d\n", sem_id);
  buf.sem_num = 0;
  buf.sem_op = 1;
  buf.sem_flg = IPC_NOWAIT;
  if ((semop(sem_id, &buf, nsems)) < 0) {
    perror("semop");
    exit(1);
  }
  system("ipcs -s ");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
movl   $0x1b6,-0x10(%rbp)
mov    0x2e5c(%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    0xe4a(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%edx
lea    -0x20(%rbp),%rsi
call   1070 <semop@plt>
cmp    $0x0,%rax
jge    120d <main+0xad>
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe21(%rip),%rdi
call   1040 <system@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
mov    $0x1,%esi
mov    $0x1b6,%edx
call   1030 <semget@plt>
test   %eax,%eax
jns    118a <main+0x2a>
lea    0xe78(%rip),%rdi
jmp    11d2 <main+0x72>
mov    %eax,%ebx
lea    0xe75(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movq   $0x0,0x8(%rsp)
movl   $0x1,(%rsp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
test   %rax,%rax
jns    11e1 <main+0x81>
lea    0xe5d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe4d(%rip),%rdi
call   1040 <system@plt>
xor    %edi,%edi
call   1080 <exit@plt>

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

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