#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 */
scalar_t__ EEXIST;
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ *SEM_FAILED;
scalar_t__ errno;
int getpid();
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sem_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int, int, int);
int /*<<< orphan*/ sem_unlink(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(void) {
  sem_t *mysemp;
  char semname[50];
  sprintf(semname,
          "/"
          "sem_open"
          "_"
          "4-1"
          "_%d",
          getpid());
  mysemp = sem_open(semname, O_CREAT, 0444, 1);
  sem_close(mysemp);
  mysemp = sem_open(semname, O_CREAT | O_EXCL, 0444, 1);
  if ((mysemp == SEM_FAILED) && (errno == EEXIST)) {
    puts("TEST PASSED");
    sem_unlink(semname);
    return 0;
  } else {
    puts("TEST FAILED");
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%edx
lea    0xe75(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    $0x124,%edx
mov    $0x1,%ecx
call   1050 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <sem_close@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    0x2dfb(%rip),%rax
or     (%rax),%esi
mov    $0x124,%edx
mov    $0x1,%ecx
call   1050 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2df3(%rip),%rcx
cmp    (%rcx),%rax
jne    1231 <main+0xd1>
mov    0x2dc3(%rip),%rax
mov    (%rax),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
jne    1231 <main+0xd1>
lea    0xdfa(%rip),%rdi
call   1030 <puts@plt>
lea    -0x50(%rbp),%rdi
call   1080 <sem_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    1244 <main+0xe4>
lea    0xde5(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe8a(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
mov    0x2e6e(%rip),%rbp
mov    0x0(%rbp),%esi
mov    $0x1,%r14d
mov    %rbx,%rdi
mov    $0x124,%edx
mov    $0x1,%ecx
call   1050 <sem_open@plt>
mov    %rax,%rdi
call   1060 <sem_close@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
or     0x0(%rbp),%esi
mov    %rbx,%rdi
mov    $0x124,%edx
mov    $0x1,%ecx
call   1050 <sem_open@plt>
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
jne    1206 <main+0xa6>
mov    0x2de2(%rip),%rax
mov    (%rax),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
jne    1206 <main+0xa6>
lea    0xe1d(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   1080 <sem_unlink@plt>
xor    %r14d,%r14d
jmp    1212 <main+0xb2>
lea    0xe10(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/sem_open_4-1_%d\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@SEM_FAILED = global i32* null
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [12 x i8] c"TEST PASSED\00"
@.str.2 = [12 x i8] c"TEST FAILED\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [50 x i8]
store i32 0, i32* %1
%4 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%5 = call i32 (...) @getpid()
%6 = call i32 @sprintf(i8* %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%8 = load i32, i32* @O_CREAT
%9 = call i32* @sem_open(i8* %7, i32 %8, i32 292, i32 1)
store i32* %9, i32** %2
%10 = load i32*, i32** %2
%11 = call i32 @sem_close(i32* %10)
%12 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_EXCL
%15 = or i32 %13, %14
%16 = call i32* @sem_open(i8* %12, i32 %15, i32 292, i32 1)
store i32* %16, i32** %2
%17 = load i32*, i32** %2
%18 = load i32*, i32** @SEM_FAILED
%19 = icmp eq i32* %17, %18
br i1 %19, label %20, label %28
20: ; preds = %0
%21 = load i64, i64* @errno
%22 = load i64, i64* @EEXIST
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%26 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%27 = call i32 @sem_unlink(i8* %26)
store i32 0, i32* %1
br label %30
28: ; preds = %20, %0
%29 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %30
30: ; preds = %28, %24
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@.str = [17 x i8] c"/sem_open_4-1_%d\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@SEM_FAILED = global i32* null
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [12 x i8] c"TEST PASSED\00"
@.str.2 = [12 x i8] c"TEST FAILED\00"
define i32 @main() {
%1 = alloca [50 x i8]
%2 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @getpid()
%4 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @O_CREAT
%6 = call i32* @sem_open(i8* %2, i32 %5, i32 292, i32 1)
%7 = call i32 @sem_close(i32* %6)
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_EXCL
%10 = or i32 %9, %8
%11 = call i32* @sem_open(i8* %2, i32 %10, i32 292, i32 1)
%12 = load i32*, i32** @SEM_FAILED
%13 = icmp eq i32* %11, %12
br i1 %13, label %14, label %21
14: ; preds = %0
%15 = load i64, i64* @errno
%16 = load i64, i64* @EEXIST
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @sem_unlink(i8* %2)
br label %23
21: ; preds = %14, %0
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
%24 = phi i32 [ 0, %18 ], [ 1, %21 ]
ret i32 %24
}