#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 PATH_MAX;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ execlp(char *, char *, char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int vfork();
int main(int argc, char **argv) {
  int pid;
  pid = 1 + argc;
  pid = vfork();
  if (pid == 0) {
    char prog[PATH_MAX];
    int len;
    strcpy(prog, argv[0]);
    len = strlen(prog);
    memcpy(prog + len - 10, "vforked-prog", 12);
    prog[len + 2] = 0;
    printf("I'm the child!\n");
    execlp(prog, prog, (char *)0);
    perror("exec failed");
    _exit(1);
  } else {
    printf("I'm the proud parent of child #%d!\n", pid);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
call   1090 <vfork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1253 <main+0xd3>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
add    $0xfffffffffffffff6,%rdi
lea    0xdfc(%rip),%rsi
mov    $0xc,%edx
call   1070 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   10a0 <execlp@plt>
lea    0xdd9(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x14(%rbp),%esi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
call   10a0 <vfork@plt>
test   %eax,%eax
je     11c0 <main+0x30>
mov    %eax,%esi
lea    0xe6d(%rip),%rdi
xor    %ebx,%ebx
mov    %ebx,%eax
call   1070 <printf@plt>
mov    %ebx,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    (%rbx),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
movslq %eax,%rbx
lea    (%rbx,%r14,1),%rdi
add    $0xfffffffffffffff6,%rdi
lea    0xe01(%rip),%rsi
mov    $0xc,%edx
call   1080 <memcpy@plt>
movb   $0x0,0x2(%rbx,%r14,1)
lea    0xe27(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
mov    %eax,%edx
mov    %r14,%rdi
mov    %r14,%rsi
call   10b0 <execlp@plt>
lea    0xddc(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"vforked-prog\00"
@.str.1 = [16 x i8] c"I'm the child!\0A\00"
@.str.2 = [12 x i8] c"exec failed\00"
@.str.3 = [36 x i8] c"I'm the proud parent of child #%d!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%9 = load i32, i32* %3
%10 = add nsw i32 1, %9
store i32 %10, i32* %5
%11 = call i32 @vfork()
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %2
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %6
%18 = alloca i8, i64 %16
store i64 %16, i64* %7
%19 = load i8**, i8*** %4
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @strcpy(i8* %18, i8* %21)
%23 = call i32 @strlen(i8* %18)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %18, i64 %25
%27 = getelementptr inbounds i8, i8* %26, i64 -10
%28 = call i32 @memcpy(i8* %27, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%29 = load i32, i32* %8
%30 = add nsw i32 %29, 2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %18, i64 %31
store i8 0, i8* %32
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @execlp(i8* %18, i8* %18, i8* null)
%35 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @_exit(i32 1)
unreachable
37: ; preds = %2
%38 = load i32, i32* %5
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %37
ret i32 0
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"vforked-prog\00"
@.str.2 = [12 x i8] c"exec failed\00"
@.str.3 = [36 x i8] c"I'm the proud parent of child #%d!\0A\00"
@str = [15 x i8] c"I'm the child!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = call i32 @vfork()
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %23
5: ; preds = %2
%6 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = load i8*, i8** %1
%10 = call i32 @strcpy(i8* %8, i8* %9)
%11 = call i32 @strlen(i8* %8)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
%14 = getelementptr inbounds i8, i8* %13, i64 -10
%15 = call i32 @memcpy(i8* %14, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%16 = add nsw i32 %11, 2
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %8, i64 %17
store i8 0, i8* %18
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%20 = call i32 @execlp(i8* %8, i8* %8, i8* null)
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @_exit(i32 1)
unreachable
23: ; preds = %2
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %3)
ret i32 0
}