#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*/ increment(char *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *);
int /*<<< orphan*/ tell_wait();
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) {
      increment("Child increment", *argv);
      i++;
    }
  } else {
    while (i < ulimit) {
      increment("Parent increment", *argv);
      i++;
    }
  }
  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    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    0x2e2c(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11f9 <main+0x69>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0e(%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   1070 <memset@plt>
lea    -0x420(%rbp),%rdi
lea    0xdf8(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x424(%rbp)
movl   $0x64,-0x428(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    127e <main+0xee>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1040 <err_sys@plt>
jmp    1314 <main+0x184>
cmpq   $0x0,-0x20(%rbp)
jne    12cc <main+0x13c>
jmp    128e <main+0xfe>
mov    -0x424(%rbp),%eax
cmp    -0x428(%rbp),%eax
jge    12c7 <main+0x137>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd7d(%rip),%rdi
call   1060 <increment@plt>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    128e <main+0xfe>
jmp    130f <main+0x17f>
jmp    12d1 <main+0x141>
mov    -0x424(%rbp),%eax
cmp    -0x428(%rbp),%eax
jge    130a <main+0x17a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd4a(%rip),%rdi
call   1060 <increment@plt>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    12d1 <main+0x141>
jmp    130f <main+0x17f>
jmp    1314 <main+0x184>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e37(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11c6 <main+0x46>
mov    0x8(%rbx),%rsi
lea    0xe41(%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   1070 <memset@plt>
movw   $0x30,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1233 <main+0xb3>
mov    $0x64,%ebp
je     1243 <main+0xc3>
lea    0xe24(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%rbx),%rsi
mov    %r14,%rdi
call   1060 <increment@plt>
add    $0xffffffff,%ebp
jne    1220 <main+0xa0>
jmp    1261 <main+0xe1>
lea    0xddf(%rip),%rdi
xor    %eax,%eax
call   1040 <err_sys@plt>
jmp    1261 <main+0xe1>
lea    0xddf(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    0x8(%rbx),%rsi
mov    %r14,%rdi
call   1060 <increment@plt>
add    $0xffffffff,%ebp
jne    1250 <main+0xd0>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
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 %66
38: ; preds = %23
%39 = load i64, i64* %7
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %53
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 %52
46: ; preds = %42
%47 = load i8**, i8*** %5
%48 = load i8*, i8** %47
%49 = call i32 @increment(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %48)
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
store i32 %51, i32* %9
br label %42
52: ; preds = %42
br label %65
53: ; preds = %38
br label %54
54: ; preds = %58, %53
%55 = load i32, i32* %9
%56 = load i32, i32* %10
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %64
58: ; preds = %54
%59 = load i8**, i8*** %5
%60 = load i8*, i8** %59
%61 = call i32 @increment(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %60)
%62 = load i32, i32* %9
%63 = add nsw i32 %62, 1
store i32 %63, i32* %9
br label %54
64: ; preds = %54
br label %65
65: ; preds = %64, %52
br label %66
66: ; preds = %65, %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 %39
25: ; preds = %14
%26 = icmp eq i64 %21, 0
br i1 %26, label %27, label %33
27: ; preds = %25, %27
%28 = phi i32 [ %31, %27 ], [ 0, %25 ]
%29 = load i8*, i8** %4
%30 = call i32 @increment(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = add nuw nsw i32 %28, 1
%32 = icmp eq i32 %31, 100
br i1 %32, label %39, label %27
33: ; preds = %25, %33
%34 = phi i32 [ %37, %33 ], [ 0, %25 ]
%35 = load i8*, i8** %4
%36 = call i32 @increment(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %35)
%37 = add nuw nsw i32 %34, 1
%38 = icmp eq i32 %37, 100
br i1 %38, label %39, label %33
39: ; preds = %33, %27, %23
ret i32 0
}