#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 EXIT_FAILURE;
int EXIT_SUCCESS;
scalar_t__ RAND_MAX;
int WEXITSTATUS(int);
int atoi(char *);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int getppid();
int *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
double rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ wait(int *);
int main(int argc, char **argv) {
  srand(time(NULL));
  if (argc <= 1) {
    perror("./procstr [nbproc]");
    exit(EXIT_FAILURE);
  }
  int nb = atoi(argv[1]);
  pid_t *pids = malloc((nb + 1) * sizeof(pid_t));
  if (!pids) {
    perror("cant allocate memory");
    exit(EXIT_FAILURE);
  }
  pids[0] = getpid();
  printf("initial process %d\n", pids[0]);
  int i, r, status;
  for (i = 1; i <= nb; i++) {
    pid_t t;
    if ((t = fork()) == 0) {
      pids[i] = getpid();
      if (i == nb) {
        int j;
        r = (int)(rand() / (((double)RAND_MAX + 1) / 100));
        for (j = 0; j <= nb; j++)
          printf("%d: %d . %d\n", getpid(), j, pids[j]);
        printf("rand : %d\n", r);
        exit(r);
      }
    } else if (t == -1) {
      perror("cannot create a processus");
      exit(EXIT_FAILURE);
    } else {
      wait(&status);
      r = WEXITSTATUS(status);
      printf("%d: father %d, child %d\n", getpid(), getppid(), t);
      if (i == 1)
        printf("rand : %d\n", r);
      exit(r);
    }
  }
  return EXIT_SUCCESS;
}


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)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
cmpl   $0x1,-0x8(%rbp)
jg     121a <main+0x4a>
lea    0xe09(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1266 <main+0x96>
lea    0xdd0(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     13fe <main+0x22e>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jne    1369 <main+0x199>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edx
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1364 <main+0x194>
mov    $0x0,%al
call   10f0 <rand@plt>
mov    0x2d0b(%rip),%rax
cvtsi2sdq (%rax),%xmm1
movsd  0xd1e(%rip),%xmm2
addsd  %xmm2,%xmm1
movsd  0xd0a(%rip),%xmm2
divsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     134b <main+0x17b>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x34(%rbp),%edx
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xd17(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    130c <main+0x13c>
mov    -0x28(%rbp),%esi
lea    0xd05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%edi
call   10c0 <exit@plt>
jmp    13eb <main+0x21b>
cmpl   $0xffffffff,-0x30(%rbp)
jne    138d <main+0x1bd>
lea    0xceb(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c52(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x2c(%rbp),%rdi
call   10d0 <wait@plt>
mov    -0x2c(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   10b0 <getppid@plt>
mov    -0x38(%rbp),%esi
mov    %eax,%edx
mov    -0x30(%rbp),%ecx
lea    0xcbe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x1,-0x24(%rbp)
jne    13e3 <main+0x213>
mov    -0x28(%rbp),%esi
lea    0xc7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%edi
call   10c0 <exit@plt>
jmp    13f0 <main+0x220>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1290 <main+0xc0>
mov    0x2bcb(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
cmp    $0x1,%ebp
jle    124a <main+0x7a>
mov    0x8(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebp
lea    0x4(,%rbp,4),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1253 <main+0x83>
mov    %rax,%rbx
mov    %ebp,%r12d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,(%rbx)
lea    0xe12(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %r12d,%r12d
jg     125c <main+0x8c>
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbf(%rip),%rdi
jmp    12a3 <main+0xd3>
lea    0xdc9(%rip),%rdi
jmp    12a3 <main+0xd3>
mov    $0x1,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
jne    1291 <main+0xc1>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,(%rbx,%r15,4)
cmp    %r15,%r12
je     12b6 <main+0xe6>
add    $0x1,%r15
jmp    1270 <main+0xa0>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    132e <main+0x15e>
lea    0xdc2(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
xor    %eax,%eax
call   10f0 <rand@plt>
mov    0x2d24(%rip),%rax
cvtsi2sdq (%rax),%xmm1
addsd  0xd2f(%rip),%xmm1
divsd  0xd2f(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%r14d
test   %r12d,%r12d
js     1315 <main+0x145>
add    $0x1,%r12d
lea    0xd5b(%rip),%r15
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
mov    (%rbx,%rbp,4),%ecx
mov    %r15,%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%r12
jne    12f4 <main+0x124>
lea    0xd3e(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%edi
call   10c0 <exit@plt>
lea    0xc(%rsp),%rdi
call   10d0 <wait@plt>
mov    0xc(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10b0 <getppid@plt>
lea    0xd24(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x1,%r15d
jne    137f <main+0x1af>
lea    0xce5(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"./procstr [nbproc]\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [21 x i8] c"cant allocate memory\00"
@.str.2 = [21 x i8] c"initial process\C2\A0%d\0A\00"
@RAND_MAX = global i64 0
@.str.3 = [13 x i8] c"%d: %d . %d\0A\00"
@.str.4 = [11 x i8] c"rand : %d\0A\00"
@.str.5 = [26 x i8] c"cannot create a processus\00"
@.str.6 = [25 x i8] c"%d: father %d, child %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i32 @time(i32* null)
%14 = call i32 @srand(i32 %13)
%15 = load i32, i32* %4
%16 = icmp sle i32 %15, 1
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = mul i64 %28, 4
%30 = trunc i64 %29 to i32
%31 = call i32* @malloc(i32 %30)
store i32* %31, i32** %7
%32 = load i32*, i32** %7
%33 = icmp ne i32* %32, null
br i1 %33, label %38, label %34
34: ; preds = %21
%35 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %21
%39 = call i32 (...) @getpid()
%40 = load i32*, i32** %7
%41 = getelementptr inbounds i32, i32* %40, i64 0
store i32 %39, i32* %41
%42 = load i32*, i32** %7
%43 = getelementptr inbounds i32, i32* %42, i64 0
%44 = load i32, i32* %43
%45 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %44)
store i32 1, i32* %8
br label %46
46: ; preds = %116, %38
%47 = load i32, i32* %8
%48 = load i32, i32* %6
%49 = icmp sle i32 %47, %48
br i1 %49, label %50, label %119
50: ; preds = %46
%51 = call i32 (...) @fork()
store i32 %51, i32* %11
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %92
53: ; preds = %50
%54 = call i32 (...) @getpid()
%55 = load i32*, i32** %7
%56 = load i32, i32* %8
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %55, i64 %57
store i32 %54, i32* %58
%59 = load i32, i32* %8
%60 = load i32, i32* %6
%61 = icmp eq i32 %59, %60
br i1 %61, label %62, label %91
62: ; preds = %53
%63 = call double (...) @rand()
%64 = load i64, i64* @RAND_MAX
%65 = sitofp i64 %64 to double
%66 = fadd double %65, 1.000000e+00
%67 = fdiv double %66, 1.000000e+02
%68 = fdiv double %63, %67
%69 = fptosi double %68 to i32
store i32 %69, i32* %9
store i32 0, i32* %12
br label %70
70: ; preds = %83, %62
%71 = load i32, i32* %12
%72 = load i32, i32* %6
%73 = icmp sle i32 %71, %72
br i1 %73, label %74, label %86
74: ; preds = %70
%75 = call i32 (...) @getpid()
%76 = load i32, i32* %12
%77 = load i32*, i32** %7
%78 = load i32, i32* %12
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i32, i32* %77, i64 %79
%81 = load i32, i32* %80
%82 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %75, i32 %76, i32 %81)
br label %83
83: ; preds = %74
%84 = load i32, i32* %12
%85 = add nsw i32 %84, 1
store i32 %85, i32* %12
br label %70
86: ; preds = %70
%87 = load i32, i32* %9
%88 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %87)
%89 = load i32, i32* %9
%90 = call i32 @exit(i32 %89)
unreachable
91: ; preds = %53
br label %115
92: ; preds = %50
%93 = load i32, i32* %11
%94 = icmp eq i32 %93, -1
br i1 %94, label %95, label %99
95: ; preds = %92
%96 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%97 = load i32, i32* @EXIT_FAILURE
%98 = call i32 @exit(i32 %97)
unreachable
99: ; preds = %92
%100 = call i32 @wait(i32* %10)
%101 = load i32, i32* %10
%102 = call i32 @WEXITSTATUS(i32 %101)
store i32 %102, i32* %9
%103 = call i32 (...) @getpid()
%104 = call i32 (...) @getppid()
%105 = load i32, i32* %11
%106 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i32 %103, i32 %104, i32 %105)
%107 = load i32, i32* %8
%108 = icmp eq i32 %107, 1
br i1 %108, label %109, label %112
109: ; preds = %99
%110 = load i32, i32* %9
%111 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %110)
br label %112
112: ; preds = %109, %99
%113 = load i32, i32* %9
%114 = call i32 @exit(i32 %113)
unreachable
115: ; preds = %91
br label %116
116: ; preds = %115
%117 = load i32, i32* %8
%118 = add nsw i32 %117, 1
store i32 %118, i32* %8
br label %46
119: ; preds = %46
%120 = load i32, i32* @EXIT_SUCCESS
ret i32 %120
}

LLVM IR (-O2):
@.str = [19 x i8] c"./procstr [nbproc]\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [21 x i8] c"cant allocate memory\00"
@.str.2 = [21 x i8] c"initial process\C2\A0%d\0A\00"
@RAND_MAX = global i64 0
@.str.3 = [13 x i8] c"%d: %d . %d\0A\00"
@.str.4 = [11 x i8] c"rand : %d\0A\00"
@.str.5 = [26 x i8] c"cannot create a processus\00"
@.str.6 = [25 x i8] c"%d: father %d, child %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = tail call i32 @time(i32* null)
%5 = tail call i32 @srand(i32 %4)
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = shl i32 %14, 2
%16 = add i32 %15, 4
%17 = tail call i32* @malloc(i32 %16)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %11
%20 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %11
%24 = tail call i32 (...) @getpid()
store i32 %24, i32* %17
%25 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = bitcast i32* %3 to i8*
%27 = icmp slt i32 %14, 1
br i1 %27, label %80, label %28
28: ; preds = %23
%29 = zext i32 %14 to i64
br label %30
30: ; preds = %78, %28
%31 = phi i64 [ 1, %28 ], [ %79, %78 ]
%32 = tail call i32 (...) @fork()
switch i32 %32, label %65 [
i32 0, label %33
i32 -1, label %61
]
33: ; preds = %30
%34 = tail call i32 (...) @getpid()
%35 = getelementptr inbounds i32, i32* %17, i64 %31
store i32 %34, i32* %35
%36 = icmp eq i64 %31, %29
br i1 %36, label %37, label %78
37: ; preds = %33
%38 = tail call double (...) @rand()
%39 = load i64, i64* @RAND_MAX
%40 = sitofp i64 %39 to double
%41 = fadd double %40, 1.000000e+00
%42 = fdiv double %41, 1.000000e+02
%43 = fdiv double %38, %42
%44 = fptosi double %43 to i32
%45 = icmp slt i32 %14, 0
br i1 %45, label %58, label %46
46: ; preds = %37
%47 = add nuw i32 %14, 1
%48 = zext i32 %47 to i64
br label %49
49: ; preds = %46, %49
%50 = phi i64 [ 0, %46 ], [ %56, %49 ]
%51 = tail call i32 (...) @getpid()
%52 = getelementptr inbounds i32, i32* %17, i64 %50
%53 = load i32, i32* %52
%54 = trunc i64 %50 to i32
%55 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %51, i32 %54, i32 %53)
%56 = add nuw nsw i64 %50, 1
%57 = icmp eq i64 %56, %48
br i1 %57, label %58, label %49
58: ; preds = %49, %37
%59 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %44)
%60 = tail call i32 @exit(i32 %44)
unreachable
61: ; preds = %30
%62 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%63 = load i32, i32* @EXIT_FAILURE
%64 = tail call i32 @exit(i32 %63)
unreachable
65: ; preds = %30
%66 = trunc i64 %31 to i32
%67 = call i32 @wait(i32* %3)
%68 = load i32, i32* %3
%69 = call i32 @WEXITSTATUS(i32 %68)
%70 = call i32 (...) @getpid()
%71 = call i32 (...) @getppid()
%72 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i32 %70, i32 %71, i32 %32)
%73 = icmp eq i32 %66, 1
br i1 %73, label %74, label %76
74: ; preds = %65
%75 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %69)
br label %76
76: ; preds = %74, %65
%77 = call i32 @exit(i32 %69)
unreachable
78: ; preds = %33
%79 = add nuw nsw i64 %31, 1
br label %30
80: ; preds = %23
%81 = load i32, i32* @EXIT_SUCCESS
ret i32 %81
}