// File: /workspace/github_cpp_makefile_star2_O2/craigbarstow[P]cmd-line-text-editor/text_editor.cc
// Function: find_and_type(text_ed&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool)
bool find_and_type(text_ed & T, string cl, bool toggle) { //search area from first given line to second for a given word. Type all lines where word is found.
  bool error_state=false;
  string aline,m_string,n_string, word;
  int i = 0,m=0,n=0,p=1,m_begin, m_end, n_begin, n_end,document_length, word_start, word_end,count=0; 
  if (cl.find_first_not_of(" ",1)!=string::npos){ //section to isolate substring representing the first integer from the string
    m_begin= cl.find_first_not_of(" ",1);
    m_end = cl.find_first_of(" ", m_begin)-2;
    m_string=cl.substr(m_begin,m_end);
    if (istringstream(m_string) >> m) { //if substring can be converted to an integer, increment count to show the first integer is present
      count++;}
    if (cl.find_first_not_of(" ", m_end+2)!=string::npos){ //section to isolate substring representing the second integer from the string
      n_begin = cl.find_first_not_of(" ", m_end+2);
      n_end = cl.find_first_of("",n_begin);
      n_string=cl.substr(n_begin,n_end);
      if (istringstream(n_string) >> n){ //if substring can be converted to an integer, increment count to show the second integer is present
	count++;
      }
    }
    //SECTION TO MAKE SURE THE INTEGER VALUES MAKE SENSE
    document_length = T.get_last_line();
    if ((m<=0 || m>document_length) && count!=0){ //if first integer is less than the starting line number toggle error state on
      error_state=true;
    }
    if (n>document_length) { //if ending integer value is greater than the document length, toggle error state on
      error_state=true;
    }
    if (n<m && count==2){ //if the second integer is greater than the first, toggle error state on
      error_state=true;
    }
  }
  //section to isolate the substring representing the word to be searched for from the string
  if (cl.find_first_of("/")!=string::npos){ 
    word_start = cl.find_first_of("/")+1;
    word_end = cl.find_first_of("/",word_start);
    word = cl.substr(word_start,word_end-word_start); //select the correct substring from the string
  } else { //if word can't be found, return false, as there is no point in continuing
    return false;
  }

  if (!error_state) {
    switch (count) {
    case 0: //if no integers are present, search the whole document for the word
      n = T.get_last_line();
      for (i=1; i<=document_length; i++){
	if (T.get_line(aline,i)) {
	  if (aline.find(word)!=string::npos){
	    if (toggle) {  
	      cout << setw(2) << i << " ";
	    } else {
	      cout << " ";
	    }
	    cout << aline << endl;
	  }
	} else {
	  cout << "No line " << i << endl;
	  return false;
	}
      }
      break;
    case 1: //if 1 integer is present, search just that line for the given word
      if (T.get_line(aline,m)) {
	if (aline.find(word)!=string::npos){
	  if (toggle) {  
	    cout << setw(2) << m << " ";
	  } else {
	    cout << " ";
	  }
	  cout << aline << endl;
	} else {
	  cout << "No line " << i << endl;
	  return false;
	}
      }
      break;
    case 2: //if two integers are present, search from the starting int to the ending int for the word
      for (i = m; i<=n; i++){
	if (T.get_line(aline,i)) {
	  if (aline.find(word)!=string::npos){
	    if (toggle) {  
	      cout << setw(2) << i << " ";
	    } else {
	      cout << " ";
	    }
	    cout << aline << endl;
	  }
	} else {
	  cout << "No line " << i << endl;
	  return false;
	}
      }
    }
    return true; //return true if !error state
  } 
  return false; //return true if error state
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movl %edx, -0x34(%rbp)
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rsi, %r15
movq %rdi, -0x40(%rbp)
xorl %r14d, %r14d
leaq -0xb8(%rbp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb %r14b, (%rax)
movb %r14b, (%rcx)
leaq -0x98(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq -0x78(%rbp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb %r14b, (%rax)
andl $0x0, -0x2c(%rbp)
movb %r14b, (%rcx)
andl $0x0, -0x30(%rbp)
leaq 0x2876(%rip), %rsi      # 0x63ce
pushq $0x1
popq %rdx
movq %r15, %rdi
callq 0x2120
cmpq $-0x1, %rax
je 0x3d0e
leaq 0x285a(%rip), %rbx      # 0x63ce
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2120
movslq %eax, %r12
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2100
movq %rax, %rbx
shlq $0x20, %rbx
movabsq $-0x200000000, %rcx     # imm = 0xFFFFFFFE00000000
addq %rbx, %rcx
sarq $0x20, %rcx
leaq -0x248(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21a0
leaq -0xc8(%rbp), %r12
leaq -0x248(%rbp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x21c0
movq %r13, %rdi
callq 0x20d0
leaq -0x248(%rbp), %rdi
pushq $0x8
popq %rdx
movq %r12, %rsi
callq 0x2220
leaq -0x248(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x2130
leaq -0x248(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
movl -0x228(%rbp,%rax), %r13d
xorl %r14d, %r14d
testb $0x5, %r13b
sete %r14b
callq 0x20e0
sarq $0x20, %rbx
leaq 0x279d(%rip), %rsi      # 0x63ce
movq %r15, %rdi
movq %rbx, %rdx
callq 0x2120
cmpq $-0x1, %rax
je 0x3cf1
leaq 0x2781(%rip), %rsi      # 0x63ce
movq %r15, %rdi
movq %rbx, %rdx
callq 0x2120
movslq %eax, %rbx
leaq 0x276d(%rip), %rsi      # 0x63cf
movq %r15, %rdi
movq %rbx, %rdx
callq 0x2100
movslq %eax, %rcx
leaq -0x248(%rbp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x21a0
leaq -0xa8(%rbp), %rbx
leaq -0x248(%rbp), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x21c0
movq %r12, %rdi
callq 0x20d0
leaq -0x248(%rbp), %rdi
pushq $0x8
popq %rdx
movq %rbx, %rsi
callq 0x2220
leaq -0x248(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x2130
andl $0x5, %r13d
leaq -0x248(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
xorl %ecx, %ecx
cmpl $0x1, %r13d
adcl $0x1, %ecx
testb $0x5, -0x228(%rbp,%rax)
cmovel %ecx, %r14d
callq 0x20e0
movq -0x40(%rbp), %rdi
callq 0x5182
movl -0x2c(%rbp), %edi
testl %edi, %edi
jle 0x3d17
xorl %ecx, %ecx
cmpl %eax, %edi
jle 0x3d22
testl %r14d, %r14d
jne 0x3d1c
jmp 0x3d22
movq %rax, -0x48(%rbp)
xorl %r12d, %r12d
jmp 0x3d45
testl %r14d, %r14d
je 0x3d20
movb $0x1, %cl
jmp 0x3d22
xorl %ecx, %ecx
movl -0x30(%rbp), %edx
movq %rax, -0x48(%rbp)
cmpl %eax, %edx
setg %sil
orb %cl, %sil
cmpl %edi, %edx
setl %al
cmpl $0x2, %r14d
sete %r12b
andb %al, %r12b
orb %sil, %r12b
leaq 0x26b9(%rip), %rsi      # 0x6405
xorl %r13d, %r13d
movq %r15, %rdi
xorl %edx, %edx
callq 0x2100
cmpq $-0x1, %rax
je 0x3fe1
leaq 0x269b(%rip), %rbx      # 0x6405
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2100
incl %eax
movslq %eax, %r13
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x2100
subl %r13d, %eax
movslq %eax, %rcx
leaq -0x248(%rbp), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x21a0
leaq -0x88(%rbp), %rdi
leaq -0x248(%rbp), %rbx
movq %rbx, %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x20d0
testb %r12b, %r12b
jne 0x3fde
cmpl $0x2, %r14d
je 0x3e44
cmpl $0x1, %r14d
jne 0x3edd
movl -0x2c(%rbp), %edx
leaq -0x68(%rbp), %rsi
movq -0x40(%rbp), %rdi
callq 0x5436
movb $0x1, %r13b
testb %al, %al
je 0x3fe1
xorl %r15d, %r15d
leaq -0x68(%rbp), %rdi
leaq -0x88(%rbp), %rsi
xorl %edx, %edx
callq 0x22a0
cmpq $-0x1, %rax
je 0x3fb8
cmpb $0x0, -0x34(%rbp)
je 0x4023
movq 0x51a4(%rip), %rdi      # 0x8fc8
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x2, 0x10(%rdi,%rax)
movl -0x2c(%rbp), %esi
callq 0x2250
movq %rax, %rdi
jmp 0x402a
movl -0x2c(%rbp), %r15d
leaq -0x68(%rbp), %rbx
leaq -0x88(%rbp), %r12
movq 0x516e(%rip), %r13      # 0x8fc8
movq -0x40(%rbp), %r14
cmpl -0x30(%rbp), %r15d
jg 0x3f89
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x5436
testb %al, %al
je 0x3f8e
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x22a0
cmpq $-0x1, %rax
je 0x3ed8
movq %r13, %rax
cmpb $0x0, -0x34(%rbp)
je 0x3eb6
movq (%r13), %rax
movq -0x18(%rax), %rax
movq $0x2, 0x10(%r13,%rax)
movq %r13, %rdi
movl %r15d, %esi
callq 0x2250
movq %rax, %rdi
leaq 0x250e(%rip), %rsi      # 0x63ce
callq 0x2190
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2170
movq %rax, %rdi
callq 0x20f0
incl %r15d
jmp 0x3e5e
movq -0x40(%rbp), %rdi
callq 0x5182
movq -0x48(%rbp), %r12
movl %eax, -0x30(%rbp)
xorl %eax, %eax
testl %r12d, %r12d
cmovlel %eax, %r12d
negl %r12d
pushq $0x1
popq %r15
leaq -0x68(%rbp), %r13
movq 0x50c0(%rip), %rbx      # 0x8fc8
movq -0x40(%rbp), %r14
leal (%r12,%r15), %eax
cmpl $0x1, %eax
je 0x3f89
movq %r14, %rdi
movq %r13, %rsi
movl %r15d, %edx
callq 0x5436
testb %al, %al
je 0x3fa3
movq %r13, %rdi
leaq -0x88(%rbp), %rsi
xorl %edx, %edx
callq 0x22a0
cmpq $-0x1, %rax
je 0x3f84
movq %rbx, %rax
cmpb $0x0, -0x34(%rbp)
je 0x3f62
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x2, 0x10(%rbx,%rax)
movq %rbx, %rdi
movl %r15d, %esi
callq 0x2250
movq %rax, %rdi
leaq 0x2462(%rip), %rsi      # 0x63ce
callq 0x2190
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2170
movq %rax, %rdi
callq 0x20f0
incl %r15d
jmp 0x3f0c
movb $0x1, %r13b
jmp 0x3fe1
movq 0x5033(%rip), %rdi      # 0x8fc8
leaq 0x246b(%rip), %rsi      # 0x6407
callq 0x2190
jmp 0x3fcb
movq 0x501e(%rip), %rdi      # 0x8fc8
leaq 0x2456(%rip), %rsi      # 0x6407
callq 0x2190
jmp 0x3fcb
movq 0x5009(%rip), %rdi      # 0x8fc8
leaq 0x2441(%rip), %rsi      # 0x6407
callq 0x2190
movq %rax, %rdi
movl %r15d, %esi
callq 0x2250
movq %rax, %rdi
callq 0x20f0
xorl %r13d, %r13d
leaq -0x88(%rbp), %rdi
callq 0x20d0
leaq -0xa8(%rbp), %rdi
callq 0x20d0
leaq -0xc8(%rbp), %rdi
callq 0x20d0
leaq -0x68(%rbp), %rdi
callq 0x20d0
movl %r13d, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4f9e(%rip), %rdi      # 0x8fc8
leaq 0x239d(%rip), %rsi      # 0x63ce
callq 0x2190
movq 0x4f8b(%rip), %rdi      # 0x8fc8
leaq -0x68(%rbp), %rsi
callq 0x2170
movq %rax, %rdi
callq 0x20f0
jmp 0x3fe1
jmp 0x4058
jmp 0x4071
jmp 0x4071
jmp 0x4071
movq %rax, %rbx
leaq -0x248(%rbp), %rdi
callq 0x20e0
jmp 0x4074
jmp 0x4071
jmp 0x4071
jmp 0x4071
jmp 0x4071
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x20d0
leaq -0xa8(%rbp), %rdi
callq 0x20d0
leaq -0xc8(%rbp), %rdi
callq 0x20d0
leaq -0x68(%rbp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x2260