#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ dup2(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ *stdin;
void cmd_input_direction(char *argv[], char *filename) {
  if (fork() == 0) {
    FILE *f = fopen(filename, "r");
    dup2(fileno(f), fileno(stdin));
    fclose(f);
    execvp(argv[0], argv);
  }
}


x86 assembly (clang, -O0):
<cmd_input_direction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jne    11d3 <cmd_input_direction+0x73>
mov    -0x10(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%rdi
call   1050 <fileno@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <execvp@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_input_direction>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     117b <cmd_input_direction+0x1b>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe7e(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fileno@plt>
mov    %eax,%ebp
mov    0x2e4a(%rip),%rax
mov    (%rax),%rdi
call   1050 <fileno@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1040 <dup2@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <execvp@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stdin = global i32* null
define void @cmd_input_direction(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i32*
store i8** %0, i8*** %3
store i8* %1, i8** %4
%6 = call i64 (...) @fork()
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %23
8: ; preds = %2
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = call i32 @fileno(i32* %11)
%13 = load i32*, i32** @stdin
%14 = call i32 @fileno(i32* %13)
%15 = call i32 @dup2(i32 %12, i32 %14)
%16 = load i32*, i32** %5
%17 = call i32 @fclose(i32* %16)
%18 = load i8**, i8*** %3
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = load i8**, i8*** %3
%22 = call i32 @execvp(i8* %20, i8** %21)
br label %23
23: ; preds = %8, %2
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stdin = global i32* null
define void @cmd_input_direction(i8** %0, i8* nocapture %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %14
5: ; preds = %2
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @fileno(i32* %6)
%8 = load i32*, i32** @stdin
%9 = tail call i32 @fileno(i32* %8)
%10 = tail call i32 @dup2(i32 %7, i32 %9)
%11 = tail call i32 @fclose(i32* %6)
%12 = load i8*, i8** %0
%13 = tail call i32 @execvp(i8* %12, i8** %0)
br label %14
14: ; preds = %5, %2
ret void
}