#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;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_CREAT;
int /*<<< orphan*/ *SEM_FAILED;
scalar_t__ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ printf(char *);
scalar_t__ sem_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/, int,
                             int /*<<< orphan*/);
scalar_t__ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_unlink(char *);
scalar_t__ sem_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int test_named(void) {
  sem_t *s, *s2;
  pid_t pid;
  int status;
  printf("testing named process-shared semaphore\n");
  sem_unlink("/semtst");
  s = sem_open("/semtst", O_CREAT, 0777, 0);
  if (s == SEM_FAILED)
    err(1, "sem_open failed");
  s2 = sem_open("/semtst", O_CREAT, 0777, 0);
  if (s2 == SEM_FAILED)
    err(2, "second sem_open call failed");
  if (s != s2)
    errx(3, "two sem_open calls for same semaphore do not return same address");
  if (sem_close(s2))
    err(4, "sem_close failed");
  if ((pid = fork()) == 0) {
    printf("child: sem_wait()\n");
    if (sem_wait(s))
      err(5, "sem_wait failed");
    printf("child: sem_wait() returned\n");
    exit(0);
  } else {
    sleep(1);
    printf("parent: sem_post()\n");
    if (sem_post(s))
      err(6, "sem_post failed");
    waitpid(pid, &status, 0);
    if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
      printf("OK.\n");
    else
      printf("Failure.");
  }
  if (sem_close(s))
    err(7, "sem_close failed");
  return (0);
}


