#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 */
long fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ getpid();
scalar_t__ getppid();
long parse_integer(char *);
int /*<<< orphan*/ printf(char *, long, long, long);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "This program expects one argument.\n");
    return 1;
  }
  int fork_failed = 0;
  long children_count = parse_integer(argv[1]);
  for (long i = 0; i < children_count; ++i) {
    long child = fork();
    if (child < 0) {
      fprintf(stderr, "Failed to fork() child #%zu: %m\n", i + 1);
      fork_failed = 1;
    } else if (child == 0) {
      printf("child #%zu pid %ld ppid %ld\n", i + 1, (long)getpid(),
             (long)getppid());
      return 0;
    }
  }
  return fork_failed ? 1 : 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)
cmpl   $0x2,-0x8(%rbp)
je     11a3 <main+0x43>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    128c <main+0x12c>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <parse_integer@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jge    1279 <main+0x119>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    1212 <main+0xb2>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x14(%rbp)
jmp    1263 <main+0x103>
cmpq   $0x0,-0x30(%rbp)
jne    125e <main+0xfe>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdf8(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    128c <main+0x12c>
jmp    1263 <main+0x103>
jmp    1268 <main+0x108>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11c3 <main+0x63>
mov    -0x14(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1211 <main+0xb1>
mov    0x8(%rsi),%rdi
call   1060 <parse_integer@plt>
test   %rax,%rax
jle    122f <main+0xcf>
mov    %rax,%r14
mov    $0x1,%bpl
mov    0x2e4d(%rip),%r13
lea    0xe8a(%rip),%r15
xor    %ebx,%ebx
xor    %r12d,%r12d
jmp    11d4 <main+0x74>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%edi
add    $0x1,%rbx
mov    %r15,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%r12d
cmp    %r14,%rbx
setl   %bpl
je     1234 <main+0xd4>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11b0 <main+0x50>
add    $0x1,%rbx
test   %rax,%rax
jne    11cb <main+0x6b>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xe44(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %rax,%rcx
call   1040 <printf@plt>
jmp    1234 <main+0xd4>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    1244 <main+0xe4>
xor    %r12d,%r12d
xor    %ebp,%ebp
test   %r12d,%r12d
setne  %al
not    %bpl
and    %al,%bpl
movzbl %bpl,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [36 x i8] c"This program expects one argument.\0A\00"
@.str.1 = [33 x i8] c"Failed to fork() child #%zu: %m\0A\00"
@.str.2 = [29 x i8] c"child #%zu pid %ld ppid %ld\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 i64
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %52
15: ; preds = %2
store i32 0, i32* %6
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i64 @parse_integer(i8* %18)
store i64 %19, i64* %7
store i64 0, i64* %8
br label %20
20: ; preds = %44, %15
%21 = load i64, i64* %8
%22 = load i64, i64* %7
%23 = icmp slt i64 %21, %22
br i1 %23, label %24, label %47
24: ; preds = %20
%25 = call i64 (...) @fork()
store i64 %25, i64* %9
%26 = load i64, i64* %9
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = load i64, i64* %8
%31 = add nsw i64 %30, 1
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i64 %31)
store i32 1, i32* %6
br label %43
33: ; preds = %24
%34 = load i64, i64* %9
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %33
%37 = load i64, i64* %8
%38 = add nsw i64 %37, 1
%39 = call i64 (...) @getpid()
%40 = call i64 (...) @getppid()
%41 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i64 %38, i64 %39, i64 %40)
store i32 0, i32* %3
br label %52
42: ; preds = %33
br label %43
43: ; preds = %42, %28
br label %44
44: ; preds = %43
%45 = load i64, i64* %8
%46 = add nsw i64 %45, 1
store i64 %46, i64* %8
br label %20
47: ; preds = %20
%48 = load i32, i32* %6
%49 = icmp ne i32 %48, 0
%50 = zext i1 %49 to i64
%51 = select i1 %49, i32 1, i32 0
store i32 %51, i32* %3
br label %52
52: ; preds = %47, %36, %12
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [36 x i8] c"This program expects one argument.\0A\00"
@.str.1 = [33 x i8] c"Failed to fork() child #%zu: %m\0A\00"
@.str.2 = [29 x i8] c"child #%zu pid %ld ppid %ld\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %41
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i64 @parse_integer(i8* %9)
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %34
12: ; preds = %7, %29
%13 = phi i1 [ %32, %29 ], [ true, %7 ]
%14 = phi i64 [ %30, %29 ], [ 0, %7 ]
%15 = phi i32 [ %31, %29 ], [ 0, %7 ]
%16 = tail call i64 (...) @fork()
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = add nuw nsw i64 %14, 1
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i64 %20)
br label %29
22: ; preds = %12
%23 = icmp eq i64 %16, 0
%24 = add nuw nsw i64 %14, 1
br i1 %23, label %25, label %29
25: ; preds = %22
%26 = tail call i64 (...) @getpid()
%27 = tail call i64 (...) @getppid()
%28 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i64 %24, i64 %26, i64 %27)
br label %34
29: ; preds = %22, %18
%30 = phi i64 [ %20, %18 ], [ %24, %22 ]
%31 = phi i32 [ 1, %18 ], [ %15, %22 ]
%32 = icmp slt i64 %30, %10
%33 = icmp eq i64 %30, %10
br i1 %33, label %34, label %12
34: ; preds = %29, %7, %25
%35 = phi i32 [ %15, %25 ], [ 0, %7 ], [ %31, %29 ]
%36 = phi i1 [ %13, %25 ], [ false, %7 ], [ %32, %29 ]
%37 = icmp ne i32 %35, 0
%38 = xor i1 %36, true
%39 = select i1 %38, i1 %37, i1 false
%40 = zext i1 %39 to i32
br label %41
41: ; preds = %34, %4
%42 = phi i32 [ 1, %4 ], [ %40, %34 ]
ret i32 %42
}