// File: /workspace/github_cpp_makefile_star2_O1/Sweet-john[P]Linux_bash/OS2.cpp
// Function: Redirection(char**, int)
bool Redirection(char *argv[], int size)
{
 bool check = false;
	for (int i = 0 ; i < size ; i++)
	{

	if (check == true)
		break;

	if (strcmp(argv[i],">")==0)
		{
		check = true;
		int fdo = dup(STDOUT_FILENO);
		int fd = open(argv[i+1],O_WRONLY|O_CREAT,0666);
		int fd2 = dup2(fd,STDOUT_FILENO);
		int j = 0;
		char* temp[20];
		while(j<i)
			{
			temp[j]=argv[j];
			j++;
			}
		temp[j]=NULL;
		bool isSpecial = checkSpecialOrder(temp);
		if (!isSpecial)
		execute(temp);
		close(fd2);
		dup2(fdo,STDOUT_FILENO);
		}
	if (strcmp(argv[i],"<")==0)
		{
		check = true;
		int fdi = dup(STDIN_FILENO);
		int fd = open(argv[i-1],O_RDONLY);
		if (fd<0)
			{
			perror("Open error ");
			return check;
			}
		int fd2 = dup2(fd,STDIN_FILENO);
		char* temp[20];
		int j;
		for (j = 0; j < size-i-1 ; j++)
			temp[j]=argv[j+i+1];
		temp[j]=NULL;
		bool isSpecial = checkSpecialOrder(temp);
		if (!isSpecial)
		execute(temp);
		close(fd2);
		dup2(fdi,STDIN_FILENO);
		}
	if (strcmp(argv[i],"|")==0)
		{
		check = true;
		char* input[20];
		char* output[20];
		int j = 0;		
		while(j<i)
			{
			input[j]=argv[j];
			j++;
			}
		input[j]=NULL;
		
		for (j = 0; j < size-i-1 ; j++)
			output[j]=argv[j+i+1];
		output[j]=NULL;
		
		pipe(input,output);
		}
	}
 return check;
}

// Assembly:
testl %esi, %esi
jle 0x31a9
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rdi, %r14
leal -0x1(%rsi), %eax
movq %rsi, -0x48(%rbp)
movl %esi, %ecx
movq %rcx, -0x58(%rbp)
leaq 0x8(%rdi), %rcx
movq %rcx, -0x60(%rbp)
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
xorl %r15d, %r15d
movl %eax, -0x3c(%rbp)
movl %eax, %r13d
xorl %r12d, %r12d
testb $0x1, %r15b
jne 0x3191
leaq (,%r12,8), %rax
movq %rax, -0x50(%rbp)
incl -0x2c(%rbp)
movq (%r14,%r12,8), %rdi
leaq 0x115b(%rip), %rsi      # 0x40ad
callq 0x21d0
testl %eax, %eax
jne 0x2ff6
movl $0x1, %edi
callq 0x2210
movl %eax, -0x38(%rbp)
movq 0x8(%r14,%r12,8), %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x20b0
movl %eax, %edi
movl $0x1, %esi
callq 0x2110
movl %eax, %r15d
testq %r12, %r12
je 0x2fb1
leaq -0x100(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq -0x50(%rbp), %rdx
callq 0x20e0
movl -0x2c(%rbp), %eax
jmp 0x2fba
xorl %eax, %eax
leaq -0x100(%rbp), %rbx
movq $0x0, -0x100(%rbp,%rax,8)
movq %rbx, %rdi
callq 0x24dc
testb %al, %al
jne 0x2fde
leaq -0x100(%rbp), %rdi
callq 0x27d2
movl %r15d, %edi
callq 0x22b0
movl -0x38(%rbp), %edi
movl $0x1, %esi
callq 0x2110
movb $0x1, %r15b
movl %r13d, %r13d
movq -0x60(%rbp), %rax
leaq (%rax,%r12,8), %rax
movq %rax, -0x38(%rbp)
movq (%r14,%r12,8), %rdi
leaq 0x109f(%rip), %rsi      # 0x40af
callq 0x21d0
testl %eax, %eax
jne 0x30cd
xorl %edi, %edi
callq 0x2210
movl %eax, %r15d
movq -0x8(%r14,%r12,8), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x20b0
movl %eax, %ebx
testl %eax, %eax
js 0x3180
movl %r15d, -0x40(%rbp)
movl %ebx, %edi
xorl %esi, %esi
callq 0x2110
movl %eax, %r15d
movl %r12d, %eax
subl -0x48(%rbp), %eax
cmpl $-0x2, %eax
jg 0x3084
movl -0x3c(%rbp), %eax
movl %eax, %edx
subl %r12d, %edx
shlq $0x3, %rdx
leaq -0x100(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x20e0
xorl %eax, %eax
incq %rax
cmpq %rax, %r13
jne 0x3078
movl %eax, %eax
jmp 0x3086
xorl %eax, %eax
movq $0x0, -0x100(%rbp,%rax,8)
leaq -0x100(%rbp), %rdi
callq 0x24dc
testb %al, %al
jne 0x30ae
leaq -0x100(%rbp), %rdi
callq 0x27d2
movl %r15d, %edi
callq 0x22b0
movl -0x40(%rbp), %edi
xorl %esi, %esi
callq 0x2110
movb $0x1, %r15b
movb $0x1, %al
testl %ebx, %ebx
js 0x3193
movq (%r14,%r12,8), %rdi
leaq 0xfe5(%rip), %rsi       # 0x40bd
callq 0x21d0
testl %eax, %eax
jne 0x316e
testq %r12, %r12
movq -0x48(%rbp), %rbx
je 0x3109
leaq -0x100(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq -0x50(%rbp), %rdx
callq 0x20e0
movl -0x2c(%rbp), %eax
jmp 0x3112
xorl %eax, %eax
leaq -0x100(%rbp), %r15
movq $0x0, -0x100(%rbp,%rax,8)
movl %r12d, %eax
subl %ebx, %eax
cmpl $-0x2, %eax
jg 0x314e
leaq (,%r13,8), %rdx
leaq -0x1a0(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x20e0
xorl %eax, %eax
incq %rax
cmpq %rax, %r13
jne 0x3142
movl %eax, %eax
jmp 0x3150
xorl %eax, %eax
movq $0x0, -0x1a0(%rbp,%rax,8)
movq %r15, %rdi
leaq -0x1a0(%rbp), %rsi
callq 0x31ae
movb $0x1, %r15b
incq %r12
decl %r13d
cmpq -0x58(%rbp), %r12
jne 0x2f2e
jmp 0x3191
leaq 0xf2a(%rip), %rdi       # 0x40b1
callq 0x20f0
jmp 0x30c0
xorl %eax, %eax
orb %r15b, %al
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x31ab
xorl %eax, %eax
andb $0x1, %al
retq