#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 /*<<< orphan*/ ssize_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fill_some(int /*<<< orphan*/, char);
scalar_t__ fork();
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  char ch = argv[1][0];
  printf("Parent PID is %zd\n", (ssize_t)getpid());
  fill_some(atoi(argv[2]), ch);
  for (int r = 3; r < argc; ++r) {
    pid_t ret = fork();
    if (ret < 0) {
      perror("Fork failed");
      exit(2);
    }
    if (!ret) {
      printf("New Child: %zd\n", (ssize_t)getpid());
      fill_some(atoi(argv[r]), ch);
      exit(0);
    }
  }
  sleep(argc);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%al
mov    %al,-0x11(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe4d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,%edi
movsbl -0x11(%rbp),%esi
call   1080 <fill_some@plt>
movl   $0x3,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1265 <main+0xe5>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    120f <main+0x8f>
lea    0xe13(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1252 <main+0xd2>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdf5(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <atoi@plt>
mov    %eax,%edi
movsbl -0x11(%rbp),%esi
call   1080 <fill_some@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1257 <main+0xd7>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11d7 <main+0x57>
mov    -0x8(%rbp),%edi
call   1090 <sleep@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %edi,%r12d
mov    0x8(%rsi),%rax
mov    (%rax),%bl
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe5e(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    0x10(%r15),%rdi
call   1060 <atoi@plt>
movsbl %bl,%r14d
mov    %eax,%edi
mov    %r14d,%esi
call   1080 <fill_some@plt>
cmp    $0x4,%r12d
jl     11e6 <main+0x66>
mov    %r12d,%ebp
mov    $0x3,%ebx
xchg   %ax,%ax
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11f9 <main+0x79>
je     120f <main+0x8f>
add    $0x1,%rbx
cmp    %ebx,%ebp
jne    11d0 <main+0x50>
mov    %r12d,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe13(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe02(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    (%r15,%rbx,8),%rdi
call   1060 <atoi@plt>
mov    %eax,%edi
mov    %r14d,%esi
call   1080 <fill_some@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Parent PID is %zd\0A\00"
@.str.1 = [12 x i8] c"Fork failed\00"
@.str.2 = [16 x i8] c"New Child: %zd\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8, i8* %11, i64 0
%13 = load i8, i8* %12
store i8 %13, i8* %6
%14 = call i64 (...) @getpid()
%15 = trunc i64 %14 to i32
%16 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 2
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
%21 = load i8, i8* %6
%22 = call i32 @fill_some(i32 %20, i8 signext %21)
store i32 3, i32* %7
br label %23
23: ; preds = %51, %2
%24 = load i32, i32* %7
%25 = load i32, i32* %4
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %54
27: ; preds = %23
%28 = call i64 (...) @fork()
store i64 %28, i64* %8
%29 = load i64, i64* %8
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 2)
unreachable
34: ; preds = %27
%35 = load i64, i64* %8
%36 = icmp ne i64 %35, 0
br i1 %36, label %50, label %37
37: ; preds = %34
%38 = call i64 (...) @getpid()
%39 = trunc i64 %38 to i32
%40 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = load i8**, i8*** %5
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8*, i8** %41, i64 %43
%45 = load i8*, i8** %44
%46 = call i32 @atoi(i8* %45)
%47 = load i8, i8* %6
%48 = call i32 @fill_some(i32 %46, i8 signext %47)
%49 = call i32 @exit(i32 0)
unreachable
50: ; preds = %34
br label %51
51: ; preds = %50
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 1
store i32 %53, i32* %7
br label %23
54: ; preds = %23
%55 = load i32, i32* %4
%56 = call i32 @sleep(i32 %55)
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"Parent PID is %zd\0A\00"
@.str.1 = [12 x i8] c"Fork failed\00"
@.str.2 = [16 x i8] c"New Child: %zd\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = load i8, i8* %4
%6 = tail call i64 (...) @getpid()
%7 = trunc i64 %6 to i32
%8 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = tail call i32 @fill_some(i32 %11, i8 signext %5)
%13 = icmp sgt i32 %0, 3
br i1 %13, label %16, label %14
14: ; preds = %35, %2
%15 = tail call i32 @sleep(i32 %0)
ret i32 0
16: ; preds = %2, %35
%17 = phi i32 [ %36, %35 ], [ 3, %2 ]
%18 = tail call i64 (...) @fork()
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @exit(i32 2)
unreachable
23: ; preds = %16
%24 = icmp eq i64 %18, 0
br i1 %24, label %25, label %35
25: ; preds = %23
%26 = tail call i64 (...) @getpid()
%27 = trunc i64 %26 to i32
%28 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = zext i32 %17 to i64
%30 = getelementptr inbounds i8*, i8** %1, i64 %29
%31 = load i8*, i8** %30
%32 = tail call i32 @atoi(i8* %31)
%33 = tail call i32 @fill_some(i32 %32, i8 signext %5)
%34 = tail call i32 @exit(i32 0)
unreachable
35: ; preds = %23
%36 = add nuw nsw i32 %17, 1
%37 = icmp eq i32 %36, %0
br i1 %37, label %14, label %16
}