#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ getpid();
int /*<<< orphan*/ open(char *, int, int /*<<< orphan*/);
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) {
    close(STDOUT_FILENO);
    open("./05-p4.txt", O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
    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    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1203 <main+0x63>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe20(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    12aa <main+0x10a>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    0x2d85(%rip),%rax
or     (%rax),%esi
mov    0x2dbc(%rip),%rax
or     (%rax),%esi
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
lea    0xdea(%rip),%rdi
call   1070 <open@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xde6(%rip),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x30(%rbp)
lea    0xdd9(%rip),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <execvp@plt>
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12df <main+0x13f>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <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    0xd97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12e4 <main+0x144>
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    0xe4c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
js     129d <main+0xfd>
je     11f9 <main+0x59>
xor    %edi,%edi
call   10b0 <wait@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe88(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1294 <main+0xf4>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2dea(%rip),%rax
mov    0x2d9b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
lea    0xdfe(%rip),%rdi
call   1070 <open@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdf7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdfa(%rip),%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbx
mov    %rax,(%rsp)
lea    0xdea(%rip),%rdi
call   10a0 <strdup@plt>
mov    %rax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <execvp@plt>
lea    0xdcd(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
lea    0xd67(%rip),%rsi
mov    %ebx,%edx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <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"
@STDOUT_FILENO = global i32 0
@.str.2 = [12 x i8] c"./05-p4.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@.str.3 = [19 x i8] c"I am child pid:%d\0A\00"
@.str.4 = [3 x i8] c"wc\00"
@.str.5 = [8 x i8] c"05-p3.c\00"
@.str.6 = [21 x i8] c"you shall not see me\00"
@.str.7 = [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 %46
23: ; preds = %20
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* @O_CREAT
%27 = load i32, i32* @O_WRONLY
%28 = or i32 %26, %27
%29 = load i32, i32* @O_TRUNC
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IRWXU
%32 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %30, i32 %31)
%33 = call i64 (...) @getpid()
%34 = trunc i64 %33 to i32
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = call i8* @strdup(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%37 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
store i8* %36, i8** %37
%38 = call i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%39 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 1
store i8* %38, i8** %39
%40 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 2
store i8* null, i8** %40
%41 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
%42 = load i8*, i8** %41
%43 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
%44 = call i32 @execvp(i8* %42, i8** %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %53
46: ; preds = %20
%47 = call i32 @wait(i32* null)
store i32 %47, i32* %8
%48 = load i32, i32* %6
%49 = load i32, i32* %8
%50 = call i64 (...) @getpid()
%51 = trunc i64 %50 to i32
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i32 %48, i32 %49, i32 %51)
br label %53
53: ; preds = %46, %23
br label %54
54: ; preds = %53
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"
@STDOUT_FILENO = global i32 0
@.str.2 = [12 x i8] c"./05-p4.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@.str.3 = [19 x i8] c"I am child pid:%d\0A\00"
@.str.4 = [3 x i8] c"wc\00"
@.str.5 = [8 x i8] c"05-p3.c\00"
@.str.6 = [21 x i8] c"you shall not see me\00"
@.str.7 = [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 %36
15: ; preds = %13
%16 = load i32, i32* @STDOUT_FILENO
%17 = tail call i32 @close(i32 %16)
%18 = load i32, i32* @O_CREAT
%19 = load i32, i32* @O_WRONLY
%20 = or i32 %19, %18
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IRWXU
%24 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %23)
%25 = tail call i64 (...) @getpid()
%26 = trunc i64 %25 to i32
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = bitcast [3 x i8*]* %3 to i8*
%29 = tail call dereferenceable_or_null(3) i8* @strdup(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%30 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
store i8* %29, i8** %30
%31 = tail call dereferenceable_or_null(8) i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%32 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 1
store i8* %31, i8** %32
%33 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 2
store i8* null, i8** %33
%34 = call i32 @execvp(i8* %29, i8** %30)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %41
36: ; preds = %13
%37 = tail call i32 @wait(i32* null)
%38 = tail call i64 (...) @getpid()
%39 = trunc i64 %38 to i32
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i32 %7, i32 %37, i32 %39)
br label %41
41: ; preds = %15, %36
ret i32 0
}