#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
scalar_t__ getpid();
scalar_t__ getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int wait(int /*<<< orphan*/ *);
int main() {
  printf("\nSono un processo con pid %d.\n", (int)getpid());
  pid_t child = fork();
  if (child < 0) {
    perror("Fork fallita");
    exit(-1);
  } else if ((int)child == 0) {
    printf("Sono %d, figlio di %d.\n", (int)getpid(), (int)getppid());
  } else {
    printf("Sono ancora %d.\n", (int)getpid());
    if (wait(NULL) == -1) {
      perror("Wait fallita");
      exit(-1);
    }
    printf("Sono %d, sono tornato.\n\n", (int)getpid());
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11bb <main+0x4b>
lea    0xe73(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
cmp    $0x0,%eax
jne    11f1 <main+0x81>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0xc(%rbp),%esi
mov    %eax,%edx
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    124e <main+0xde>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    1230 <main+0xc0>
lea    0xe34(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1253 <main+0xe3>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe7e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11cc <main+0x5c>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%r14
test   %ebx,%ebx
jne    11d5 <main+0x65>
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe76(%rip),%rdi
mov    %r14d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe4c(%rip),%rdi
jmp    11f9 <main+0x89>
lea    0xe68(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    1208 <main+0x98>
lea    0xe5c(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe4c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"\0ASono un processo con pid %d.\0A\00"
@.str.1 = [13 x i8] c"Fork fallita\00"
@.str.2 = [24 x i8] c"Sono %d, figlio di %d.\0A\00"
@.str.3 = [17 x i8] c"Sono ancora %d.\0A\00"
@.str.4 = [13 x i8] c"Wait fallita\00"
@.str.5 = [25 x i8] c"Sono %d, sono tornato.\0A\0A\00"
define i32 @main() {
%1 = alloca i64
%2 = call i64 (...) @getpid()
%3 = trunc i64 %2 to i32
%4 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i64 (...) @fork()
store i64 %5, i64* %1
%6 = load i64, i64* %1
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = load i64, i64* %1
%13 = trunc i64 %12 to i32
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = call i64 (...) @getpid()
%17 = trunc i64 %16 to i32
%18 = call i64 (...) @getppid()
%19 = trunc i64 %18 to i32
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %19)
br label %35
21: ; preds = %11
%22 = call i64 (...) @getpid()
%23 = trunc i64 %22 to i32
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = call i32 @wait(i32* null)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %21
%31 = call i64 (...) @getpid()
%32 = trunc i64 %31 to i32
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %32)
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %15
br label %36
36: ; preds = %35
ret i32 0
}

LLVM IR (-O2):
@.str = [31 x i8] c"\0ASono un processo con pid %d.\0A\00"
@.str.1 = [13 x i8] c"Fork fallita\00"
@.str.2 = [24 x i8] c"Sono %d, figlio di %d.\0A\00"
@.str.3 = [17 x i8] c"Sono ancora %d.\0A\00"
@.str.4 = [13 x i8] c"Wait fallita\00"
@.str.5 = [25 x i8] c"Sono %d, sono tornato.\0A\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @getpid()
%2 = trunc i64 %1 to i32
%3 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
%10 = trunc i64 %4 to i32
%11 = icmp eq i32 %10, 0
%12 = tail call i64 (...) @getpid()
%13 = trunc i64 %12 to i32
br i1 %11, label %14, label %18
14: ; preds = %9
%15 = tail call i64 (...) @getppid()
%16 = trunc i64 %15 to i32
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %16)
ret i32 0
18: ; preds = %9
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%20 = tail call i32 @wait(i32* null)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%24 = tail call i32 @exit(i32 -1)
unreachable
25: ; preds = %18
%26 = tail call i64 (...) @getpid()
%27 = trunc i64 %26 to i32
%28 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%29 = tail call i32 @exit(i32 0)
unreachable
}