#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int main(void) {
  int i = 10, pid;
  if ((pid = fork()) == -1) {
    perror("fork");
    exit(-1);
  } else if (pid == 0) {
    while (i) {
      printf("PROCESO HIJO: %d\n", i--);
    }
  } else {
    while (i) {
      printf("PROCESO PADRE: %d\n", i--);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xa,-0x8(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    117f <main+0x3f>
lea    0xe90(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jne    11b9 <main+0x79>
jmp    118e <main+0x4e>
cmpl   $0x0,-0x8(%rbp)
je     11b4 <main+0x74>
mov    -0x8(%rbp),%esi
mov    %esi,%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
lea    0xe5b(%rip),%rdi
call   1030 <printf@plt>
jmp    118e <main+0x4e>
jmp    11e9 <main+0xa9>
jmp    11be <main+0x7e>
cmpl   $0x0,-0x8(%rbp)
je     11e4 <main+0xa4>
mov    -0x8(%rbp),%esi
mov    %esi,%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
lea    0xe3d(%rip),%rdi
call   1030 <printf@plt>
jmp    11be <main+0x7e>
jmp    11e9 <main+0xa9>
jmp    11ee <main+0xae>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     115e <main+0x1e>
cmp    $0xffffffff,%eax
je     11eb <main+0xab>
lea    0xebb(%rip),%rbx
jmp    1165 <main+0x25>
lea    0xea0(%rip),%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x6,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe0e(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@.str.1 = [18 x i8] c"PROCESO HIJO: %d\0A\00"
@.str.2 = [19 x i8] c"PROCESO PADRE: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 10, i32* %2
%4 = call i32 (...) @fork()
store i32 %4, i32* %3
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %9
br label %13
13: ; preds = %16, %12
%14 = load i32, i32* %2
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i32, i32* %2
%18 = add nsw i32 %17, -1
store i32 %18, i32* %2
%19 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %13
20: ; preds = %13
br label %30
21: ; preds = %9
br label %22
22: ; preds = %25, %21
%23 = load i32, i32* %2
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* %2
%27 = add nsw i32 %26, -1
store i32 %27, i32* %2
%28 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %22
29: ; preds = %22
br label %30
30: ; preds = %29, %20
br label %31
31: ; preds = %30
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@.str.1 = [18 x i8] c"PROCESO HIJO: %d\0A\00"
@.str.2 = [19 x i8] c"PROCESO PADRE: %d\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %2 [
i32 -1, label %3
i32 0, label %6
]
2: ; preds = %0
br label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 -1)
unreachable
6: ; preds = %0, %2
%7 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), %2 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), %0 ]
%8 = tail call i32 @printf(i8* %7, i32 10)
%9 = tail call i32 @printf(i8* %7, i32 9)
%10 = tail call i32 @printf(i8* %7, i32 8)
%11 = tail call i32 @printf(i8* %7, i32 7)
%12 = tail call i32 @printf(i8* %7, i32 6)
%13 = tail call i32 @printf(i8* %7, i32 5)
%14 = tail call i32 @printf(i8* %7, i32 4)
%15 = tail call i32 @printf(i8* %7, i32 3)
%16 = tail call i32 @printf(i8* %7, i32 2)
%17 = tail call i32 @printf(i8* %7, i32 1)
ret i32 0
}