#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ *SEM_FAILED;
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sem_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int, int, int);
int /*<<< orphan*/ sem_unlink(char *);
int main(void) {
  sem_t *sem;
  sem = sem_open("/ipc-sem", O_RDWR | O_CREAT, 0700, 28);
  if (sem == SEM_FAILED) {
    puts("sem_open failed");
    return 1;
  }
  sem_close(sem);
  sem_unlink("/ipc-sem");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2e99(%rip),%rax
or     (%rax),%esi
lea    0xe98(%rip),%rdi
mov    $0x1c0,%edx
mov    $0x1c,%ecx
call   1040 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e52(%rip),%rcx
cmp    (%rcx),%rax
jne    11a7 <main+0x67>
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    11c3 <main+0x83>
mov    -0x10(%rbp),%rdi
call   1050 <sem_close@plt>
lea    0xe49(%rip),%rdi
call   1060 <sem_unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2e98(%rip),%rax
mov    0x2ea9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea6(%rip),%rdi
mov    $0x1c0,%edx
mov    $0x1c,%ecx
call   1040 <sem_open@plt>
mov    0x2e68(%rip),%rcx
cmp    (%rcx),%rax
je     118d <main+0x4d>
mov    %rax,%rdi
call   1050 <sem_close@plt>
lea    0xe7c(%rip),%rdi
call   1060 <sem_unlink@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/ipc-sem\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [16 x i8] c"sem_open failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
store i32 0, i32* %1
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %3, %4
%6 = call i32* @sem_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5, i32 448, i32 28)
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = load i32*, i32** @SEM_FAILED
%9 = icmp eq i32* %7, %8
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %16
12: ; preds = %0
%13 = load i32*, i32** %2
%14 = call i32 @sem_close(i32* %13)
%15 = call i32 @sem_unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %16
16: ; preds = %12, %10
%17 = load i32, i32* %1
ret i32 %17
}

LLVM IR (-O2):
@.str = [9 x i8] c"/ipc-sem\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [16 x i8] c"sem_open failed\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = tail call i32* @sem_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3, i32 448, i32 28)
%5 = load i32*, i32** @SEM_FAILED
%6 = icmp eq i32* %4, %5
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %12
9: ; preds = %0
%10 = tail call i32 @sem_close(i32* %4)
%11 = tail call i32 @sem_unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %9, %7
%13 = phi i32 [ 1, %7 ], [ 0, %9 ]
ret i32 %13
}