#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 */
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  fprintf(stdout, "starting\n");
  int counter = 0;
  pid_t pid = fork();
  int i = 0;
  if (pid == 0) {
    for (; i < 100; ++i)
      fprintf(stdout, "\tchild: counter=%d\n", ++counter);
  } else if (pid > 0) {
    for (; i < 100; ++i)
      fprintf(stdout, "\t\tparent: counter=%d\n", ++counter);
  } else {
    fprintf(stderr, "Fork failed!\n");
    return 1;
  }
  fprintf(stdout, "ending\n");
  return 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)
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
lea    0xeba(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1040 <fork@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b3 <main+0x93>
jmp    1176 <main+0x56>
cmpl   $0x64,-0x24(%rbp)
jge    11ae <main+0x8e>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
lea    0xe71(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1176 <main+0x56>
jmp    1228 <main+0x108>
cmpq   $0x0,-0x20(%rbp)
jle    1200 <main+0xe0>
jmp    11c3 <main+0xa3>
cmpl   $0x64,-0x24(%rbp)
jge    11fb <main+0xdb>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
lea    0xe38(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11c3 <main+0xa3>
jmp    1223 <main+0x103>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1246 <main+0x126>
jmp    1228 <main+0x108>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2ea9(%rip),%r12
mov    (%r12),%edi
lea    0xec6(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
xor    %eax,%eax
call   1040 <fork@plt>
test   %rax,%rax
je     118a <main+0x6a>
jle    11ca <main+0xaa>
mov    $0x1,%ebx
lea    0xec3(%rip),%r15
lea    0xee0(%rip),%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12),%edi
mov    %r15,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
add    $0x1,%ebx
cmp    $0x65,%ebx
jne    1170 <main+0x50>
jmp    11dd <main+0xbd>
mov    $0x1,%ebx
mov    0x2e42(%rip),%r12
lea    0xe6d(%rip),%r15
lea    0xe9e(%rip),%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    (%r12),%edi
mov    %r15,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
add    $0x1,%ebx
cmp    $0x65,%ebx
jne    11b0 <main+0x90>
jmp    11dd <main+0xbd>
mov    $0x1,%ebp
lea    0xe5e(%rip),%r14
mov    0x2e03(%rip),%r12
mov    (%r12),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [10 x i8] c"starting\0A\00"
@.str.1 = [20 x i8] c"\09child: counter=%d\0A\00"
@.str.2 = [22 x i8] c"\09\09parent: counter=%d\0A\00"
@stderr = global i32 0
@.str.3 = [14 x i8] c"Fork failed!\0A\00"
@.str.4 = [8 x i8] c"ending\0A\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 i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @stdout
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
%11 = call i64 (...) @fork()
store i64 %11, i64* %7
store i32 0, i32* %8
%12 = load i64, i64* %7
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %2
br label %15
15: ; preds = %23, %14
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 100
br i1 %17, label %18, label %26
18: ; preds = %15
%19 = load i32, i32* @stdout
%20 = load i32, i32* %6
%21 = add nsw i32 %20, 1
store i32 %21, i32* %6
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %18
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
store i32 %25, i32* %8
br label %15
26: ; preds = %15
br label %47
27: ; preds = %2
%28 = load i64, i64* %7
%29 = icmp sgt i64 %28, 0
br i1 %29, label %30, label %43
30: ; preds = %27
br label %31
31: ; preds = %39, %30
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 100
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = load i32, i32* @stdout
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
store i32 %37, i32* %6
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %34
%40 = load i32, i32* %8
%41 = add nsw i32 %40, 1
store i32 %41, i32* %8
br label %31
42: ; preds = %31
br label %46
43: ; preds = %27
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %50
46: ; preds = %42
br label %47
47: ; preds = %46, %26
%48 = load i32, i32* @stdout
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %50
50: ; preds = %47, %43
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [10 x i8] c"starting\0A\00"
@.str.1 = [20 x i8] c"\09child: counter=%d\0A\00"
@.str.2 = [22 x i8] c"\09\09parent: counter=%d\0A\00"
@stderr = global i32 0
@.str.3 = [14 x i8] c"Fork failed!\0A\00"
@.str.4 = [8 x i8] c"ending\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @stdout
%4 = tail call i32 (i32, i8*, ...) @fprintf(i32 %3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %2, %7
%8 = phi i32 [ %10, %7 ], [ 0, %2 ]
%9 = load i32, i32* @stdout
%10 = add nuw nsw i32 %8, 1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %10, 100
br i1 %12, label %21, label %7
13: ; preds = %2
%14 = icmp sgt i64 %5, 0
br i1 %14, label %15, label %21
15: ; preds = %13, %15
%16 = phi i32 [ %18, %15 ], [ 0, %13 ]
%17 = load i32, i32* @stdout
%18 = add nuw nsw i32 %16, 1
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = icmp eq i32 %18, 100
br i1 %20, label %21, label %15
21: ; preds = %15, %7, %13
%22 = phi i32* [ @stderr, %13 ], [ @stdout, %7 ], [ @stdout, %15 ]
%23 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), %13 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), %7 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), %15 ]
%24 = phi i32 [ 1, %13 ], [ 0, %7 ], [ 0, %15 ]
%25 = load i32, i32* %22
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* %23)
ret i32 %24
}