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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, ...);
long strtol(char *, char **, int);
int main(int argc, char *argv[]) {
  const long MAX_COUNT = 2000;
  long count;
  pid_t pid;
  if (argc != 2) {
    printf("Error - usage: %s count\n", argv[0]);
    exit(EXIT_FAILURE);
  } else {
    int j;
    for (j = 1; j < argc; j++) {
      char *end;
      count = strtol(argv[j], &end, 10);
      if (argc == 2 && !end[0] && count >= 0 && count <= MAX_COUNT) {
        printf("%s is valid, good to go!\n", argv[j]);
      } else {
        printf("%s is invalid input, argument 2 must be a integer and less "
               "than or equal to 2000, exiting program\n",
               argv[j]);
        exit(EXIT_FAILURE);
      }
    }
  }
  printf("This is testfork.c\n");
  pid = fork();
  if (pid == -1) {
    printf("The child process was not created successfully. Exiting program");
    exit(EXIT_FAILURE);
  } else if (pid == 0) {
    printf("Child process started \n");
    int i;
    for (i = 1; i <= count; i++) {
      printf("I am the child process and my process id is...(%d) count = %d\n",
             (int)getpid(), i);
    }
    printf("Child process ended \n");
    printf("This is testfork.c after child process end\n");
    exit(EXIT_SUCCESS);
  } else {
    printf("Parent process started \n");
    int i;
    for (i = 1; i <= count; i++) {
      printf("I am the parent process and my process id is...(%d) count = %d\n",
             (int)getpid(), i);
    }
    printf("Parent process ended\n");
    printf("This was testfork.c\n");
    exit(EXIT_SUCCESS);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x7d0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     119b <main+0x4b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
movl   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1256 <main+0x106>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    -0x30(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    121b <main+0xcb>
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
jne    121b <main+0xcb>
cmpq   $0x0,-0x20(%rbp)
jl     121b <main+0xcb>
cmpq   $0x7d0,-0x20(%rbp)
jg     121b <main+0xcb>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1243 <main+0xf3>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    1248 <main+0xf8>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11a2 <main+0x52>
jmp    125b <main+0x10b>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1299 <main+0x149>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jne    1318 <main+0x1c8>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    -0x20(%rbp),%rax
jg     12ee <main+0x19e>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x34(%rbp),%edx
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12b8 <main+0x168>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x38(%rbp)
movslq -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jg     1363 <main+0x213>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x38(%rbp),%edx
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    132d <main+0x1dd>
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c52(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1246 <main+0xe6>
mov    0x8(%rbx),%rdi
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,%r14
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
mov    0x8(%rbx),%rsi
jne    1252 <main+0xf2>
test   %r14,%r14
js     1252 <main+0xf2>
cmp    $0x7d1,%r14
jge    1252 <main+0xf2>
lea    0xe5f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xf8d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     126e <main+0x10e>
test   %eax,%eax
jne    127e <main+0x11e>
lea    0xfbd(%rip),%rdi
call   1030 <puts@plt>
test   %r14,%r14
je     122e <main+0xce>
mov    $0x1,%ebx
lea    0xed4(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jge    1210 <main+0xb0>
lea    0xf8b(%rip),%rdi
call   1030 <puts@plt>
lea    0xf94(%rip),%rdi
jmp    12d1 <main+0x171>
mov    (%rbx),%rsi
lea    0xdb0(%rip),%rdi
jmp    1259 <main+0xf9>
lea    0xdda(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe21(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1260 <main+0x100>
lea    0xee3(%rip),%rdi
call   1030 <puts@plt>
test   %r14,%r14
je     12be <main+0x15e>
mov    $0x1,%ebx
lea    0xe7a(%rip),%r15
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jge    12a0 <main+0x140>
lea    0xebb(%rip),%rdi
call   1030 <puts@plt>
lea    0xec4(%rip),%rdi
call   1030 <puts@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Error - usage: %s count\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [26 x i8] c"%s is valid, good to go!\0A\00"
@.str.2 = [99 x i8] c"%s is invalid input, argument 2 must be a integer and less than or equal to 2000, exiting program\0A\00"
@.str.3 = [20 x i8] c"This is testfork.c\0A\00"
@.str.4 = [64 x i8] c"The child process was not created successfully. Exiting program\00"
@.str.5 = [24 x i8] c"Child process started \0A\00"
@.str.6 = [63 x i8] c"I am the child process and my process id is...(%d) count = %d\0A\00"
@.str.7 = [22 x i8] c"Child process ended \0A\00"
@.str.8 = [44 x i8] c"This is testfork.c after child process end\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.9 = [25 x i8] c"Parent process started \0A\00"
@.str.10 = [64 x i8] c"I am the parent process and my process id is...(%d) count = %d\0A\00"
@.str.11 = [22 x i8] c"Parent process ended\0A\00"
@.str.12 = [21 x i8] c"This was testfork.c\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
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 2000, i64* %6
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %22
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
store i32 1, i32* %9
br label %23
23: ; preds = %64, %22
%24 = load i32, i32* %9
%25 = load i32, i32* %4
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %67
27: ; preds = %23
%28 = load i8**, i8*** %5
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8*, i8** %28, i64 %30
%32 = load i8*, i8** %31
%33 = call i64 @strtol(i8* %32, i8** %10, i32 10)
store i64 %33, i64* %7
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, 2
br i1 %35, label %36, label %54
36: ; preds = %27
%37 = load i8*, i8** %10
%38 = getelementptr inbounds i8, i8* %37, i64 0
%39 = load i8, i8* %38
%40 = icmp ne i8 %39, 0
br i1 %40, label %54, label %41
41: ; preds = %36
%42 = load i64, i64* %7
%43 = icmp sge i64 %42, 0
br i1 %43, label %44, label %54
44: ; preds = %41
%45 = load i64, i64* %7
%46 = icmp sle i64 %45, 2000
br i1 %46, label %47, label %54
47: ; preds = %44
%48 = load i8**, i8*** %5
%49 = load i32, i32* %9
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8*, i8** %48, i64 %50
%52 = load i8*, i8** %51
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %52)
br label %63
54: ; preds = %44, %41, %36, %27
%55 = load i8**, i8*** %5
%56 = load i32, i32* %9
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8*, i8** %55, i64 %57
%59 = load i8*, i8** %58
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([99 x i8], [99 x i8]* @.str.2, i64 0, i64 0), i8* %59)
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %47
br label %64
64: ; preds = %63
%65 = load i32, i32* %9
%66 = add nsw i32 %65, 1
store i32 %66, i32* %9
br label %23
67: ; preds = %23
br label %68
68: ; preds = %67
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%70 = call i32 (...) @fork()
store i32 %70, i32* %8
%71 = load i32, i32* %8
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %77
73: ; preds = %68
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.4, i64 0, i64 0))
%75 = load i32, i32* @EXIT_FAILURE
%76 = call i32 @exit(i32 %75)
unreachable
77: ; preds = %68
%78 = load i32, i32* %8
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %100
80: ; preds = %77
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %11
br label %82
82: ; preds = %92, %80
%83 = load i32, i32* %11
%84 = sext i32 %83 to i64
%85 = load i64, i64* %7
%86 = icmp sle i64 %84, %85
br i1 %86, label %87, label %95
87: ; preds = %82
%88 = call i64 (...) @getpid()
%89 = trunc i64 %88 to i32
%90 = load i32, i32* %11
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.6, i64 0, i64 0), i32 %89, i32 %90)
br label %92
92: ; preds = %87
%93 = load i32, i32* %11
%94 = add nsw i32 %93, 1
store i32 %94, i32* %11
br label %82
95: ; preds = %82
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
%97 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.8, i64 0, i64 0))
%98 = load i32, i32* @EXIT_SUCCESS
%99 = call i32 @exit(i32 %98)
unreachable
100: ; preds = %77
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %12
br label %102
102: ; preds = %112, %100
%103 = load i32, i32* %12
%104 = sext i32 %103 to i64
%105 = load i64, i64* %7
%106 = icmp sle i64 %104, %105
br i1 %106, label %107, label %115
107: ; preds = %102
%108 = call i64 (...) @getpid()
%109 = trunc i64 %108 to i32
%110 = load i32, i32* %12
%111 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.10, i64 0, i64 0), i32 %109, i32 %110)
br label %112
112: ; preds = %107
%113 = load i32, i32* %12
%114 = add nsw i32 %113, 1
store i32 %114, i32* %12
br label %102
115: ; preds = %102
%116 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.11, i64 0, i64 0))
%117 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.12, i64 0, i64 0))
%118 = load i32, i32* @EXIT_SUCCESS
%119 = call i32 @exit(i32 %118)
unreachable
}

