#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 scalar_t__ pid_t;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_sys(char *, ...);
scalar_t__ fork();
int /*<<< orphan*/ getppid();
int /*<<< orphan*/ increment(char *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *);
int /*<<< orphan*/ tell_child(scalar_t__);
int /*<<< orphan*/ tell_parent(int /*<<< orphan*/);
int /*<<< orphan*/ tell_wait();
int /*<<< orphan*/ wait_child();
int /*<<< orphan*/ wait_parent();
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  if ((fd = open(*++argv, O_WRONLY | O_CREAT, 0644)) < 0) {
    err_sys("error on opening file %s", *argv);
  }
  tell_wait();
  pid_t pid;
  char buf[1024];
  memset(buf, 0, 1024);
  sprintf(buf, "0");
  write(fd, buf, 1);
  close(fd);
  int i = 0;
  int ulimit = 100;
  if ((pid = fork()) < 0) {
    err_sys("error on fork()");
  } else if (pid == 0) {
    while (i < ulimit) {
      wait_parent();
      increment("Child increment", *argv);
      i++;
      tell_parent(getppid());
    }
  } else {
    while (i < ulimit) {
      increment("Parent increment", *argv);
      i++;
      tell_child(pid);
      wait_child();
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1249 <main+0x69>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1040 <err_sys@plt>
mov    $0x0,%al
call   1030 <tell_wait@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    -0x420(%rbp),%rdi
lea    0xda8(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x1,%edx
call   1060 <write@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x424(%rbp)
movl   $0x64,-0x428(%rbp)
mov    $0x0,%al
call   1100 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    12ce <main+0xee>
lea    0xd59(%rip),%rdi
mov    $0x0,%al
call   1040 <err_sys@plt>
jmp    1389 <main+0x1a9>
cmpq   $0x0,-0x20(%rbp)
jne    1331 <main+0x151>
jmp    12de <main+0xfe>
mov    -0x424(%rbp),%eax
cmp    -0x428(%rbp),%eax
jge    132c <main+0x14c>
mov    $0x0,%al
call   1050 <wait_parent@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd26(%rip),%rdi
call   1070 <increment@plt>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
mov    $0x0,%al
call   10e0 <getppid@plt>
mov    %eax,%edi
call   10f0 <tell_parent@plt>
jmp    12de <main+0xfe>
jmp    1384 <main+0x1a4>
jmp    1336 <main+0x156>
mov    -0x424(%rbp),%eax
cmp    -0x428(%rbp),%eax
jge    137f <main+0x19f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xce5(%rip),%rdi
call   1070 <increment@plt>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
mov    -0x20(%rbp),%rdi
call   10a0 <tell_child@plt>
mov    $0x0,%al
call   10b0 <wait_child@plt>
jmp    1336 <main+0x156>
jmp    1384 <main+0x1a4>
jmp    1389 <main+0x1a9>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r15
mov    0x8(%rsi),%rdi
mov    0x2de5(%rip),%rax
mov    0x2e06(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   10c0 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1218 <main+0x48>
mov    0x8(%r15),%rsi
lea    0xdef(%rip),%rdi
xor    %eax,%eax
call   1040 <err_sys@plt>
xor    %eax,%eax
call   1030 <tell_wait@plt>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
movw   $0x30,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1060 <write@plt>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
js     1292 <main+0xc2>
je     12a2 <main+0xd2>
mov    %rax,%rbp
mov    $0x64,%ebx
lea    0xdcf(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    0x8(%r15),%rsi
mov    %r14,%rdi
call   1070 <increment@plt>
mov    %rbp,%rdi
call   10a0 <tell_child@plt>
xor    %eax,%eax
call   10b0 <wait_child@plt>
add    $0xffffffff,%ebx
jne    1270 <main+0xa0>
jmp    12d6 <main+0x106>
lea    0xd80(%rip),%rdi
xor    %eax,%eax
call   1040 <err_sys@plt>
jmp    12d6 <main+0x106>
mov    $0x64,%ebp
lea    0xd7b(%rip),%r14
xchg   %ax,%ax
xor    %eax,%eax
call   1050 <wait_parent@plt>
mov    0x8(%r15),%rsi
mov    %r14,%rdi
call   1070 <increment@plt>
xor    %eax,%eax
call   10d0 <getppid@plt>
mov    %eax,%edi
call   10e0 <tell_parent@plt>
add    $0xffffffff,%ebp
jne    12b0 <main+0xe0>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str = [25 x i8] c"error on opening file %s\00"
@.str.1 = [2 x i8] c"0\00"
@.str.2 = [16 x i8] c"error on fork()\00"
@.str.3 = [16 x i8] c"Child increment\00"
@.str.4 = [17 x i8] c"Parent increment\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i32 1
store i8** %12, i8*** %5
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %13, i32 %16, i32 420)
store i32 %17, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %19, %2
%24 = call i32 (...) @tell_wait()
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%26 = call i32 @memset(i8* %25, i32 0, i32 1024)
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%28 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* %6
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%31 = call i32 @write(i32 %29, i8* %30, i32 1)
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
store i32 0, i32* %9
store i32 100, i32* %10
%34 = call i64 (...) @fork()
store i64 %34, i64* %7
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %23
%37 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %72
38: ; preds = %23
%39 = load i64, i64* %7
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %56
41: ; preds = %38
br label %42
42: ; preds = %46, %41
%43 = load i32, i32* %9
%44 = load i32, i32* %10
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %55
46: ; preds = %42
%47 = call i32 (...) @wait_parent()
%48 = load i8**, i8*** %5
%49 = load i8*, i8** %48
%50 = call i32 @increment(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %49)
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
store i32 %52, i32* %9
%53 = call i32 (...) @getppid()
%54 = call i32 @tell_parent(i32 %53)
br label %42
55: ; preds = %42
br label %71
56: ; preds = %38
br label %57
57: ; preds = %61, %56
%58 = load i32, i32* %9
%59 = load i32, i32* %10
%60 = icmp slt i32 %58, %59
br i1 %60, label %61, label %70
61: ; preds = %57
%62 = load i8**, i8*** %5
%63 = load i8*, i8** %62
%64 = call i32 @increment(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %63)
%65 = load i32, i32* %9
%66 = add nsw i32 %65, 1
store i32 %66, i32* %9
%67 = load i64, i64* %7
%68 = call i32 @tell_child(i64 %67)
%69 = call i32 (...) @wait_child()
br label %57
70: ; preds = %57
br label %71
71: ; preds = %70, %55
br label %72
72: ; preds = %71, %36
ret i32 0
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str = [25 x i8] c"error on opening file %s\00"
@.str.2 = [16 x i8] c"error on fork()\00"
@.str.3 = [16 x i8] c"Child increment\00"
@.str.4 = [17 x i8] c"Parent increment\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* %5, i32 %8, i32 420)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = tail call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %11, %2
%15 = tail call i32 (...) @tell_wait()
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%17 = call i32 @memset(i8* %16, i32 0, i32 1024)
%18 = bitcast [1024 x i8]* %3 to i16*
store i16 48, i16* %18
%19 = call i32 @write(i32 %9, i8* %16, i32 1)
%20 = call i32 @close(i32 %9)
%21 = call i64 (...) @fork()
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %44
25: ; preds = %14
%26 = icmp eq i64 %21, 0
br i1 %26, label %27, label %36
27: ; preds = %25, %27
%28 = phi i32 [ %32, %27 ], [ 0, %25 ]
%29 = call i32 (...) @wait_parent()
%30 = load i8*, i8** %4
%31 = call i32 @increment(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = add nuw nsw i32 %28, 1
%33 = call i32 (...) @getppid()
%34 = call i32 @tell_parent(i32 %33)
%35 = icmp eq i32 %32, 100
br i1 %35, label %44, label %27
36: ; preds = %25, %36
%37 = phi i32 [ %40, %36 ], [ 0, %25 ]
%38 = load i8*, i8** %4
%39 = call i32 @increment(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %38)
%40 = add nuw nsw i32 %37, 1
%41 = call i32 @tell_child(i64 %21)
%42 = call i32 (...) @wait_child()
%43 = icmp eq i32 %40, 100
br i1 %43, label %44, label %36
44: ; preds = %36, %27, %23
ret i32 0
}