#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 fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strdup(char *);
int wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int rc = fork();
  if (rc < 0) {
    fprintf(stderr, "fork failed\n");
  } else if (rc == 0) {
    close(STDOUT_FILENO);
    open("./p4.output", O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
    char *myargs[3];
    myargs[0] = strdup("wc");
    myargs[1] = strdup("p4.c");
    myargs[2] = NULL;
    execvp(myargs[0], myargs);
  } else {
    int wc = wait(NULL);
  }
  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   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b4 <main+0x44>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1247 <main+0xd7>
cmpl   $0x0,-0x14(%rbp)
jne    1236 <main+0xc6>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    0x2dd4(%rip),%rax
or     (%rax),%esi
mov    0x2e0b(%rip),%rax
or     (%rax),%esi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
lea    0xe16(%rip),%rdi
call   1050 <open@plt>
lea    0xe16(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x30(%rbp)
lea    0xe09(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <execvp@plt>
jmp    1242 <main+0xd2>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
mov    %eax,-0x34(%rbp)
jmp    1247 <main+0xd7>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     118e <main+0x1e>
je     11a5 <main+0x35>
xor    %edi,%edi
call   1080 <wait@plt>
jmp    121b <main+0xab>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
call   1040 <fprintf@plt>
jmp    121b <main+0xab>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2e3e(%rip),%rax
mov    0x2def(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e24(%rip),%rax
or     (%rax),%esi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
lea    0xe2f(%rip),%rdi
call   1050 <open@plt>
lea    0xe2f(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
mov    %rax,(%rsp)
lea    0xe1f(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <execvp@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [13 x i8] c"fork failed\0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [12 x i8] c"./p4.output\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@.str.2 = [3 x i8] c"wc\00"
@.str.3 = [5 x i8] c"p4.c\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 i32 (...) @fork()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %40
15: ; preds = %2
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %37
18: ; preds = %15
%19 = load i32, i32* @STDOUT_FILENO
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* @O_CREAT
%22 = load i32, i32* @O_WRONLY
%23 = or i32 %21, %22
%24 = load i32, i32* @O_TRUNC
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IRWXU
%27 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %25, i32 %26)
%28 = call i8* @strdup(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%29 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
store i8* %28, i8** %29
%30 = call i8* @strdup(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%31 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 1
store i8* %30, i8** %31
%32 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 2
store i8* null, i8** %32
%33 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
%34 = load i8*, i8** %33
%35 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
%36 = call i32 @execvp(i8* %34, i8** %35)
br label %39
37: ; preds = %15
%38 = call i32 @wait(i32* null)
store i32 %38, i32* %8
br label %39
39: ; preds = %37, %18
br label %40
40: ; preds = %39, %12
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [13 x i8] c"fork failed\0A\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [12 x i8] c"./p4.output\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@.str.2 = [3 x i8] c"wc\00"
@.str.3 = [5 x i8] c"p4.c\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [3 x i8*]
%4 = tail call i32 (...) @fork()
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %30
9: ; preds = %2
%10 = icmp eq i32 %4, 0
br i1 %10, label %11, label %28
11: ; preds = %9
%12 = load i32, i32* @STDOUT_FILENO
%13 = tail call i32 @close(i32 %12)
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_WRONLY
%16 = or i32 %15, %14
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRWXU
%20 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19)
%21 = bitcast [3 x i8*]* %3 to i8*
%22 = tail call dereferenceable_or_null(3) i8* @strdup(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%23 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
store i8* %22, i8** %23
%24 = tail call dereferenceable_or_null(5) i8* @strdup(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%25 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 1
store i8* %24, i8** %25
%26 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 2
store i8* null, i8** %26
%27 = call i32 @execvp(i8* %22, i8** %23)
br label %30
28: ; preds = %9
%29 = tail call i32 @wait(i32* null)
br label %30
30: ; preds = %11, %28, %6
ret i32 0
}