#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 atol(char const *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, const char *argv[]) {
  int count, i;
  pid_t pid;
  count = (argc > 1) ? atol(argv[1]) : 10;
  srand(time(NULL));
  for (i = 0; i < count; i++) {
    if ((pid = fork()) < 0) {
      perror("fork");
      exit(1);
    }
    if (pid != 0)
      continue;
    printf("%d\n", rand());
    exit(0);
  }
  for (i = 0; i < count; i++) {
    wait(NULL);
  }
  return 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)
cmpl   $0x1,-0x8(%rbp)
jle    11c5 <main+0x35>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atol@plt>
mov    %eax,-0x24(%rbp)
jmp    11d2 <main+0x42>
mov    $0xa,%eax
mov    %eax,-0x24(%rbp)
jmp    11d2 <main+0x42>
mov    -0x24(%rbp),%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1260 <main+0xd0>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    1226 <main+0x96>
lea    0xde9(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
je     1236 <main+0xa6>
jmp    1252 <main+0xc2>
mov    $0x0,%al
call   10b0 <rand@plt>
mov    %eax,%esi
lea    0xdbf(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ef <main+0x5f>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    128a <main+0xfa>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1267 <main+0xd7>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0xa,%ebx
cmp    $0x2,%edi
jl     11a8 <main+0x18>
mov    0x8(%rsi),%rdi
call   1060 <atol@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
test   %ebx,%ebx
jle    11ec <main+0x5c>
mov    %ebx,%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11f5 <main+0x65>
je     120b <main+0x7b>
add    $0xffffffff,%ebp
jne    11c0 <main+0x30>
test   %ebx,%ebx
jle    11ec <main+0x5c>
nopw   0x0(%rax,%rax,1)
xor    %edi,%edi
call   1090 <wait@plt>
add    $0xffffffff,%ebx
jne    11e0 <main+0x50>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe04(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   10b0 <rand@plt>
lea    0xdec(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @atol(i8* %14)
br label %17
16: ; preds = %2
br label %17
17: ; preds = %16, %11
%18 = phi i32 [ %15, %11 ], [ 10, %16 ]
store i32 %18, i32* %6
%19 = call i32 @time(i32* null)
%20 = call i32 @srand(i32 %19)
store i32 0, i32* %7
br label %21
21: ; preds = %39, %17
%22 = load i32, i32* %7
%23 = load i32, i32* %6
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %42
25: ; preds = %21
%26 = call i64 (...) @fork()
store i64 %26, i64* %8
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %25
%32 = load i64, i64* %8
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %31
br label %39
35: ; preds = %31
%36 = call i32 (...) @rand()
%37 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %34
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
br label %21
42: ; preds = %21
store i32 0, i32* %7
br label %43
43: ; preds = %49, %42
%44 = load i32, i32* %7
%45 = load i32, i32* %6
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %52
47: ; preds = %43
%48 = call i32 @wait(i32* null)
br label %49
49: ; preds = %47
%50 = load i32, i32* %7
%51 = add nsw i32 %50, 1
store i32 %51, i32* %7
br label %43
52: ; preds = %43
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atol(i8* %6)
br label %8
8: ; preds = %2, %4
%9 = phi i32 [ %7, %4 ], [ 10, %2 ]
%10 = tail call i32 @time(i32* null)
%11 = tail call i32 @srand(i32 %10)
%12 = icmp sgt i32 %9, 0
br i1 %12, label %16, label %35
13: ; preds = %23
%14 = icmp eq i32 %25, %9
br i1 %14, label %15, label %16
15: ; preds = %13
br i1 %12, label %30, label %35
16: ; preds = %8, %13
%17 = phi i32 [ %25, %13 ], [ 0, %8 ]
%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 ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = icmp eq i64 %18, 0
%25 = add nuw nsw i32 %17, 1
br i1 %24, label %26, label %13
26: ; preds = %23
%27 = tail call i32 (...) @rand()
%28 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = tail call i32 @exit(i32 0)
unreachable
30: ; preds = %15, %30
%31 = phi i32 [ %33, %30 ], [ 0, %15 ]
%32 = tail call i32 @wait(i32* null)
%33 = add nuw nsw i32 %31, 1
%34 = icmp eq i32 %33, %9
br i1 %34, label %35, label %30
35: ; preds = %30, %8, %15
ret i32 0
}