#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 */
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execv(char *, char **);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
char *getenv(char *);
char *malloc(int);
int /*<<< orphan*/ pipe(int *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
char *strtok(char *, char *);
void cmd_output_direction(char *argv[], char *filename) {
  int pipefd[2];
  pipe(pipefd);
  if (fork() == 0) {
    close(pipefd[0]);
    dup2(pipefd[1], 1);
    dup2(pipefd[1], 2);
    close(pipefd[1]);
    execv(argv[0], argv);
    char *path = getenv("PATH");
    char *pch;
    pch = strtok(path, ":");
    while (pch != NULL) {
      char *str = malloc(100);
      strcat(str, pch);
      strcat(str, "/");
      strcat(str, argv[0]);
      pch = strtok(NULL, ":");
      execv(str, argv);
    }
  } else {
    char buffer[1024];
    close(pipefd[1]);
    while (read(pipefd[0], buffer, sizeof(buffer)) != 0) {
      FILE *f = fopen(filename, "w");
      fputs(buffer, f);
      fclose(f);
    }
  }
}


x86 assembly (clang, -O0):
<cmd_output_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1080 <pipe@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
cmp    $0x0,%rax
jne    12cc <cmd_output_direction+0xfc>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   10e0 <execv@plt>
lea    0xdc2(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdb3(%rip),%rsi
call   10c0 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12c7 <cmd_output_direction+0xf7>
mov    $0x64,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10d0 <strcat@plt>
mov    -0x30(%rbp),%rdi
lea    0xd7b(%rip),%rsi
call   10d0 <strcat@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
call   10d0 <strcat@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd59(%rip),%rsi
call   10c0 <strtok@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10e0 <execv@plt>
jmp    125b <cmd_output_direction+0x8b>
jmp    1332 <cmd_output_direction+0x162>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
cmp    $0x0,%rax
je     132d <cmd_output_direction+0x15d>
mov    -0x10(%rbp),%rdi
lea    0xd0c(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x438(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x438(%rbp),%rsi
call   1060 <fputs@plt>
mov    -0x438(%rbp),%rdi
call   1040 <fclose@plt>
jmp    12d4 <cmd_output_direction+0x104>
jmp    1332 <cmd_output_direction+0x162>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_output_direction>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
je     126c <cmd_output_direction+0x9c>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %rax,%rax
je     132b <cmd_output_direction+0x15b>
lea    0xde1(%rip),%r14
lea    0x10(%rsp),%r12
nopl   (%rax)
mov    %r15,%rdi
mov    %r14,%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %rax,%rsi
call   1060 <fputs@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %rax,%rax
jne    1230 <cmd_output_direction+0x60>
jmp    132b <cmd_output_direction+0x15b>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    0xc(%rsp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   10e0 <execv@plt>
lea    0xd54(%rip),%rdi
call   1030 <getenv@plt>
lea    0xd4d(%rip),%rsi
mov    %rax,%rdi
call   10c0 <strtok@plt>
test   %rax,%rax
je     132b <cmd_output_direction+0x15b>
mov    %rax,%r13
lea    0xd38(%rip),%r15
lea    0xd2f(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    $0x64,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13,%rsi
call   10d0 <strcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10d0 <strcat@plt>
mov    (%r14),%rsi
mov    %rbx,%rdi
call   10d0 <strcat@plt>
xor    %edi,%edi
mov    %r12,%rsi
call   10c0 <strtok@plt>
mov    %rax,%r13
mov    %rbx,%rdi
mov    %r14,%rsi
call   10e0 <execv@plt>
test   %r13,%r13
jne    12e0 <cmd_output_direction+0x110>
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [2 x i8] c"w\00"
define void @cmd_output_direction(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca [2 x i32]
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [1024 x i8]
%10 = alloca i32*
store i8** %0, i8*** %3
store i8* %1, i8** %4
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %56
15: ; preds = %2
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%17 = load i32, i32* %16
%18 = call i32 @close(i32 %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @dup2(i32 %20, i32 1)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @dup2(i32 %23, i32 2)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = load i8**, i8*** %3
%29 = getelementptr inbounds i8*, i8** %28, i64 0
%30 = load i8*, i8** %29
%31 = load i8**, i8*** %3
%32 = call i32 @execv(i8* %30, i8** %31)
%33 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = call i8* @strtok(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %35, i8** %7
br label %36
36: ; preds = %39, %15
%37 = load i8*, i8** %7
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %55
39: ; preds = %36
%40 = call i8* @malloc(i32 100)
store i8* %40, i8** %8
%41 = load i8*, i8** %8
%42 = load i8*, i8** %7
%43 = call i32 @strcat(i8* %41, i8* %42)
%44 = load i8*, i8** %8
%45 = call i32 @strcat(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%46 = load i8*, i8** %8
%47 = load i8**, i8*** %3
%48 = getelementptr inbounds i8*, i8** %47, i64 0
%49 = load i8*, i8** %48
%50 = call i32 @strcat(i8* %46, i8* %49)
%51 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %51, i8** %7
%52 = load i8*, i8** %8
%53 = load i8**, i8*** %3
%54 = call i32 @execv(i8* %52, i8** %53)
br label %36
55: ; preds = %36
br label %75
56: ; preds = %2
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%58 = load i32, i32* %57
%59 = call i32 @close(i32 %58)
br label %60
60: ; preds = %66, %56
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%62 = load i32, i32* %61
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%64 = call i64 @read(i32 %62, i8* %63, i32 1024)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %74
66: ; preds = %60
%67 = load i8*, i8** %4
%68 = call i32* @fopen(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %68, i32** %10
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%70 = load i32*, i32** %10
%71 = call i32 @fputs(i8* %69, i32* %70)
%72 = load i32*, i32** %10
%73 = call i32 @fclose(i32* %72)
br label %60
74: ; preds = %60
br label %75
75: ; preds = %74, %55
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [2 x i8] c"w\00"
define void @cmd_output_direction(i8** %0, i8* nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca [1024 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = call i64 (...) @fork()
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %35
10: ; preds = %2
%11 = load i32, i32* %6
%12 = call i32 @close(i32 %11)
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%14 = load i32, i32* %13
%15 = call i32 @dup2(i32 %14, i32 1)
%16 = load i32, i32* %13
%17 = call i32 @dup2(i32 %16, i32 2)
%18 = load i32, i32* %13
%19 = call i32 @close(i32 %18)
%20 = load i8*, i8** %0
%21 = call i32 @execv(i8* %20, i8** %0)
%22 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%23 = call i8* @strtok(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i8* %23, null
br i1 %24, label %51, label %25
25: ; preds = %10, %25
%26 = phi i8* [ %32, %25 ], [ %23, %10 ]
%27 = call dereferenceable_or_null(100) i8* @malloc(i32 100)
%28 = call i32 @strcat(i8* %27, i8* %26)
%29 = call i32 @strcat(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = load i8*, i8** %0
%31 = call i32 @strcat(i8* %27, i8* %30)
%32 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @execv(i8* %27, i8** %0)
%34 = icmp eq i8* %32, null
br i1 %34, label %51, label %25
35: ; preds = %2
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %6
%41 = call i64 @read(i32 %40, i8* %36, i32 1024)
%42 = icmp eq i64 %41, 0
br i1 %42, label %50, label %43
43: ; preds = %35, %43
%44 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @fputs(i8* %36, i32* %44)
%46 = call i32 @fclose(i32* %44)
%47 = load i32, i32* %6
%48 = call i64 @read(i32 %47, i8* %36, i32 1024)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %43
50: ; preds = %43, %35
br label %51
51: ; preds = %25, %10, %50
ret void
}