// File: brandoncanaday[P]1730sh/1730sh.cpp
// Function: set_redirects(Input*, int&, int&, int&)
int set_redirects(Input * job, int& fdSTDIN, int& fdSTDOUT, int& fdSTDERR) {
  fdSTDIN = STDIN_FILENO;
  fdSTDOUT = STDOUT_FILENO;
  fdSTDERR = STDERR_FILENO;
  // if user input specified any i/o redirection, open/create the given fd's
  if(job->getSTDIN_fd() != "STDIN_FILENO") {
    if((fdSTDIN = open(job->getSTDIN_fd().c_str(), O_RDONLY)) == -1) {
      cout << "1730sh: " << job->getSTDIN_fd() << ": No such file or directory" << endl;
      return -1;
    } // if
  } // if
  if(job->getSTDOUT_fd() != "STDOUT_FILENO") {
    if(job->getSTDOUT_type() == ">") {
      if((fdSTDOUT = open(job->getSTDOUT_fd().c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644)) == -1) {
	cout << "1730sh: `" << job->getSTDOUT_fd() << "' cannot be opened" << endl;
	return -1;
      } // if
    } else {
      if((fdSTDOUT = open(job->getSTDOUT_fd().c_str(), O_CREAT | O_WRONLY | O_APPEND, 0666)) == -1) {
	cout << "1730sh: `" << job->getSTDOUT_fd() << "' cannot be opened" << endl;
	return -1;
      } // if
    } // if/else
  } // if
  if(job->getSTDERR_fd() != "STDERR_FILENO") {
    if(job->getSTDERR_type() == "e>") {
      if((fdSTDERR = open(job->getSTDERR_fd().c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644)) == -1) {
	cout << "1730sh: `" << job->getSTDERR_fd() << "' cannot be opened" << endl;
	return -1;
      } // if
    } else {
      if((fdSTDERR = open(job->getSTDERR_fd().c_str(), O_CREAT | O_WRONLY | O_APPEND, 0666)) == -1) {
	cout << "1730sh: `" << job->getSTDERR_fd() << "' cannot be opened" << endl;
	return -1;
      } // if
    } // if/else
  } // if
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x360, %rsp            # imm = 0x360
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl $0x0, (%rax)
movq -0x20(%rbp), %rax
movl $0x1, (%rax)
movq -0x28(%rbp), %rax
movl $0x2, (%rax)
movq -0x10(%rbp), %rsi
leaq -0x48(%rbp), %rdi
movq %rdi, -0x228(%rbp)
callq 0xb980
movq -0x228(%rbp), %rdi
leaq 0xda2e(%rip), %rsi      # 0x14089
callq 0xb950
movb %al, -0x219(%rbp)
jmp 0x6668
leaq -0x48(%rbp), %rdi
callq 0x31e0
movb -0x219(%rbp), %al
testb $0x1, %al
jne 0x6680
jmp 0x67c2
movq -0x10(%rbp), %rsi
leaq -0x78(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0xb980
movq -0x238(%rbp), %rdi
callq 0x3130
movq %rax, %rdi
xorl %esi, %esi
movb %sil, %al
callq 0x3220
movl %eax, -0x22c(%rbp)
jmp 0x66b5
movl -0x22c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
leaq -0x78(%rbp), %rdi
callq 0x31e0
movl -0x22c(%rbp), %eax
cmpl $-0x1, %eax
jne 0x67c0
movq 0x158e8(%rip), %rdi     # 0x1bfc8
leaq 0xd97f(%rip), %rsi      # 0x14066
callq 0x32e0
movq %rax, -0x250(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x98(%rbp), %rdi
movq %rdi, -0x248(%rbp)
callq 0xb980
movq -0x250(%rbp), %rdi
movq -0x248(%rbp), %rsi
callq 0x32a0
movq %rax, -0x240(%rbp)
jmp 0x6726
movq -0x240(%rbp), %rdi
leaq 0xd962(%rip), %rsi      # 0x14096
callq 0x32e0
movq %rax, -0x258(%rbp)
jmp 0x6742
movq -0x258(%rbp), %rdi
movq 0x15858(%rip), %rsi     # 0x1bfa8
callq 0x3330
jmp 0x6757
leaq -0x98(%rbp), %rdi
callq 0x31e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6e52
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
jmp 0x67c2
movq -0x10(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x268(%rbp)
callq 0xb9b0
movq -0x268(%rbp), %rdi
leaq 0xd8cb(%rip), %rsi      # 0x140b2
callq 0xb950
movb %al, -0x259(%rbp)
jmp 0x67f4
leaq -0xb8(%rbp), %rdi
callq 0x31e0
movb -0x259(%rbp), %al
testb $0x1, %al
jne 0x680f
jmp 0x6b0b
movq -0x10(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
movq %rdi, -0x278(%rbp)
callq 0xb9e0
movq -0x278(%rbp), %rdi
leaq 0xd830(%rip), %rsi      # 0x14064
callq 0xb390
movb %al, -0x269(%rbp)
jmp 0x6841
leaq -0xd8(%rbp), %rdi
callq 0x31e0
movb -0x269(%rbp), %al
testb $0x1, %al
jne 0x685c
jmp 0x69d1
movq -0x10(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
movq %rdi, -0x288(%rbp)
callq 0xb9b0
movq -0x288(%rbp), %rdi
callq 0x3130
movq %rax, %rdi
xorl %eax, %eax
movl $0x241, %esi            # imm = 0x241
movl $0x1a4, %edx            # imm = 0x1A4
callq 0x3220
movl %eax, -0x27c(%rbp)
jmp 0x689b
movl -0x27c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
leaq -0xf8(%rbp), %rdi
callq 0x31e0
movl -0x27c(%rbp), %eax
cmpl $-0x1, %eax
jne 0x69cc
movq 0x156ff(%rip), %rdi     # 0x1bfc8
leaq 0xd7f0(%rip), %rsi      # 0x140c0
callq 0x32e0
movq %rax, -0x2a0(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x118(%rbp), %rdi
movq %rdi, -0x298(%rbp)
callq 0xb9b0
movq -0x2a0(%rbp), %rdi
movq -0x298(%rbp), %rsi
callq 0x32a0
movq %rax, -0x290(%rbp)
jmp 0x690f
movq -0x290(%rbp), %rdi
leaq 0xd7ad(%rip), %rsi      # 0x140ca
callq 0x32e0
movq %rax, -0x2a8(%rbp)
jmp 0x692b
movq -0x2a8(%rbp), %rdi
movq 0x1566f(%rip), %rsi     # 0x1bfa8
callq 0x3330
jmp 0x6940
leaq -0x118(%rbp), %rdi
callq 0x31e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6e52
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
jmp 0x6b09
movq -0x10(%rbp), %rsi
leaq -0x138(%rbp), %rdi
movq %rdi, -0x2b8(%rbp)
callq 0xb9b0
movq -0x2b8(%rbp), %rdi
callq 0x3130
movq %rax, %rdi
xorl %eax, %eax
movl $0x441, %esi            # imm = 0x441
movl $0x1b6, %edx            # imm = 0x1B6
callq 0x3220
movl %eax, -0x2ac(%rbp)
jmp 0x6a10
movl -0x2ac(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
leaq -0x138(%rbp), %rdi
callq 0x31e0
movl -0x2ac(%rbp), %eax
cmpl $-0x1, %eax
jne 0x6b07
movq 0x1558a(%rip), %rdi     # 0x1bfc8
leaq 0xd67b(%rip), %rsi      # 0x140c0
callq 0x32e0
movq %rax, -0x2d0(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x158(%rbp), %rdi
movq %rdi, -0x2c8(%rbp)
callq 0xb9b0
movq -0x2d0(%rbp), %rdi
movq -0x2c8(%rbp), %rsi
callq 0x32a0
movq %rax, -0x2c0(%rbp)
jmp 0x6a84
movq -0x2c0(%rbp), %rdi
leaq 0xd638(%rip), %rsi      # 0x140ca
callq 0x32e0
movq %rax, -0x2d8(%rbp)
jmp 0x6aa0
movq -0x2d8(%rbp), %rdi
movq 0x154fa(%rip), %rsi     # 0x1bfa8
callq 0x3330
jmp 0x6ab5
leaq -0x158(%rbp), %rdi
callq 0x31e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6e52
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x158(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
jmp 0x6b09
jmp 0x6b0b
movq -0x10(%rbp), %rsi
leaq -0x178(%rbp), %rdi
movq %rdi, -0x2e8(%rbp)
callq 0xba20
movq -0x2e8(%rbp), %rdi
leaq 0xd5ad(%rip), %rsi      # 0x140dd
callq 0xb950
movb %al, -0x2d9(%rbp)
jmp 0x6b3d
leaq -0x178(%rbp), %rdi
callq 0x31e0
movb -0x2d9(%rbp), %al
testb $0x1, %al
jne 0x6b58
jmp 0x6e4b
movq -0x10(%rbp), %rsi
leaq -0x198(%rbp), %rdi
movq %rdi, -0x2f8(%rbp)
callq 0xba60
movq -0x2f8(%rbp), %rdi
leaq 0xd4e2(%rip), %rsi      # 0x1405f
callq 0xb390
movb %al, -0x2e9(%rbp)
jmp 0x6b8a
leaq -0x198(%rbp), %rdi
callq 0x31e0
movb -0x2e9(%rbp), %al
testb $0x1, %al
jne 0x6ba5
jmp 0x6d1a
movq -0x10(%rbp), %rsi
leaq -0x1b8(%rbp), %rdi
movq %rdi, -0x308(%rbp)
callq 0xba20
movq -0x308(%rbp), %rdi
callq 0x3130
movq %rax, %rdi
xorl %eax, %eax
movl $0x241, %esi            # imm = 0x241
movl $0x1a4, %edx            # imm = 0x1A4
callq 0x3220
movl %eax, -0x2fc(%rbp)
jmp 0x6be4
movl -0x2fc(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
leaq -0x1b8(%rbp), %rdi
callq 0x31e0
movl -0x2fc(%rbp), %eax
cmpl $-0x1, %eax
jne 0x6d15
movq 0x153b6(%rip), %rdi     # 0x1bfc8
leaq 0xd4a7(%rip), %rsi      # 0x140c0
callq 0x32e0
movq %rax, -0x320(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x1d8(%rbp), %rdi
movq %rdi, -0x318(%rbp)
callq 0xba20
movq -0x320(%rbp), %rdi
movq -0x318(%rbp), %rsi
callq 0x32a0
movq %rax, -0x310(%rbp)
jmp 0x6c58
movq -0x310(%rbp), %rdi
leaq 0xd464(%rip), %rsi      # 0x140ca
callq 0x32e0
movq %rax, -0x328(%rbp)
jmp 0x6c74
movq -0x328(%rbp), %rdi
movq 0x15326(%rip), %rsi     # 0x1bfa8
callq 0x3330
jmp 0x6c89
leaq -0x1d8(%rbp), %rdi
callq 0x31e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6e52
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x1b8(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
jmp 0x6e49
movq -0x10(%rbp), %rsi
leaq -0x1f8(%rbp), %rdi
movq %rdi, -0x338(%rbp)
callq 0xba20
movq -0x338(%rbp), %rdi
callq 0x3130
movq %rax, %rdi
xorl %eax, %eax
movl $0x441, %esi            # imm = 0x441
movl $0x1b6, %edx            # imm = 0x1B6
callq 0x3220
movl %eax, -0x32c(%rbp)
jmp 0x6d59
movl -0x32c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
leaq -0x1f8(%rbp), %rdi
callq 0x31e0
movl -0x32c(%rbp), %eax
cmpl $-0x1, %eax
jne 0x6e47
movq 0x15241(%rip), %rdi     # 0x1bfc8
leaq 0xd332(%rip), %rsi      # 0x140c0
callq 0x32e0
movq %rax, -0x350(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x218(%rbp), %rdi
movq %rdi, -0x348(%rbp)
callq 0xba20
movq -0x350(%rbp), %rdi
movq -0x348(%rbp), %rsi
callq 0x32a0
movq %rax, -0x340(%rbp)
jmp 0x6dcd
movq -0x340(%rbp), %rdi
leaq 0xd2ef(%rip), %rsi      # 0x140ca
callq 0x32e0
movq %rax, -0x358(%rbp)
jmp 0x6de9
movq -0x358(%rbp), %rdi
movq 0x151b1(%rip), %rsi     # 0x1bfa8
callq 0x3330
jmp 0x6dfe
leaq -0x218(%rbp), %rdi
callq 0x31e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6e52
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x1f8(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x31e0
jmp 0x6e5e
jmp 0x6e49
jmp 0x6e4b
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x360, %rsp            # imm = 0x360
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x3580
nopw (%rax,%rax)