#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;
  int cont = 0;
  int p;
  for (i = 0; i < 2; i++) {
    p = fork();
    if (p > 0) {
      cont++;
      p = fork();
    }
    if (p == 0)
      cont = 0;
  }
  printf("Il sono il processo con pid %d  cicli: %d \n", getpid(), cont);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a2 <main+0x72>
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    117e <main+0x4e>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    118f <main+0x5f>
movl   $0x0,-0xc(%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    -0xc(%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    %ebx,%ebx
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
jle    114a <main+0x1a>
xor    %eax,%eax
call   1050 <fork@plt>
mov    $0x1,%ebx
test   %eax,%eax
cmove  %eax,%ebx
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
jle    1164 <main+0x34>
add    $0x1,%ebx
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
cmove  %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe89(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [44 x i8] c"Il sono il processo con pid %d cicli: %d \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* %3
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 = call i32 (...) @fork()
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = load i32, i32* %3
%14 = add nsw i32 %13, 1
store i32 %14, i32* %3
%15 = call i32 (...) @fork()
store i32 %15, i32* %4
br label %16
16: ; preds = %12, %8
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 0, i32* %3
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* %3
%27 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %25, i32 %26)
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [44 x i8] c"Il sono il processo con pid %d cicli: %d \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 [ 1, %3 ], [ 0, %0 ]
%7 = phi i32 [ %4, %3 ], [ %1, %0 ]
%8 = icmp eq i32 %7, 0
%9 = select i1 %8, i32 0, i32 %6
%10 = tail call i32 (...) @fork()
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %5
%13 = add nuw nsw i32 %9, 1
%14 = tail call i32 (...) @fork()
br label %15
15: ; preds = %12, %5
%16 = phi i32 [ %13, %12 ], [ %9, %5 ]
%17 = phi i32 [ %14, %12 ], [ %10, %5 ]
%18 = icmp eq i32 %17, 0
%19 = select i1 %18, i32 0, i32 %16
%20 = tail call i32 (...) @getpid()
%21 = tail call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %19)
ret i32 0
}