#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 /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ S_IRUSR;
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 *, 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_dev_zero(int loop, const char *sem_name) {
  int fd;
  int i;
  int *ptr;
  sem_t *mutex;
  fd = open("/dev/zero", O_RDWR);
  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):
<incr_dev_zero>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    0xe31(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    0x2dd1(%rip),%rax
or     (%rax),%edx
mov    0x2df0(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    0x2d99(%rip),%rax
or     (%rax),%esi
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%ecx
call   1090 <sem_open@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   10c0 <sem_unlink@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setbuf@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
cmp    $0x0,%rax
jne    12bf <incr_dev_zero+0x10f>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12b8 <incr_dev_zero+0x108>
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    0xd6e(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1080 <sem_post@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1273 <incr_dev_zero+0xc3>
xor    %edi,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    130b <incr_dev_zero+0x15b>
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    0xd26(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1080 <sem_post@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12c6 <incr_dev_zero+0x116>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<incr_dev_zero>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %edi,%ebx
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
lea    0xe33(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
mov    0x2e15(%rip),%rax
mov    0x2dd6(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df3(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2de6(%rip),%rax
mov    0x2da7(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    $0x1,%ecx
call   1090 <sem_open@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   10c0 <sem_unlink@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1050 <setbuf@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
jne    1298 <incr_dev_zero+0xe8>
test   %ebx,%ebx
jle    12d6 <incr_dev_zero+0x126>
lea    0xda5(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
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   1080 <sem_post@plt>
add    $0xffffffff,%ebx
jne    1270 <incr_dev_zero+0xc0>
jmp    12d6 <incr_dev_zero+0x126>
test   %ebx,%ebx
jle    12d6 <incr_dev_zero+0x126>
lea    0xd72(%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   1080 <sem_post@plt>
add    $0xffffffff,%ebx
jne    12b0 <incr_dev_zero+0x100>
xor    %edi,%edi
call   10b0 <exit@plt>

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

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