#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 O_CREAT;
int O_EXCL;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ S_IRUSR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *sem_open(char const *, int, int /*<<< orphan*/, int);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_unlink(char const *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
void incr_anonymous(int loop, const char *sem_name) {
  int i;
  int *ptr;
  sem_t *mutex;
  ptr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON,
             -1, 0);
  mutex = sem_open(sem_name, O_CREAT | O_EXCL, S_IRUSR, 1);
  sem_unlink(sem_name);
  setbuf(stdout, NULL);
  if (fork() == 0) {
    for (i = 0; i < loop; i++) {
      sem_wait(mutex);
      printf("child: %d\n", (*ptr)++);
      sem_post(mutex);
    }
    exit(0);
  }
  for (i = 0; i < loop; i++) {
    sem_wait(mutex);
    printf("parent: %d\n", (*ptr)++);
    sem_post(mutex);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<incr_anonymous>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
mov    0x2e11(%rip),%rax
or     (%rax),%edx
mov    0x2e28(%rip),%rax
mov    (%rax),%ecx
mov    0x2de7(%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,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2dd6(%rip),%rax
or     (%rax),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%ecx
call   1080 <sem_open@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <sem_unlink@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setbuf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jne    128a <incr_anonymous+0xfa>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1283 <incr_anonymous+0xf3>
mov    -0x28(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    %esi,%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
lea    0xd99(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1070 <sem_post@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    123e <incr_anonymous+0xae>
xor    %edi,%edi
call   1090 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12d6 <incr_anonymous+0x146>
mov    -0x28(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    %esi,%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
lea    0xd51(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1070 <sem_post@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1291 <incr_anonymous+0x101>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<incr_anonymous>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %edi,%ebx
mov    0x2e4c(%rip),%rax
mov    0x2e15(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e2a(%rip),%rax
mov    0x2deb(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r14
mov    0x2e18(%rip),%rax
mov    0x2de1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    $0x1,%ecx
call   1080 <sem_open@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   10a0 <sem_unlink@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1050 <setbuf@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jne    1268 <incr_anonymous+0xd8>
test   %ebx,%ebx
jle    12a6 <incr_anonymous+0x116>
lea    0xdcd(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
call   1030 <sem_wait@plt>
mov    (%r14),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%r14)
mov    %r12,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1070 <sem_post@plt>
add    $0xffffffff,%ebx
jne    1240 <incr_anonymous+0xb0>
jmp    12a6 <incr_anonymous+0x116>
test   %ebx,%ebx
jle    12a6 <incr_anonymous+0x116>
lea    0xd98(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
call   1030 <sem_wait@plt>
mov    (%r14),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%r14)
mov    %r12,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1070 <sem_post@plt>
add    $0xffffffff,%ebx
jne    1280 <incr_anonymous+0xf0>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANON = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@stdout = global i32 0
@.str = [11 x i8] c"child: %d\0A\00"
@.str.1 = [12 x i8] c"parent: %d\0A\00"
define void @incr_anonymous(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
store i32 %0, i32* %3
store i8* %1, i8** %4
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %8, %9
%11 = load i32, i32* @MAP_SHARED
%12 = load i32, i32* @MAP_ANON
%13 = or i32 %11, %12
%14 = call i32* @mmap(i32* null, i32 4, i32 %10, i32 %13, i32 -1, i32 0)
store i32* %14, i32** %6
%15 = load i8*, i8** %4
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_EXCL
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRUSR
%20 = call i32* @sem_open(i8* %15, i32 %18, i32 %19, i32 1)
store i32* %20, i32** %7
%21 = load i8*, i8** %4
%22 = call i32 @sem_unlink(i8* %21)
%23 = load i32, i32* @stdout
%24 = call i32 @setbuf(i32 %23, i32* null)
%25 = call i64 (...) @fork()
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %46
27: ; preds = %2
store i32 0, i32* %5
br label %28
28: ; preds = %41, %27
%29 = load i32, i32* %5
%30 = load i32, i32* %3
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %44
32: ; preds = %28
%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 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %36)
%39 = load i32*, i32** %7
%40 = call i32 @sem_post(i32* %39)
br label %41
41: ; preds = %32
%42 = load i32, i32* %5
%43 = add nsw i32 %42, 1
store i32 %43, i32* %5
br label %28
44: ; preds = %28
%45 = call i32 @exit(i32 0)
unreachable
46: ; preds = %2
store i32 0, i32* %5
br label %47
47: ; preds = %60, %46
%48 = load i32, i32* %5
%49 = load i32, i32* %3
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %63
51: ; preds = %47
%52 = load i32*, i32** %7
%53 = call i32 @sem_wait(i32* %52)
%54 = load i32*, i32** %6
%55 = load i32, i32* %54
%56 = add nsw i32 %55, 1
store i32 %56, i32* %54
%57 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %55)
%58 = load i32*, i32** %7
%59 = call i32 @sem_post(i32* %58)
br label %60
60: ; preds = %51
%61 = load i32, i32* %5
%62 = add nsw i32 %61, 1
store i32 %62, i32* %5
br label %47
63: ; preds = %47
%64 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANON = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@stdout = global i32 0
@.str = [11 x i8] c"child: %d\0A\00"
@.str.1 = [12 x i8] c"parent: %d\0A\00"
define void @incr_anonymous(i32 %0, i8* %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 4, i32 %5, i32 %8, i32 -1, i32 0)
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_EXCL
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IRUSR
%14 = tail call i32* @sem_open(i8* %1, i32 %12, i32 %13, i32 1)
%15 = tail call i32 @sem_unlink(i8* %1)
%16 = load i32, i32* @stdout
%17 = tail call i32 @setbuf(i32 %16, i32* null)
%18 = tail call i64 (...) @fork()
%19 = icmp eq i64 %18, 0
%20 = icmp sgt i32 %0, 0
br i1 %19, label %22, label %21
21: ; preds = %2
br i1 %20, label %34, label %43
22: ; preds = %2
br i1 %20, label %23, label %32
23: ; preds = %22, %23
%24 = phi i32 [ %30, %23 ], [ 0, %22 ]
%25 = tail call i32 @sem_wait(i32* %14)
%26 = load i32, i32* %9
%27 = add nsw i32 %26, 1
store i32 %27, i32* %9
%28 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %26)
%29 = tail call i32 @sem_post(i32* %14)
%30 = add nuw nsw i32 %24, 1
%31 = icmp eq i32 %30, %0
br i1 %31, label %32, label %23
32: ; preds = %23, %22
%33 = tail call i32 @exit(i32 0)
unreachable
34: ; preds = %21, %34
%35 = phi i32 [ %41, %34 ], [ 0, %21 ]
%36 = tail call i32 @sem_wait(i32* %14)
%37 = load i32, i32* %9
%38 = add nsw i32 %37, 1
store i32 %38, i32* %9
%39 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%40 = tail call i32 @sem_post(i32* %14)
%41 = add nuw nsw i32 %35, 1
%42 = icmp eq i32 %41, %0
br i1 %42, label %43, label %34
43: ; preds = %34, %21
%44 = tail call i32 @exit(i32 0)
unreachable
}