#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)
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1060 <sem_close@plt>
lea    -0x50(%rbp),%rdi
call   1080 <sem_unlink@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x38,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe8b(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
mov    $0x1ff,%edx
mov    $0x1,%ecx
call   1050 <sem_open@plt>
mov    %rax,%rbx
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <sem_close@plt>
mov    %r14,%rdi
call   1080 <sem_unlink@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
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"
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 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32*, i32** %2
%12 = call i32 @sem_close(i32* %11)
%13 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%14 = call i32 @sem_unlink(i8* %13)
ret i32 0
}

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"
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 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 @sem_close(i32* %6)
%9 = call i32 @sem_unlink(i8* %2)
ret i32 0
}