#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 */
int atoi(char *);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ wait();
int main(int argc, char *argv[]) {
  int i = 0;
  int n = 0;
  int pid = 0;
  if (argc == 2)
    n = atoi(argv[1]);
  else if (argc > 2) {
    printf("uso scorretto, non verranno generati thread\n");
    printf("uso : nthread [number of threads]\n\n");
  } else {
    printf("numero di thread da creare:\n");
    scanf("%d", &n);
  }
  printf("pid padre: %d\n", getpid());
  while (i < n && !pid) {
    pid = fork();
    wait();
    i++;
  }
  if (pid) {
    printf("\tpid figlio: %d\t padre: %d\n", getpid(), getppid());
  }
  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)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11ba <main+0x4a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    1208 <main+0x98>
cmpl   $0x2,-0x8(%rbp)
jle    11e5 <main+0x75>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1203 <main+0x93>
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe74(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <scanf@plt>
jmp    1208 <main+0x98>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%ecx
xor    %eax,%eax
cmp    -0x18(%rbp),%ecx
mov    %al,-0x1d(%rbp)
jge    123c <main+0xcc>
cmpl   $0x0,-0x1c(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    124c <main+0xdc>
jmp    126b <main+0xfb>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1080 <wait@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    121f <main+0xaf>
cmpl   $0x0,-0x1c(%rbp)
je     1299 <main+0x129>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
cmp    $0x2,%edi
jne    119f <main+0x1f>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,0x4(%rsp)
jmp    11d8 <main+0x58>
jle    11bb <main+0x3b>
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
lea    0xec2(%rip),%rdi
call   1030 <puts@plt>
jmp    11d8 <main+0x58>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
lea    0xe32(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1060 <scanf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe1d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmpl   $0x0,0x4(%rsp)
jle    1243 <main+0xc3>
mov    $0x1,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <wait@plt>
cmp    0x4(%rsp),%ebp
jge    121d <main+0x9d>
add    $0x1,%ebp
test   %ebx,%ebx
je     1200 <main+0x80>
test   %ebx,%ebx
je     1243 <main+0xc3>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xdda(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [45 x i8] c"uso scorretto, non verranno generati thread\0A\00"
@.str.1 = [36 x i8] c"uso : nthread [number of threads]\0A\0A\00"
@.str.2 = [29 x i8] c"numero di thread da creare:\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [15 x i8] c"pid padre: %d\0A\00"
@.str.5 = [28 x i8] c"\09pid figlio: %d\09 padre: %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 i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 2
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 @atoi(i8* %14)
store i32 %15, i32* %7
br label %26
16: ; preds = %2
%17 = load i32, i32* %4
%18 = icmp sgt i32 %17, 2
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %25
22: ; preds = %16
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %7)
br label %25
25: ; preds = %22, %19
br label %26
26: ; preds = %25, %11
%27 = call i32 (...) @getpid()
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %39, %26
%30 = load i32, i32* %6
%31 = load i32, i32* %7
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i32, i32* %8
%35 = icmp ne i32 %34, 0
%36 = xor i1 %35, true
br label %37
37: ; preds = %33, %29
%38 = phi i1 [ false, %29 ], [ %36, %33 ]
br i1 %38, label %39, label %44
39: ; preds = %37
%40 = call i32 (...) @fork()
store i32 %40, i32* %8
%41 = call i32 (...) @wait()
%42 = load i32, i32* %6
%43 = add nsw i32 %42, 1
store i32 %43, i32* %6
br label %29
44: ; preds = %37
%45 = load i32, i32* %8
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = call i32 (...) @getpid()
%49 = call i32 (...) @getppid()
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %48, i32 %49)
br label %51
51: ; preds = %47, %44
ret i32 0
}

LLVM IR (-O2):
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [15 x i8] c"pid padre: %d\0A\00"
@.str.5 = [28 x i8] c"\09pid figlio: %d\09 padre: %d\0A\00"
@str = [28 x i8] c"numero di thread da creare:\00"
@str.6 = [44 x i8] c"uso scorretto, non verranno generati thread\00"
@str.7 = [35 x i8] c"uso : nthread [number of threads]\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = icmp eq i32 %0, 2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
store i32 %9, i32* %3
br label %18
10: ; preds = %2
%11 = icmp sgt i32 %0, 2
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.6, i64 0, i64 0))
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.7, i64 0, i64 0))
br label %18
15: ; preds = %10
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%17 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
br label %18
18: ; preds = %12, %15, %6
%19 = call i32 (...) @getpid()
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %3
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %18, %23
%24 = phi i32 [ %27, %23 ], [ 0, %18 ]
%25 = call i32 (...) @fork()
%26 = call i32 (...) @wait()
%27 = add nuw nsw i32 %24, 1
%28 = load i32, i32* %3
%29 = icmp slt i32 %27, %28
%30 = icmp eq i32 %25, 0
%31 = select i1 %29, i1 %30, i1 false
br i1 %31, label %23, label %32
32: ; preds = %23
br i1 %30, label %37, label %33
33: ; preds = %32
%34 = call i32 (...) @getpid()
%35 = call i32 (...) @getppid()
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %34, i32 %35)
br label %37
37: ; preds = %18, %33, %32
ret i32 0
}