#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 /*<<< orphan*/ execv(char *, char **);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait(int *);
int main(int args, char *argv[]) {
  pid_t pid = fork();
  if (pid == 0) {
    execv("echo", &argv[1]);
  } else if (pid > 0) {
    int stat;
    printf("Parent waiting child");
    wait(&stat);
    printf("Parent waked by child, exiting with code %d\n", stat);
    return stat;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1185 <main+0x45>
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
lea    0xe85(%rip),%rdi
call   1050 <execv@plt>
jmp    11c8 <main+0x88>
cmpq   $0x0,-0x18(%rbp)
jle    11c3 <main+0x83>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x1c(%rbp),%rdi
call   1040 <wait@plt>
mov    -0x1c(%rbp),%esi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c8 <main+0x88>
jmp    11c8 <main+0x88>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
je     1186 <main+0x46>
jle    1199 <main+0x59>
lea    0xea8(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0x4(%rsp),%rdi
call   1040 <wait@plt>
mov    0x4(%rsp),%esi
lea    0xea1(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x4(%rsp),%ebp
jmp    1199 <main+0x59>
add    $0x8,%rbx
lea    0xe6f(%rip),%rdi
mov    %rbx,%rsi
call   1050 <execv@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"echo\00"
@.str.1 = [21 x i8] c"Parent waiting child\00"
@.str.2 = [45 x i8] c"Parent waked by child, exiting with code %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 (...) @fork()
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = call i32 @execv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %13)
br label %25
15: ; preds = %2
%16 = load i64, i64* %6
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %15
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @wait(i32* %7)
%21 = load i32, i32* %7
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %7
store i32 %23, i32* %3
br label %25
24: ; preds = %15
br label %25
25: ; preds = %18, %24, %11
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@.str = [5 x i8] c"echo\00"
@.str.1 = [21 x i8] c"Parent waiting child\00"
@.str.2 = [45 x i8] c"Parent waked by child, exiting with code %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = tail call i32 @execv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %7)
br label %18
9: ; preds = %2
%10 = icmp sgt i64 %4, 0
br i1 %10, label %11, label %18
11: ; preds = %9
%12 = bitcast i32* %3 to i8*
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @wait(i32* %3)
%15 = load i32, i32* %3
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %3
br label %18
18: ; preds = %6, %9, %11
%19 = phi i32 [ %17, %11 ], [ 0, %9 ], [ 0, %6 ]
ret i32 %19
}