#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 /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
          int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *sem_open(char const *, int, int, 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;
int /*<<< orphan*/ write(int, int *, int);
void incr2(int loop, const char *pathname, const char *sem_name) {
  int fd;
  int i;
  int zero = 0;
  int *ptr;
  sem_t *mutex;
  fd = open(pathname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  write(fd, &zero, sizeof(int));
  ptr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  close(fd);
  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):
<incr2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
or     (%rax),%esi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    0x2dc8(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
mov    0x2d85(%rip),%rax
or     (%rax),%edx
mov    0x2dac(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
mov    0x2d4d(%rip),%rax
or     (%rax),%esi
mov    0x2d34(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%ecx
call   10a0 <sem_open@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   10d0 <sem_unlink@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <setbuf@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
cmp    $0x0,%rax
jne    1303 <incr2+0x143>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12fc <incr2+0x13c>
mov    -0x38(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
mov    %esi,%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
lea    0xd20(%rip),%rdi
call   1070 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1090 <sem_post@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12b7 <incr2+0xf7>
xor    %edi,%edi
call   10c0 <exit@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    134f <incr2+0x18f>
mov    -0x38(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
mov    %esi,%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
lea    0xcd8(%rip),%rdi
call   1070 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1090 <sem_post@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    130a <incr2+0x14a>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<incr2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%rax
mov    %edi,%ebx
movl   $0x0,0x4(%rsp)
mov    0x2dee(%rip),%rcx
mov    0x2e0f(%rip),%r15
mov    (%r15),%esi
or     (%rcx),%esi
mov    0x2db3(%rip),%r13
mov    0x2dcc(%rip),%rcx
mov    (%rcx),%edx
or     0x0(%r13),%edx
mov    %rax,%rdi
call   10b0 <open@plt>
mov    %eax,%ebp
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <write@plt>
mov    0x2dcc(%rip),%rax
mov    0x2d85(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2daa(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1080 <close@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
or     (%r15),%esi
mov    0x0(%r13),%edx
mov    %r12,%rdi
mov    $0x1,%ecx
call   10a0 <sem_open@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   10d0 <sem_unlink@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <setbuf@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
jne    12d8 <incr2+0x118>
test   %ebx,%ebx
jle    1316 <incr2+0x156>
lea    0xd5d(%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   1070 <printf@plt>
mov    %r15,%rdi
call   1090 <sem_post@plt>
add    $0xffffffff,%ebx
jne    12b0 <incr2+0xf0>
jmp    1316 <incr2+0x156>
test   %ebx,%ebx
jle    1316 <incr2+0x156>
lea    0xd28(%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   1070 <printf@plt>
mov    %r15,%rdi
call   1090 <sem_post@plt>
add    $0xffffffff,%ebx
jne    12f0 <incr2+0x130>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@O_EXCL = 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 @incr2(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 0, i32* %9
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %12, i32 %15, i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = call i32 @write(i32 %20, i32* %9, i32 4)
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %22, %23
%25 = load i32, i32* @MAP_SHARED
%26 = load i32, i32* %7
%27 = call i32* @mmap(i32* null, i32 4, i32 %24, i32 %25, i32 %26, i32 0)
store i32* %27, i32** %10
%28 = load i32, i32* %7
%29 = call i32 @close(i32 %28)
%30 = load i8*, i8** %6
%31 = load i32, i32* @O_CREAT
%32 = load i32, i32* @O_EXCL
%33 = or i32 %31, %32
%34 = load i32, i32* @S_IRUSR
%35 = call i32* @sem_open(i8* %30, i32 %33, i32 %34, i32 1)
store i32* %35, i32** %11
%36 = load i8*, i8** %6
%37 = call i32 @sem_unlink(i8* %36)
%38 = load i32, i32* @stdout
%39 = call i32 @setbuf(i32 %38, i32* null)
%40 = call i64 (...) @fork()
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %61
42: ; preds = %3
store i32 0, i32* %8
br label %43
43: ; preds = %56, %42
%44 = load i32, i32* %8
%45 = load i32, i32* %4
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %59
47: ; preds = %43
%48 = load i32*, i32** %11
%49 = call i32 @sem_wait(i32* %48)
%50 = load i32*, i32** %10
%51 = load i32, i32* %50
%52 = add nsw i32 %51, 1
store i32 %52, i32* %50
%53 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %51)
%54 = load i32*, i32** %11
%55 = call i32 @sem_post(i32* %54)
br label %56
56: ; preds = %47
%57 = load i32, i32* %8
%58 = add nsw i32 %57, 1
store i32 %58, i32* %8
br label %43
59: ; preds = %43
%60 = call i32 @exit(i32 0)
unreachable
61: ; preds = %3
store i32 0, i32* %8
br label %62
62: ; preds = %75, %61
%63 = load i32, i32* %8
%64 = load i32, i32* %4
%65 = icmp slt i32 %63, %64
br i1 %65, label %66, label %78
66: ; preds = %62
%67 = load i32*, i32** %11
%68 = call i32 @sem_wait(i32* %67)
%69 = load i32*, i32** %10
%70 = load i32, i32* %69
%71 = add nsw i32 %70, 1
store i32 %71, i32* %69
%72 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %70)
%73 = load i32*, i32** %11
%74 = call i32 @sem_post(i32* %73)
br label %75
75: ; preds = %66
%76 = load i32, i32* %8
%77 = add nsw i32 %76, 1
store i32 %77, i32* %8
br label %62
78: ; preds = %62
%79 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@O_EXCL = 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 @incr2(i32 %0, i8* nocapture %1, i8* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %1, i32 %8, i32 %11)
%13 = call i32 @write(i32 %12, i32* %4, i32 4)
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %15, %14
%17 = load i32, i32* @MAP_SHARED
%18 = tail call i32* @mmap(i32* null, i32 4, i32 %16, i32 %17, i32 %12, i32 0)
%19 = tail call i32 @close(i32 %12)
%20 = load i32, i32* @O_CREAT
%21 = load i32, i32* @O_EXCL
%22 = or i32 %21, %20
%23 = load i32, i32* @S_IRUSR
%24 = tail call i32* @sem_open(i8* %2, i32 %22, i32 %23, i32 1)
%25 = tail call i32 @sem_unlink(i8* %2)
%26 = load i32, i32* @stdout
%27 = tail call i32 @setbuf(i32 %26, i32* null)
%28 = tail call i64 (...) @fork()
%29 = icmp eq i64 %28, 0
%30 = icmp sgt i32 %0, 0
br i1 %29, label %32, label %31
31: ; preds = %3
br i1 %30, label %44, label %53
32: ; preds = %3
br i1 %30, label %33, label %42
33: ; preds = %32, %33
%34 = phi i32 [ %40, %33 ], [ 0, %32 ]
%35 = tail call i32 @sem_wait(i32* %24)
%36 = load i32, i32* %18
%37 = add nsw i32 %36, 1
store i32 %37, i32* %18
%38 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %36)
%39 = tail call i32 @sem_post(i32* %24)
%40 = add nuw nsw i32 %34, 1
%41 = icmp eq i32 %40, %0
br i1 %41, label %42, label %33
42: ; preds = %33, %32
%43 = tail call i32 @exit(i32 0)
unreachable
44: ; preds = %31, %44
%45 = phi i32 [ %51, %44 ], [ 0, %31 ]
%46 = tail call i32 @sem_wait(i32* %24)
%47 = load i32, i32* %18
%48 = add nsw i32 %47, 1
store i32 %48, i32* %18
%49 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %47)
%50 = tail call i32 @sem_post(i32* %24)
%51 = add nuw nsw i32 %45, 1
%52 = icmp eq i32 %51, %0
br i1 %52, label %53, label %44
53: ; preds = %44, %31
%54 = tail call i32 @exit(i32 0)
unreachable
}