// File: /workspace/github_cpp_makefile_star2_O1/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 %r12d, %r12d
movq %r12, -0x8(%rax)
movb %r12b, (%rax)
leaq -0xc0(%rbp), %rax
movq %rax, -0x10(%rax)
movq %r12, -0x8(%rax)
movb %r12b, (%rax)
leaq -0xa0(%rbp), %rax
movq %rax, -0x10(%rax)
movq %r12, -0x8(%rax)
movb %r12b, (%rax)
leaq -0x78(%rbp), %rax
movq %rax, -0x10(%rax)
movq %r12, -0x8(%rax)
movb %r12b, (%rax)
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
movq %r12, -0x8(%rax)
movb %r12b, (%rax)
movl %r12d, -0x30(%rbp)
movl %r12d, -0x34(%rbp)
leaq 0x24d4(%rip), %rsi      # 0x73ce
movl $0x1, %edx
movl $0x1, %ecx
movq %r14, %rdi
callq 0x2210
cmpq $-0x1, %rax
je 0x5118
leaq 0x24b1(%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 0x4f9a
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, %rbx      # imm = 0x100000000
leaq -0x330(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
movl -0x310(%rbp,%rax), %r13d
xorl %r12d, %r12d
testb $0x5, %r13b
sete %r12b
movq 0x4fcc(%rip), %rsi      # 0x9fc0
callq 0x21c0
leaq -0x2b8(%rbp), %rdi
callq 0x2080
shlq $0x20, %r15
addq %rbx, %r15
sarq $0x20, %r15
leaq 0x23b7(%rip), %rsi      # 0x73ce
movl $0x1, %ecx
movq %r14, %rdi
movq %r15, %rdx
callq 0x2210
cmpq $-0x1, %rax
je 0x5118
leaq 0x2396(%rip), %rbx      # 0x73ce
movl $0x1, %ecx
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2210
movslq %eax, %r15
movl $0x1, %ecx
movq %r14, %rdi
movq %rbx, %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 0x50ad
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 0x4ec0(%rip), %rsi      # 0x9fc0
cmovel %ecx, %r12d
leaq -0x330(%rbp), %rdi
callq 0x21c0
movq %r15, %rdi
callq 0x2080
movq -0x40(%rbp), %rdi
callq 0x66b4
movl -0x30(%rbp), %edx
testl %edx, %edx
movq %r14, -0x90(%rbp)
jle 0x513c
xorl %ecx, %ecx
cmpl %eax, %edx
jle 0x5147
testl %r12d, %r12d
jne 0x5141
jmp 0x5147
testl %r12d, %r12d
je 0x5145
movb $0x1, %cl
jmp 0x5147
xorl %ecx, %ecx
movl %ecx, -0x48(%rbp)
movl -0x34(%rbp), %ecx
movl %eax, -0x44(%rbp)
cmpl %eax, %ecx
setg %bl
cmpl %edx, %ecx
setl %r14b
cmpl $0x2, %r12d
sete %r13b
leaq 0x229b(%rip), %rsi      # 0x7405
movl $0x1, %ecx
movq -0x90(%rbp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x21e0
cmpq $-0x1, %rax
je 0x5221
movb %r13b, -0x29(%rbp)
movb %bl, -0x2a(%rbp)
leaq 0x226d(%rip), %r13      # 0x7405
movl $0x1, %ecx
movq %r15, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x21e0
incl %eax
movslq %eax, %rbx
movl $0x1, %ecx
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x21e0
movq %rax, %r13
subl %ebx, %eax
movslq %eax, %rcx
leaq -0x330(%rbp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2160
movl -0x48(%rbp), %r15d
orb -0x2a(%rbp), %r15b
andb -0x29(%rbp), %r14b
leaq -0x88(%rbp), %rdi
leaq -0x330(%rbp), %rbx
movq %rbx, %rsi
callq 0x2190
movq (%rbx), %rdi
leaq -0x320(%rbp), %rax
cmpq %rax, %rdi
je 0x521c
movq -0x320(%rbp), %rsi
incq %rsi
callq 0x2140
orb %r15b, %r14b
jmp 0x5224
movb $0x1, %r14b
movb $0x1, %r15b
leal 0x1(%r13), %eax
movslq %eax, %rdx
leaq 0x21d0(%rip), %rsi      # 0x7405
movl $0x1, %ecx
movq -0x90(%rbp), %rbx
movq %rbx, %rdi
callq 0x21e0
cmpq $-0x1, %rax
je 0x52cb
movslq %r13d, %rdx
leaq 0x21ac(%rip), %r13      # 0x7405
movl $0x1, %ecx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2210
movslq %eax, %r15
movl $0x1, %ecx
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x21e0
subl %r15d, %eax
movslq %eax, %rcx
leaq -0x330(%rbp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2160
leaq -0x68(%rbp), %rdi
leaq -0x330(%rbp), %rbx
movq %rbx, %rsi
callq 0x2190
movq (%rbx), %rdi
leaq -0x320(%rbp), %rax
cmpq %rax, %rdi
movl %r14d, %r15d
je 0x52cb
movq -0x320(%rbp), %rsi
incq %rsi
callq 0x2140
testb %r15b, %r15b
jne 0x54a1
movl %r15d, %ebx
cmpl $0x2, %r12d
je 0x5376
cmpl $0x1, %r12d
jne 0x5402
leaq -0x180(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
leaq -0x190(%rbp), %rdi
callq 0x5f86
leaq -0x100(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x110(%rbp), %rdi
callq 0x5f86
movl -0x30(%rbp), %ecx
leaq -0x190(%rbp), %rsi
leaq -0x110(%rbp), %rdx
movq -0x40(%rbp), %rdi
movl %ecx, %r8d
callq 0x6b94
movq -0x110(%rbp), %rdi
cmpq %r15, %rdi
je 0x536a
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x190(%rbp), %rdi
jmp 0x548e
leaq -0x160(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
leaq -0x170(%rbp), %rdi
callq 0x5f86
leaq -0xe0(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0xf0(%rbp), %rdi
callq 0x5f86
movl -0x30(%rbp), %ecx
movl -0x34(%rbp), %r8d
leaq -0x170(%rbp), %rsi
leaq -0xf0(%rbp), %rdx
movq -0x40(%rbp), %rdi
callq 0x6b94
movq -0xf0(%rbp), %rdi
cmpq %r15, %rdi
je 0x53f6
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x170(%rbp), %rdi
jmp 0x548e
leaq -0x1a0(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
leaq -0x1b0(%rbp), %rdi
callq 0x5f86
movl -0x44(%rbp), %r12d
leaq -0x120(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x130(%rbp), %rdi
callq 0x5f86
leaq -0x1b0(%rbp), %rsi
leaq -0x130(%rbp), %rdx
movq -0x40(%rbp), %rdi
movl $0x1, %ecx
movl %r12d, %r8d
callq 0x6b94
movq -0x130(%rbp), %rdi
cmpq %r15, %rdi
je 0x5487
movq -0x120(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x1b0(%rbp), %rdi
cmpq %r14, %rdi
movl %ebx, %r15d
je 0x54a1
movq (%r14), %rsi
incq %rsi
callq 0x2140
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x54ba
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x54d6
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x54f8
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x551a
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x150(%rbp), %rdi
leaq -0x140(%rbp), %rax
cmpq %rax, %rdi
je 0x553c
movq -0x140(%rbp), %rsi
incq %rsi
callq 0x2140
xorb $0x1, %r15b
movl %r15d, %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 0x55b8
movq -0x120(%rbp), %rsi
incq %rsi
callq 0x2140
jmp 0x55b8
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
cmpq %r15, %rdi
je 0x55d0
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x2140
jmp 0x55d0
movq %rax, %rbx
movq -0x110(%rbp), %rdi
cmpq %r15, %rdi
je 0x55e8
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x2140
jmp 0x55e8
movq %rax, %rbx
movq -0x1b0(%rbp), %rdi
cmpq %r14, %rdi
je 0x563a
movq -0x1a0(%rbp), %rsi
jmp 0x55fb
movq %rax, %rbx
movq -0x170(%rbp), %rdi
cmpq %r14, %rdi
je 0x563a
movq -0x160(%rbp), %rsi
jmp 0x55fb
movq %rax, %rbx
movq -0x190(%rbp), %rdi
cmpq %r14, %rdi
je 0x563a
movq -0x180(%rbp), %rsi
incq %rsi
callq 0x2140
jmp 0x563a
jmp 0x560f
jmp 0x5637
jmp 0x5637
jmp 0x5637
jmp 0x5637
movq %rax, %rbx
movq 0x49a7(%rip), %rsi      # 0x9fc0
leaq -0x330(%rbp), %rdi
callq 0x21c0
leaq -0x2b8(%rbp), %rdi
callq 0x2080
jmp 0x563a
jmp 0x5637
jmp 0x5637
movq %rax, %rbx
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x5653
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x566f
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x5691
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x56b3
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x2140
movq -0x150(%rbp), %rdi
leaq -0x140(%rbp), %rax
cmpq %rax, %rdi
je 0x56d5
movq -0x140(%rbp), %rsi
incq %rsi
callq 0x2140
movq %rbx, %rdi
callq 0x2260