// File: 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
subq $0x540, %rsp            # imm = 0x540
movq %rsi, -0x458(%rbp)
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movb $0x0, -0x1a(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x32d0
leaq -0x60(%rbp), %rdi
callq 0x32d0
leaq -0x80(%rbp), %rdi
callq 0x32d0
leaq -0xa0(%rbp), %rdi
callq 0x32d0
movq -0x458(%rbp), %rdi
movl $0x0, -0xa4(%rbp)
movl $0x0, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
movl $0x1, -0xb0(%rbp)
movl $0x0, -0xd0(%rbp)
leaq 0x3952(%rip), %rsi      # 0x93ce
movl $0x1, %edx
callq 0x3140
cmpq $-0x1, %rax
je 0x5da9
movq -0x458(%rbp), %rdi
leaq 0x3930(%rip), %rsi      # 0x93ce
movq %rsi, -0x460(%rbp)
movl $0x1, %edx
callq 0x3140
movq -0x460(%rbp), %rsi
movq -0x458(%rbp), %rdi
movl %eax, -0xb4(%rbp)
movslq -0xb4(%rbp), %rdx
callq 0x3130
movq -0x458(%rbp), %rsi
addl $-0x2, %eax
movl %eax, -0xb8(%rbp)
movslq -0xb4(%rbp), %rdx
movslq -0xb8(%rbp), %rcx
leaq -0xf0(%rbp), %rdi
callq 0x3200
jmp 0x5afb
leaq -0x60(%rbp), %rdi
movq %rdi, -0x468(%rbp)
leaq -0xf0(%rbp), %rsi
movq %rsi, -0x470(%rbp)
callq 0x3280
movq -0x470(%rbp), %rdi
callq 0x3160
movq -0x468(%rbp), %rsi
leaq -0x280(%rbp), %rdi
movl $0x8, %edx
callq 0x3340
jmp 0x5b3f
leaq -0x280(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x7690
movq %rax, -0x478(%rbp)
jmp 0x5b5b
movq -0x478(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x32a0
movb %al, -0x479(%rbp)
jmp 0x5b79
leaq -0x280(%rbp), %rdi
callq 0x3120
movb -0x479(%rbp), %al
testb $0x1, %al
jne 0x5b91
jmp 0x5bdc
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd0(%rbp)
jmp 0x5bdc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
jmp 0x63b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
leaq -0x280(%rbp), %rdi
callq 0x3120
jmp 0x63b0
movq -0x458(%rbp), %rdi
movl -0xb8(%rbp), %eax
addl $0x2, %eax
movslq %eax, %rdx
leaq 0x37d8(%rip), %rsi      # 0x93ce
callq 0x3140
cmpq $-0x1, %rax
je 0x5d39
movq -0x458(%rbp), %rdi
movl -0xb8(%rbp), %eax
addl $0x2, %eax
movslq %eax, %rdx
leaq 0x37af(%rip), %rsi      # 0x93ce
callq 0x3140
movq -0x458(%rbp), %rdi
movl %eax, -0xbc(%rbp)
movslq -0xbc(%rbp), %rdx
leaq 0x3790(%rip), %rsi      # 0x93cf
callq 0x3130
movq -0x458(%rbp), %rsi
movl %eax, -0xc0(%rbp)
movslq -0xbc(%rbp), %rdx
movslq -0xc0(%rbp), %rcx
leaq -0x2a0(%rbp), %rdi
callq 0x3200
jmp 0x5c6d
leaq -0x80(%rbp), %rdi
movq %rdi, -0x488(%rbp)
leaq -0x2a0(%rbp), %rsi
movq %rsi, -0x490(%rbp)
callq 0x3280
movq -0x490(%rbp), %rdi
callq 0x3160
movq -0x488(%rbp), %rsi
leaq -0x420(%rbp), %rdi
movl $0x8, %edx
callq 0x3340
jmp 0x5cb1
leaq -0x420(%rbp), %rdi
leaq -0xac(%rbp), %rsi
callq 0x7690
movq %rax, -0x498(%rbp)
jmp 0x5ccd
movq -0x498(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x32a0
movb %al, -0x499(%rbp)
jmp 0x5ceb
leaq -0x420(%rbp), %rdi
callq 0x3120
movb -0x499(%rbp), %al
testb $0x1, %al
jne 0x5d03
jmp 0x5d37
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd0(%rbp)
jmp 0x5d37
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
leaq -0x420(%rbp), %rdi
callq 0x3120
jmp 0x63b0
jmp 0x5d39
movq -0x10(%rbp), %rdi
callq 0x82c0
movl %eax, -0x4a0(%rbp)
jmp 0x5d4a
movl -0x4a0(%rbp), %eax
movl %eax, -0xc4(%rbp)
cmpl $0x0, -0xa8(%rbp)
jle 0x5d6d
movl -0xa8(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jle 0x5d7a
cmpl $0x0, -0xd0(%rbp)
je 0x5d7a
movb $0x1, -0x1a(%rbp)
movl -0xac(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jle 0x5d8c
movb $0x1, -0x1a(%rbp)
movl -0xac(%rbp), %eax
cmpl -0xa8(%rbp), %eax
jge 0x5da7
cmpl $0x2, -0xd0(%rbp)
jne 0x5da7
movb $0x1, -0x1a(%rbp)
jmp 0x5da9
movq -0x458(%rbp), %rdi
leaq 0x364e(%rip), %rsi      # 0x9405
xorl %eax, %eax
movl %eax, %edx
callq 0x3130
cmpq $-0x1, %rax
je 0x5e5a
movq -0x458(%rbp), %rdi
leaq 0x362d(%rip), %rsi      # 0x9405
movq %rsi, -0x4a8(%rbp)
xorl %eax, %eax
movl %eax, %edx
callq 0x3130
movq -0x4a8(%rbp), %rsi
movq -0x458(%rbp), %rdi
incl %eax
movl %eax, -0xc8(%rbp)
movslq -0xc8(%rbp), %rdx
callq 0x3130
movq -0x458(%rbp), %rsi
movl %eax, -0xcc(%rbp)
movslq -0xc8(%rbp), %rdx
movl %edx, %ecx
movl -0xcc(%rbp), %eax
subl %ecx, %eax
movslq %eax, %rcx
leaq -0x440(%rbp), %rdi
callq 0x3200
jmp 0x5e39
leaq -0xa0(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0x3280
leaq -0x440(%rbp), %rdi
callq 0x3160
jmp 0x5e6d
movb $0x0, -0x1(%rbp)
movl $0x1, -0x444(%rbp)
jmp 0x637b
testb $0x1, -0x1a(%rbp)
jne 0x636d
movl -0xd0(%rbp), %eax
movl %eax, -0x4ac(%rbp)
testl %eax, %eax
je 0x5eae
jmp 0x5e89
movl -0x4ac(%rbp), %eax
subl $0x1, %eax
je 0x6063
jmp 0x5e9a
movl -0x4ac(%rbp), %eax
subl $0x2, %eax
je 0x61c9
jmp 0x635d
movq -0x10(%rbp), %rdi
callq 0x82c0
movl %eax, -0x4b0(%rbp)
jmp 0x5ebf
movl -0x4b0(%rbp), %eax
movl %eax, -0xac(%rbp)
movl $0x1, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jg 0x605e
movq -0x10(%rbp), %rdi
movl -0xa4(%rbp), %edx
leaq -0x40(%rbp), %rsi
callq 0x8710
movb %al, -0x4b1(%rbp)
jmp 0x5f02
movb -0x4b1(%rbp), %al
testb $0x1, %al
jne 0x5f11
jmp 0x5fe9
leaq -0x40(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x33f0
cmpq $-0x1, %rax
je 0x5fe7
testb $0x1, -0x19(%rbp)
je 0x5fa0
movl $0x2, %edi
callq 0x76c0
movl %eax, -0x4b8(%rbp)
jmp 0x5f47
movl -0x4b8(%rbp), %eax
movl %eax, -0x448(%rbp)
movl -0x448(%rbp), %esi
movq 0x6068(%rip), %rdi      # 0xbfc8
callq 0x3260
movq %rax, -0x4c0(%rbp)
jmp 0x5f6e
movq -0x4c0(%rbp), %rdi
movl -0xa4(%rbp), %esi
callq 0x3370
movq %rax, -0x4c8(%rbp)
jmp 0x5f89
movq -0x4c8(%rbp), %rdi
leaq 0x3437(%rip), %rsi      # 0x93ce
callq 0x31f0
jmp 0x5f9e
jmp 0x5fb7
movq 0x6021(%rip), %rdi      # 0xbfc8
leaq 0x3420(%rip), %rsi      # 0x93ce
callq 0x31f0
jmp 0x5fb5
jmp 0x5fb7
movq 0x600a(%rip), %rdi      # 0xbfc8
leaq -0x40(%rbp), %rsi
callq 0x31d0
movq %rax, -0x4d0(%rbp)
jmp 0x5fd0
movq -0x4d0(%rbp), %rdi
movq 0x5fda(%rip), %rsi      # 0xbfb8
callq 0x3210
jmp 0x5fe5
jmp 0x5fe7
jmp 0x6048
movq 0x5fd8(%rip), %rdi      # 0xbfc8
leaq 0x3410(%rip), %rsi      # 0x9407
callq 0x31f0
movq %rax, -0x4d8(%rbp)
jmp 0x6005
movq -0x4d8(%rbp), %rdi
movl -0xa4(%rbp), %esi
callq 0x3370
movq %rax, -0x4e0(%rbp)
jmp 0x6020
movq -0x4e0(%rbp), %rdi
movq 0x5f8a(%rip), %rsi      # 0xbfb8
callq 0x3210
jmp 0x6035
movb $0x0, -0x1(%rbp)
movl $0x1, -0x444(%rbp)
jmp 0x637b
jmp 0x604a
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x5ed5
jmp 0x635d
movq -0x10(%rbp), %rdi
movl -0xa8(%rbp), %edx
leaq -0x40(%rbp), %rsi
callq 0x8710
movb %al, -0x4e1(%rbp)
jmp 0x607e
movb -0x4e1(%rbp), %al
testb $0x1, %al
jne 0x608d
jmp 0x61c4
leaq -0x40(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x33f0
cmpq $-0x1, %rax
je 0x6163
testb $0x1, -0x19(%rbp)
je 0x611c
movl $0x2, %edi
callq 0x76c0
movl %eax, -0x4e8(%rbp)
jmp 0x60c3
movl -0x4e8(%rbp), %eax
movl %eax, -0x44c(%rbp)
movl -0x44c(%rbp), %esi
movq 0x5eec(%rip), %rdi      # 0xbfc8
callq 0x3260
movq %rax, -0x4f0(%rbp)
jmp 0x60ea
movq -0x4f0(%rbp), %rdi
movl -0xa8(%rbp), %esi
callq 0x3370
movq %rax, -0x4f8(%rbp)
jmp 0x6105
movq -0x4f8(%rbp), %rdi
leaq 0x32bb(%rip), %rsi      # 0x93ce
callq 0x31f0
jmp 0x611a
jmp 0x6133
movq 0x5ea5(%rip), %rdi      # 0xbfc8
leaq 0x32a4(%rip), %rsi      # 0x93ce
callq 0x31f0
jmp 0x6131
jmp 0x6133
movq 0x5e8e(%rip), %rdi      # 0xbfc8
leaq -0x40(%rbp), %rsi
callq 0x31d0
movq %rax, -0x500(%rbp)
jmp 0x614c
movq -0x500(%rbp), %rdi
movq 0x5e5e(%rip), %rsi      # 0xbfb8
callq 0x3210
jmp 0x6161
jmp 0x61c2
movq 0x5e5e(%rip), %rdi      # 0xbfc8
leaq 0x3296(%rip), %rsi      # 0x9407
callq 0x31f0
movq %rax, -0x508(%rbp)
jmp 0x617f
movq -0x508(%rbp), %rdi
movl -0xa4(%rbp), %esi
callq 0x3370
movq %rax, -0x510(%rbp)
jmp 0x619a
movq -0x510(%rbp), %rdi
movq 0x5e10(%rip), %rsi      # 0xbfb8
callq 0x3210
jmp 0x61af
movb $0x0, -0x1(%rbp)
movl $0x1, -0x444(%rbp)
jmp 0x637b
jmp 0x61c4
jmp 0x635d
movl -0xa8(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl -0xac(%rbp), %eax
jg 0x635b
movq -0x10(%rbp), %rdi
movl -0xa4(%rbp), %edx
leaq -0x40(%rbp), %rsi
callq 0x8710
movb %al, -0x511(%rbp)
jmp 0x6202
movb -0x511(%rbp), %al
testb $0x1, %al
jne 0x6211
jmp 0x62e9
leaq -0x40(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x33f0
cmpq $-0x1, %rax
je 0x62e7
testb $0x1, -0x19(%rbp)
je 0x62a0
movl $0x2, %edi
callq 0x76c0
movl %eax, -0x518(%rbp)
jmp 0x6247
movl -0x518(%rbp), %eax
movl %eax, -0x450(%rbp)
movl -0x450(%rbp), %esi
movq 0x5d68(%rip), %rdi      # 0xbfc8
callq 0x3260
movq %rax, -0x520(%rbp)
jmp 0x626e
movq -0x520(%rbp), %rdi
movl -0xa4(%rbp), %esi
callq 0x3370
movq %rax, -0x528(%rbp)
jmp 0x6289
movq -0x528(%rbp), %rdi
leaq 0x3137(%rip), %rsi      # 0x93ce
callq 0x31f0
jmp 0x629e
jmp 0x62b7
movq 0x5d21(%rip), %rdi      # 0xbfc8
leaq 0x3120(%rip), %rsi      # 0x93ce
callq 0x31f0
jmp 0x62b5
jmp 0x62b7
movq 0x5d0a(%rip), %rdi      # 0xbfc8
leaq -0x40(%rbp), %rsi
callq 0x31d0
movq %rax, -0x530(%rbp)
jmp 0x62d0
movq -0x530(%rbp), %rdi
movq 0x5cda(%rip), %rsi      # 0xbfb8
callq 0x3210
jmp 0x62e5
jmp 0x62e7
jmp 0x6345
movq 0x5cd8(%rip), %rdi      # 0xbfc8
leaq 0x3110(%rip), %rsi      # 0x9407
callq 0x31f0
movq %rax, -0x538(%rbp)
jmp 0x6305
movq -0x538(%rbp), %rdi
movl -0xa4(%rbp), %esi
callq 0x3370
movq %rax, -0x540(%rbp)
jmp 0x6320
movq -0x540(%rbp), %rdi
movq 0x5c8a(%rip), %rsi      # 0xbfb8
callq 0x3210
jmp 0x6335
movb $0x0, -0x1(%rbp)
movl $0x1, -0x444(%rbp)
jmp 0x637b
jmp 0x6347
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x61d5
jmp 0x635d
movb $0x1, -0x1(%rbp)
movl $0x1, -0x444(%rbp)
jmp 0x637b
movb $0x0, -0x1(%rbp)
movl $0x1, -0x444(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x3160
leaq -0x80(%rbp), %rdi
callq 0x3160
leaq -0x60(%rbp), %rdi
callq 0x3160
leaq -0x40(%rbp), %rdi
callq 0x3160
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x540, %rsp            # imm = 0x540
popq %rbp
retq
leaq -0xa0(%rbp), %rdi
callq 0x3160
leaq -0x80(%rbp), %rdi
callq 0x3160
leaq -0x60(%rbp), %rdi
callq 0x3160
leaq -0x40(%rbp), %rdi
callq 0x3160
movq -0xf8(%rbp), %rdi
callq 0x3380
nopw %cs:(%rax,%rax)