#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_CREAT;
int getpid();
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sem_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/, 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"
          "_"
          "1-2"
          "_%d",
          getpid());
  mysemp = sem_open(semname, O_CREAT, 0777, 1);
  if (mysemp != NULL) {
    puts("TEST PASSED");
    sem_close(mysemp);
    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    $0x1ff,%edx
mov    $0x1,%ecx
call   1050 <sem_open@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11e5 <main+0x85>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1060 <sem_close@plt>
lea    -0x50(%rbp),%rdi
call   1080 <sem_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <main+0x98>
lea    0xe31(%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   %rbx
sub    $0x38,%rsp
xor    %ebp,%ebp
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),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    $0x1ff,%edx
mov    $0x1,%ecx
call   1050 <sem_open@plt>
test   %rax,%rax
je     11c4 <main+0x64>
mov    %rax,%rbx
lea    0xe64(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <sem_close@plt>
mov    %rsp,%rdi
call   1080 <sem_unlink@plt>
jmp    11d5 <main+0x75>
lea    0xe52(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

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

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