#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 /*<<< orphan*/ sem_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ sem_close(int /*<<< orphan*/ *);
scalar_t__ sem_destroy(int /*<<< orphan*/ *);
scalar_t__ sem_getvalue(int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ *sem_open(char const *, int /*<<< orphan*/);
scalar_t__ sem_unlink(char const *);
int sem_delete(const char *sem_name) {
  int rv = -1;
  sem_t *sp = sem_open(sem_name, O_RDWR);
  if (sp != NULL) {
    int value = -1;
    if (sem_getvalue(sp, &value) == 0 && value < 0) {
      printf("Processe(s) blocked on [%s]?\n", sem_name);
      rv = 1;
    } else if (sem_destroy(sp) == 0 && sem_unlink(sem_name) == 0)
      rv = 0;
    sem_close(sp);
  }
  return rv;
}


x86 assembly (clang, -O0):
<sem_delete>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1050 <sem_open@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1213 <sem_delete+0xb3>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1040 <sem_getvalue@plt>
cmp    $0x0,%rax
jne    11d8 <sem_delete+0x78>
cmpl   $0x0,-0x1c(%rbp)
jge    11d8 <sem_delete+0x78>
mov    -0x8(%rbp),%rsi
lea    0xe39(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0xc(%rbp)
jmp    120a <sem_delete+0xaa>
mov    -0x18(%rbp),%rdi
call   1080 <sem_destroy@plt>
cmp    $0x0,%rax
jne    1205 <sem_delete+0xa5>
mov    -0x8(%rbp),%rdi
call   1070 <sem_unlink@plt>
cmp    $0x0,%rax
jne    1205 <sem_delete+0xa5>
movl   $0x0,-0xc(%rbp)
jmp    120a <sem_delete+0xaa>
mov    -0x18(%rbp),%rdi
call   1060 <sem_close@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sem_delete>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
call   1050 <sem_open@plt>
test   %rax,%rax
je     11b8 <sem_delete+0x58>
mov    %rax,%rbx
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
call   1040 <sem_getvalue@plt>
test   %rax,%rax
jne    11bf <sem_delete+0x5f>
cmpl   $0x0,0xc(%rsp)
jns    11bf <sem_delete+0x5f>
lea    0xe57(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    $0x1,%ebp
jmp    11de <sem_delete+0x7e>
mov    $0xffffffff,%ebp
jmp    11e6 <sem_delete+0x86>
mov    %rbx,%rdi
call   1080 <sem_destroy@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11de <sem_delete+0x7e>
mov    %r14,%rdi
call   1070 <sem_unlink@plt>
neg    %rax
sbb    %ebp,%ebp
mov    %rbx,%rdi
call   1060 <sem_close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [30 x i8] c"Processe(s) blocked on [%s]?\0A\00"
define i32 @sem_delete(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %3
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDWR
%8 = call i32* @sem_open(i8* %6, i32 %7)
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %34
11: ; preds = %1
store i32 -1, i32* %5
%12 = load i32*, i32** %4
%13 = call i64 @sem_getvalue(i32* %12, i32* %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i8*, i8** %2
%20 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 1, i32* %3
br label %31
21: ; preds = %15, %11
%22 = load i32*, i32** %4
%23 = call i64 @sem_destroy(i32* %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i8*, i8** %2
%27 = call i64 @sem_unlink(i8* %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 0, i32* %3
br label %30
30: ; preds = %29, %25, %21
br label %31
31: ; preds = %30, %18
%32 = load i32*, i32** %4
%33 = call i32 @sem_close(i32* %32)
br label %34
34: ; preds = %31, %1
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [30 x i8] c"Processe(s) blocked on [%s]?\0A\00"
define i32 @sem_delete(i8* %0) {
%2 = alloca i32
%3 = load i32, i32* @O_RDWR
%4 = tail call i32* @sem_open(i8* %0, i32 %3)
%5 = icmp eq i32* %4, null
br i1 %5, label %25, label %6
6: ; preds = %1
%7 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
%8 = call i64 @sem_getvalue(i32* %4, i32* %2)
%9 = icmp eq i64 %8, 0
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
%12 = select i1 %9, i1 %11, i1 false
br i1 %12, label %13, label %15
13: ; preds = %6
%14 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %22
15: ; preds = %6
%16 = call i64 @sem_destroy(i32* %4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = call i64 @sem_unlink(i8* %0)
%20 = icmp ne i64 %19, 0
%21 = sext i1 %20 to i32
br label %22
22: ; preds = %18, %15, %13
%23 = phi i32 [ 1, %13 ], [ -1, %15 ], [ %21, %18 ]
%24 = call i32 @sem_close(i32* %4)
br label %25
25: ; preds = %22, %1
%26 = phi i32 [ %23, %22 ], [ -1, %1 ]
ret i32 %26
}