#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, scalar_t__, ...);
int /*<<< orphan*/ wait(int *);
int main(int argc, char **argv) {
  pid_t child_id;
  pid_t my_id;
  int child_status;
  int i;
  child_id = fork();
  if (child_id != 0) {
    my_id = getpid();
    printf("pid: %d -- I just forked a child with id %d\n", (int)my_id,
           (int)child_id);
    printf("pid: %d -- I am waiting for process %d to finish\n", (int)my_id,
           (int)child_id);
    wait(&child_status);
    printf("pid: %d -- my child has completed with status: %d\n", (int)my_id,
           child_status);
  } else {
    my_id = getpid();
    printf("pid: %d -- I am the child and I am going to count to 10\n",
           (int)my_id);
    for (i = 0; i < 10; i++) {
      printf("pid: %d -- %d\n", my_id, i + 1);
    }
  }
  printf("pid: %d -- I am exiting\n", my_id);
  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)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e3 <main+0x93>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq %eax,%rsi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
movslq %eax,%rsi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x24(%rbp),%rdi
call   1060 <wait@plt>
mov    -0x20(%rbp),%rax
movslq %eax,%rsi
mov    -0x24(%rbp),%edx
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123f <main+0xef>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq %eax,%rsi
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0xa,-0x28(%rbp)
jge    123a <main+0xea>
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
add    $0x1,%edx
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    120a <main+0xba>
jmp    123f <main+0xef>
mov    -0x20(%rbp),%rsi
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1070 <fork@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%r15
movslq %r15d,%r14
test   %rbx,%rbx
jne    1240 <main+0xf0>
lea    0xf12(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xf3a(%rip),%r14
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x3,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x5,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x6,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x7,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x8,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x9,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0xa,%edx
jmp    127e <main+0x12e>
lea    0xdb9(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdd3(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xc(%rsp),%rdi
call   1060 <wait@plt>
mov    0xc(%rsp),%edx
lea    0xde4(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe4e(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [45 x i8] c"pid: %d -- I just forked a child with id %d\0A\00"
@.str.1 = [50 x i8] c"pid: %d -- I am waiting for process %d to finish\0A\00"
@.str.2 = [51 x i8] c"pid: %d -- my child has completed with status: %d\0A\00"
@.str.3 = [57 x i8] c"pid: %d -- I am the child and I am going to count to 10\0A\00"
@.str.4 = [15 x i8] c"pid: %d -- %d\0A\00"
@.str.5 = [25 x i8] c"pid: %d -- I am exiting\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i64 (...) @fork()
store i64 %10, i64* %6
%11 = load i64, i64* %6
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %33
13: ; preds = %2
%14 = call i64 (...) @getpid()
store i64 %14, i64* %7
%15 = load i64, i64* %7
%16 = trunc i64 %15 to i32
%17 = sext i32 %16 to i64
%18 = load i64, i64* %6
%19 = trunc i64 %18 to i32
%20 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i64 %17, i32 %19)
%21 = load i64, i64* %7
%22 = trunc i64 %21 to i32
%23 = sext i32 %22 to i64
%24 = load i64, i64* %6
%25 = trunc i64 %24 to i32
%26 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i64 %23, i32 %25)
%27 = call i32 @wait(i32* %8)
%28 = load i64, i64* %7
%29 = trunc i64 %28 to i32
%30 = sext i32 %29 to i64
%31 = load i32, i32* %8
%32 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i64 %30, i32 %31)
br label %51
33: ; preds = %2
%34 = call i64 (...) @getpid()
store i64 %34, i64* %7
%35 = load i64, i64* %7
%36 = trunc i64 %35 to i32
%37 = sext i32 %36 to i64
%38 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i64 %37)
store i32 0, i32* %9
br label %39
39: ; preds = %47, %33
%40 = load i32, i32* %9
%41 = icmp slt i32 %40, 10
br i1 %41, label %42, label %50
42: ; preds = %39
%43 = load i64, i64* %7
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 1
%46 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i64 %43, i32 %45)
br label %47
47: ; preds = %42
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 1
store i32 %49, i32* %9
br label %39
50: ; preds = %39
br label %51
51: ; preds = %50, %13
%52 = load i64, i64* %7
%53 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i64 %52)
%54 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [45 x i8] c"pid: %d -- I just forked a child with id %d\0A\00"
@.str.1 = [50 x i8] c"pid: %d -- I am waiting for process %d to finish\0A\00"
@.str.2 = [51 x i8] c"pid: %d -- my child has completed with status: %d\0A\00"
@.str.3 = [57 x i8] c"pid: %d -- I am the child and I am going to count to 10\0A\00"
@.str.4 = [15 x i8] c"pid: %d -- %d\0A\00"
@.str.5 = [25 x i8] c"pid: %d -- I am exiting\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
%7 = tail call i64 (...) @getpid()
%8 = shl i64 %7, 32
%9 = ashr exact i64 %8, 32
br i1 %6, label %17, label %10
10: ; preds = %2
%11 = trunc i64 %5 to i32
%12 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i64 %9, i32 %11)
%13 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i64 %9, i32 %11)
%14 = call i32 @wait(i32* %3)
%15 = load i32, i32* %3
%16 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i64 %9, i32 %15)
br label %29
17: ; preds = %2
%18 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i64 %9)
%19 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i64 %7, i32 1)
%20 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i64 %7, i32 2)
%21 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i64 %7, i32 3)
%22 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i64 %7, i32 4)
%23 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i64 %7, i32 5)
%24 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i64 %7, i32 6)
%25 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i64 %7, i32 7)
%26 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i64 %7, i32 8)
%27 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i64 %7, i32 9)
%28 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i64 %7, i32 10)
br label %29
29: ; preds = %17, %10
%30 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i64 %7)
%31 = call i32 @exit(i32 0)
unreachable
}