LLVM IR (-O2):
@.str = [25 x i8] c"Error - usage: %s count\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [26 x i8] c"%s is valid, good to go!\0A\00"
@.str.2 = [99 x i8] c"%s is invalid input, argument 2 must be a integer and less than or equal to 2000, exiting program\0A\00"
@.str.4 = [64 x i8] c"The child process was not created successfully. Exiting program\00"
@.str.6 = [63 x i8] c"I am the child process and my process id is...(%d) count = %d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.10 = [64 x i8] c"I am the parent process and my process id is...(%d) count = %d\0A\00"
@str = [19 x i8] c"This is testfork.c\00"
@str.13 = [24 x i8] c"Parent process started \00"
@str.14 = [21 x i8] c"Parent process ended\00"
@str.15 = [20 x i8] c"This was testfork.c\00"
@str.16 = [23 x i8] c"Child process started \00"
@str.17 = [21 x i8] c"Child process ended \00"
@str.18 = [43 x i8] c"This is testfork.c after child process end\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = icmp eq i32 %0, 2
br i1 %4, label %5, label %18
5: ; preds = %2
%6 = bitcast i8** %3 to i8*
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = call i64 @strtol(i8* %8, i8** %3, i32 10)
%10 = load i8*, i8** %3
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 0
%13 = icmp sgt i64 %9, -1
%14 = select i1 %12, i1 %13, i1 false
%15 = icmp slt i64 %9, 2001
%16 = select i1 %14, i1 %15, i1 false
%17 = load i8*, i8** %7
br i1 %16, label %23, label %27
18: ; preds = %2
%19 = load i8*, i8** %1
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %5
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%26 = tail call i32 (...) @fork()
switch i32 %26, label %51 [
i32 -1, label %31
i32 0, label %35
]
27: ; preds = %5
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([99 x i8], [99 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %23
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @.str.4, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %23
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.16, i64 0, i64 0))
%37 = icmp eq i64 %9, 0
br i1 %37, label %46, label %38
38: ; preds = %35, %38
%39 = phi i64 [ %44, %38 ], [ 1, %35 ]
%40 = tail call i64 (...) @getpid()
%41 = trunc i64 %40 to i32
%42 = trunc i64 %39 to i32
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([63 x i8], [63 x i8]* @.str.6, i64 0, i64 0), i32 %41, i32 %42)
%44 = add nuw i64 %39, 1
%45 = icmp slt i64 %9, %44
br i1 %45, label %46, label %38
46: ; preds = %38, %35
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.17, i64 0, i64 0))
%48 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.18, i64 0, i64 0))
%49 = load i32, i32* @EXIT_SUCCESS
%50 = tail call i32 @exit(i32 %49)
unreachable
51: ; preds = %23
%52 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.13, i64 0, i64 0))
%53 = icmp eq i64 %9, 0
br i1 %53, label %62, label %54
54: ; preds = %51, %54
%55 = phi i64 [ %60, %54 ], [ 1, %51 ]
%56 = tail call i64 (...) @getpid()
%57 = trunc i64 %56 to i32
%58 = trunc i64 %55 to i32
%59 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @.str.10, i64 0, i64 0), i32 %57, i32 %58)
%60 = add nuw i64 %55, 1
%61 = icmp slt i64 %9, %60
br i1 %61, label %62, label %54
62: ; preds = %54, %51
%63 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.14, i64 0, i64 0))
%64 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.15, i64 0, i64 0))
%65 = load i32, i32* @EXIT_SUCCESS
%66 = tail call i32 @exit(i32 %65)
unreachable
}