#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 execve(char *, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ printf(char *, ...);
int wait(int);
void main(void) {
  int child_status, exec_status;
  int pid = fork();
  if (pid == 0) {
    printf("Child: EXEC lec7_1\n");
    exec_status = execve("lec7_1", NULL, NULL);
    printf("Child: Why would I execute?\n");
  } else {
    printf("Parent: Whose your daddy?\n");
    child_status = wait(pid);
    printf("Parent: the child %d exit with %d\n", pid, child_status);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1193 <main+0x53>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xea3(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1040 <execve@plt>
mov    %eax,-0x8(%rbp)
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11c0 <main+0x80>
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <wait@plt>
mov    %eax,-0x4(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x4(%rbp),%edx
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     1184 <main+0x34>
mov    %eax,%ebx
lea    0xebe(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <wait@plt>
lea    0xe88(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
pop    %rbx
jmp    1040 <printf@plt>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb2(%rip),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <execve@plt>
lea    0xea9(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"Child: EXEC lec7_1\0A\00"
@.str.1 = [7 x i8] c"lec7_1\00"
@.str.2 = [29 x i8] c"Child: Why would I execute?\0A\00"
@.str.3 = [27 x i8] c"Parent: Whose your daddy?\0A\00"
@.str.4 = [35 x i8] c"Parent: the child %d exit with %d\0A\00"
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @fork()
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @execve(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32* null, i32* null)
store i32 %9, i32* %2
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %18
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%13 = load i32, i32* %3
%14 = call i32 @wait(i32 %13)
store i32 %14, i32* %1
%15 = load i32, i32* %3
%16 = load i32, i32* %1
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %15, i32 %16)
br label %18
18: ; preds = %11, %7
ret void
}

LLVM IR (-O2):
@.str.1 = [7 x i8] c"lec7_1\00"
@.str.4 = [35 x i8] c"Parent: the child %d exit with %d\0A\00"
@str = [26 x i8] c"Parent: Whose your daddy?\00"
@str.5 = [19 x i8] c"Child: EXEC lec7_1\00"
@str.6 = [28 x i8] c"Child: Why would I execute?\00"
define void @main() {
%1 = tail call i32 (...) @fork()
%2 = icmp eq i32 %1, 0
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%5 = tail call i32 @execve(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32* null, i32* null)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.6, i64 0, i64 0))
br label %11
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @wait(i32 %1)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %1, i32 %9)
br label %11
11: ; preds = %7, %3
ret void
}