#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ grandchild();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
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);
    fprintf(stderr, "CHILD 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    11a3 <main+0x33>
mov    $0x0,%al
call   1070 <grandchild@plt>
movl   $0x0,-0x4(%rbp)
jmp    1248 <main+0xd8>
lea    0xe56(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1090 <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    122c <main+0xbc>
lea    0xe32(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
lea    0xe2a(%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>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1241 <main+0xd1>
lea    -0x34(%rbp),%rdi
call   1080 <wait@plt>
lea    0xdf5(%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     1188 <main+0x18>
xor    %eax,%eax
call   1070 <grandchild@plt>
jmp    1212 <main+0xa2>
mov    %rsi,%r14
lea    0xe80(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %rax,%rbx
mov    %rax,%rdi
shr    $0x3f,%rdi
xor    $0x1,%edi
call   1050 <assert@plt>
test   %rbx,%rbx
je     11cb <main+0x5b>
mov    %rsp,%rdi
call   1080 <wait@plt>
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
jmp    1212 <main+0xa2>
lea    0xe5b(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%rdi
mov    %rdi,(%rsp)
lea    0xe1b(%rip),%rax
mov    %rax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsp,%rsi
xor    %edx,%edx
call   1040 <execve@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
call   1060 <fprintf@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"
@stderr = global i32 0
@.str.3 = [14 x i8] c"CHILD 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 %40
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 %36
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 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %39
36: ; preds = %13
%37 = call i32 @wait(i32* %8)
%38 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %36, %22
store i32 0, i32* %3
br label %40
40: ; preds = %39, %11
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str.2 = [4 x i8] c"yep\00"
@stderr = global i32 0
@.str.3 = [14 x i8] c"CHILD FAILED\0A\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"
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 %30
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 %26
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 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %30
26: ; preds = %8
%27 = bitcast i32* %4 to i8*
%28 = call i32 @wait(i32* %4)
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
br label %30
30: ; preds = %16, %26, %6
ret i32 0
}