#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 */

/* Variables and functions */
int fork();
int getpid();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, int, ...);
int main() {
  int i;
  char strbuf[256];
  int magicvalue = 0xF00;
  sprintf(strbuf, "Parent is starting (pid %d).  The magic value is %03X\n",
          getpid(), magicvalue);
  printf(strbuf);
  for (i = 0; i < 10; i++) {
    int child = fork();
    if (child < 0) {
      sprintf(strbuf, "Fork %d failed!\n", i);
      printf(strbuf);
    } else if (child) {
      sprintf(strbuf, "Child %d has been started with pid %d\n", i, child);
      printf(strbuf);
    } else {
      int pid = getpid();
      sprintf(strbuf, "I am child %d with pid %d.  The magic value is %03X\n",
              i, pid, magicvalue);
      printf(strbuf);
      return pid;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xf00,-0x114(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x128(%rbp),%rdi
mov    %eax,%edx
mov    -0x114(%rbp),%ecx
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x110(%rbp),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    128e <main+0x14e>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0x0,-0x118(%rbp)
jge    11ee <main+0xae>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe61(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x110(%rbp),%rdi
call   1040 <printf@plt>
jmp    127b <main+0x13b>
cmpl   $0x0,-0x118(%rbp)
je     122a <main+0xea>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    -0x118(%rbp),%ecx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x110(%rbp),%rdi
call   1040 <printf@plt>
jmp    1276 <main+0x136>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x11c(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    -0x11c(%rbp),%ecx
mov    -0x114(%rbp),%r8d
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x110(%rbp),%rdi
call   1040 <printf@plt>
mov    -0x11c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1295 <main+0x155>
jmp    127b <main+0x13b>
jmp    1280 <main+0x140>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11a1 <main+0x61>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xea3(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %eax,%edx
mov    $0xf00,%ecx
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %r14,%rdi
call   1040 <printf@plt>
lea    0xeb7(%rip),%r15
lea    0xec1(%rip),%r12
xor    %ebx,%ebx
jmp    11af <main+0x6f>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %r14,%rdi
call   1040 <printf@plt>
add    $0x1,%ebx
cmp    $0xa,%ebx
je     11cf <main+0x8f>
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
js     1190 <main+0x50>
je     11d3 <main+0x93>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <sprintf@plt>
jmp    119f <main+0x5f>
xor    %ebp,%ebp
jmp    1202 <main+0xc2>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
lea    0xe8c(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebx,%edx
mov    %eax,%ecx
mov    $0xf00,%r8d
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [55 x i8] c"Parent is starting (pid %d). The magic value is %03X\0A\00"
@.str.1 = [17 x i8] c"Fork %d failed!\0A\00"
@.str.2 = [39 x i8] c"Child %d has been started with pid %d\0A\00"
@.str.3 = [53 x i8] c"I am child %d with pid %d. The magic value is %03X\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 3840, i32* %4
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = call i32 (...) @getpid()
%9 = load i32, i32* %4
%10 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %7, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%12 = call i32 @printf(i8* %11)
store i32 0, i32* %2
br label %13
13: ; preds = %48, %0
%14 = load i32, i32* %2
%15 = icmp slt i32 %14, 10
br i1 %15, label %16, label %51
16: ; preds = %13
%17 = call i32 (...) @fork()
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%22 = load i32, i32* %2
%23 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%25 = call i32 @printf(i8* %24)
br label %47
26: ; preds = %16
%27 = load i32, i32* %5
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %26
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%31 = load i32, i32* %2
%32 = load i32, i32* %5
%33 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %31, i32 %32)
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%35 = call i32 @printf(i8* %34)
br label %46
36: ; preds = %26
%37 = call i32 (...) @getpid()
store i32 %37, i32* %6
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%39 = load i32, i32* %2
%40 = load i32, i32* %6
%41 = load i32, i32* %4
%42 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %38, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %39, i32 %40, i32 %41)
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%44 = call i32 @printf(i8* %43)
%45 = load i32, i32* %6
store i32 %45, i32* %1
br label %52
46: ; preds = %29
br label %47
47: ; preds = %46, %20
br label %48
48: ; preds = %47
%49 = load i32, i32* %2
%50 = add nsw i32 %49, 1
store i32 %50, i32* %2
br label %13
51: ; preds = %13
store i32 0, i32* %1
br label %52
52: ; preds = %51, %36
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
@.str = [55 x i8] c"Parent is starting (pid %d). The magic value is %03X\0A\00"
@.str.1 = [17 x i8] c"Fork %d failed!\0A\00"
@.str.2 = [39 x i8] c"Child %d has been started with pid %d\0A\00"
@.str.3 = [53 x i8] c"I am child %d with pid %d. The magic value is %03X\0A\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @getpid()
%4 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %3, i32 3840)
%5 = call i32 @printf(i8* %2)
br label %6
6: ; preds = %0, %20
%7 = phi i32 [ 0, %0 ], [ %22, %20 ]
%8 = tail call i32 (...) @fork()
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %20
12: ; preds = %6
%13 = icmp eq i32 %8, 0
br i1 %13, label %16, label %14
14: ; preds = %12
%15 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %7, i32 %8)
br label %20
16: ; preds = %12
%17 = tail call i32 (...) @getpid()
%18 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i32 %7, i32 %17, i32 3840)
%19 = call i32 @printf(i8* %2)
br label %24
20: ; preds = %14, %10
%21 = call i32 @printf(i8* %2)
%22 = add nuw nsw i32 %7, 1
%23 = icmp eq i32 %22, 10
br i1 %23, label %24, label %6
24: ; preds = %20, %16
%25 = phi i32 [ %17, %16 ], [ 0, %20 ]
ret i32 %25
}