#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;

/* Variables and functions */
int WEXITSTATUS(int);
int /*<<< orphan*/ execl(char *, char *, char *, char *);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(void) {
  pid_t f, w;
  int status;
  printf("Processo principal (PID=%u)\n", getpid());
  f = fork();
  if (f == 0) {
    sleep(3);
    printf("Filho executando \"ls /\"...\n\n");
    execl("/bin/ls", "/bin/ls", "/naoexiste", (char *)NULL);
    puts("isto nao eh executado");
  } else {
    printf("Pai (PID=%u) esperando pelo filho (PID=%u)\n", getpid(), f);
    w = waitpid(f, &status, 0);
    printf("\nStatus = %d\n", WEXITSTATUS(status));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ff <main+0x7f>
mov    $0x3,%edi
call   1090 <sleep@plt>
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe5f(%rip),%rsi
lea    0xe60(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   1080 <execl@plt>
lea    0xe58(%rip),%rdi
call   1030 <puts@plt>
jmp    1245 <main+0xc5>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe6d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     11ef <main+0x6f>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe8d(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
lea    0xe8e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1229 <main+0xa9>
mov    $0x3,%edi
call   1090 <sleep@plt>
lea    0xe80(%rip),%rdi
call   1030 <puts@plt>
lea    0xe11(%rip),%rdi
lea    0xe12(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   1080 <execl@plt>
lea    0xe0c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Processo principal (PID=%u)\0A\00"
@.str.1 = [29 x i8] c"Filho executando \22ls /\22...\0A\0A\00"
@.str.2 = [8 x i8] c"/bin/ls\00"
@.str.3 = [11 x i8] c"/naoexiste\00"
@.str.4 = [22 x i8] c"isto nao eh executado\00"
@.str.5 = [44 x i8] c"Pai (PID=%u) esperando pelo filho (PID=%u)\0A\00"
@.str.6 = [14 x i8] c"\0AStatus = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (...) @getpid()
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i64 (...) @fork()
store i64 %7, i64* %2
%8 = load i64, i64* %2
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = call i32 @sleep(i32 3)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* null)
%14 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %24
15: ; preds = %0
%16 = call i32 (...) @getpid()
%17 = load i64, i64* %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i32 %16, i64 %17)
%19 = load i64, i64* %2
%20 = call i64 @waitpid(i64 %19, i32* %4, i32 0)
store i64 %20, i64* %3
%21 = load i32, i32* %4
%22 = call i32 @WEXITSTATUS(i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %15, %10
ret i32 0
}

LLVM IR (-O2):
@.str = [29 x i8] c"Processo principal (PID=%u)\0A\00"
@.str.2 = [8 x i8] c"/bin/ls\00"
@.str.3 = [11 x i8] c"/naoexiste\00"
@.str.4 = [22 x i8] c"isto nao eh executado\00"
@.str.5 = [44 x i8] c"Pai (PID=%u) esperando pelo filho (PID=%u)\0A\00"
@.str.6 = [14 x i8] c"\0AStatus = %d\0A\00"
@str = [28 x i8] c"Filho executando \22ls /\22...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = tail call i32 @sleep(i32 3)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* null)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %19
12: ; preds = %0
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0), i32 %13, i64 %5)
%15 = call i64 @waitpid(i64 %5, i32* %1, i32 0)
%16 = load i32, i32* %1
%17 = call i32 @WEXITSTATUS(i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %12, %7
ret i32 0
}