#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, cont = 0;
  for (i = 0; i < 2; i++) {
    cont++;
    p = fork();
    if (p > 0)
      p = fork();
    if (p == 0) {
      cont = 0;
    }
  }
  printf("Sono il processo %d, ho eseguito %d cicli\n", getpid(), 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    11a2 <main+0x72>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    117e <main+0x4e>
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    118f <main+0x5f>
movl   $0x0,-0x10(%rbp)
jmp    1194 <main+0x64>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    114d <main+0x1d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%edx
lea    0xe4b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
jle    1143 <main+0x13>
xor    %eax,%eax
call   1050 <fork@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
jle    1157 <main+0x27>
xor    %eax,%eax
call   1050 <fork@plt>
cmp    $0x1,%ebx
mov    $0x2,%ebx
sbb    $0x0,%ebx
test   %eax,%eax
cmove  %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe8b(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

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

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