#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_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ S_IRWXU;
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, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
scalar_t__ sem_init(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, int *, int);
int main(int argc, char **argv) {
  int fd, i;
  int nloop = 10, zero = 0;
  int *ptr;
  sem_t mutex;
  fd = open("log.txt", O_RDWR | O_CREAT, S_IRWXU);
  write(fd, &zero, sizeof(int));
  ptr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  close(fd);
  if (sem_init(&mutex, 1, 1) < 0) {
    perror("semaphore initilization");
    exit(0);
  }
  if (fork() == 0) {
    for (i = 0; i < nloop; i++) {
      sem_wait(&mutex);
      printf("child: %d\n", (*ptr)++);
      sem_post(&mutex);
    }
    exit(0);
  }
  for (i = 0; i < nloop; i++) {
    sem_wait(&mutex);
    printf("parent: %d\n", (*ptr)++);
    sem_post(&mutex);
  }
  exit(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)
movl   $0xa,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    0x2e14(%rip),%rax
or     (%rax),%esi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
lea    0xe0a(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
mov    0x2d99(%rip),%rax
or     (%rax),%edx
mov    0x2db8(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    -0x2c(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <sem_init@plt>
cmp    $0x0,%rax
jge    1278 <main+0xc8>
lea    0xd9c(%rip),%rdi
call   10b0 <perror@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
cmp    $0x0,%rax
jne    12dc <main+0x12c>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12d5 <main+0x125>
lea    -0x2c(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    %esi,%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
lea    0xd67(%rip),%rdi
call   1060 <printf@plt>
lea    -0x2c(%rbp),%rdi
call   1080 <sem_post@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1290 <main+0xe0>
xor    %edi,%edi
call   10c0 <exit@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1328 <main+0x178>
lea    -0x2c(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    %esi,%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
lea    0xd1f(%rip),%rdi
call   1060 <printf@plt>
lea    -0x2c(%rbp),%rdi
call   1080 <sem_post@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12e3 <main+0x133>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
mov    0x2e02(%rip),%rax
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
lea    0xe1f(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <write@plt>
mov    0x2df0(%rip),%rax
mov    0x2db1(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dce(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1070 <close@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1090 <sem_init@plt>
test   %rax,%rax
jns    125a <main+0xaa>
lea    0xdba(%rip),%rdi
call   10b0 <perror@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %rax,%rbp
mov    %rsp,%rdi
call   1030 <sem_wait@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
test   %rbp,%rbp
jne    1281 <main+0xd1>
lea    0xda1(%rip),%r14
jmp    1288 <main+0xd8>
lea    0xda3(%rip),%r14
mov    %r14,%rdi
call   1060 <printf@plt>
mov    %rsp,%r15
mov    %r15,%rdi
call   1080 <sem_post@plt>
mov    %r15,%rdi
call   1030 <sem_wait@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1080 <sem_post@plt>
mov    %r15,%rdi
call   1030 <sem_wait@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1080 <sem_post@plt>
mov    %r15,%rdi
call   1030 <sem_wait@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1080 <sem_post@plt>
mov    %r15,%rdi
call   1030 <sem_wait@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1080 <sem_post@plt>
mov    %r15,%rdi
call   1030 <sem_wait@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1080 <sem_post@plt>
mov    %r15,%rdi
call   1030 <sem_wait@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1080 <sem_post@plt>
mov    %r15,%rdi
call   1030 <sem_wait@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1080 <sem_post@plt>
mov    %r15,%rdi
call   1030 <sem_wait@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1080 <sem_post@plt>
mov    %r15,%rdi
call   1030 <sem_wait@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1080 <sem_post@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

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

LLVM IR (-O2):
@.str = [8 x i8] c"log.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [24 x i8] c"semaphore initilization\00"
@.str.2 = [11 x i8] c"child: %d\0A\00"
@.str.3 = [12 x i8] c"parent: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @S_IRWXU
%11 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10)
%12 = call i32 @write(i32 %11, i32* %3, i32 4)
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %14, %13
%16 = load i32, i32* @MAP_SHARED
%17 = tail call i32* @mmap(i32* null, i32 4, i32 %15, i32 %16, i32 %11, i32 0)
%18 = tail call i32 @close(i32 %11)
%19 = call i64 @sem_init(i32* %4, i32 1, i32 1)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %2
%25 = call i64 (...) @fork()
%26 = icmp eq i64 %25, 0
%27 = call i32 @sem_wait(i32* %4)
%28 = load i32, i32* %17
%29 = add nsw i32 %28, 1
store i32 %29, i32* %17
br i1 %26, label %79, label %30
30: ; preds = %24
%31 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%32 = call i32 @sem_post(i32* %4)
%33 = call i32 @sem_wait(i32* %4)
%34 = load i32, i32* %17
%35 = add nsw i32 %34, 1
store i32 %35, i32* %17
%36 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%37 = call i32 @sem_post(i32* %4)
%38 = call i32 @sem_wait(i32* %4)
%39 = load i32, i32* %17
%40 = add nsw i32 %39, 1
store i32 %40, i32* %17
%41 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%42 = call i32 @sem_post(i32* %4)
%43 = call i32 @sem_wait(i32* %4)
%44 = load i32, i32* %17
%45 = add nsw i32 %44, 1
store i32 %45, i32* %17
%46 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %44)
%47 = call i32 @sem_post(i32* %4)
%48 = call i32 @sem_wait(i32* %4)
%49 = load i32, i32* %17
%50 = add nsw i32 %49, 1
store i32 %50, i32* %17
%51 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %49)
%52 = call i32 @sem_post(i32* %4)
%53 = call i32 @sem_wait(i32* %4)
%54 = load i32, i32* %17
%55 = add nsw i32 %54, 1
store i32 %55, i32* %17
%56 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %54)
%57 = call i32 @sem_post(i32* %4)
%58 = call i32 @sem_wait(i32* %4)
%59 = load i32, i32* %17
%60 = add nsw i32 %59, 1
store i32 %60, i32* %17
%61 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %59)
%62 = call i32 @sem_post(i32* %4)
%63 = call i32 @sem_wait(i32* %4)
%64 = load i32, i32* %17
%65 = add nsw i32 %64, 1
store i32 %65, i32* %17
%66 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %64)
%67 = call i32 @sem_post(i32* %4)
%68 = call i32 @sem_wait(i32* %4)
%69 = load i32, i32* %17
%70 = add nsw i32 %69, 1
store i32 %70, i32* %17
%71 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %69)
%72 = call i32 @sem_post(i32* %4)
%73 = call i32 @sem_wait(i32* %4)
%74 = load i32, i32* %17
%75 = add nsw i32 %74, 1
store i32 %75, i32* %17
%76 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %74)
%77 = call i32 @sem_post(i32* %4)
%78 = call i32 @exit(i32 0)
unreachable
79: ; preds = %24
%80 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%81 = call i32 @sem_post(i32* %4)
%82 = call i32 @sem_wait(i32* %4)
%83 = load i32, i32* %17
%84 = add nsw i32 %83, 1
store i32 %84, i32* %17
%85 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %83)
%86 = call i32 @sem_post(i32* %4)
%87 = call i32 @sem_wait(i32* %4)
%88 = load i32, i32* %17
%89 = add nsw i32 %88, 1
store i32 %89, i32* %17
%90 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %88)
%91 = call i32 @sem_post(i32* %4)
%92 = call i32 @sem_wait(i32* %4)
%93 = load i32, i32* %17
%94 = add nsw i32 %93, 1
store i32 %94, i32* %17
%95 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %93)
%96 = call i32 @sem_post(i32* %4)
%97 = call i32 @sem_wait(i32* %4)
%98 = load i32, i32* %17
%99 = add nsw i32 %98, 1
store i32 %99, i32* %17
%100 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %98)
%101 = call i32 @sem_post(i32* %4)
%102 = call i32 @sem_wait(i32* %4)
%103 = load i32, i32* %17
%104 = add nsw i32 %103, 1
store i32 %104, i32* %17
%105 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %103)
%106 = call i32 @sem_post(i32* %4)
%107 = call i32 @sem_wait(i32* %4)
%108 = load i32, i32* %17
%109 = add nsw i32 %108, 1
store i32 %109, i32* %17
%110 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %108)
%111 = call i32 @sem_post(i32* %4)
%112 = call i32 @sem_wait(i32* %4)
%113 = load i32, i32* %17
%114 = add nsw i32 %113, 1
store i32 %114, i32* %17
%115 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %113)
%116 = call i32 @sem_post(i32* %4)
%117 = call i32 @sem_wait(i32* %4)
%118 = load i32, i32* %17
%119 = add nsw i32 %118, 1
store i32 %119, i32* %17
%120 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %118)
%121 = call i32 @sem_post(i32* %4)
%122 = call i32 @sem_wait(i32* %4)
%123 = load i32, i32* %17
%124 = add nsw i32 %123, 1
store i32 %124, i32* %17
%125 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %123)
%126 = call i32 @sem_post(i32* %4)
%127 = call i32 @exit(i32 0)
unreachable
}