#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 fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int main() {
  pid_t f;
  pid_t pid, ppid, pgid, sid;
  f = fork();
  if (f == -1)
    perror("ERROR FORK.\n");
  else if (f == 0) {
    sleep(1);
    pid = getpid();
    printf("Pid del hijo: %d\n", pid);
  } else {
    pid = getpid();
    printf("Pid del padre: %d\n", pid);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1184 <main+0x34>
lea    0xe86(%rip),%rdi
call   1050 <perror@plt>
jmp    11d4 <main+0x84>
cmpl   $0x0,-0x8(%rbp)
jne    11b6 <main+0x66>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
jmp    11cf <main+0x7f>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
jmp    11d4 <main+0x84>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     1171 <main+0x21>
cmp    $0xffffffff,%eax
je     1194 <main+0x44>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xeb0(%rip),%rdi
jmp    1189 <main+0x39>
mov    $0x1,%edi
call   1060 <sleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe84(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe65(%rip),%rdi
call   1050 <perror@plt>
jmp    1190 <main+0x40>

LLVM IR (-O0):
@.str = [13 x i8] c"ERROR FORK.\0A\00"
@.str.1 = [18 x i8] c"Pid del hijo: %d\0A\00"
@.str.2 = [19 x i8] c"Pid del padre: %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 %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %25
12: ; preds = %0
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = call i32 @sleep(i32 1)
%17 = call i32 (...) @getpid()
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %24
20: ; preds = %12
%21 = call i32 (...) @getpid()
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %20, %15
br label %25
25: ; preds = %24, %10
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"ERROR FORK.\0A\00"
@.str.1 = [18 x i8] c"Pid del hijo: %d\0A\00"
@.str.2 = [19 x i8] c"Pid del padre: %d\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %8 [
i32 -1, label %2
i32 0, label %4
]
2: ; preds = %0
%3 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %11
4: ; preds = %0
%5 = tail call i32 @sleep(i32 1)
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %11
8: ; preds = %0
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %9)
br label %11
11: ; preds = %4, %8, %2
ret i32 0
}