// File: /workspace/github_cpp_makefile_star2_O1/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)
movq %rsi, %r12
movq %rdi, -0x40(%rbp)
leaq -0x50(%rbp), %rax
movq %rax, -0x10(%rax)
xorl %r15d, %r15d
movq %r15, -0x8(%rax)
movb %r15b, (%rax)
leaq -0xb8(%rbp), %rax
movq %rax, -0x10(%rax)
movq %r15, -0x8(%rax)
movb %r15b, (%rax)
leaq -0x98(%rbp), %rax
movq %rax, -0x10(%rax)
movq %r15, -0x8(%rax)
movb %r15b, (%rax)
leaq -0x70(%rbp), %rax
movq %rax, -0x10(%rax)
movq %r15, -0x8(%rax)
movb %r15b, (%rax)
movl %r15d, -0x30(%rbp)
movl %r15d, -0x2c(%rbp)
leaq 0x2d0c(%rip), %rsi      # 0x73ce
movl $0x1, %edx
movl $0x1, %ecx
movq %r12, %rdi
callq 0x2210
cmpq $-0x1, %rax
je 0x48ec
leaq 0x2ce9(%rip), %rbx      # 0x73ce
movl $0x1, %edx
movl $0x1, %ecx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2210
movslq %eax, %r14
movl $0x1, %ecx
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x21e0
movq %rax, %rbx
shlq $0x20, %rbx
movabsq $-0x200000000, %rcx     # imm = 0xFFFFFFFE00000000
addq %rbx, %rcx
sarq $0x20, %rcx
leaq -0x248(%rbp), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2160
leaq -0xc8(%rbp), %rdi
leaq -0x248(%rbp), %r14
movq %r14, %rsi
callq 0x2190
movq (%r14), %rdi
leaq -0x238(%rbp), %r13
cmpq %r13, %rdi
je 0x476e
movq -0x238(%rbp), %rsi
incq %rsi
callq 0x2140
leaq -0x248(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
movl $0x8, %edx
callq 0x2200
leaq -0x248(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x20c0
leaq -0x248(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
movl -0x228(%rbp,%rax), %r14d
xorl %r15d, %r15d
testb $0x5, %r14b
sete %r15b
movq 0x5802(%rip), %rsi      # 0x9fc0
callq 0x21c0
leaq -0x1d0(%rbp), %rdi
callq 0x2080
sarq $0x20, %rbx
leaq 0x2bf4(%rip), %rsi      # 0x73ce
movl $0x1, %ecx
movq %r12, %rdi
movq %rbx, %rdx
callq 0x2210
cmpq $-0x1, %rax
je 0x48cf
leaq 0x2bd3(%rip), %rsi      # 0x73ce
movl $0x1, %ecx
movq %r12, %rdi
movq %rbx, %rdx
callq 0x2210
movslq %eax, %rbx
leaq 0x2bba(%rip), %rsi      # 0x73cf
movq %r12, %rdi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x21e0
movslq %eax, %rcx
leaq -0x248(%rbp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x2160
leaq -0xa8(%rbp), %rdi
leaq -0x248(%rbp), %rbx
movq %rbx, %rsi
callq 0x2190
movq (%rbx), %rdi
cmpq %r13, %rdi
je 0x4864
movq -0x238(%rbp), %rsi
incq %rsi
callq 0x2140
leaq -0x248(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
movl $0x8, %edx
callq 0x2200
leaq -0x248(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x20c0
andl $0x5, %r14d
leaq -0x1d0(%rbp), %rbx
movq -0x78(%rbx), %rax
movq -0x18(%rax), %rax
xorl %ecx, %ecx
cmpl $0x1, %r14d
adcl $0x1, %ecx
testb $0x5, -0x228(%rbp,%rax)
movq 0x5709(%rip), %rsi      # 0x9fc0
cmovel %ecx, %r15d
leaq -0x248(%rbp), %rdi
callq 0x21c0
movq %rbx, %rdi
callq 0x2080
movq -0x40(%rbp), %rdi
callq 0x66b4
movl -0x30(%rbp), %edi
testl %edi, %edi
jle 0x48f8
xorl %ecx, %ecx
cmpl %eax, %edi
jle 0x4903
testl %r15d, %r15d
jne 0x48fd
jmp 0x4903
movq %rax, -0x88(%rbp)
xorl %r13d, %r13d
jmp 0x4929
testl %r15d, %r15d
je 0x4901
movb $0x1, %cl
jmp 0x4903
xorl %ecx, %ecx
movl -0x2c(%rbp), %edx
movq %rax, -0x88(%rbp)
cmpl %eax, %edx
setg %sil
orb %cl, %sil
cmpl %edi, %edx
setl %al
cmpl $0x2, %r15d
sete %r13b
andb %al, %r13b
orb %sil, %r13b
leaq 0x2ad5(%rip), %rsi      # 0x7405
xorl %r14d, %r14d
movl $0x1, %ecx
movq %r12, %rdi
xorl %edx, %edx
callq 0x21e0
cmpq $-0x1, %rax
je 0x49d2
leaq 0x2ab2(%rip), %rbx      # 0x7405
movl $0x1, %ecx
movq %r12, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x21e0
incl %eax
movslq %eax, %r14
movl $0x1, %ecx
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x21e0
subl %r14d, %eax
movslq %eax, %rcx
leaq -0x248(%rbp), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2160
leaq -0x80(%rbp), %rdi
leaq -0x248(%rbp), %rbx
movq %rbx, %rsi
callq 0x2190
movq (%rbx), %rdi
leaq -0x238(%rbp), %rax
cmpq %rax, %rdi
je 0x49c6
movq -0x238(%rbp), %rsi
incq %rsi
callq 0x2140
testb %r13b, %r13b
je 0x4a5d
xorl %r14d, %r14d
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x49eb
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x4a0d
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0x4a2f
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x4a48
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2140
movl %r14d, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x2, %r15d
je 0x4ad6
cmpl $0x1, %r15d
jne 0x4bad
movl -0x30(%rbp), %edx
leaq -0x60(%rbp), %rsi
movq -0x40(%rbp), %rdi
callq 0x6a84
movb $0x1, %r14b
testb %al, %al
je 0x49d2
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rcx
leaq -0x60(%rbp), %rdi
xorl %edx, %edx
callq 0x2250
cmpq $-0x1, %rax
je 0x4d36
cmpb $0x0, -0x34(%rbp)
je 0x4d69
movq 0x5512(%rip), %rdi      # 0x9fc8
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x2, 0x10(%rdi,%rax)
movl -0x30(%rbp), %esi
callq 0x2240
movq %rax, %rdi
jmp 0x4d70
movl -0x30(%rbp), %r12d
movb $0x1, %r14b
cmpl -0x2c(%rbp), %r12d
jg 0x49d2
leaq -0x60(%rbp), %rbx
movq 0x54d6(%rip), %r14      # 0x9fc8
leaq 0x28d5(%rip), %r13      # 0x73ce
movq -0x40(%rbp), %rdi
movq %rbx, %rsi
movl %r12d, %edx
callq 0x6a84
testb %al, %al
je 0x4ce0
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rcx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x2250
cmpq $-0x1, %rax
je 0x4b96
movq %r14, %rax
cmpb $0x0, -0x34(%rbp)
je 0x4b4c
movq (%r14), %rax
movq -0x18(%rax), %rax
movq $0x2, 0x10(%r14,%rax)
movq %r14, %rdi
movl %r12d, %esi
callq 0x2240
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2180
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq %r14, %rdi
callq 0x2180
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20d0
leal 0x1(%r12), %eax
cmpl -0x2c(%rbp), %r12d
movl %eax, %r12d
jl 0x4af9
jmp 0x4c98
movq -0x40(%rbp), %rdi
callq 0x66b4
movq -0x88(%rbp), %r13
movl %eax, -0x2c(%rbp)
movb $0x1, %r14b
testl %r13d, %r13d
jle 0x49d2
negl %r13d
movl $0x1, %r12d
leaq -0x60(%rbp), %rbx
movq 0x53e8(%rip), %r14      # 0x9fc8
movq -0x40(%rbp), %rdi
movq %rbx, %rsi
movl %r12d, %edx
callq 0x6a84
testb %al, %al
je 0x4ca0
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rcx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x2250
cmpq $-0x1, %rax
je 0x4c81
movq %r14, %rax
cmpb $0x0, -0x34(%rbp)
je 0x4c33
movq (%r14), %rax
movq -0x18(%rax), %rax
movq $0x2, 0x10(%r14,%rax)
movq %r14, %rdi
movl %r12d, %esi
callq 0x2240
movl $0x1, %edx
movq %rax, %rdi
leaq 0x278c(%rip), %rsi      # 0x73ce
callq 0x2180
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq %r14, %rdi
callq 0x2180
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20d0
leal (%r12,%r13), %eax
incl %eax
movl %r12d, %ecx
incl %ecx
movl %ecx, %r12d
cmpl $0x1, %eax
jne 0x4be0
movb $0x1, %r14b
jmp 0x49d2
movq 0x5321(%rip), %rdi      # 0x9fc8
leaq 0x2759(%rip), %rsi      # 0x7407
movl $0x8, %edx
callq 0x2180
movq 0x5309(%rip), %rdi      # 0x9fc8
movl %r12d, %esi
callq 0x2240
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2130
jmp 0x4d1e
movq 0x52e1(%rip), %rdi      # 0x9fc8
leaq 0x2719(%rip), %rsi      # 0x7407
movl $0x8, %edx
callq 0x2180
movq 0x52c9(%rip), %rdi      # 0x9fc8
movl %r12d, %esi
callq 0x2240
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20d0
jmp 0x49cf
movq 0x528b(%rip), %rdi      # 0x9fc8
leaq 0x26c3(%rip), %rsi      # 0x7407
movl $0x8, %edx
callq 0x2180
movq 0x5273(%rip), %rdi      # 0x9fc8
xorl %esi, %esi
callq 0x2240
movq %rax, %rdi
callq 0x20a0
jmp 0x49cf
movq 0x5258(%rip), %rdi      # 0x9fc8
leaq 0x2657(%rip), %rsi      # 0x73ce
movl $0x1, %edx
callq 0x2180
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq 0x5238(%rip), %rdi      # 0x9fc8
callq 0x2180
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20d0
jmp 0x49d2
jmp 0x4dcc
jmp 0x4df8
jmp 0x4df8
jmp 0x4df8
movq %rax, %rbx
movq 0x51ea(%rip), %rsi      # 0x9fc0
leaq -0x248(%rbp), %rdi
callq 0x21c0
leaq -0x1d0(%rbp), %rdi
callq 0x2080
jmp 0x4dfb
jmp 0x4df8
jmp 0x4df8
jmp 0x4df8
jmp 0x4df8
movq %rax, %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x4e14
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x4e36
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0x4e58
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x4e71
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2140
movq %rbx, %rdi
callq 0x2260