#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*/ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  pid_t pid;
  int i;
  printf("Let's create some zombies!\n");
  for (i = 0; i < 10; i++) {
    if ((pid = fork()) < 0) {
      fprintf(stderr, "%s: fork error: %s\n", argv[0], strerror(errno));
      exit(1);
    }
    if (pid == 0) {
      exit(0);
    } else {
      printf("====\n");
      system("ps a | grep a.ou[t]");
      sleep(2);
      if (i % 2)
        wait(NULL);
    }
  }
  printf("That's enough zombies. Let's have init clean them up.\n");
  printf("Remember to run 'ps a | grep a.ou[t]' to verify.\n");
  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)
lea    0xe63(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0xa,-0x1c(%rbp)
jge    1276 <main+0xf6>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    120d <main+0x8d>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xe1e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    121f <main+0x9f>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xe0a(%rip),%rdi
call   1040 <printf@plt>
lea    0xe04(%rip),%rdi
call   1030 <system@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     125e <main+0xde>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
jmp    1263 <main+0xe3>
jmp    1268 <main+0xe8>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a9 <main+0x29>
lea    0xdcd(%rip),%rdi
call   1040 <printf@plt>
lea    0xdf8(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe97(%rip),%rdi
call   1030 <puts@plt>
lea    0xf0d(%rip),%r15
lea    0xe70(%rip),%rbx
xor    %ebp,%ebp
jmp    11b8 <main+0x38>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    $0xa,%ebp
je     11f3 <main+0x73>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     1212 <main+0x92>
je     124a <main+0xca>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
test   $0x1,%bpl
je     11b0 <main+0x30>
xor    %edi,%edi
call   1090 <wait@plt>
jmp    11b0 <main+0x30>
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%ebx
mov    (%r14),%r14
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdcd(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"Let's create some zombies!\0A\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"%s: fork error: %s\0A\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"====\0A\00"
@.str.3 = [20 x i8] c"ps a | grep a.ou[t]\00"
@.str.4 = [55 x i8] c"That's enough zombies. Let's have init clean them up.\0A\00"
@.str.5 = [50 x i8] c"Remember to run 'ps a | grep a.ou[t]' to verify.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %7
br label %9
9: ; preds = %40, %2
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 10
br i1 %11, label %12, label %43
12: ; preds = %9
%13 = call i64 (...) @fork()
store i64 %13, i64* %6
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %19, i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %12
%25 = load i64, i64* %6
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %24
%30 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @system(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @sleep(i32 2)
%33 = load i32, i32* %7
%34 = srem i32 %33, 2
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @wait(i32* null)
br label %38
38: ; preds = %36, %29
br label %39
39: ; preds = %38
br label %40
40: ; preds = %39
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %9
43: ; preds = %9
%44 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0))
%46 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [20 x i8] c"%s: fork error: %s\0A\00"
@errno = global i32 0
@.str.3 = [20 x i8] c"ps a | grep a.ou[t]\00"
@str = [27 x i8] c"Let's create some zombies!\00"
@str.6 = [54 x i8] c"That's enough zombies. Let's have init clean them up.\00"
@str.7 = [49 x i8] c"Remember to run 'ps a | grep a.ou[t]' to verify.\00"
@str.8 = [5 x i8] c"====\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %4
4: ; preds = %2, %27
%5 = phi i32 [ 0, %2 ], [ %28, %27 ]
%6 = tail call i64 (...) @fork()
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %4
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %10, i8* %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %4
%16 = icmp eq i64 %6, 0
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = tail call i32 @exit(i32 0)
unreachable
19: ; preds = %15
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.8, i64 0, i64 0))
%21 = tail call i32 @system(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%22 = tail call i32 @sleep(i32 2)
%23 = and i32 %5, 1
%24 = icmp eq i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %19
%26 = tail call i32 @wait(i32* null)
br label %27
27: ; preds = %25, %19
%28 = add nuw nsw i32 %5, 1
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %4
30: ; preds = %27
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str.6, i64 0, i64 0))
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.7, i64 0, i64 0))
%33 = tail call i32 @exit(i32 0)
unreachable
}