#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*/ assert(int);
int /*<<< orphan*/ execve(char *const, char *const *, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ grandchild();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  if (argc > 1) {
    grandchild();
    return 0;
  }
  printf("parent started\n");
  pid_t child = fork();
  assert(child >= 0);
  if (child == 0) {
    printf("child started\n");
    char *const new_args[] = {argv[0], "yep", NULL};
    execve(new_args[0], (char *const *)&new_args, NULL);
    printf("CHILED FAILED\n");
  } else {
    int status;
    wait(&status);
    printf("Parent done\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1193 <main+0x33>
mov    $0x0,%al
call   1060 <grandchild@plt>
movl   $0x0,-0x4(%rbp)
jmp    122f <main+0xcf>
lea    0xe66(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1213 <main+0xb3>
lea    0xe42(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
lea    0xe3a(%rip),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <execve@plt>
lea    0xe1a(%rip),%rdi
call   1030 <printf@plt>
jmp    1228 <main+0xc8>
lea    -0x34(%rbp),%rdi
call   1070 <wait@plt>
lea    0xe0f(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jl     1175 <main+0x15>
xor    %eax,%eax
call   1060 <grandchild@plt>
jmp    11f1 <main+0x91>
mov    %rsi,%r14
lea    0xe85(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbx
mov    %rax,%rdi
shr    $0x3f,%rdi
xor    $0x1,%edi
call   1050 <assert@plt>
test   %rbx,%rbx
je     11b3 <main+0x53>
mov    %rsp,%rdi
call   1070 <wait@plt>
lea    0xe62(%rip),%rdi
jmp    11ec <main+0x8c>
lea    0xe65(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%rdi
mov    %rdi,(%rsp)
lea    0xe33(%rip),%rax
mov    %rax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsp,%rsi
xor    %edx,%edx
call   1040 <execve@plt>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [16 x i8] c"parent started\0A\00"
@.str.1 = [15 x i8] c"child started\0A\00"
@.str.2 = [4 x i8] c"yep\00"
@.str.3 = [15 x i8] c"CHILED FAILED\0A\00"
@.str.4 = [13 x i8] c"Parent done\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca [3 x i8*]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (...) @grandchild()
store i32 0, i32* %3
br label %39
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%15 = call i64 (...) @fork()
store i64 %15, i64* %6
%16 = load i64, i64* %6
%17 = icmp sge i64 %16, 0
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i64, i64* %6
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %35
22: ; preds = %13
%23 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%24 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 0
%27 = load i8*, i8** %26
store i8* %27, i8** %24
%28 = getelementptr inbounds i8*, i8** %24, i64 1
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %28
%29 = getelementptr inbounds i8*, i8** %28, i64 1
store i8* null, i8** %29
%30 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
%31 = load i8*, i8** %30
%32 = bitcast [3 x i8*]* %7 to i8**
%33 = call i32 @execve(i8* %31, i8** %32, i32* null)
%34 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %38
35: ; preds = %13
%36 = call i32 @wait(i32* %8)
%37 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %35, %22
store i32 0, i32* %3
br label %39
39: ; preds = %38, %11
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str.2 = [4 x i8] c"yep\00"
@str = [15 x i8] c"parent started\00"
@str.5 = [12 x i8] c"Parent done\00"
@str.6 = [14 x i8] c"child started\00"
@str.7 = [14 x i8] c"CHILED FAILED\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [3 x i8*]
%4 = alloca i32
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (...) @grandchild()
br label %29
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%10 = tail call i64 (...) @fork()
%11 = lshr i64 %10, 63
%12 = trunc i64 %11 to i32
%13 = xor i32 %12, 1
%14 = tail call i32 @assert(i32 %13)
%15 = icmp eq i64 %10, 0
br i1 %15, label %16, label %25
16: ; preds = %8
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.6, i64 0, i64 0))
%18 = bitcast [3 x i8*]* %3 to i8*
%19 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
%20 = load i8*, i8** %1
store i8* %20, i8** %19
%21 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %21
%22 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 2
store i8* null, i8** %22
%23 = call i32 @execve(i8* %20, i8** %19, i32* null)
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0))
br label %29
25: ; preds = %8
%26 = bitcast i32* %4 to i8*
%27 = call i32 @wait(i32* %4)
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
br label %29
29: ; preds = %16, %25, %6
ret i32 0
}