#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ getppid();
int /*<<< orphan*/ printf(char *, long);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid;
  if ((pid = fork()) < 0) {
    fprintf(stderr, "Err.(%s) fork() failed\n", strerror(errno));
    exit(EXIT_FAILURE);
  } else if (pid == 0) {
    if ((pid = fork()) < 0) {
      fprintf(stderr, "Err.(%s) fork() failed\n", strerror(errno));
      exit(EXIT_FAILURE);
    } else if (pid > 0)
      exit(EXIT_SUCCESS);
    sleep(2);
    printf("Secondo figlio, PPID = %ld\n", (long)getppid());
    exit(EXIT_SUCCESS);
  }
  if (waitpid(pid, NULL, 0) != pid) {
    fprintf(stderr, "Err.(%s) waitpid() failed\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  return (EXIT_SUCCESS);
}


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)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11e5 <main+0x65>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe2e(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    128b <main+0x10b>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    123f <main+0xbf>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xdd4(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    1258 <main+0xd8>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    125d <main+0xdd>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %rax,%rsi
lea    0xda0(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    1290 <main+0x110>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    -0x18(%rbp),%rax
je     12e3 <main+0x163>
mov    0x2d30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xd64(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2cde(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11ba <main+0x3a>
je     11ae <main+0x2e>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    %rbx,%rax
jne    11da <main+0x5a>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    1210 <main+0x90>
mov    0x2e1f(%rip),%rax
mov    (%rax),%ebx
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe28(%rip),%rsi
jmp    11f8 <main+0x78>
mov    0x2dff(%rip),%rax
mov    (%rax),%ebx
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe3c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jne    1232 <main+0xb2>
mov    $0x2,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xdee(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"Err.(%s) fork() failed\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [28 x i8] c"Secondo figlio, PPID = %ld\0A\00"
@.str.2 = [27 x i8] c"Err.(%s) waitpid() failed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = load i64, i64* %6
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %42
19: ; preds = %16
%20 = call i64 (...) @fork()
store i64 %20, i64* %6
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %19
%30 = load i64, i64* %6
%31 = icmp sgt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %29
br label %36
36: ; preds = %35
%37 = call i32 @sleep(i32 2)
%38 = call i64 (...) @getppid()
%39 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i64 %38)
%40 = load i32, i32* @EXIT_SUCCESS
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %16
br label %43
43: ; preds = %42
%44 = load i64, i64* %6
%45 = call i64 @waitpid(i64 %44, i32* null, i32 0)
%46 = load i64, i64* %6
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %55
48: ; preds = %43
%49 = load i32, i32* @stderr
%50 = load i32, i32* @errno
%51 = call i8* @strerror(i32 %50)
%52 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %51)
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %43
%56 = load i32, i32* @EXIT_SUCCESS
ret i32 %56
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"Err.(%s) fork() failed\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [28 x i8] c"Secondo figlio, PPID = %ld\0A\00"
@.str.2 = [27 x i8] c"Err.(%s) waitpid() failed\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = icmp eq i64 %3, 0
br i1 %13, label %14, label %35
14: ; preds = %12
%15 = tail call i64 (...) @fork()
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %14
%25 = icmp eq i64 %15, 0
br i1 %25, label %29, label %26
26: ; preds = %24
%27 = load i32, i32* @EXIT_SUCCESS
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %24
%30 = tail call i32 @sleep(i32 2)
%31 = tail call i64 (...) @getppid()
%32 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i64 %31)
%33 = load i32, i32* @EXIT_SUCCESS
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %12
%36 = tail call i64 @waitpid(i64 %3, i32* null, i32 0)
%37 = icmp eq i64 %36, %3
br i1 %37, label %45, label %38
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = tail call i8* @strerror(i32 %40)
%42 = tail call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %41)
%43 = load i32, i32* @EXIT_FAILURE
%44 = tail call i32 @exit(i32 %43)
unreachable
45: ; preds = %35
%46 = load i32, i32* @EXIT_SUCCESS
ret i32 %46
}