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

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ getpid();
scalar_t__ getppid();
int /*<<< orphan*/ printf(char *, long, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int wait(int *);
int main(int argc, char *argv[]) {
  pid_t pid;
  pid_t wpid;
  int status;
  pid = fork();
  if (pid == -1) {
    fprintf(stderr, "%s: Fallimento della fork()\n", strerror(errno));
    exit(13);
  }
  if (pid == 0) {
    printf("PID %ld: Figlio partito, il padre è: %ld\n", (long)getpid(),
           (long)getppid());
  } else {
    printf("PID %ld: Il figlio partito PID %ld\n", (long)getpid(), (long)pid);
    wpid = wait(&status);
    if (wpid == -1) {
      fprintf(stderr, "%s: wait()\n", strerror(errno));
      return 1;
    }
    if (wpid != pid)
      abort();
    else
      printf("PID figlio %ld stato di uscita 0x%04X\n", (long)pid, status);
  }
  sleep(5);
  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   10c0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1200 <main+0x60>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe0f(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xd,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1233 <main+0x93>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xdf4(%rip),%rdi
call   1050 <printf@plt>
jmp    12c8 <main+0x128>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rsi
movslq -0x14(%rbp),%rax
mov    %eax,%edx
lea    0xdfe(%rip),%rdi
call   1050 <printf@plt>
lea    -0x1c(%rbp),%rdi
call   10b0 <wait@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    129d <main+0xfd>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xde0(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12de <main+0x13e>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     12b0 <main+0x110>
mov    $0x0,%al
call   1030 <abort@plt>
movslq -0x14(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xdba(%rip),%rdi
call   1050 <printf@plt>
jmp    12c8 <main+0x128>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     11fa <main+0x5a>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1264 <main+0xc4>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe7f(%rip),%rdi
mov    %rax,%rsi
mov    %ebx,%edx
call   1050 <printf@plt>
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
cmp    $0xffffffff,%eax
je     1235 <main+0x95>
cmp    %ebx,%eax
jne    1296 <main+0xf6>
movslq %ebx,%rsi
mov    0xc(%rsp),%edx
lea    0xe80(%rip),%rdi
jmp    1217 <main+0x77>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xe0b(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <printf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2da4(%rip),%rax
mov    (%rax),%ebx
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe19(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    122f <main+0x8f>
mov    0x2d75(%rip),%rax
mov    (%rax),%ebx
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd7e(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0xd,%edi
call   1080 <exit@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"%s: Fallimento della fork()\0A\00"
@errno = global i32 0
@.str.1 = [43 x i8] c"PID %ld: Figlio partito, il padre \C3\A8: %ld\0A\00"
@.str.2 = [36 x i8] c"PID %ld: Il figlio partito PID %ld\0A\00"
@.str.3 = [12 x i8] c"%s: wait()\0A\00"
@.str.4 = [39 x i8] c"PID figlio %ld stato di uscita 0x%04X\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 13)
unreachable
18: ; preds = %2
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = call i64 (...) @getpid()
%23 = call i64 (...) @getppid()
%24 = trunc i64 %23 to i32
%25 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i64 %22, i32 %24)
br label %52
26: ; preds = %18
%27 = call i64 (...) @getpid()
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = trunc i64 %29 to i32
%31 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i64 %27, i32 %30)
%32 = call i32 @wait(i32* %8)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %40
35: ; preds = %26
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %38)
store i32 1, i32* %3
br label %55
40: ; preds = %26
%41 = load i32, i32* %7
%42 = load i32, i32* %6
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 (...) @abort()
unreachable
46: ; preds = %40
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = load i32, i32* %8
%50 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i64 %48, i32 %49)
br label %51
51: ; preds = %46
br label %52
52: ; preds = %51, %21
%53 = call i32 @sleep(i32 5)
%54 = load i32, i32* @EXIT_SUCCESS
store i32 %54, i32* %3
br label %55
55: ; preds = %52, %35
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"%s: Fallimento della fork()\0A\00"
@errno = global i32 0
@.str.1 = [43 x i8] c"PID %ld: Figlio partito, il padre \C3\A8: %ld\0A\00"
@.str.2 = [36 x i8] c"PID %ld: Il figlio partito PID %ld\0A\00"
@.str.3 = [12 x i8] c"%s: wait()\0A\00"
@.str.4 = [39 x i8] c"PID figlio %ld stato di uscita 0x%04X\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @fork()
switch i32 %5, label %17 [
i32 -1, label %6
i32 0, label %12
]
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 13)
unreachable
12: ; preds = %2
%13 = tail call i64 (...) @getpid()
%14 = tail call i64 (...) @getppid()
%15 = trunc i64 %14 to i32
%16 = tail call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i64 %13, i32 %15)
br label %35
17: ; preds = %2
%18 = tail call i64 (...) @getpid()
%19 = sext i32 %5 to i64
%20 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i64 %18, i32 %5)
%21 = call i32 @wait(i32* %3)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %26)
br label %38
28: ; preds = %17
%29 = icmp eq i32 %21, %5
br i1 %29, label %32, label %30
30: ; preds = %28
%31 = call i32 (...) @abort()
unreachable
32: ; preds = %28
%33 = load i32, i32* %3
%34 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i64 %19, i32 %33)
br label %35
35: ; preds = %32, %12
%36 = call i32 @sleep(i32 5)
%37 = load i32, i32* @EXIT_SUCCESS
br label %38
38: ; preds = %35, %23
%39 = phi i32 [ %37, %35 ], [ 1, %23 ]
ret i32 %39
}