#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 fork();
int getpid();
int /*<<< orphan*/ printf(char *, int, int);
int main() {
  int i, p;
  int cont = 0;
  for (i = 0; i < 2; i++) {
    p = fork();
    if (p > 0) {
      fork();
      printf("Sono il processo %d\n Ho eseguito il ciclo %d volte", getpid(),
             cont);
    }
    if (p > 0)
      cont++;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b0 <main+0x80>
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    118a <main+0x5a>
mov    $0x0,%al
call   1050 <fork@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%edx
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
jle    119d <main+0x6d>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11a2 <main+0x72>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    114d <main+0x1d>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1050 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    115e <main+0x2e>
xor    %eax,%eax
call   1050 <fork@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xeab(%rip),%rdi
mov    %eax,%esi
xor    %edx,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
jle    118f <main+0x5f>
xor    %ebp,%ebp
test   %ebx,%ebx
setg   %bpl
xor    %eax,%eax
call   1050 <fork@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe7a(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [51 x i8] c"Sono il processo %d\0A Ho eseguito il ciclo %d volte\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %4
store i32 0, i32* %2
br label %5
5: ; preds = %24, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 2
br i1 %7, label %8, label %27
8: ; preds = %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = call i32 (...) @fork()
%14 = call i32 (...) @getpid()
%15 = load i32, i32* %4
%16 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %15)
br label %17
17: ; preds = %12, %8
%18 = load i32, i32* %3
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
store i32 %22, i32* %4
br label %23
23: ; preds = %20, %17
br label %24
24: ; preds = %23
%25 = load i32, i32* %2
%26 = add nsw i32 %25, 1
store i32 %26, i32* %2
br label %5
27: ; preds = %5
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [51 x i8] c"Sono il processo %d\0A Ho eseguito il ciclo %d volte\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
%2 = icmp sgt i32 %1, 0
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = tail call i32 (...) @fork()
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %5, i32 0)
br label %7
7: ; preds = %3, %0
%8 = tail call i32 (...) @fork()
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = zext i1 %2 to i32
%12 = tail call i32 (...) @fork()
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %11)
br label %15
15: ; preds = %10, %7
ret i32 0
}