#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__ ENOENT;
int /*<<< orphan*/ *SEM_FAILED;
scalar_t__ errno;
int getpid();
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sem_unlink(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(void) {
  sem_t *mysemp;
  char semname[50];
  sprintf(semname,
          "/"
          "sem_open"
          "_"
          "6-1"
          "_%d",
          getpid());
  mysemp = sem_open(semname, 0);
  if ((mysemp == SEM_FAILED) && (errno == ENOENT)) {
    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    0xe85(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
call   1050 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    11de <main+0x8e>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rax
mov    0x2e14(%rip),%rcx
cmp    (%rcx),%rax
jne    11de <main+0x8e>
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
lea    -0x50(%rbp),%rdi
call   1070 <sem_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f1 <main+0xa1>
lea    0xe38(%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   %rbx
sub    $0x40,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe9d(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1060 <sprintf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <sem_open@plt>
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%rax
jne    11b4 <main+0x64>
mov    0x2e4b(%rip),%rax
mov    (%rax),%rax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%rax
jne    11b4 <main+0x64>
lea    0xe6e(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   1070 <sem_unlink@plt>
xor    %eax,%eax
jmp    11c5 <main+0x75>
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/sem_open_6-1_%d\00"
@SEM_FAILED = global i32* null
@errno = global i64 0
@ENOENT = 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 = call i32* @sem_open(i8* %7, i32 0)
store i32* %8, i32** %2
%9 = load i32*, i32** %2
%10 = load i32*, i32** @SEM_FAILED
%11 = icmp eq i32* %9, %10
br i1 %11, label %12, label %20
12: ; preds = %0
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENOENT
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%18 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%19 = call i32 @sem_unlink(i8* %18)
store i32 0, i32* %1
br label %22
20: ; preds = %12, %0
%21 = 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 %22
22: ; preds = %20, %16
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [17 x i8] c"/sem_open_6-1_%d\00"
@SEM_FAILED = global i32* null
@errno = global i64 0
@ENOENT = 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 = call i32* @sem_open(i8* %2, i32 0)
%6 = load i32*, i32** @SEM_FAILED
%7 = icmp eq i32* %5, %6
br i1 %7, label %8, label %15
8: ; preds = %0
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @sem_unlink(i8* %2)
br label %17
15: ; preds = %8, %0
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = phi i32 [ 0, %12 ], [ 1, %15 ]
ret i32 %18
}