#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__ EEXIST;
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ *SEM_FAILED;
int /*<<< orphan*/ err(int, char *);
scalar_t__ errno;
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ sem_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int, ...);
int /*<<< orphan*/ sem_unlink(char *);
int test_named2(void) {
  sem_t *s, *s2, *s3;
  printf("testing named process-shared semaphore, O_EXCL cases\n");
  sem_unlink("/semtst");
  s = sem_open("/semtst", O_CREAT | O_EXCL, 0777, 0);
  if (s == SEM_FAILED)
    err(1, "sem_open failed");
  s2 = sem_open("/semtst", O_CREAT | O_EXCL, 0777, 0);
  if (s2 != SEM_FAILED)
    errx(2, "second sem_open call wrongly succeeded");
  if (errno != EEXIST)
    err(3, "second sem_open call failed with wrong errno");
  s3 = sem_open("/semtst", 0);
  if (s3 == SEM_FAILED)
    err(4, "third sem_open call failed");
  if (s != s3)
    errx(5, "two sem_open calls for same semaphore do not return same address");
  if (sem_close(s3))
    err(6, "sem_close failed");
  if (sem_close(s))
    err(7, "sem_close failed");
  printf("OK.\n");
  return (0);
}


x86 assembly (clang, -O0):
<test_named2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe91(%rip),%rdi
call   1040 <printf@plt>
lea    0xebb(%rip),%rdi
call   1080 <sem_unlink@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
lea    0xe9d(%rip),%rdi
mov    $0x1ff,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1050 <sem_open@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
jne    11d0 <test_named2+0x70>
mov    $0x1,%edi
lea    0xe73(%rip),%rsi
call   1060 <err@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    0x2de8(%rip),%rax
or     (%rax),%esi
lea    0xe4d(%rip),%rdi
mov    $0x1ff,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1050 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2dda(%rip),%rcx
cmp    (%rcx),%rax
je     1220 <test_named2+0xc0>
mov    $0x2,%edi
lea    0xe33(%rip),%rsi
call   1030 <errx@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%rax
mov    0x2d9f(%rip),%rcx
cmp    (%rcx),%rax
je     124b <test_named2+0xeb>
mov    $0x3,%edi
lea    0xe2f(%rip),%rsi
call   1060 <err@plt>
lea    0xde4(%rip),%rdi
xor    %esi,%esi
mov    $0x0,%al
call   1050 <sem_open@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d76(%rip),%rcx
cmp    (%rcx),%rax
jne    1284 <test_named2+0x124>
mov    $0x4,%edi
lea    0xe23(%rip),%rsi
call   1060 <err@plt>
mov    -0x8(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     12a3 <test_named2+0x143>
mov    $0x5,%edi
lea    0xe1f(%rip),%rsi
call   1030 <errx@plt>
mov    -0x18(%rbp),%rdi
call   1070 <sem_close@plt>
cmp    $0x0,%rax
je     12c7 <test_named2+0x167>
mov    $0x6,%edi
lea    0xe3c(%rip),%rsi
call   1060 <err@plt>
mov    -0x8(%rbp),%rdi
call   1070 <sem_close@plt>
cmp    $0x0,%rax
je     12eb <test_named2+0x18b>
mov    $0x7,%edi
lea    0xe18(%rip),%rsi
call   1060 <err@plt>
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_named2>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
lea    0xf69(%rip),%rdi
call   1030 <puts@plt>
lea    0xe84(%rip),%rbx
mov    %rbx,%rdi
call   1080 <sem_unlink@plt>
mov    0x2e6d(%rip),%r12
mov    0x2e36(%rip),%r13
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %rbx,%rdi
mov    $0x1ff,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1050 <sem_open@plt>
mov    %rax,%r14
mov    0x2e2b(%rip),%r15
cmp    (%r15),%rax
jne    11cb <test_named2+0x6b>
lea    0xe47(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    0x0(%r13),%esi
or     (%r12),%esi
lea    0xe26(%rip),%rdi
mov    $0x1ff,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1050 <sem_open@plt>
cmp    (%r15),%rax
je     11fe <test_named2+0x9e>
lea    0xe24(%rip),%rsi
mov    $0x2,%edi
call   1040 <errx@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%rax
mov    0x2dc1(%rip),%rcx
cmp    (%rcx),%rax
je     1225 <test_named2+0xc5>
lea    0xe24(%rip),%rsi
mov    $0x3,%edi
call   1060 <err@plt>
lea    0xdd4(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <sem_open@plt>
mov    %rax,%rbx
cmp    (%r15),%rax
je     12a7 <test_named2+0x147>
cmp    %rbx,%r14
je     1253 <test_named2+0xf3>
lea    0xe3e(%rip),%rsi
mov    $0x5,%edi
call   1040 <errx@plt>
mov    %rbx,%rdi
call   1070 <sem_close@plt>
test   %rax,%rax
je     1271 <test_named2+0x111>
lea    0xe61(%rip),%rsi
mov    $0x6,%edi
call   1060 <err@plt>
mov    %r14,%rdi
call   1070 <sem_close@plt>
test   %rax,%rax
je     128f <test_named2+0x12f>
lea    0xe43(%rip),%rsi
mov    $0x7,%edi
call   1060 <err@plt>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xdbe(%rip),%rsi
mov    $0x4,%edi
call   1060 <err@plt>
cmp    %rbx,%r14
jne    1242 <test_named2+0xe2>
jmp    1253 <test_named2+0xf3>

LLVM IR (-O0):
@.str = [54 x i8] c"testing named process-shared semaphore, O_EXCL cases\0A\00"
@.str.1 = [8 x i8] c"/semtst\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@SEM_FAILED = global i32* null
@.str.2 = [16 x i8] c"sem_open failed\00"
@.str.3 = [39 x i8] c"second sem_open call wrongly succeeded\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.4 = [45 x i8] c"second sem_open call failed with wrong errno\00"
@.str.5 = [27 x i8] c"third sem_open call failed\00"
@.str.6 = [65 x i8] c"two sem_open calls for same semaphore do not return same address\00"
@.str.7 = [17 x i8] c"sem_close failed\00"
@.str.8 = [5 x i8] c"OK.\0A\00"
define i32 @test_named2() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca i32*
%4 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @sem_unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_EXCL
%8 = or i32 %6, %7
%9 = call i32* (i8*, i32, ...) @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 511, i32 0)
store i32* %9, i32** %1
%10 = load i32*, i32** %1
%11 = load i32*, i32** @SEM_FAILED
%12 = icmp eq i32* %10, %11
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_EXCL
%18 = or i32 %16, %17
%19 = call i32* (i8*, i32, ...) @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 511, i32 0)
store i32* %19, i32** %2
%20 = load i32*, i32** %2
%21 = load i32*, i32** @SEM_FAILED
%22 = icmp ne i32* %20, %21
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 @errx(i32 2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %15
%26 = load i64, i64* @errno
%27 = load i64, i64* @EEXIST
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @err(i32 3, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
%32 = call i32* (i8*, i32, ...) @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32* %32, i32** %3
%33 = load i32*, i32** %3
%34 = load i32*, i32** @SEM_FAILED
%35 = icmp eq i32* %33, %34
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @err(i32 4, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %38
38: ; preds = %36, %31
%39 = load i32*, i32** %1
%40 = load i32*, i32** %3
%41 = icmp ne i32* %39, %40
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @errx(i32 5, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %42, %38
%45 = load i32*, i32** %3
%46 = call i64 @sem_close(i32* %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 @err(i32 6, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
br label %50
50: ; preds = %48, %44
%51 = load i32*, i32** %1
%52 = call i64 @sem_close(i32* %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 @err(i32 7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
br label %56
56: ; preds = %54, %50
%57 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"/semtst\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@SEM_FAILED = global i32* null
@.str.2 = [16 x i8] c"sem_open failed\00"
@.str.3 = [39 x i8] c"second sem_open call wrongly succeeded\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.4 = [45 x i8] c"second sem_open call failed with wrong errno\00"
@.str.5 = [27 x i8] c"third sem_open call failed\00"
@.str.6 = [65 x i8] c"two sem_open calls for same semaphore do not return same address\00"
@.str.7 = [17 x i8] c"sem_close failed\00"
@str = [53 x i8] c"testing named process-shared semaphore, O_EXCL cases\00"
@str.9 = [4 x i8] c"OK.\00"
define i32 @test_named2() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 @sem_unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_EXCL
%5 = or i32 %4, %3
%6 = tail call i32* (i8*, i32, ...) @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %5, i32 511, i32 0)
%7 = load i32*, i32** @SEM_FAILED
%8 = icmp eq i32* %6, %7
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = load i32, i32* @O_CREAT
%13 = load i32, i32* @O_EXCL
%14 = or i32 %13, %12
%15 = tail call i32* (i8*, i32, ...) @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 511, i32 0)
%16 = load i32*, i32** @SEM_FAILED
%17 = icmp eq i32* %15, %16
br i1 %17, label %20, label %18
18: ; preds = %11
%19 = tail call i32 @errx(i32 2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %20
20: ; preds = %18, %11
%21 = load i64, i64* @errno
%22 = load i64, i64* @EEXIST
%23 = icmp eq i64 %21, %22
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = tail call i32 @err(i32 3, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = tail call i32* (i8*, i32, ...) @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 0)
%28 = load i32*, i32** @SEM_FAILED
%29 = icmp eq i32* %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = tail call i32 @err(i32 4, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = icmp eq i32* %6, %27
br i1 %33, label %36, label %34
34: ; preds = %32
%35 = tail call i32 @errx(i32 5, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.6, i64 0, i64 0))
br label %36
36: ; preds = %34, %32
%37 = tail call i64 @sem_close(i32* %27)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = tail call i32 @err(i32 6, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
%42 = tail call i64 @sem_close(i32* %6)
%43 = icmp eq i64 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %41
%45 = tail call i32 @err(i32 7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
br label %46
46: ; preds = %44, %41
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.9, i64 0, i64 0))
ret i32 0
}