#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*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, int *, int);
int main(int argc, char **argv) {
  int fd, i, nloop = 10, zero = 5, *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 (fork() == 0) {
    for (i = 0; i < nloop; i++) {
      printf("child: %d\n", (*ptr)++);
    }
    exit(0);
  }
  for (i = 0; i < nloop; i++) {
    printf("parent: %d\n", (*ptr)++);
  }
  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   $0x5,-0x20(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2e54(%rip),%rax
or     (%rax),%esi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
lea    0xe4a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
mov    0x2dd9(%rip),%rax
or     (%rax),%edx
mov    0x2df8(%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,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    125d <main+0xed>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1256 <main+0xe6>
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    %esi,%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
lea    0xdc5(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1223 <main+0xb3>
xor    %edi,%edi
call   1080 <exit@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1297 <main+0x127>
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    %esi,%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
lea    0xd8f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1264 <main+0xf4>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x5,0xc(%rsp)
mov    0x2e41(%rip),%rax
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
lea    0xe5e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1030 <write@plt>
mov    0x2e2f(%rip),%rax
mov    0x2df0(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1090 <fork@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%ecx
mov    %ecx,(%rbx)
test   %rax,%rax
jne    120d <main+0x9d>
lea    0xdfd(%rip),%r14
jmp    1214 <main+0xa4>
lea    0xdff(%rip),%r14
mov    %r14,%rdi
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <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 = [11 x i8] c"child: %d\0A\00"
@.str.2 = [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 5, 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 (...) @fork()
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %44
29: ; preds = %2
store i32 0, i32* %7
br label %30
30: ; preds = %39, %29
%31 = load i32, i32* %7
%32 = load i32, i32* %8
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %42
34: ; preds = %30
%35 = load i32*, i32** %10
%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.1, i64 0, i64 0), i32 %36)
br label %39
39: ; preds = %34
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
br label %30
42: ; preds = %30
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %2
store i32 0, i32* %7
br label %45
45: ; preds = %54, %44
%46 = load i32, i32* %7
%47 = load i32, i32* %8
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %57
49: ; preds = %45
%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 ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %51)
br label %54
54: ; preds = %49
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %45
57: ; preds = %45
%58 = 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 = [11 x i8] c"child: %d\0A\00"
@.str.2 = [12 x i8] c"parent: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 5, i32* %3
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IRWXU
%9 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = call i32 @write(i32 %9, i32* %3, i32 4)
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %12, %11
%14 = load i32, i32* @MAP_SHARED
%15 = tail call i32* @mmap(i32* null, i32 4, i32 %13, i32 %14, i32 %9, i32 0)
%16 = tail call i32 @close(i32 %9)
%17 = tail call i64 (...) @fork()
%18 = icmp eq i64 %17, 0
%19 = load i32, i32* %15
%20 = add nsw i32 %19, 1
store i32 %20, i32* %15
br i1 %18, label %51, label %21
21: ; preds = %2
%22 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%23 = load i32, i32* %15
%24 = add nsw i32 %23, 1
store i32 %24, i32* %15
%25 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%26 = load i32, i32* %15
%27 = add nsw i32 %26, 1
store i32 %27, i32* %15
%28 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%29 = load i32, i32* %15
%30 = add nsw i32 %29, 1
store i32 %30, i32* %15
%31 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%32 = load i32, i32* %15
%33 = add nsw i32 %32, 1
store i32 %33, i32* %15
%34 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%35 = load i32, i32* %15
%36 = add nsw i32 %35, 1
store i32 %36, i32* %15
%37 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%38 = load i32, i32* %15
%39 = add nsw i32 %38, 1
store i32 %39, i32* %15
%40 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%41 = load i32, i32* %15
%42 = add nsw i32 %41, 1
store i32 %42, i32* %15
%43 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %41)
%44 = load i32, i32* %15
%45 = add nsw i32 %44, 1
store i32 %45, i32* %15
%46 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%47 = load i32, i32* %15
%48 = add nsw i32 %47, 1
store i32 %48, i32* %15
%49 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%50 = tail call i32 @exit(i32 0)
unreachable
51: ; preds = %2
%52 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%53 = load i32, i32* %15
%54 = add nsw i32 %53, 1
store i32 %54, i32* %15
%55 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %53)
%56 = load i32, i32* %15
%57 = add nsw i32 %56, 1
store i32 %57, i32* %15
%58 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %56)
%59 = load i32, i32* %15
%60 = add nsw i32 %59, 1
store i32 %60, i32* %15
%61 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %59)
%62 = load i32, i32* %15
%63 = add nsw i32 %62, 1
store i32 %63, i32* %15
%64 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %62)
%65 = load i32, i32* %15
%66 = add nsw i32 %65, 1
store i32 %66, i32* %15
%67 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %65)
%68 = load i32, i32* %15
%69 = add nsw i32 %68, 1
store i32 %69, i32* %15
%70 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %68)
%71 = load i32, i32* %15
%72 = add nsw i32 %71, 1
store i32 %72, i32* %15
%73 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %71)
%74 = load i32, i32* %15
%75 = add nsw i32 %74, 1
store i32 %75, i32* %15
%76 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %74)
%77 = load i32, i32* %15
%78 = add nsw i32 %77, 1
store i32 %78, i32* %15
%79 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %77)
%80 = tail call i32 @exit(i32 0)
unreachable
}