// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rdi, %r14
leaq 0x8(%rdi), %r13
xorl %ecx, %ecx
testl %esi, %esi
movl $0x0, %r15d
movl %esi, -0x2c(%rbp)
cmovgl %esi, %r15d
xorl %ebx, %ebx
movq %r15, -0x38(%rbp)
cmpq %r15, %rbx
je 0x3014
testb $0x1, %cl
jne 0x3014
movq (%r14,%rbx,8), %rax
movb (%rax), %dl
cmpb $0x3e, %dl
jne 0x2ef6
cmpb $0x0, 0x1(%rax)
jne 0x3008
pushq $0x1
popq %r15
movl %r15d, %edi
callq 0x2220
movl %eax, %r12d
movq 0x8(%r14,%rbx,8), %rdi
pushq $0x41
popq %rsi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x20e0
movl %eax, %edi
movl %r15d, %esi
callq 0x2130
movl %eax, %r15d
xorl %eax, %eax
cmpq %rax, %rbx
je 0x2eb0
movq (%r14,%rax,8), %rcx
movq %rcx, -0xe0(%rbp,%rax,8)
incq %rax
jmp 0x2e9a
movl %ebx, %eax
andq $0x0, -0xe0(%rbp,%rax,8)
leaq -0xe0(%rbp), %rdi
callq 0x246c
testb %al, %al
jne 0x2ed7
leaq -0xe0(%rbp), %rdi
callq 0x26c1
movl %r15d, %edi
callq 0x22a0
movl %r12d, %edi
pushq $0x1
popq %rsi
callq 0x2130
movq (%r14,%rbx,8), %rax
movb (%rax), %dl
movb $0x1, %cl
movq -0x38(%rbp), %r15
cmpb $0x3c, %dl
jne 0x2f9a
cmpb $0x0, 0x1(%rax)
jne 0x3008
xorl %edi, %edi
callq 0x2220
movl %eax, %r12d
movq -0x8(%r14,%rbx,8), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x20e0
testl %eax, %eax
js 0x3030
movl %eax, %edi
xorl %esi, %esi
callq 0x2130
movl %eax, %r15d
movl %ebx, %eax
notl %eax
addl -0x2c(%rbp), %eax
cltq
xorl %ecx, %ecx
cmpq %rax, %rcx
jge 0x2f57
movq (%r13,%rcx,8), %rdx
movq %rdx, -0xe0(%rbp,%rcx,8)
incq %rcx
jmp 0x2f40
andq $0x0, -0xe0(%rbp,%rcx,8)
leaq -0xe0(%rbp), %rdi
callq 0x246c
testb %al, %al
jne 0x2f7c
leaq -0xe0(%rbp), %rdi
callq 0x26c1
movl %r15d, %edi
callq 0x22a0
movl %r12d, %edi
xorl %esi, %esi
callq 0x2130
movq (%r14,%rbx,8), %rax
movb (%rax), %dl
movb $0x1, %cl
movq -0x38(%rbp), %r15
cmpb $0x7c, %dl
jne 0x3008
cmpb $0x0, 0x1(%rax)
jne 0x3008
xorl %eax, %eax
cmpq %rax, %rbx
je 0x2fbd
movq (%r14,%rax,8), %rcx
movq %rcx, -0xe0(%rbp,%rax,8)
incq %rax
jmp 0x2fa7
movl %ebx, %eax
andq $0x0, -0xe0(%rbp,%rax,8)
movl %ebx, %eax
notl %eax
addl -0x2c(%rbp), %eax
cltq
xorl %ecx, %ecx
cmpq %rax, %rcx
jge 0x2fea
movq (%r13,%rcx,8), %rdx
movq %rdx, -0x180(%rbp,%rcx,8)
incq %rcx
jmp 0x2fd3
andq $0x0, -0x180(%rbp,%rcx,8)
leaq -0xe0(%rbp), %rdi
leaq -0x180(%rbp), %rsi
callq 0x3040
movb $0x1, %cl
incq %rbx
addq $0x8, %r13
jmp 0x2e3d
cmpq %r15, %rbx
setne %al
orb %cl, %al
andb $0x1, %al
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1073(%rip), %rdi      # 0x40aa
callq 0x2110
movb $0x1, %al
jmp 0x301c