// File: 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
subq $0x2e0, %rsp            # imm = 0x2E0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movb $0x0, -0x15(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x44f7
movb -0x15(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x41db
jmp 0x44f7
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1ea3(%rip), %rsi      # 0x6091
callq 0x21e0
cmpl $0x0, %eax
jne 0x42c2
movb $0x1, -0x15(%rbp)
movl $0x1, %edi
callq 0x2220
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x20e0
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %edi
movl $0x1, %esi
callq 0x2130
movl %eax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4274
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movslq -0x2c(%rbp), %rax
movq %rcx, -0xd0(%rbp,%rax,8)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x4249
movslq -0x2c(%rbp), %rax
movq $0x0, -0xd0(%rbp,%rax,8)
leaq -0xd0(%rbp), %rdi
callq 0x2490
andb $0x1, %al
movb %al, -0xd1(%rbp)
testb $0x1, -0xd1(%rbp)
jne 0x42ad
leaq -0xd0(%rbp), %rdi
callq 0x27c0
movl -0x28(%rbp), %edi
callq 0x2290
movl -0x20(%rbp), %edi
movl $0x1, %esi
callq 0x2130
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1dbe(%rip), %rsi      # 0x6093
callq 0x21e0
cmpl $0x0, %eax
jne 0x43f1
movb $0x1, -0x15(%rbp)
xorl %edi, %edi
callq 0x2220
movl %eax, -0xd8(%rbp)
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x20e0
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
jge 0x4336
leaq 0x1d71(%rip), %rdi      # 0x6095
callq 0x2110
movb -0x15(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
jmp 0x44ff
movl -0xdc(%rbp), %edi
xorl %esi, %esi
callq 0x2130
movl %eax, -0xe0(%rbp)
movl $0x0, -0x184(%rbp)
movl -0x184(%rbp), %eax
movl -0x14(%rbp), %ecx
subl -0x1c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x439d
movq -0x10(%rbp), %rax
movl -0x184(%rbp), %ecx
addl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
movslq -0x184(%rbp), %rax
movq %rcx, -0x180(%rbp,%rax,8)
movl -0x184(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x184(%rbp)
jmp 0x4353
movslq -0x184(%rbp), %rax
movq $0x0, -0x180(%rbp,%rax,8)
leaq -0x180(%rbp), %rdi
callq 0x2490
andb $0x1, %al
movb %al, -0x185(%rbp)
testb $0x1, -0x185(%rbp)
jne 0x43d9
leaq -0x180(%rbp), %rdi
callq 0x27c0
movl -0xe0(%rbp), %edi
callq 0x2290
movl -0xd8(%rbp), %edi
xorl %esi, %esi
callq 0x2130
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1c9d(%rip), %rsi      # 0x60a1
callq 0x21e0
cmpl $0x0, %eax
jne 0x44e7
movb $0x1, -0x15(%rbp)
movl $0x0, -0x2d4(%rbp)
movl -0x2d4(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x445a
movq -0x10(%rbp), %rax
movslq -0x2d4(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movslq -0x2d4(%rbp), %rax
movq %rcx, -0x230(%rbp,%rax,8)
movl -0x2d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2d4(%rbp)
jmp 0x4420
movslq -0x2d4(%rbp), %rax
movq $0x0, -0x230(%rbp,%rax,8)
movl $0x0, -0x2d4(%rbp)
movl -0x2d4(%rbp), %eax
movl -0x14(%rbp), %ecx
subl -0x1c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x44c1
movq -0x10(%rbp), %rax
movl -0x2d4(%rbp), %ecx
addl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rcx
movslq -0x2d4(%rbp), %rax
movq %rcx, -0x2d0(%rbp,%rax,8)
movl -0x2d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2d4(%rbp)
jmp 0x4477
movslq -0x2d4(%rbp), %rax
movq $0x0, -0x2d0(%rbp,%rax,8)
leaq -0x230(%rbp), %rdi
leaq -0x2d0(%rbp), %rsi
callq 0x4510
jmp 0x44e9
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x41bd
movb -0x15(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x2e0, %rsp            # imm = 0x2E0
popq %rbp
retq
nopl (%rax)