#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 /*<<< 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");
    exit(1);
  } else if (rc == 0) {
    close(STDOUT_FILENO);
    open("./p4-output.txt", O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
    char *myargs[3];
    myargs[0] = strdup("wc");
    myargs[1] = strdup("p4-redirect.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   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c9 <main+0x49>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    124b <main+0xcb>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    0x2dbf(%rip),%rax
or     (%rax),%esi
mov    0x2df6(%rip),%rax
or     (%rax),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
lea    0xe01(%rip),%rdi
call   1050 <open@plt>
lea    0xe05(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x30(%rbp)
lea    0xdf8(%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>
jmp    1257 <main+0xd7>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
mov    %eax,-0x34(%rbp)
jmp    125c <main+0xdc>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
js     121d <main+0x9d>
je     119f <main+0x1f>
xor    %edi,%edi
call   1090 <wait@plt>
jmp    1215 <main+0x95>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1030 <close@plt>
mov    0x2e44(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2a(%rip),%rax
or     (%rax),%esi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
lea    0xe35(%rip),%rdi
call   1050 <open@plt>
lea    0xe39(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
mov    %rax,(%rsp)
lea    0xe29(%rip),%rdi
call   1080 <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
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

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