#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__ EINVAL;
int INT_MAX;
int /*<<< orphan*/ O_CREAT;
int /*<<< orphan*/ *SEM_FAILED;
int SEM_VALUE_MAX;
scalar_t__ errno;
int getpid();
int /*<<< orphan*/ puts(char *);
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];
  int counter = SEM_VALUE_MAX;
  if (counter >= INT_MAX) {
    return 0;
  }
  sprintf(semname,
          "/"
          "sem_open"
          "_"
          "5-1"
          "_%d",
          getpid());
  ++counter;
  mysemp = sem_open(semname, O_CREAT, 0444, counter);
  if ((mysemp == SEM_FAILED) && (errno == EINVAL)) {
    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)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%eax
jl     1189 <main+0x39>
movl   $0x0,-0x4(%rbp)
jmp    1233 <main+0xe3>
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%edx
lea    0xe5b(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
lea    -0x50(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    -0x54(%rbp),%ecx
mov    $0x124,%edx
call   1050 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rax
jne    1220 <main+0xd0>
mov    0x2dd4(%rip),%rax
mov    (%rax),%rax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%rax
jne    1220 <main+0xd0>
lea    0xe0b(%rip),%rdi
call   1030 <puts@plt>
lea    -0x50(%rbp),%rdi
call   1070 <sem_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    1233 <main+0xe3>
lea    0xdf6(%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   %r14
push   %rbx
sub    $0x38,%rsp
mov    0x2e72(%rip),%rax
mov    (%rax),%ebx
mov    0x2e71(%rip),%rcx
xor    %eax,%eax
cmp    (%rcx),%ebx
jge    11f2 <main+0xa2>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe81(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %eax,%edx
call   1060 <sprintf@plt>
add    $0x1,%ebx
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
mov    $0x124,%edx
mov    %ebx,%ecx
call   1050 <sem_open@plt>
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%rax
jne    11e1 <main+0x91>
mov    0x2e06(%rip),%rax
mov    (%rax),%rax
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%rax
jne    11e1 <main+0x91>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   1070 <sem_unlink@plt>
xor    %eax,%eax
jmp    11f2 <main+0xa2>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@SEM_VALUE_MAX = global i32 0
@INT_MAX = global i32 0
@.str = [17 x i8] c"/sem_open_5-1_%d\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@errno = global i64 0
@EINVAL = 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]
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @SEM_VALUE_MAX
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = load i32, i32* @INT_MAX
%8 = icmp sge i32 %6, %7
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 0, i32* %1
br label %33
10: ; preds = %0
%11 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%12 = call i32 (...) @getpid()
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 1
store i32 %15, i32* %4
%16 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%17 = load i32, i32* @O_CREAT
%18 = load i32, i32* %4
%19 = call i32* @sem_open(i8* %16, i32 %17, i32 292, i32 %18)
store i32* %19, i32** %2
%20 = load i32*, i32** %2
%21 = load i32*, i32** @SEM_FAILED
%22 = icmp eq i32* %20, %21
br i1 %22, label %23, label %31
23: ; preds = %10
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINVAL
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%29 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%30 = call i32 @sem_unlink(i8* %29)
store i32 0, i32* %1
br label %33
31: ; preds = %23, %10
%32 = 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 %33
33: ; preds = %31, %27, %9
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@SEM_VALUE_MAX = global i32 0
@INT_MAX = global i32 0
@.str = [17 x i8] c"/sem_open_5-1_%d\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@errno = global i64 0
@EINVAL = 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 = load i32, i32* @SEM_VALUE_MAX
%4 = load i32, i32* @INT_MAX
%5 = icmp slt i32 %3, %4
br i1 %5, label %6, label %23
6: ; preds = %0
%7 = tail call i32 (...) @getpid()
%8 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = add nsw i32 %3, 1
%10 = load i32, i32* @O_CREAT
%11 = call i32* @sem_open(i8* %2, i32 %10, i32 292, i32 %9)
%12 = load i32*, i32** @SEM_FAILED
%13 = icmp eq i32* %11, %12
br i1 %13, label %14, label %21
14: ; preds = %6
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINVAL
%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, %6
%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 = %0, %21, %18
%24 = phi i32 [ 0, %18 ], [ 1, %21 ], [ 0, %0 ]
ret i32 %24
}