#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid;
  int status, c;
  FILE *fp;
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Err.(%s) fork failed\n", strerror(errno));
    exit(EXIT_FAILURE);
  } else if (pid == 0) {
    sleep(2);
    printf("Processo Figlio:\n");
    if ((fp = fopen("/etc/fstab", "r")) == NULL) {
      fprintf(stderr, "Err.(%s) fopen() failed\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
    while ((c = fgetc(fp)) != EOF)
      fputc(c, stdout);
    fclose(fp);
    exit(EXIT_SUCCESS);
  } else {
    waitpid(pid, &status, 0);
    printf("Processo Padre\n");
  }
  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   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1216 <main+0x66>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xdfd(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12df <main+0x12f>
mov    $0x2,%edi
call   10c0 <sleep@plt>
lea    0xde4(%rip),%rdi
call   1040 <printf@plt>
lea    0xdea(%rip),%rdi
lea    0xdee(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1292 <main+0xe2>
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xdb6(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1297 <main+0xe7>
mov    -0x28(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d3e(%rip),%rcx
cmp    (%rcx),%eax
je     12c8 <main+0x118>
mov    -0x20(%rbp),%edi
mov    0x2d04(%rip),%rax
mov    (%rax),%esi
call   1060 <fputc@plt>
jmp    1297 <main+0xe7>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
lea    0xd59(%rip),%rdi
call   1040 <printf@plt>
jmp    12ff <main+0x14f>
mov    0x2cb2(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     11f5 <main+0x45>
je     1215 <main+0x65>
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1080 <waitpid@plt>
lea    0xe5f(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2ddc(%rip),%rax
mov    (%rax),%ebx
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xded(%rip),%rsi
jmp    1261 <main+0xb1>
mov    $0x2,%edi
call   10c0 <sleep@plt>
lea    0xe25(%rip),%rdi
call   1030 <puts@plt>
lea    0xde4(%rip),%rdi
lea    0xde8(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
jne    1279 <main+0xc9>
mov    0x2d8e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdc2(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fgetc@plt>
mov    0x2d5d(%rip),%r14
cmp    (%r14),%eax
je     12b7 <main+0x107>
mov    0x2d29(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r15),%esi
mov    %eax,%edi
call   1060 <fputc@plt>
mov    %rbx,%rdi
call   1050 <fgetc@plt>
cmp    (%r14),%eax
jne    12a0 <main+0xf0>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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

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