x86 assembly (clang, -O0):
<test_named>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe11(%rip),%rdi
call   1050 <printf@plt>
lea    0xe2d(%rip),%rdi
call   10f0 <sem_unlink@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
lea    0xe18(%rip),%rdi
mov    $0x1ff,%edx
xor    %ecx,%ecx
call   1070 <sem_open@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2db5(%rip),%rcx
cmp    (%rcx),%rax
jne    1245 <test_named+0x65>
mov    $0x1,%edi
lea    0xdf0(%rip),%rsi
call   1080 <err@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
lea    0xdd3(%rip),%rdi
mov    $0x1ff,%edx
xor    %ecx,%ecx
call   1070 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d70(%rip),%rcx
cmp    (%rcx),%rax
jne    128a <test_named+0xaa>
mov    $0x2,%edi
lea    0xdbb(%rip),%rsi
call   1080 <err@plt>
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     12a9 <test_named+0xc9>
mov    $0x3,%edi
lea    0xdb8(%rip),%rsi
call   1040 <errx@plt>
mov    -0x10(%rbp),%rdi
call   10b0 <sem_close@plt>
cmp    $0x0,%rax
je     12cd <test_named+0xed>
mov    $0x4,%edi
lea    0xdd5(%rip),%rsi
call   1080 <err@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    1323 <test_named+0x143>
lea    0xdc7(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0x0,%rax
je     1310 <test_named+0x130>
mov    $0x5,%edi
lea    0xdb6(%rip),%rsi
call   1080 <err@plt>
lea    0xdba(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
lea    0xdb9(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1060 <sem_post@plt>
cmp    $0x0,%rax
je     135d <test_named+0x17d>
mov    $0x6,%edi
lea    0xda9(%rip),%rsi
call   1080 <err@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    -0x18(%rbp),%edi
call   10c0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     13a0 <test_named+0x1c0>
mov    -0x18(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
cmp    $0x0,%rax
jne    13a0 <test_named+0x1c0>
lea    0xd7b(%rip),%rdi
call   1050 <printf@plt>
jmp    13ac <test_named+0x1cc>
lea    0xd6f(%rip),%rdi
call   1050 <printf@plt>
jmp    13b1 <test_named+0x1d1>
mov    -0x8(%rbp),%rdi
call   10b0 <sem_close@plt>
cmp    $0x0,%rax
je     13d5 <test_named+0x1f5>
mov    $0x7,%edi
lea    0xccd(%rip),%rsi
call   1080 <err@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_named>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xeb0(%rip),%rdi
call   1040 <puts@plt>
lea    0xdf5(%rip),%rbx
mov    %rbx,%rdi
call   1100 <sem_unlink@plt>
mov    0x2dde(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
mov    $0x1ff,%edx
xor    %ecx,%ecx
call   1080 <sem_open@plt>
mov    %rax,%r14
mov    0x2daa(%rip),%rbx
cmp    (%rbx),%rax
jne    124c <test_named+0x5c>
lea    0xdc6(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
mov    0x0(%rbp),%esi
lea    0xdaa(%rip),%rdi
mov    $0x1ff,%edx
xor    %ecx,%ecx
call   1080 <sem_open@plt>
mov    %rax,%rbp
cmp    (%rbx),%rax
jne    127b <test_named+0x8b>
lea    0xda7(%rip),%rsi
mov    $0x2,%edi
call   1090 <err@plt>
cmp    %rbp,%r14
je     1291 <test_named+0xa1>
lea    0xdad(%rip),%rsi
mov    $0x3,%edi
call   1050 <errx@plt>
mov    %rbp,%rdi
call   10c0 <sem_close@plt>
test   %rax,%rax
je     12af <test_named+0xbf>
lea    0xdd0(%rip),%rsi
mov    $0x4,%edi
call   1090 <err@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %eax,%eax
je     1361 <test_named+0x171>
mov    %eax,%ebp
mov    $0x1,%edi
call   10f0 <sleep@plt>
lea    0xe05(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1070 <sem_post@plt>
test   %rax,%rax
je     12f4 <test_named+0x104>
lea    0xdac(%rip),%rsi
mov    $0x6,%edi
call   1090 <err@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   10b0 <waitpid@plt>
mov    0xc(%rsp),%edi
call   10d0 <WIFEXITED@plt>
test   %rax,%rax
je     131e <test_named+0x12e>
mov    0xc(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
test   %rax,%rax
je     132c <test_named+0x13c>
lea    0xd81(%rip),%rdi
call   1060 <printf@plt>
jmp    1338 <test_named+0x148>
lea    0xdb6(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   10c0 <sem_close@plt>
test   %rax,%rax
je     1356 <test_named+0x166>
lea    0xd29(%rip),%rsi
mov    $0x7,%edi
call   1090 <err@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd85(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <sem_wait@plt>
test   %rax,%rax
je     138b <test_named+0x19b>
lea    0xd05(%rip),%rsi
mov    $0x5,%edi
call   1090 <err@plt>
lea    0xd6d(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [40 x i8] c"testing named process-shared semaphore\0A\00"
@.str.1 = [8 x i8] c"/semtst\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.2 = [16 x i8] c"sem_open failed\00"
@.str.3 = [28 x i8] c"second sem_open call failed\00"
@.str.4 = [65 x i8] c"two sem_open calls for same semaphore do not return same address\00"
@.str.5 = [17 x i8] c"sem_close failed\00"
@.str.6 = [19 x i8] c"child: sem_wait()\0A\00"
@.str.7 = [16 x i8] c"sem_wait failed\00"
@.str.8 = [28 x i8] c"child: sem_wait() returned\0A\00"
@.str.9 = [20 x i8] c"parent: sem_post()\0A\00"
@.str.10 = [16 x i8] c"sem_post failed\00"
@.str.11 = [5 x i8] c"OK.\0A\00"
@.str.12 = [9 x i8] c"Failure.\00"
define i32 @test_named() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @sem_unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @O_CREAT
%8 = call i32* @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 511, i32 0)
store i32* %8, i32** %1
%9 = load i32*, i32** %1
%10 = load i32*, i32** @SEM_FAILED
%11 = icmp eq i32* %9, %10
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %0
%15 = load i32, i32* @O_CREAT
%16 = call i32* @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 511, i32 0)
store i32* %16, i32** %2
%17 = load i32*, i32** %2
%18 = load i32*, i32** @SEM_FAILED
%19 = icmp eq i32* %17, %18
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @err(i32 2, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %14
%23 = load i32*, i32** %1
%24 = load i32*, i32** %2
%25 = icmp ne i32* %23, %24
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @errx(i32 3, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
%29 = load i32*, i32** %2
%30 = call i64 @sem_close(i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @err(i32 4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = call i32 (...) @fork()
store i32 %35, i32* %3
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %47
37: ; preds = %34
%38 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%39 = load i32*, i32** %1
%40 = call i64 @sem_wait(i32* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @err(i32 5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %44
44: ; preds = %42, %37
%45 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
%46 = call i32 @exit(i32 0)
unreachable
47: ; preds = %34
%48 = call i32 @sleep(i32 1)
%49 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0))
%50 = load i32*, i32** %1
%51 = call i64 @sem_post(i32* %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @err(i32 6, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0))
br label %55
55: ; preds = %53, %47
%56 = load i32, i32* %3
%57 = call i32 @waitpid(i32 %56, i32* %4, i32 0)
%58 = load i32, i32* %4
%59 = call i64 @WIFEXITED(i32 %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %67
61: ; preds = %55
%62 = load i32, i32* %4
%63 = call i64 @WEXITSTATUS(i32 %62)
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %61
%66 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.11, i64 0, i64 0))
br label %69
67: ; preds = %61, %55
%68 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.12, i64 0, i64 0))
br label %69
69: ; preds = %67, %65
br label %70
70: ; preds = %69
%71 = load i32*, i32** %1
%72 = call i64 @sem_close(i32* %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %70
%75 = call i32 @err(i32 7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %76
76: ; preds = %74, %70
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"/semtst\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.2 = [16 x i8] c"sem_open failed\00"
@.str.3 = [28 x i8] c"second sem_open call failed\00"
@.str.4 = [65 x i8] c"two sem_open calls for same semaphore do not return same address\00"
@.str.5 = [17 x i8] c"sem_close failed\00"
@.str.7 = [16 x i8] c"sem_wait failed\00"
@.str.10 = [16 x i8] c"sem_post failed\00"
@.str.12 = [9 x i8] c"Failure.\00"
@str = [39 x i8] c"testing named process-shared semaphore\00"
@str.13 = [19 x i8] c"parent: sem_post()\00"
@str.14 = [4 x i8] c"OK.\00"
@str.15 = [18 x i8] c"child: sem_wait()\00"
@str.16 = [27 x i8] c"child: sem_wait() returned\00"
define i32 @test_named() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @sem_unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%5 = load i32, i32* @O_CREAT
%6 = tail call 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 = tail call i32* @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 511, i32 0)
%14 = load i32*, i32** @SEM_FAILED
%15 = icmp eq i32* %13, %14
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 @err(i32 2, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
%19 = icmp eq i32* %6, %13
br i1 %19, label %22, label %20
20: ; preds = %18
%21 = tail call i32 @errx(i32 3, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %18
%23 = tail call i64 @sem_close(i32* %13)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = tail call i32 @err(i32 4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
%28 = tail call i32 (...) @fork()
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %27
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.15, i64 0, i64 0))
%32 = tail call i64 @sem_wait(i32* %6)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %30
%35 = tail call i32 @err(i32 5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.16, i64 0, i64 0))
%38 = tail call i32 @exit(i32 0)
unreachable
39: ; preds = %27
%40 = tail call i32 @sleep(i32 1)
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.13, i64 0, i64 0))
%42 = tail call i64 @sem_post(i32* %6)
%43 = icmp eq i64 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %39
%45 = tail call i32 @err(i32 6, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0))
br label %46
46: ; preds = %44, %39
%47 = call i32 @waitpid(i32 %28, i32* %1, i32 0)
%48 = load i32, i32* %1
%49 = call i64 @WIFEXITED(i32 %48)
%50 = icmp eq i64 %49, 0
br i1 %50, label %57, label %51
51: ; preds = %46
%52 = load i32, i32* %1
%53 = call i64 @WEXITSTATUS(i32 %52)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %51
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.14, i64 0, i64 0))
br label %59
57: ; preds = %51, %46
%58 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.12, i64 0, i64 0))
br label %59
59: ; preds = %55, %57
%60 = call i64 @sem_close(i32* %6)
%61 = icmp eq i64 %60, 0
br i1 %61, label %64, label %62
62: ; preds = %59
%63 = call i32 @err(i32 7, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %64
64: ; preds = %62, %59
ret i32 0
}