#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 /*<<< orphan*/ *SEM_FAILED;
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-1"
          "_%d",
          getpid());
  mysemp = sem_open(semname, O_CREAT, 0777, 1);
  if ((mysemp == SEM_FAILED) || (mysemp == NULL)) {
    puts("TEST FAILED");
    return 1;
  } else {
    puts("TEST PASSED");
    sem_close(mysemp);
    sem_unlink(semname);
    return 0;
  }
}


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)
mov    -0x10(%rbp),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
je     11cf <main+0x6f>
cmpq   $0x0,-0x10(%rbp)
jne    11e7 <main+0x87>
lea    0xe3b(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    120c <main+0xac>
lea    0xe2f(%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)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe8c(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%ebp
mov    %rbx,%rdi
mov    $0x1ff,%edx
mov    $0x1,%ecx
call   1050 <sem_open@plt>
test   %rax,%rax
je     11d5 <main+0x75>
mov    %rax,%rbx
mov    0x2e30(%rip),%rax
cmp    (%rax),%rbx
je     11d5 <main+0x75>
lea    0xe61(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <sem_close@plt>
mov    %rsp,%rdi
call   1080 <sem_unlink@plt>
xor    %ebp,%ebp
jmp    11e1 <main+0x81>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

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

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