#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 MAP_ANON;
int /*<<< orphan*/ *MAP_FAILED;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
scalar_t__ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ sem_init(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ sem_post(int /*<<< orphan*/ *);
scalar_t__ sem_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int test_unnamed(void) {
  sem_t *s;
  pid_t pid;
  int status;
  printf("testing unnamed process-shared semaphore\n");
  s = (sem_t *)mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE,
                    MAP_ANON | MAP_SHARED, -1, 0);
  if (s == MAP_FAILED)
    err(1, "mmap failed");
  if (sem_init(s, 1, 0))
    err(2, "sem_init failed");
  if ((pid = fork()) == 0) {
    printf("child: sem_wait()\n");
    if (sem_wait(s))
      err(3, "sem_wait failed");
    printf("child: sem_wait() returned\n");
    exit(0);
  } else {
    sleep(1);
    printf("parent: sem_post()\n");
    if (sem_post(s))
      err(4, "sem_post failed");
    waitpid(pid, &status, 0);
    if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
      printf("OK.\n");
    else
      printf("Failure.");
  }
  return (0);
}


x86 assembly (clang, -O0):
<test_unnamed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe31(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
mov    0x2dec(%rip),%rax
or     (%rax),%edx
mov    0x2ddb(%rip),%rax
mov    (%rax),%ecx
mov    0x2df2(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2da2(%rip),%rcx
cmp    (%rcx),%rax
jne    1238 <test_unnamed+0x78>
mov    $0x1,%edi
lea    0xdf7(%rip),%rsi
call   1070 <err@plt>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1080 <sem_init@plt>
cmp    $0x0,%rax
je     1263 <test_unnamed+0xa3>
mov    $0x2,%edi
lea    0xdd8(%rip),%rsi
call   1070 <err@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jne    12b9 <test_unnamed+0xf9>
lea    0xdc9(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0x0,%rax
je     12a6 <test_unnamed+0xe6>
mov    $0x3,%edi
lea    0xdb8(%rip),%rsi
call   1070 <err@plt>
lea    0xdbc(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
lea    0xdbb(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1060 <sem_post@plt>
cmp    $0x0,%rax
je     12f3 <test_unnamed+0x133>
mov    $0x4,%edi
lea    0xdab(%rip),%rsi
call   1070 <err@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    -0x10(%rbp),%edi
call   10b0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1336 <test_unnamed+0x176>
mov    -0x10(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
cmp    $0x0,%rax
jne    1336 <test_unnamed+0x176>
lea    0xd7d(%rip),%rdi
call   1050 <printf@plt>
jmp    1342 <test_unnamed+0x182>
lea    0xd71(%rip),%rdi
call   1050 <printf@plt>
jmp    1347 <test_unnamed+0x187>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_unnamed>:
push   %rbp
push   %rbx
push   %rax
lea    0xe6b(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e12(%rip),%rax
mov    0x2de3(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd0(%rip),%rax
mov    0x2de9(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2d9e(%rip),%rax
cmp    %rbx,(%rax)
jne    1238 <test_unnamed+0x68>
lea    0xdd2(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1090 <sem_init@plt>
test   %rax,%rax
je     125d <test_unnamed+0x8d>
lea    0xdb9(%rip),%rsi
mov    $0x2,%edi
call   1080 <err@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     12ef <test_unnamed+0x11f>
mov    %eax,%ebp
mov    $0x1,%edi
call   10e0 <sleep@plt>
lea    0xdef(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1070 <sem_post@plt>
test   %rax,%rax
je     12a2 <test_unnamed+0xd2>
lea    0xd94(%rip),%rsi
mov    $0x4,%edi
call   1080 <err@plt>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   10b0 <waitpid@plt>
mov    0x4(%rsp),%edi
call   10c0 <WIFEXITED@plt>
test   %rax,%rax
je     12cc <test_unnamed+0xfc>
mov    0x4(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
test   %rax,%rax
je     12da <test_unnamed+0x10a>
lea    0xd69(%rip),%rdi
call   1060 <printf@plt>
jmp    12e6 <test_unnamed+0x116>
lea    0xda0(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd8f(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
test   %rax,%rax
je     1319 <test_unnamed+0x149>
lea    0xd0d(%rip),%rsi
mov    $0x3,%edi
call   1080 <err@plt>
lea    0xd77(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [42 x i8] c"testing unnamed process-shared semaphore\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.1 = [12 x i8] c"mmap failed\00"
@.str.2 = [16 x i8] c"sem_init failed\00"
@.str.3 = [19 x i8] c"child: sem_wait()\0A\00"
@.str.4 = [16 x i8] c"sem_wait failed\00"
@.str.5 = [28 x i8] c"child: sem_wait() returned\0A\00"
@.str.6 = [20 x i8] c"parent: sem_post()\0A\00"
@.str.7 = [16 x i8] c"sem_post failed\00"
@.str.8 = [5 x i8] c"OK.\0A\00"
@.str.9 = [9 x i8] c"Failure.\00"
define i32 @test_unnamed() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32
%4 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %5, %6
%8 = load i32, i32* @MAP_ANON
%9 = load i32, i32* @MAP_SHARED
%10 = or i32 %8, %9
%11 = call i64 @mmap(i32* null, i32 4, i32 %7, i32 %10, i32 -1, i32 0)
%12 = inttoptr i64 %11 to i32*
store i32* %12, i32** %1
%13 = load i32*, i32** %1
%14 = load i32*, i32** @MAP_FAILED
%15 = icmp eq i32* %13, %14
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %0
%19 = load i32*, i32** %1
%20 = call i64 @sem_init(i32* %19, i32 1, i32 0)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @err(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = call i32 (...) @fork()
store i32 %25, i32* %2
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %24
%28 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32*, i32** %1
%30 = call i64 @sem_wait(i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @err(i32 3, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
%35 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %24
%38 = call i32 @sleep(i32 1)
%39 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
%40 = load i32*, i32** %1
%41 = call i64 @sem_post(i32* %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 @err(i32 4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %45
45: ; preds = %43, %37
%46 = load i32, i32* %2
%47 = call i32 @waitpid(i32 %46, i32* %3, i32 0)
%48 = load i32, i32* %3
%49 = call i64 @WIFEXITED(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %45
%52 = load i32, i32* %3
%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 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %59
57: ; preds = %51, %45
%58 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
br label %59
59: ; preds = %57, %55
br label %60
60: ; preds = %59
ret i32 0
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.1 = [12 x i8] c"mmap failed\00"
@.str.2 = [16 x i8] c"sem_init failed\00"
@.str.4 = [16 x i8] c"sem_wait failed\00"
@.str.7 = [16 x i8] c"sem_post failed\00"
@.str.9 = [9 x i8] c"Failure.\00"
@str = [41 x i8] c"testing unnamed process-shared semaphore\00"
@str.10 = [19 x i8] c"parent: sem_post()\00"
@str.11 = [4 x i8] c"OK.\00"
@str.12 = [18 x i8] c"child: sem_wait()\00"
@str.13 = [27 x i8] c"child: sem_wait() returned\00"
define i32 @test_unnamed() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %5, %4
%7 = load i32, i32* @MAP_ANON
%8 = load i32, i32* @MAP_SHARED
%9 = or i32 %8, %7
%10 = tail call i64 @mmap(i32* null, i32 4, i32 %6, i32 %9, i32 -1, i32 0)
%11 = inttoptr i64 %10 to i32*
%12 = load i32*, i32** @MAP_FAILED
%13 = icmp eq i32* %12, %11
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = tail call i64 @sem_init(i32* %11, i32 1, i32 0)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = tail call i32 @err(i32 2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = tail call i32 (...) @fork()
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %21
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.12, i64 0, i64 0))
%26 = tail call i64 @sem_wait(i32* %11)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = tail call i32 @err(i32 3, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.13, i64 0, i64 0))
%32 = tail call i32 @exit(i32 0)
unreachable
33: ; preds = %21
%34 = tail call i32 @sleep(i32 1)
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.10, i64 0, i64 0))
%36 = tail call i64 @sem_post(i32* %11)
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %33
%39 = tail call i32 @err(i32 4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %40
40: ; preds = %38, %33
%41 = call i32 @waitpid(i32 %22, i32* %1, i32 0)
%42 = load i32, i32* %1
%43 = call i64 @WIFEXITED(i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %51, label %45
45: ; preds = %40
%46 = load i32, i32* %1
%47 = call i64 @WEXITSTATUS(i32 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.11, i64 0, i64 0))
br label %53
51: ; preds = %45, %40
%52 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
br label %53
53: ; preds = %49, %51
ret i32 0
}