#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 /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strdup(char *);
int wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  printf("hello world pid:%d\n", (int)getpid());
  int rc = fork();
  if (rc < 0) {
    fprintf(stderr, "fork failed return code:%d\n", rc);
    exit(1);
  } else if (rc == 0) {
    printf("I am child pid:%d\n", (int)getpid());
    char *myargs[3];
    myargs[0] = strdup("wc");
    myargs[1] = strdup("05-p3.c");
    myargs[2] = NULL;
    execvp(myargs[0], myargs);
    printf("you shall not see me");
  } else {
    int wc = wait(NULL);
    printf("I am parent of %d wc:%d pid:%d\n", rc, wc, (int)getpid());
  }
  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)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e3 <main+0x63>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe40(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    124c <main+0xcc>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe38(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x30(%rbp)
lea    0xe2b(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <execvp@plt>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1281 <main+0x101>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%edx
mov    %eax,%ecx
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1286 <main+0x106>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe6c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
js     123c <main+0xbc>
je     11d6 <main+0x56>
xor    %edi,%edi
call   1090 <wait@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe9c(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1233 <main+0xb3>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe4c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe4f(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
mov    %rax,(%rsp)
lea    0xe3f(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <execvp@plt>
lea    0xe22(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xdc8(%rip),%rsi
mov    %ebx,%edx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"hello world pid:%d\0A\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"fork failed return code:%d\0A\00"
@.str.2 = [19 x i8] c"I am child pid:%d\0A\00"
@.str.3 = [3 x i8] c"wc\00"
@.str.4 = [8 x i8] c"05-p3.c\00"
@.str.5 = [21 x i8] c"you shall not see me\00"
@.str.6 = [32 x i8] c"I am parent of %d wc:%d pid:%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [3 x i8*]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 (...) @getpid()
%10 = trunc i64 %9 to i32
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = call i32 (...) @fork()
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i32, i32* %6
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %20
%24 = call i64 (...) @getpid()
%25 = trunc i64 %24 to i32
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = call i8* @strdup(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%28 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
store i8* %27, i8** %28
%29 = call i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%30 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 1
store i8* %29, i8** %30
%31 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 2
store i8* null, i8** %31
%32 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
%33 = load i8*, i8** %32
%34 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
%35 = call i32 @execvp(i8* %33, i8** %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %44
37: ; preds = %20
%38 = call i32 @wait(i32* null)
store i32 %38, i32* %8
%39 = load i32, i32* %6
%40 = load i32, i32* %8
%41 = call i64 (...) @getpid()
%42 = trunc i64 %41 to i32
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i32 %39, i32 %40, i32 %42)
br label %44
44: ; preds = %37, %23
br label %45
45: ; preds = %44
ret i32 0
}

LLVM IR (-O2):
@.str = [20 x i8] c"hello world pid:%d\0A\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"fork failed return code:%d\0A\00"
@.str.2 = [19 x i8] c"I am child pid:%d\0A\00"
@.str.3 = [3 x i8] c"wc\00"
@.str.4 = [8 x i8] c"05-p3.c\00"
@.str.5 = [21 x i8] c"you shall not see me\00"
@.str.6 = [32 x i8] c"I am parent of %d wc:%d pid:%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [3 x i8*]
%4 = tail call i64 (...) @getpid()
%5 = trunc i64 %4 to i32
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = tail call i32 (...) @fork()
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = icmp eq i32 %7, 0
br i1 %14, label %15, label %27
15: ; preds = %13
%16 = tail call i64 (...) @getpid()
%17 = trunc i64 %16 to i32
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = bitcast [3 x i8*]* %3 to i8*
%20 = tail call dereferenceable_or_null(3) i8* @strdup(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%21 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
store i8* %20, i8** %21
%22 = tail call dereferenceable_or_null(8) i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%23 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 1
store i8* %22, i8** %23
%24 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 2
store i8* null, i8** %24
%25 = call i32 @execvp(i8* %20, i8** %21)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %32
27: ; preds = %13
%28 = tail call i32 @wait(i32* null)
%29 = tail call i64 (...) @getpid()
%30 = trunc i64 %29 to i32
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i32 %7, i32 %28, i32 %30)
br label %32
32: ; preds = %15, %27
ret i32 0
}