// File: /workspace/github_cpp_makefile_star2_O3/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 0x3189
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
leal -0x1(%rsi), %r12d
movl %esi, %r8d
movq %rdi, -0x58(%rbp)
addq $0x8, %rdi
negl %esi
movq %rsi, -0x60(%rbp)
xorl %eax, %eax
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %r8, -0x48(%rbp)
movq %rdi, -0x40(%rbp)
testb $0x1, %al
jne 0x3176
leal -0x1(%r15), %ebx
incq %rbx
movq -0x8(%rdi,%r15,8), %rcx
movb (%rcx), %dl
cmpb $0x3e, %dl
jne 0x2fe5
cmpb $0x0, 0x1(%rcx)
jne 0x3163
movq %rdi, %r14
movl $0x1, %edi
callq 0x2210
movl %eax, -0x38(%rbp)
movq (%r14,%r15,8), %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x20c0
movl %eax, %edi
movl $0x1, %esi
callq 0x2120
movl %eax, %r14d
testq %r15, %r15
je 0x2f95
movl %r14d, -0x30(%rbp)
leaq -0x100(%rbp), %r14
movq %r14, %rdi
movq -0x58(%rbp), %rsi
movq %r13, %rdx
callq 0x20f0
movq %r14, %rdi
movl -0x30(%rbp), %r14d
movl %ebx, %eax
jmp 0x2f9e
xorl %eax, %eax
leaq -0x100(%rbp), %rdi
movq $0x0, -0x100(%rbp,%rax,8)
callq 0x24dc
testb %al, %al
jne 0x2fbf
leaq -0x100(%rbp), %rdi
callq 0x27c1
movl %r14d, %edi
callq 0x22b0
movl -0x38(%rbp), %edi
movl $0x1, %esi
callq 0x2120
movq -0x40(%rbp), %rdi
movq -0x8(%rdi,%r15,8), %rcx
movb (%rcx), %dl
movb $0x1, %al
movq -0x48(%rbp), %r8
leaq (%rdi,%r15,8), %rsi
movl %r12d, %r14d
leaq (,%r14,8), %r9
cmpb $0x3c, %dl
jne 0x30c0
cmpb $0x0, 0x1(%rcx)
jne 0x3163
movq %r9, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
xorl %edi, %edi
callq 0x2210
movl %eax, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq -0x10(%rax,%r15,8), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x20c0
testl %eax, %eax
js 0x318e
movl %eax, %edi
xorl %esi, %esi
callq 0x2120
movl %eax, -0x4c(%rbp)
movq -0x60(%rbp), %rax
addl %r15d, %eax
cmpl $-0x2, %eax
jg 0x306b
leaq -0x100(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x20f0
leaq -0x100(%rbp), %rdi
movq %r14, %rax
jmp 0x3074
xorl %eax, %eax
leaq -0x100(%rbp), %rdi
movq $0x0, -0x100(%rbp,%rax,8)
callq 0x24dc
testb %al, %al
jne 0x3095
leaq -0x100(%rbp), %rdi
callq 0x27c1
movl -0x4c(%rbp), %edi
callq 0x22b0
movl -0x50(%rbp), %edi
xorl %esi, %esi
callq 0x2120
movq -0x40(%rbp), %rdi
movq -0x8(%rdi,%r15,8), %rcx
movb (%rcx), %dl
movb $0x1, %al
movq -0x48(%rbp), %r8
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %r9
cmpb $0x7c, %dl
jne 0x3163
cmpb $0x0, 0x1(%rcx)
jne 0x3163
testq %r15, %r15
je 0x30ff
leaq -0x100(%rbp), %rdi
movq %rsi, -0x38(%rbp)
movq -0x58(%rbp), %rsi
movq %r13, %rdx
movq %r9, -0x30(%rbp)
callq 0x20f0
movq -0x30(%rbp), %r9
movq -0x38(%rbp), %rsi
movl %ebx, %eax
jmp 0x3101
xorl %eax, %eax
movq $0x0, -0x100(%rbp,%rax,8)
movq -0x60(%rbp), %rax
addl %r15d, %eax
cmpl $-0x2, %eax
jg 0x3137
leaq -0x1a0(%rbp), %rbx
movq %rbx, %rdi
movq %r9, %rdx
callq 0x20f0
movq %rbx, %rsi
leaq -0x100(%rbp), %rdi
jmp 0x3148
xorl %r14d, %r14d
leaq -0x100(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
movq $0x0, -0x1a0(%rbp,%r14,8)
callq 0x319e
movb $0x1, %al
movq -0x48(%rbp), %r8
movq -0x40(%rbp), %rdi
incq %r15
decl %r12d
addq $0x8, %r13
cmpq %r15, %r8
jne 0x2f0e
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x318b
xorl %eax, %eax
andb $0x1, %al
retq
leaq 0xf15(%rip), %rdi       # 0x40aa
callq 0x2100
movb $0x1, %al
jmp 0x3176