#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 MAP_ANON;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
scalar_t__ fork();
int *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int sem_init(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char const *argv[]) {
  int *ptr = mmap(NULL, sizeof(int) + sizeof(sem_t), PROT_READ | PROT_WRITE,
                  MAP_SHARED | MAP_ANON, -1, 0);
  *ptr = 0;
  sem_t *sem = (sem_t *)ptr + 1;
  if (sem_init(sem, -1, 1) == -1) {
    perror("sem_init");
    return -1;
  }
  if (fork() == 0) {
    int i = 0;
    for (i = 0; i < 2000000; i++) {
      sem_wait(sem);
      (*ptr)++;
      sem_post(sem);
    }
    return 0;
  }
  int i = 0;
  for (i = 0; i < 2000000; i++) {
    sem_wait(sem);
    (*ptr)--;
    sem_post(sem);
  }
  wait(NULL);
  printf("%d\n", *ptr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
mov    0x2e2a(%rip),%rax
or     (%rax),%edx
mov    0x2e39(%rip),%rax
mov    (%rax),%ecx
mov    0x2e10(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x8,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0xffffffff,%esi
mov    $0x1,%edx
call   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    121f <main+0x9f>
lea    0xdf2(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <main+0x16a>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    1282 <main+0x102>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x1e8480,-0x24(%rbp)
jge    1276 <main+0xf6>
mov    -0x20(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1060 <sem_post@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    123e <main+0xbe>
movl   $0x0,-0x4(%rbp)
jmp    12ea <main+0x16a>
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x1e8480,-0x28(%rbp)
jge    12c8 <main+0x148>
mov    -0x20(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1060 <sem_post@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1290 <main+0x110>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xd2b(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e6a(%rip),%rax
mov    0x2e3b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e48(%rip),%rax
mov    0x2e21(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    $0xffffffff,%r15d
xor    %edi,%edi
mov    $0x8,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r14
movl   $0x0,(%rax)
lea    0x4(%rax),%rbx
mov    %rbx,%rdi
mov    $0xffffffff,%esi
mov    $0x1,%edx
call   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
je     126a <main+0xea>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    $0x1e8480,%ebp
test   %rax,%rax
je     1240 <main+0xc0>
nop
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
addl   $0xffffffff,(%r14)
mov    %rbx,%rdi
call   1060 <sem_post@plt>
add    $0xffffffff,%ebp
jne    1200 <main+0x80>
xor    %r15d,%r15d
xor    %edi,%edi
call   1090 <wait@plt>
mov    (%r14),%esi
lea    0xddc(%rip),%rdi
call   1050 <printf@plt>
jmp    125c <main+0xdc>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
addl   $0x1,(%r14)
mov    %rbx,%rdi
call   1060 <sem_post@plt>
add    $0xffffffff,%ebp
jne    1240 <main+0xc0>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8f(%rip),%rdi
call   1080 <perror@plt>
jmp    125c <main+0xdc>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANON = global i32 0
@.str = [9 x i8] c"sem_init\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %10, %11
%13 = load i32, i32* @MAP_SHARED
%14 = load i32, i32* @MAP_ANON
%15 = or i32 %13, %14
%16 = call i32* @mmap(i32* null, i32 8, i32 %12, i32 %15, i32 -1, i32 0)
store i32* %16, i32** %6
%17 = load i32*, i32** %6
store i32 0, i32* %17
%18 = load i32*, i32** %6
%19 = getelementptr inbounds i32, i32* %18, i64 1
store i32* %19, i32** %7
%20 = load i32*, i32** %7
%21 = call i32 @sem_init(i32* %20, i32 -1, i32 1)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %64
25: ; preds = %2
%26 = call i64 (...) @fork()
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %44
28: ; preds = %25
store i32 0, i32* %8
store i32 0, i32* %8
br label %29
29: ; preds = %40, %28
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 2000000
br i1 %31, label %32, label %43
32: ; preds = %29
%33 = load i32*, i32** %7
%34 = call i32 @sem_wait(i32* %33)
%35 = load i32*, i32** %6
%36 = load i32, i32* %35
%37 = add nsw i32 %36, 1
store i32 %37, i32* %35
%38 = load i32*, i32** %7
%39 = call i32 @sem_post(i32* %38)
br label %40
40: ; preds = %32
%41 = load i32, i32* %8
%42 = add nsw i32 %41, 1
store i32 %42, i32* %8
br label %29
43: ; preds = %29
store i32 0, i32* %3
br label %64
44: ; preds = %25
store i32 0, i32* %9
store i32 0, i32* %9
br label %45
45: ; preds = %56, %44
%46 = load i32, i32* %9
%47 = icmp slt i32 %46, 2000000
br i1 %47, label %48, label %59
48: ; preds = %45
%49 = load i32*, i32** %7
%50 = call i32 @sem_wait(i32* %49)
%51 = load i32*, i32** %6
%52 = load i32, i32* %51
%53 = add nsw i32 %52, -1
store i32 %53, i32* %51
%54 = load i32*, i32** %7
%55 = call i32 @sem_post(i32* %54)
br label %56
56: ; preds = %48
%57 = load i32, i32* %9
%58 = add nsw i32 %57, 1
store i32 %58, i32* %9
br label %45
59: ; preds = %45
%60 = call i32 @wait(i32* null)
%61 = load i32*, i32** %6
%62 = load i32, i32* %61
%63 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %62)
store i32 0, i32* %3
br label %64
64: ; preds = %59, %43, %23
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANON = global i32 0
@.str = [9 x i8] c"sem_init\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @PROT_WRITE
%5 = or i32 %4, %3
%6 = load i32, i32* @MAP_SHARED
%7 = load i32, i32* @MAP_ANON
%8 = or i32 %7, %6
%9 = tail call i32* @mmap(i32* null, i32 8, i32 %5, i32 %8, i32 -1, i32 0)
store i32 0, i32* %9
%10 = getelementptr inbounds i32, i32* %9, i64 1
%11 = tail call i32 @sem_init(i32* %10, i32 -1, i32 1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %38
15: ; preds = %2
%16 = tail call i64 (...) @fork()
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %15, %18
%19 = phi i32 [ %24, %18 ], [ 0, %15 ]
%20 = tail call i32 @sem_wait(i32* %10)
%21 = load i32, i32* %9
%22 = add nsw i32 %21, 1
store i32 %22, i32* %9
%23 = tail call i32 @sem_post(i32* %10)
%24 = add nuw nsw i32 %19, 1
%25 = icmp eq i32 %24, 2000000
br i1 %25, label %38, label %18
26: ; preds = %15, %26
%27 = phi i32 [ %32, %26 ], [ 0, %15 ]
%28 = tail call i32 @sem_wait(i32* %10)
%29 = load i32, i32* %9
%30 = add nsw i32 %29, -1
store i32 %30, i32* %9
%31 = tail call i32 @sem_post(i32* %10)
%32 = add nuw nsw i32 %27, 1
%33 = icmp eq i32 %32, 2000000
br i1 %33, label %34, label %26
34: ; preds = %26
%35 = tail call i32 @wait(i32* null)
%36 = load i32, i32* %9
%37 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %18, %34, %13
%39 = phi i32 [ -1, %13 ], [ 0, %34 ], [ 0, %18 ]
ret i32 %39
}