// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq %rdi, -0x40(%rbp)
leaq -0x140(%rbp), %rax
movq %rax, -0x10(%rax)
xorl %ebx, %ebx
movq %rbx, -0x8(%rax)
movb %bl, (%rax)
leaq -0xc0(%rbp), %rax
movq %rax, -0x10(%rax)
movq %rbx, -0x8(%rax)
movb %bl, (%rax)
leaq -0xa0(%rbp), %rax
movq %rax, -0x10(%rax)
movq %rbx, -0x8(%rax)
movb %bl, (%rax)
leaq -0x78(%rbp), %rax
movq %rax, -0x10(%rax)
movq %rbx, -0x8(%rax)
movb %bl, (%rax)
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
movq %rbx, -0x8(%rax)
movb %bl, (%rax)
movl %ebx, -0x30(%rbp)
movl %ebx, -0x34(%rbp)
leaq 0x2561(%rip), %rsi      # 0x73ce
movl $0x1, %edx
movl $0x1, %ecx
movq %r14, %rdi
callq 0x2210
cmpq $-0x1, %rax
je 0x5088
leaq 0x253e(%rip), %r15      # 0x73ce
movl $0x1, %edx
movl $0x1, %ecx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2210
movq %rax, %r12
incl %eax
movslq %eax, %rdx
movl $0x1, %ecx
movq %r14, %rdi
movq %r15, %rsi
callq 0x21e0
movq %rax, %r15
subl %r12d, %eax
movslq %r12d, %rdx
movslq %eax, %rcx
leaq -0x330(%rbp), %rdi
movq %r14, %rsi
callq 0x2160
leaq -0xd0(%rbp), %rdi
leaq -0x330(%rbp), %r12
movq %r12, %rsi
callq 0x2190
movq (%r12), %rdi
leaq -0x320(%rbp), %rax
cmpq %rax, %rdi
je 0x4f0d
movq -0x320(%rbp), %rsi
incq %rsi
callq 0x2140
leaq -0x330(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
movl $0x8, %edx
callq 0x2200
leaq -0x330(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x20c0
movabsq $0x100000000, %r12      # 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
movq 0x505b(%rip), %rsi      # 0x9fc0
callq 0x21c0
leaq -0x2b8(%rbp), %rdi
callq 0x2080
shlq $0x20, %r15
addq %r12, %r15
sarq $0x20, %r15
leaq 0x2446(%rip), %rsi      # 0x73ce
movl $0x1, %ecx
movq %r14, %rdi
movq %r15, %rdx
callq 0x2210
cmpq $-0x1, %rax
je 0x5088
leaq 0x2425(%rip), %r12      # 0x73ce
movl $0x1, %ecx
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2210
movslq %eax, %r15
movl $0x1, %ecx
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x21e0
subl %r15d, %eax
movslq %eax, %rcx
leaq -0x330(%rbp), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2160
leaq -0xb0(%rbp), %rdi
leaq -0x330(%rbp), %r15
movq %r15, %rsi
callq 0x2190
movq (%r15), %rdi
leaq -0x320(%rbp), %rax
cmpq %rax, %rdi
je 0x501e
movq -0x320(%rbp), %rsi
incq %rsi
callq 0x2140
leaq -0x330(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
movl $0x8, %edx
callq 0x2200
leaq -0x330(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x20c0
andl $0x5, %r13d
leaq -0x2b8(%rbp), %r15
movq -0x78(%r15), %rax
movq -0x18(%rax), %rax
xorl %ecx, %ecx
cmpl $0x1, %r13d
adcl $0x1, %ecx
testb $0x5, -0x310(%rbp,%rax)
movq 0x4f4f(%rip), %rsi      # 0x9fc0
cmovel %ecx, %ebx
leaq -0x330(%rbp), %rdi
callq 0x21c0
movq %r15, %rdi
callq 0x2080
movq -0x40(%rbp), %rdi
callq 0x66a4
movl -0x30(%rbp), %edx
testl %edx, %edx
movq %r14, -0x90(%rbp)
jle 0x50ab
xorl %ecx, %ecx
cmpl %eax, %edx
jle 0x50b5
testl %ebx, %ebx
jne 0x50af
jmp 0x50b5
testl %ebx, %ebx
je 0x50b3
movb $0x1, %cl
jmp 0x50b5
xorl %ecx, %ecx
movl %ecx, -0x48(%rbp)
movl -0x34(%rbp), %ecx
movl %eax, -0x44(%rbp)
cmpl %eax, %ecx
setg %r12b
cmpl %edx, %ecx
setl %r14b
cmpl $0x2, %ebx
sete %r13b
leaq 0x232d(%rip), %rsi      # 0x7405
movl $0x1, %ecx
movq -0x90(%rbp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x21e0
cmpq $-0x1, %rax
je 0x5195
movb %r13b, -0x29(%rbp)
movb %r12b, -0x2a(%rbp)
leaq 0x22fe(%rip), %r12      # 0x7405
movl $0x1, %ecx
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x21e0
incl %eax
movslq %eax, %r13
movl $0x1, %ecx
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x21e0
movq %rax, %r12
subl %r13d, %eax
movslq %eax, %rcx
leaq -0x330(%rbp), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2160
movl -0x48(%rbp), %r15d
orb -0x2a(%rbp), %r15b
andb -0x29(%rbp), %r14b
leaq -0x88(%rbp), %rdi
leaq -0x330(%rbp), %r13
movq %r13, %rsi
callq 0x2190
movq (%r13), %rdi
leaq -0x320(%rbp), %rax
cmpq %rax, %rdi
je 0x518d
movq -0x320(%rbp), %rsi
incq %rsi
callq 0x2140
orb %r15b, %r14b
movl %r14d, %r15d
jmp 0x5198
movb $0x1, %r15b
leal 0x1(%r12), %eax
movslq %eax, %rdx
leaq 0x225e(%rip), %rsi      # 0x7405
movl $0x1, %ecx
movq -0x90(%rbp), %r14
movq %r14, %rdi
callq 0x21e0
cmpq $-0x1, %rax
je 0x523f
movslq %r12d, %rdx
leaq 0x223a(%rip), %r12      # 0x7405
movl $0x1, %ecx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2210
movslq %eax, %r13
movl $0x1, %ecx
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x21e0
subl %r13d, %eax
movslq %eax, %rcx
leaq -0x330(%rbp), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x2160
leaq -0x68(%rbp), %rdi
leaq -0x330(%rbp), %r14
movq %r14, %rsi
callq 0x2190
movq (%r14), %rdi
leaq -0x320(%rbp), %rax
cmpq %rax, %rdi
je 0x523a
movq -0x320(%rbp), %rsi
incq %rsi
callq 0x2140
testb %r15b, %r15b
je 0x5246
xorl %ebx, %ebx
jmp 0x540a
cmpl $0x2, %ebx
je 0x52e3
cmpl $0x1, %ebx
jne 0x536f
leaq -0x180(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
leaq -0x190(%rbp), %rdi
callq 0x5f5e
leaq -0x100(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x110(%rbp), %rdi
callq 0x5f5e
movl -0x30(%rbp), %ecx
leaq -0x190(%rbp), %rsi
leaq -0x110(%rbp), %rdx
movq -0x40(%rbp), %rdi
movl %ecx, %r8d
callq 0x6b84
movq -0x110(%rbp), %rdi
cmpq %r15, %rdi
je 0x52d7
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x190(%rbp), %rdi
jmp 0x53f8
leaq -0x160(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
leaq -0x170(%rbp), %rdi
callq 0x5f5e
leaq -0xe0(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0xf0(%rbp), %rdi
callq 0x5f5e
movl -0x30(%rbp), %ecx
movl -0x34(%rbp), %r8d
leaq -0x170(%rbp), %rsi
leaq -0xf0(%rbp), %rdx
movq -0x40(%rbp), %rdi
callq 0x6b84
movq -0xf0(%rbp), %rdi
cmpq %r15, %rdi
je 0x5363
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x170(%rbp), %rdi
jmp 0x53f8
leaq -0x1a0(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
leaq -0x1b0(%rbp), %rdi
callq 0x5f5e
leaq -0x120(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x130(%rbp), %rdi
callq 0x5f5e
leaq -0x1b0(%rbp), %rsi
leaq -0x130(%rbp), %rdx
movq -0x40(%rbp), %rdi
movl $0x1, %ecx
movl -0x44(%rbp), %r8d
callq 0x6b84
movq -0x130(%rbp), %rdi
cmpq %r15, %rdi
je 0x53f1
movq -0x120(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x1b0(%rbp), %rdi
cmpq %r14, %rdi
je 0x5408
movq (%r14), %rsi
incq %rsi
callq 0x2140
movb $0x1, %bl
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x5423
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x543f
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x5461
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x5483
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x150(%rbp), %rdi
leaq -0x140(%rbp), %rax
cmpq %rax, %rdi
je 0x54a5
movq -0x140(%rbp), %rsi
incq %rsi
callq 0x2140
movl %ebx, %eax
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x130(%rbp), %rdi
cmpq %r15, %rdi
je 0x551c
movq -0x120(%rbp), %rsi
incq %rsi
callq 0x2140
jmp 0x551c
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
cmpq %r15, %rdi
je 0x5534
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x2140
jmp 0x5534
movq %rax, %rbx
movq -0x110(%rbp), %rdi
cmpq %r15, %rdi
je 0x554c
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x2140
jmp 0x554c
movq %rax, %rbx
movq -0x1b0(%rbp), %rdi
cmpq %r14, %rdi
je 0x559e
movq -0x1a0(%rbp), %rsi
jmp 0x555f
movq %rax, %rbx
movq -0x170(%rbp), %rdi
cmpq %r14, %rdi
je 0x559e
movq -0x160(%rbp), %rsi
jmp 0x555f
movq %rax, %rbx
movq -0x190(%rbp), %rdi
cmpq %r14, %rdi
je 0x559e
movq -0x180(%rbp), %rsi
incq %rsi
callq 0x2140
jmp 0x559e
jmp 0x5573
jmp 0x559b
jmp 0x559b
jmp 0x559b
jmp 0x559b
movq %rax, %rbx
movq 0x4a43(%rip), %rsi      # 0x9fc0
leaq -0x330(%rbp), %rdi
callq 0x21c0
leaq -0x2b8(%rbp), %rdi
callq 0x2080
jmp 0x559e
jmp 0x559b
jmp 0x559b
movq %rax, %rbx
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x55b7
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x55d3
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x55f5
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x5617
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x150(%rbp), %rdi
leaq -0x140(%rbp), %rax
cmpq %rax, %rdi
je 0x5639
movq -0x140(%rbp), %rsi
incq %rsi
callq 0x2140
movq %rbx, %rdi
callq 0x2260