// File: /workspace/github_cpp_makefile_star2_O2/craigbarstow[P]cmd-line-text-editor/text_editor.cc
// Function: find_and_replace(text_ed&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool find_and_replace(text_ed & T, string cl) { 
  bool error_state=false;
  string aline,m_string,n_string, word, second_word;
  int i = 0,m=0,n=0,p=1,m_begin, m_end,m_length, n_begin, n_end, n_length, document_length, word_start, word_end, second_word_start, second_word_end, count=0;
    //SECTION TO ISOLATE INTEGERS FROM THE GIVEN STRING
  if (cl.find_first_not_of(" ",1)!=string::npos){ //section to isolate first integer from string
    m_begin= cl.find_first_not_of(" ",1);
    m_end = cl.find_first_of(" ", m_begin+1); 
    m_length = m_end-m_begin;
    m_string=cl.substr(m_begin,m_length);
    if (istringstream(m_string) >> m) { //if first substring can be converted to an integer, increment the count to show it is present
      count++;
    }
    if (cl.find_first_not_of(" ", m_end+1)!=string::npos){ //section to isolate second integer from the string
      n_begin = cl.find_first_not_of(" ", m_end+1);
      n_end = cl.find_first_of(" ",n_begin);
      n_length = n_end-n_begin;
      n_string=cl.substr(n_begin,n_length);
      if (istringstream(n_string) >> n){ //if the second substring can be converted to an integer, increment the count to show it 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, set it to the starting line number
      error_state=true;
    }
    if (n>document_length) { //if ending integer value is greater than the document length, toggle error state to true
      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 WORDS FROM THE STRING
    if (cl.find_first_of("/")!=string::npos){ //portion to isolate first word
      word_start = cl.find_first_of("/")+1;
      word_end = cl.find_first_of("/",word_start);
      word = cl.substr(word_start,word_end-word_start);
    } else {error_state=true;} //if first word can't be found, toggle error state to true
    if (cl.find_first_of("/",word_end+1)!=string::npos){ //portion to isolate the replacement word
      second_word_start = cl.find_first_not_of("/",word_end);
      second_word_end = cl.find_first_of("/",second_word_start);
      second_word = cl.substr(second_word_start, second_word_end-second_word_start);
    } else {error_state=true;} //if second word can't be found, toggle error state to true
    if (!error_state) {
      switch (count) {
      case 0: //if no integers present, find and replace throughout document
	T.replace(word,second_word,1,document_length); //if no integer present, replace old word with new word throughout the document
	break;
      case 1: //if one integer is present, find and replace on just that line
	T.replace(word,second_word,m,m);
	break;
      case 2: //if two integers present, replace old word with new word from line m to line n
	T.replace(word,second_word,m,n);
	break;
      }
      return true;
    } else {
      return false; //if error state is true, return false
    }
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x308, %rsp            # imm = 0x308
movq %rsi, %r13
movq %rdi, -0x40(%rbp)
leaq -0xe0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ebx, %ebx
movb %bl, (%rax)
leaq -0xc0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq -0xa0(%rbp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb %bl, (%rax)
movb %bl, (%rcx)
leaq -0x80(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq -0x60(%rbp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb %bl, (%rax)
andl $0x0, -0x30(%rbp)
movb %bl, (%rcx)
andl $0x0, -0x34(%rbp)
leaq 0x22a5(%rip), %rsi      # 0x63ce
pushq $0x1
popq %rdx
movq %r13, %rdi
callq 0x2120
cmpq $-0x1, %rax
movq %r13, -0x48(%rbp)
je 0x42d6
leaq 0x2285(%rip), %r15      # 0x63ce
pushq $0x1
popq %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x2120
movq %rax, %r12
incl %eax
movslq %eax, %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x2100
movq %rax, %r15
subl %r12d, %eax
movslq %r12d, %rdx
movslq %eax, %rcx
leaq -0x330(%rbp), %rdi
movq %r13, %rsi
callq 0x21a0
leaq -0xd0(%rbp), %r12
leaq -0x330(%rbp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x21c0
movq %r13, %rdi
callq 0x20d0
leaq -0x330(%rbp), %rdi
pushq $0x8
popq %rdx
movq %r12, %rsi
callq 0x2220
leaq -0x330(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x2130
movabsq $0x100000000, %r14      # imm = 0x100000000
leaq -0x330(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
movl -0x310(%rbp,%rax), %r13d
xorl %ebx, %ebx
testb $0x5, %r13b
sete %bl
callq 0x20e0
shlq $0x20, %r15
addq %r14, %r15
sarq $0x20, %r15
leaq 0x21c6(%rip), %rsi      # 0x63ce
movq -0x48(%rbp), %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x2120
cmpq $-0x1, %rax
je 0x42d3
leaq 0x21a6(%rip), %r12      # 0x63ce
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2120
movslq %eax, %r15
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2100
subl %r15d, %eax
movslq %eax, %rcx
leaq -0x330(%rbp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x21a0
leaq -0xb0(%rbp), %r15
leaq -0x330(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x21c0
movq %r12, %rdi
callq 0x20d0
leaq -0x330(%rbp), %rdi
pushq $0x8
popq %rdx
movq %r15, %rsi
callq 0x2220
leaq -0x330(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x2130
andl $0x5, %r13d
leaq -0x330(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
xorl %ecx, %ecx
cmpl $0x1, %r13d
adcl $0x1, %ecx
testb $0x5, -0x310(%rbp,%rax)
cmovel %ecx, %ebx
callq 0x20e0
movq -0x48(%rbp), %r13
jmp 0x42d6
movq %r14, %r13
movq -0x40(%rbp), %rdi
callq 0x5182
movl %eax, %r15d
movl -0x30(%rbp), %eax
testl %eax, %eax
jle 0x42f7
xorl %r14d, %r14d
cmpl %r15d, %eax
jle 0x4303
testl %ebx, %ebx
jne 0x42fb
jmp 0x4303
testl %ebx, %ebx
je 0x4300
movb $0x1, %r14b
jmp 0x4303
xorl %r14d, %r14d
movl -0x34(%rbp), %ecx
cmpl %r15d, %ecx
setg -0x2b(%rbp)
cmpl %eax, %ecx
setl %r12b
cmpl $0x2, %ebx
sete -0x2a(%rbp)
leaq 0x20e4(%rip), %rsi      # 0x6405
movq %r13, %rdi
xorl %edx, %edx
callq 0x2100
cmpq $-0x1, %rax
je 0x43ba
movb %r12b, -0x29(%rbp)
movl %r15d, -0x4c(%rbp)
leaq 0x20c1(%rip), %r12      # 0x6405
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2100
incl %eax
movq %r13, %r15
movslq %eax, %r13
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2100
movq %rax, %r12
subl %r13d, %eax
movslq %eax, %rcx
leaq -0x330(%rbp), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x21a0
orb -0x2b(%rbp), %r14b
movb -0x29(%rbp), %al
andb -0x2a(%rbp), %al
orb %r14b, %al
movb %al, -0x29(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x330(%rbp), %r13
movq %r13, %rsi
callq 0x21c0
movq %r13, %rdi
callq 0x20d0
movq -0x48(%rbp), %r13
movl -0x4c(%rbp), %r15d
jmp 0x43be
movb $0x1, -0x29(%rbp)
leal 0x1(%r12), %eax
movslq %eax, %rdx
leaq 0x2038(%rip), %rsi      # 0x6405
movq %r13, %rdi
callq 0x2100
cmpq $-0x1, %rax
je 0x443d
movslq %r12d, %rdx
leaq 0x2020(%rip), %r12      # 0x6405
movq %r13, %rdi
movq %r12, %rsi
callq 0x2120
movq %r13, %r14
movslq %eax, %r13
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2100
subl %r13d, %eax
movslq %eax, %rcx
leaq -0x330(%rbp), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x21a0
leaq -0x70(%rbp), %rdi
leaq -0x330(%rbp), %r14
movq %r14, %rsi
callq 0x21c0
movq %r14, %rdi
callq 0x20d0
cmpb $0x0, -0x29(%rbp)
je 0x4444
xorl %ebx, %ebx
jmp 0x4523
cmpl $0x2, %ebx
je 0x4492
cmpl $0x1, %ebx
jne 0x44d3
leaq -0x170(%rbp), %r14
leaq -0x90(%rbp), %rsi
movq %r14, %rdi
callq 0x21e0
leaq -0x150(%rbp), %r12
leaq -0x70(%rbp), %rsi
movq %r12, %rdi
callq 0x21e0
movl -0x30(%rbp), %ecx
movq -0x40(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movl %ecx, %r8d
callq 0x551e
jmp 0x4511
leaq -0x130(%rbp), %r14
leaq -0x90(%rbp), %rsi
movq %r14, %rdi
callq 0x21e0
leaq -0x110(%rbp), %r12
leaq -0x70(%rbp), %rsi
movq %r12, %rdi
callq 0x21e0
movl -0x30(%rbp), %ecx
movl -0x34(%rbp), %r8d
movq -0x40(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x551e
jmp 0x4511
leaq -0x1b0(%rbp), %r14
leaq -0x90(%rbp), %rsi
movq %r14, %rdi
callq 0x21e0
leaq -0x190(%rbp), %r12
leaq -0x70(%rbp), %rsi
movq %r12, %rdi
callq 0x21e0
pushq $0x1
popq %rcx
movq -0x40(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movl %r15d, %r8d
callq 0x551e
movq %r12, %rdi
callq 0x20d0
movq %r14, %rdi
callq 0x20d0
movb $0x1, %bl
leaq -0x70(%rbp), %rdi
callq 0x20d0
leaq -0x90(%rbp), %rdi
callq 0x20d0
leaq -0xb0(%rbp), %rdi
callq 0x20d0
leaq -0xd0(%rbp), %rdi
callq 0x20d0
leaq -0xf0(%rbp), %rdi
callq 0x20d0
movl %ebx, %eax
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x190(%rbp), %rdi
callq 0x20d0
jmp 0x45a6
movq %rax, %rbx
leaq -0x110(%rbp), %rdi
callq 0x20d0
jmp 0x45b2
movq %rax, %rbx
leaq -0x150(%rbp), %rdi
callq 0x20d0
jmp 0x45be
movq %rax, %rbx
leaq -0x1b0(%rbp), %rdi
jmp 0x45c5
movq %rax, %rbx
leaq -0x130(%rbp), %rdi
jmp 0x45c5
movq %rax, %rbx
leaq -0x170(%rbp), %rdi
callq 0x20d0
jmp 0x45ee
jmp 0x45d6
jmp 0x45eb
jmp 0x45eb
jmp 0x45eb
jmp 0x45eb
movq %rax, %rbx
leaq -0x330(%rbp), %rdi
callq 0x20e0
jmp 0x45ee
jmp 0x45eb
jmp 0x45eb
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x20d0
leaq -0x90(%rbp), %rdi
callq 0x20d0
leaq -0xb0(%rbp), %rdi
callq 0x20d0
leaq -0xd0(%rbp), %rdi
callq 0x20d0
leaq -0xf0(%rbp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x2260