#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, int);
int /*<<< orphan*/ wait(int *);
int main() {
  pid_t pid;
  int i = 0, estado;
  pid = fork();
  switch (pid) {
  case -1:
    perror("Error en el fork");
    break;
  case 0:
    printf("Soy el hijo: PID %d; PPID = %d i = %d\n", getpid(), getppid(), ++i);
    exit(0);
    break;
  default:
    printf("Soy el padre: PID %d; PPID = %d i = %d\n", getpid(), getppid(),
           --i);
    wait(&estado);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0xffffffff,%eax
je     11b4 <main+0x44>
jmp    11a4 <main+0x34>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     11c5 <main+0x55>
jmp    11f7 <main+0x87>
lea    0xe45(%rip),%rdi
call   1050 <perror@plt>
jmp    122b <main+0xbb>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0xc(%rbp)
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
mov    -0xc(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
lea    0xe1b(%rip),%rdi
call   1040 <printf@plt>
lea    -0x10(%rbp),%rdi
call   1080 <wait@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     11bc <main+0x4c>
test   %eax,%eax
je     11ca <main+0x5a>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe9c(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
mov    $0xffffffff,%ecx
call   1040 <printf@plt>
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe3d(%rip),%rdi
call   1050 <perror@plt>
jmp    11b4 <main+0x44>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe30(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
mov    $0x1,%ecx
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"Error en el fork\00"
@.str.1 = [39 x i8] c"Soy el hijo: PID %d; PPID = %d i = %d\0A\00"
@.str.2 = [40 x i8] c"Soy el padre: PID %d; PPID = %d i = %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
%5 = call i32 (...) @fork()
store i32 %5, i32* %2
%6 = load i32, i32* %2
switch i32 %6, label %16 [
i32 -1, label %7
i32 0, label %9
]
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %23
9: ; preds = %0
%10 = call i32 (...) @getpid()
%11 = call i32 (...) @getppid()
%12 = load i32, i32* %3
%13 = add nsw i32 %12, 1
store i32 %13, i32* %3
%14 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %11, i32 %13)
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %0
%17 = call i32 (...) @getpid()
%18 = call i32 (...) @getppid()
%19 = load i32, i32* %3
%20 = add nsw i32 %19, -1
store i32 %20, i32* %3
%21 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %18, i32 %20)
%22 = call i32 @wait(i32* %4)
br label %23
23: ; preds = %16, %7
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@.str = [17 x i8] c"Error en el fork\00"
@.str.1 = [39 x i8] c"Soy el hijo: PID %d; PPID = %d i = %d\0A\00"
@.str.2 = [40 x i8] c"Soy el padre: PID %d; PPID = %d i = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
switch i32 %3, label %11 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %16
6: ; preds = %0
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (...) @getppid()
%9 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %8, i32 1)
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %0
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (...) @getppid()
%14 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %13, i32 -1)
%15 = call i32 @wait(i32* %1)
br label %16
16: ; preds = %11, %4
ret i32 0
}