#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
scalar_t__ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ wait(int *);
void runIn(char *command, char **args, int i) {
  pid_t childPid;
  int status, fdin, _stdin;
  _stdin = dup(0);
  fdin = open(args[i + 1], O_RDONLY);
  fdin = dup(_stdin);
  close(fdin);
  if (args[i + 1] == NULL) {
    fprintf(stderr, "shox: syntax error near unexpected token `newline'");
  } else {
    if (fdin == -1) {
      fprintf(stderr, "File %s not exist\n", args[i + 1]);
      exit(EXIT_FAILURE);
    }
    if ((childPid = fork()) < 0) {
      fprintf(stderr, "Not possible to create child process\n");
      exit(EXIT_FAILURE);
    } else if (childPid == 0) {
      if (execvp(command, &args[i]) < 0) {
        fprintf(stderr, "Not possible to execute %s\n", command);
        perror("[ERROR]");
        exit(EXIT_FAILURE);
      }
    } else if (childPid < 0) {
      fprintf(stderr, "Not possible to fork\n");
    } else {
      dup2(_stdin, 0);
      close(_stdin);
      while (wait(&status) != childPid)
        ;
    }
  }
}


x86 assembly (clang, -O0):
<runIn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    1226 <runIn+0x86>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    136c <runIn+0x1cc>
cmpl   $0xffffffff,-0x28(%rbp)
jne    1266 <runIn+0xc6>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    12a0 <runIn+0x100>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1307 <runIn+0x167>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
call   1090 <execvp@plt>
cmp    $0x0,%rax
jge    1302 <runIn+0x162>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd8b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xd99(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1362 <runIn+0x1c2>
cmpq   $0x0,-0x20(%rbp)
jge    132e <runIn+0x18e>
mov    0x2cbf(%rip),%rax
mov    (%rax),%edi
lea    0xd6e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    135d <runIn+0x1bd>
mov    -0x2c(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
lea    -0x24(%rbp),%rdi
call   10b0 <wait@plt>
cmp    -0x20(%rbp),%rax
je     1358 <runIn+0x1b8>
jmp    1340 <runIn+0x1a0>
jmp    135d <runIn+0x1bd>
jmp    1362 <runIn+0x1c2>
jmp    1367 <runIn+0x1c7>
jmp    136c <runIn+0x1cc>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runIn>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%r12
mov    %rdi,%r14
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,%r15d
movslq %ebp,%rbx
mov    0x8(%r12,%rbx,8),%rdi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %r15d,%edi
call   1040 <dup@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1050 <close@plt>
mov    0x8(%r12,%rbx,8),%rdx
test   %rdx,%rdx
je     122f <runIn+0x8f>
cmp    $0xffffffff,%ebp
je     1270 <runIn+0xd0>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1289 <runIn+0xe9>
je     1252 <runIn+0xb2>
mov    %rax,%rbp
mov    %r15d,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %r15d,%edi
call   1050 <close@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   10b0 <wait@plt>
cmp    %rbp,%rax
jne    1220 <runIn+0x80>
jmp    1263 <runIn+0xc3>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fprintf@plt>
lea    (%r12,%rbx,8),%rsi
mov    %r14,%rdi
call   1090 <execvp@plt>
test   %rax,%rax
js     12a2 <runIn+0x102>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12c8 <runIn+0x128>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12c8 <runIn+0x128>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xdc5(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [51 x i8] c"shox: syntax error near unexpected token `newline'\00"
@.str.1 = [19 x i8] c"File %s not exist\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [38 x i8] c"Not possible to create child process\0A\00"
@.str.3 = [28 x i8] c"Not possible to execute %s\0A\00"
@.str.4 = [8 x i8] c"[ERROR]\00"
@.str.5 = [22 x i8] c"Not possible to fork\0A\00"
define void @runIn(i8* %0, i8** %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
%11 = call i32 @dup(i32 0)
store i32 %11, i32* %10
%12 = load i8**, i8*** %5
%13 = load i32, i32* %6
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8*, i8** %12, i64 %15
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %9
%20 = load i32, i32* %10
%21 = call i32 @dup(i32 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = call i32 @close(i32 %22)
%24 = load i8**, i8*** %5
%25 = load i32, i32* %6
%26 = add nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8*, i8** %24, i64 %27
%29 = load i8*, i8** %28
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %34
31: ; preds = %3
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
br label %95
34: ; preds = %3
%35 = load i32, i32* %9
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %48
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = load i8**, i8*** %5
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8*, i8** %39, i64 %42
%44 = load i8*, i8** %43
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %44)
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %34
%49 = call i64 (...) @fork()
store i64 %49, i64* %7
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %48
%57 = load i64, i64* %7
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %75
59: ; preds = %56
%60 = load i8*, i8** %4
%61 = load i8**, i8*** %5
%62 = load i32, i32* %6
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8*, i8** %61, i64 %63
%65 = call i64 @execvp(i8* %60, i8** %64)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %74
67: ; preds = %59
%68 = load i32, i32* @stderr
%69 = load i8*, i8** %4
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %69)
%71 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%72 = load i32, i32* @EXIT_FAILURE
%73 = call i32 @exit(i32 %72)
unreachable
74: ; preds = %59
br label %93
75: ; preds = %56
%76 = load i64, i64* %7
%77 = icmp slt i64 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load i32, i32* @stderr
%80 = call i32 (i32, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %92
81: ; preds = %75
%82 = load i32, i32* %10
%83 = call i32 @dup2(i32 %82, i32 0)
%84 = load i32, i32* %10
%85 = call i32 @close(i32 %84)
br label %86
86: ; preds = %90, %81
%87 = call i64 @wait(i32* %8)
%88 = load i64, i64* %7
%89 = icmp ne i64 %87, %88
br i1 %89, label %90, label %91
90: ; preds = %86
br label %86
91: ; preds = %86
br label %92
92: ; preds = %91, %78
br label %93
93: ; preds = %92, %74
br label %94
94: ; preds = %93
br label %95
95: ; preds = %94, %31
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [51 x i8] c"shox: syntax error near unexpected token `newline'\00"
@.str.1 = [19 x i8] c"File %s not exist\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [38 x i8] c"Not possible to create child process\0A\00"
@.str.3 = [28 x i8] c"Not possible to execute %s\0A\00"
@.str.4 = [8 x i8] c"[ERROR]\00"
define void @runIn(i8* %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32 @dup(i32 0)
%7 = add nsw i32 %2, 1
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8*, i8** %1, i64 %8
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = tail call i32 @dup(i32 %6)
%14 = tail call i32 @close(i32 %13)
%15 = load i8*, i8** %9
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
br label %54
20: ; preds = %3
%21 = icmp eq i32 %13, -1
br i1 %21, label %22, label %27
22: ; preds = %20
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%25 = load i32, i32* @EXIT_FAILURE
%26 = tail call i32 @exit(i32 %25)
unreachable
27: ; preds = %20
%28 = tail call i64 (...) @fork()
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %27
%36 = icmp eq i64 %28, 0
br i1 %36, label %37, label %48
37: ; preds = %35
%38 = sext i32 %2 to i64
%39 = getelementptr inbounds i8*, i8** %1, i64 %38
%40 = tail call i64 @execvp(i8* %0, i8** %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %54
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = tail call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%45 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = tail call i32 @exit(i32 %46)
unreachable
48: ; preds = %35
%49 = tail call i32 @dup2(i32 %6, i32 0)
%50 = tail call i32 @close(i32 %6)
br label %51
51: ; preds = %51, %48
%52 = call i64 @wait(i32* %4)
%53 = icmp eq i64 %52, %28
br i1 %53, label %54, label %51
54: ; preds = %51, %37, %17
ret void
}