#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_FAILURE;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(void) {
  pid_t pid;
  pid_t soy, papa;
  pid = fork();
  if (pid == -1) {
    fprintf(stderr, "can't fork, error %d\n", errno);
    exit(EXIT_FAILURE);
  }
  soy = getpid();
  papa = getppid();
  if (pid == 0) {
    printf("\nComprueba que mi padre %d es un proceso asociado a este "
           "programa\n\n",
           papa);
    sleep(1);
    int j;
    for (j = 0; j < 4; j++) {
      printf("Soy hijo con PID %d y estoy en la iteracion %d\n", soy, j);
      sleep(1);
    }
    _exit(0);
  } else {
    printf("\nComprueba que mi padre %d es una ventana de Bash\n\n", papa);
    sleep(1);
    int j;
    for (j = 0; j < 4; j++) {
      printf("Soy el padre con PID %d y estoy en la iteracion %d\n", soy, j);
      sleep(5);
    }
    exit(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11cf <main+0x4f>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
lea    0xe44(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    124c <main+0xcc>
mov    -0x10(%rbp),%esi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jge    1245 <main+0xc5>
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    120f <main+0x8f>
xor    %edi,%edi
call   1030 <_exit@plt>
mov    -0x10(%rbp),%esi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jge    12a4 <main+0x124>
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    126e <main+0xee>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
jne    11bc <main+0x3c>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
lea    0xe57(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getppid@plt>
test   %ebp,%ebp
jne    1267 <main+0xe7>
lea    0xe39(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
lea    0xe63(%rip),%r14
mov    %r14,%rdi
mov    %ebx,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x2,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x3,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
lea    0xe1c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
lea    0xe36(%rip),%r14
mov    %r14,%rdi
mov    %ebx,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x2,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x3,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"can't fork, error %d\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [68 x i8] c"\0AComprueba que mi padre %d es un proceso asociado a este programa\0A\0A\00"
@.str.2 = [48 x i8] c"Soy hijo con PID %d y estoy en la iteracion %d\0A\00"
@.str.3 = [52 x i8] c"\0AComprueba que mi padre %d es una ventana de Bash\0A\0A\00"
@.str.4 = [52 x i8] c"Soy el padre con PID %d y estoy en la iteracion %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (...) @fork()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = call i32 (...) @getpid()
store i32 %17, i32* %3
%18 = call i32 (...) @getppid()
store i32 %18, i32* %4
%19 = load i32, i32* %2
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %38
21: ; preds = %16
%22 = load i32, i32* %4
%23 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = call i32 @sleep(i32 1)
store i32 0, i32* %5
br label %25
25: ; preds = %33, %21
%26 = load i32, i32* %5
%27 = icmp slt i32 %26, 4
br i1 %27, label %28, label %36
28: ; preds = %25
%29 = load i32, i32* %3
%30 = load i32, i32* %5
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %29, i32 %30)
%32 = call i32 @sleep(i32 1)
br label %33
33: ; preds = %28
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 1
store i32 %35, i32* %5
br label %25
36: ; preds = %25
%37 = call i32 @_exit(i32 0)
unreachable
38: ; preds = %16
%39 = load i32, i32* %4
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%41 = call i32 @sleep(i32 1)
store i32 0, i32* %6
br label %42
42: ; preds = %50, %38
%43 = load i32, i32* %6
%44 = icmp slt i32 %43, 4
br i1 %44, label %45, label %53
45: ; preds = %42
%46 = load i32, i32* %3
%47 = load i32, i32* %6
%48 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i32 %46, i32 %47)
%49 = call i32 @sleep(i32 5)
br label %50
50: ; preds = %45
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 1
store i32 %52, i32* %6
br label %42
53: ; preds = %42
%54 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"can't fork, error %d\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [68 x i8] c"\0AComprueba que mi padre %d es un proceso asociado a este programa\0A\0A\00"
@.str.2 = [48 x i8] c"Soy hijo con PID %d y estoy en la iteracion %d\0A\00"
@.str.3 = [52 x i8] c"\0AComprueba que mi padre %d es una ventana de Bash\0A\0A\00"
@.str.4 = [52 x i8] c"Soy el padre con PID %d y estoy en la iteracion %d\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %9
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = load i32, i32* @errno
%6 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (...) @getppid()
%12 = icmp eq i32 %1, 0
br i1 %12, label %13, label %25
13: ; preds = %9
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%15 = tail call i32 @sleep(i32 1)
%16 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %10, i32 0)
%17 = tail call i32 @sleep(i32 1)
%18 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %10, i32 1)
%19 = tail call i32 @sleep(i32 1)
%20 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %10, i32 2)
%21 = tail call i32 @sleep(i32 1)
%22 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %10, i32 3)
%23 = tail call i32 @sleep(i32 1)
%24 = tail call i32 @_exit(i32 0)
unreachable
25: ; preds = %9
%26 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%27 = tail call i32 @sleep(i32 1)
%28 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i32 %10, i32 0)
%29 = tail call i32 @sleep(i32 5)
%30 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i32 %10, i32 1)
%31 = tail call i32 @sleep(i32 5)
%32 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i32 %10, i32 2)
%33 = tail call i32 @sleep(i32 5)
%34 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i32 %10, i32 3)
%35 = tail call i32 @sleep(i32 5)
%36 = tail call i32 @exit(i32 0)
unreachable
}