#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 const *, char **);
int /*<<< orphan*/ printf(char *);
scalar_t__ vfork();
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int main(int argc, const char **argv) {
  if (argc < 2) {
    printf("Specify app name\n");
    return -1;
  }
  char *newArg[2];
  newArg[0] = (char *)argv[1];
  newArg[1] = NULL;
  pid_t child_id = vfork();
  if (child_id == 0) {
    printf("APPLICATION: After vfork in child\n");
    execv(argv[1], newArg);
  } else {
    printf("APPLICATION: After vfork in parent\n");
    wait(0);
    execv(argv[1], newArg);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1178 <main+0x38>
lea    0xe99(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <main+0xaf>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1040 <vfork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c4 <main+0x84>
lea    0xe69(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <execv@plt>
jmp    11e8 <main+0xa8>
lea    0xe6a(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <wait@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <execv@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     115d <main+0x1d>
lea    0xef4(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    11ac <main+0x6c>
mov    %rsi,%rbx
mov    0x8(%rsi),%rax
mov    %rax,(%rsp)
movq   $0x0,0x8(%rsp)
xor    %eax,%eax
call   1040 <vfork@plt>
test   %rax,%rax
je     1192 <main+0x52>
lea    0xe7c(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1050 <wait@plt>
jmp    119e <main+0x5e>
lea    0xe8a(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rbx),%rdi
mov    %rsp,%rsi
call   1060 <execv@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Specify app name\0A\00"
@.str.1 = [35 x i8] c"APPLICATION: After vfork in child\0A\00"
@.str.2 = [36 x i8] c"APPLICATION: After vfork in parent\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i8*]
%7 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %37
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 0
store i8* %15, i8** %16
%17 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 1
store i8* null, i8** %17
%18 = call i64 (...) @vfork()
store i64 %18, i64* %7
%19 = load i64, i64* %7
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %12
%22 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 0
%27 = call i32 @execv(i8* %25, i8** %26)
br label %36
28: ; preds = %12
%29 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @wait(i32 0)
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 0
%35 = call i32 @execv(i8* %33, i8** %34)
br label %36
36: ; preds = %28, %21
store i32 0, i32* %3
br label %37
37: ; preds = %36, %10
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@str = [35 x i8] c"APPLICATION: After vfork in parent\00"
@str.3 = [34 x i8] c"APPLICATION: After vfork in child\00"
@str.4 = [17 x i8] c"Specify app name\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i8*]
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.4, i64 0, i64 0))
br label %23
7: ; preds = %2
%8 = bitcast [2 x i8*]* %3 to i8*
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
store i8* %10, i8** %11
%12 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 1
store i8* null, i8** %12
%13 = tail call i64 (...) @vfork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.3, i64 0, i64 0))
br label %20
17: ; preds = %7
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 @wait(i32 0)
br label %20
20: ; preds = %17, %15
%21 = load i8*, i8** %9
%22 = call i32 @execv(i8* %21, i8** %11)
br label %23
23: ; preds = %20, %5
%24 = phi i32 [ -1, %5 ], [ 0, %20 ]
ret i32 %24
}