#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 */
char EOF;
int EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execve(char *, char **, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ putchar(char);
int executeFileInCommand(char *commandName, char *argv[], char *filename) {
  int pipefd[2];
  if (pipe(pipefd)) {
    perror("pipe");
    exit(127);
  }
  switch (fork()) {
  case -1:
    perror("fork()");
    exit(127);
  case 0:
    close(pipefd[0]);
    dup2(pipefd[1], 1);
    close(pipefd[1]);
    FILE *pFile;
    char mystring;
    pFile = fopen(filename, "r");
    if (pFile == 0)
      perror("Error opening file");
    else {
      while ((mystring = fgetc(pFile)) != EOF) {
        putchar(mystring);
      }
      fclose(pFile);
    }
    exit(EXIT_SUCCESS);
  default:
    close(pipefd[1]);
    dup2(pipefd[0], 0);
    close(pipefd[0]);
    execve(commandName, argv, 0);
    perror(commandName);
    exit(125);
  }
  return 0;
}


x86 assembly (clang, -O0):
<executeFileInCommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
je     11ed <executeFileInCommand+0x3d>
lea    0xe22(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x7f,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x30(%rbp)
sub    $0xffffffff,%eax
je     1217 <executeFileInCommand+0x67>
jmp    1207 <executeFileInCommand+0x57>
mov    -0x30(%rbp),%eax
test   %eax,%eax
je     122d <executeFileInCommand+0x7d>
jmp    12c5 <executeFileInCommand+0x115>
lea    0xde7(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x7f,%edi
call   10c0 <exit@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%rdi
lea    0xdb7(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    127a <executeFileInCommand+0xca>
lea    0xd9e(%rip),%rdi
call   10b0 <perror@plt>
jmp    12b7 <executeFileInCommand+0x107>
jmp    127f <executeFileInCommand+0xcf>
mov    -0x28(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %al,-0x29(%rbp)
movsbl %al,%eax
mov    0x2d53(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     12ae <executeFileInCommand+0xfe>
movsbl -0x29(%rbp),%edi
call   1030 <putchar@plt>
jmp    127f <executeFileInCommand+0xcf>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1090 <execve@plt>
mov    -0x8(%rbp),%rdi
call   10b0 <perror@plt>
mov    $0x7d,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<executeFileInCommand>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
jne    11ea <executeFileInCommand+0x3a>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     1200 <executeFileInCommand+0x50>
cmp    $0xffffffff,%eax
jne    124e <executeFileInCommand+0x9e>
lea    0xe1d(%rip),%rdi
jmp    11f1 <executeFileInCommand+0x41>
lea    0xe0f(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x7f,%edi
call   10c0 <exit@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
lea    0xde5(%rip),%rsi
mov    %r15,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
jne    128a <executeFileInCommand+0xda>
lea    0xdd3(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1090 <execve@plt>
mov    %rbx,%rdi
call   10b0 <perror@plt>
mov    $0x7d,%edi
call   10c0 <exit@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <fgetc@plt>
mov    0x2d4c(%rip),%r14
cmp    (%r14),%al
je     12c5 <executeFileInCommand+0x115>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
cmp    (%r14),%al
jne    12b0 <executeFileInCommand+0x100>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1240 <executeFileInCommand+0x90>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [7 x i8] c"fork()\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [19 x i8] c"Error opening file\00"
@EOF = global i8 0
@EXIT_SUCCESS = global i32 0
define i32 @executeFileInCommand(i8* %0, i8** %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [2 x i32]
%8 = alloca i32*
%9 = alloca i8
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8* %2, i8** %6
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 127)
unreachable
16: ; preds = %3
%17 = call i32 (...) @fork()
switch i32 %17, label %54 [
i32 -1, label %18
i32 0, label %21
]
18: ; preds = %16
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 127)
unreachable
21: ; preds = %16
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @dup2(i32 %26, i32 1)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = load i8*, i8** %6
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %32, i32** %8
%33 = load i32*, i32** %8
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %37
35: ; preds = %21
%36 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %51
37: ; preds = %21
br label %38
38: ; preds = %45, %37
%39 = load i32*, i32** %8
%40 = call signext i8 @fgetc(i32* %39)
store i8 %40, i8* %9
%41 = sext i8 %40 to i32
%42 = load i8, i8* @EOF
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %41, %43
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = load i8, i8* %9
%47 = call i32 @putchar(i8 signext %46)
br label %38
48: ; preds = %38
%49 = load i32*, i32** %8
%50 = call i32 @fclose(i32* %49)
br label %51
51: ; preds = %48, %35
%52 = load i32, i32* @EXIT_SUCCESS
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %16
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%59 = load i32, i32* %58
%60 = call i32 @dup2(i32 %59, i32 0)
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%62 = load i32, i32* %61
%63 = call i32 @close(i32 %62)
%64 = load i8*, i8** %4
%65 = load i8**, i8*** %5
%66 = call i32 @execve(i8* %64, i8** %65, i32 0)
%67 = load i8*, i8** %4
%68 = call i32 @perror(i8* %67)
%69 = call i32 @exit(i32 125)
unreachable
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [7 x i8] c"fork()\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [19 x i8] c"Error opening file\00"
@EOF = global i8 0
@EXIT_SUCCESS = global i32 0
define i32 @executeFileInCommand(i8* %0, i8** %1, i8* nocapture %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %3
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 127)
unreachable
12: ; preds = %3
%13 = call i32 (...) @fork()
switch i32 %13, label %44 [
i32 -1, label %14
i32 0, label %17
]
14: ; preds = %12
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 127)
unreachable
17: ; preds = %12
%18 = load i32, i32* %6
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @dup2(i32 %21, i32 1)
%23 = load i32, i32* %20
%24 = call i32 @close(i32 %23)
%25 = call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %31, label %27
27: ; preds = %17
%28 = call signext i8 @fgetc(i32* %25)
%29 = load i8, i8* @EOF
%30 = icmp eq i8 %28, %29
br i1 %30, label %39, label %33
31: ; preds = %17
%32 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %41
33: ; preds = %27, %33
%34 = phi i8 [ %36, %33 ], [ %28, %27 ]
%35 = call i32 @putchar(i8 signext %34)
%36 = call signext i8 @fgetc(i32* %25)
%37 = load i8, i8* @EOF
%38 = icmp eq i8 %36, %37
br i1 %38, label %39, label %33
39: ; preds = %33, %27
%40 = call i32 @fclose(i32* %25)
br label %41
41: ; preds = %39, %31
%42 = load i32, i32* @EXIT_SUCCESS
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %12
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %6
%49 = call i32 @dup2(i32 %48, i32 0)
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
%52 = call i32 @execve(i8* %0, i8** %1, i32 0)
%53 = call i32 @perror(i8* %0)
%54 = call i32 @exit(i32 125)
unreachable
}