// File: craigbarstow[P]cmd-line-text-editor/text_editor.cc
// Function: type_lines(text_ed&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool)
bool type_lines(text_ed & T, string cl, bool toggle) { //type lines between two given integers. If only one integer is given, type just that line. If no integers are given, type the whole document.
  bool error_state=false;
  string aline,m_string,n_string;
  int i=0,m=0,n=0,p=1,m_begin, m_end, n_begin, n_end,document_length,count=0;
  if (cl.find_first_not_of(" ",1)!=string::npos){ //this section selects the first integer from the substring
    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 the substring cant be converted to an integer, set error state to false
      error_state=true;
    } else {count++;} //if it can be converted, increment the count
    if (cl.find_first_not_of(" ", m_end+2)!=string::npos){ //this section selects the second integer from the substring
      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)){
	error_state=true;
      } else {count++;} //if it can be converted, increment the count
    }
    //SECTION TO MAKE SURE INTEGERS MAKE SENSE
    if (m<=0){ //if the line number is below 1, this automatically sets it to one
      error_state=true;
    }
    document_length = T.get_last_line();
    if (m>document_length) { //this makes sure that the starting position is less than the last line of the document
      error_state = true;
      cout << "The first line number given exceeds file length." << endl;
    }
    if (n>document_length) { //if the ending position is greater than the document length, toggle error state to true
      error_state=true;
    }
    if (count!=1 && n<m){ //if both a  start position and an end position are put in, and the end value is greater than the starting value, the error state will be toggled to true
      error_state=true;
    }
  }
  if (!error_state) { 
    switch (count) {
    case 0: //if no integers are input, print the entire document
      n = T.get_last_line();
      for (i = 1; i<=n ;i++) {
	if (toggle) { //if toggling line numbers is on, print line numbers
	  cout << setw(2) << i << "  ";
	}
	if (T.get_line(aline,i)) { //if the given line can be retrieved, display it
	  cout << aline << endl;
	} else { //if it can't, return false
	  return false;
	}
      }      
      break;
    case 1: //if one integer is input, print just that line
      if (toggle) { //if toggling line numbers is on, print line numbers
	cout << setw(2) << m << "  ";
      }
      if (T.get_line(aline,m)) { //if the line can be retrieved, display it
	cout << aline << endl;
      } else {return false;} 
      break; 
    case 2:
      for (i = m; i<=n; i++){ // for each line between the start and end line positions
	if (toggle) { //if toggling line numbers is on, print the line numbers
	  cout << setw(2) << i << " ";
	} 
	if (T.get_line(aline,i)) { //if the line can be retrieved, display it
	  cout << aline << endl;
	} else {
	  return false;
	}
      }
      break;
    }
    return true;
  }
  return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4e0, %rsp            # imm = 0x4E0
movq %rsi, -0x410(%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
movq -0x410(%rbp), %rdi
movl $0x0, -0x84(%rbp)
movl $0x0, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movl $0x1, -0x90(%rbp)
movl $0x0, -0xa8(%rbp)
leaq 0x570e(%rip), %rsi      # 0x93ce
movl $0x1, %edx
callq 0x3140
cmpq $-0x1, %rax
je 0x4023
movq -0x410(%rbp), %rdi
leaq 0x56ec(%rip), %rsi      # 0x93ce
movq %rsi, -0x418(%rbp)
movl $0x1, %edx
callq 0x3140
movq -0x418(%rbp), %rsi
movq -0x410(%rbp), %rdi
movl %eax, -0x94(%rbp)
movslq -0x94(%rbp), %rdx
callq 0x3130
movq -0x410(%rbp), %rsi
addl $-0x2, %eax
movl %eax, -0x98(%rbp)
movslq -0x94(%rbp), %rdx
movslq -0x98(%rbp), %rcx
leaq -0xc8(%rbp), %rdi
callq 0x3200
jmp 0x3d3f
leaq -0x60(%rbp), %rdi
movq %rdi, -0x420(%rbp)
leaq -0xc8(%rbp), %rsi
movq %rsi, -0x428(%rbp)
callq 0x3280
movq -0x428(%rbp), %rdi
callq 0x3160
movq -0x420(%rbp), %rsi
leaq -0x258(%rbp), %rdi
movl $0x8, %edx
callq 0x3340
jmp 0x3d83
leaq -0x258(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x7690
movq %rax, -0x430(%rbp)
jmp 0x3d9f
movq -0x430(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3110
movb %al, -0x431(%rbp)
jmp 0x3dbd
leaq -0x258(%rbp), %rdi
callq 0x3120
movb -0x431(%rbp), %al
testb $0x1, %al
jne 0x3dd5
jmp 0x3e15
movb $0x1, -0x1a(%rbp)
jmp 0x3e24
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x43c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x3120
jmp 0x43c8
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
movq -0x410(%rbp), %rdi
movl -0x98(%rbp), %eax
addl $0x2, %eax
movslq %eax, %rdx
leaq 0x5590(%rip), %rsi      # 0x93ce
callq 0x3140
cmpq $-0x1, %rax
je 0x3f85
movq -0x410(%rbp), %rdi
movl -0x98(%rbp), %eax
addl $0x2, %eax
movslq %eax, %rdx
leaq 0x5567(%rip), %rsi      # 0x93ce
callq 0x3140
movq -0x410(%rbp), %rdi
movl %eax, -0x9c(%rbp)
movslq -0x9c(%rbp), %rdx
leaq 0x5548(%rip), %rsi      # 0x93cf
callq 0x3130
movq -0x410(%rbp), %rsi
movl %eax, -0xa0(%rbp)
movslq -0x9c(%rbp), %rdx
movslq -0xa0(%rbp), %rcx
leaq -0x278(%rbp), %rdi
callq 0x3200
jmp 0x3eb5
leaq -0x80(%rbp), %rdi
movq %rdi, -0x440(%rbp)
leaq -0x278(%rbp), %rsi
movq %rsi, -0x448(%rbp)
callq 0x3280
movq -0x448(%rbp), %rdi
callq 0x3160
movq -0x440(%rbp), %rsi
leaq -0x3f8(%rbp), %rdi
movl $0x8, %edx
callq 0x3340
jmp 0x3ef9
leaq -0x3f8(%rbp), %rdi
leaq -0x8c(%rbp), %rsi
callq 0x7690
movq %rax, -0x450(%rbp)
jmp 0x3f15
movq -0x450(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3110
movb %al, -0x451(%rbp)
jmp 0x3f33
leaq -0x3f8(%rbp), %rdi
callq 0x3120
movb -0x451(%rbp), %al
testb $0x1, %al
jne 0x3f4b
jmp 0x3f74
movb $0x1, -0x1a(%rbp)
jmp 0x3f83
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x3f8(%rbp), %rdi
callq 0x3120
jmp 0x43c8
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x3f85
cmpl $0x0, -0x88(%rbp)
jg 0x3f92
movb $0x1, -0x1a(%rbp)
movq -0x10(%rbp), %rdi
callq 0x82c0
movl %eax, -0x458(%rbp)
jmp 0x3fa3
movl -0x458(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0x88(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jle 0x3ff4
movb $0x1, -0x1a(%rbp)
movq 0x8000(%rip), %rdi      # 0xbfc8
leaq 0x53cd(%rip), %rsi      # 0x939c
callq 0x31f0
movq %rax, -0x460(%rbp)
jmp 0x3fdd
movq -0x460(%rbp), %rdi
movq 0x7fcd(%rip), %rsi      # 0xbfb8
callq 0x3210
jmp 0x3ff2
jmp 0x3ff4
movl -0x8c(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jle 0x4006
movb $0x1, -0x1a(%rbp)
cmpl $0x1, -0xa8(%rbp)
je 0x4021
movl -0x8c(%rbp), %eax
cmpl -0x88(%rbp), %eax
jge 0x4021
movb $0x1, -0x1a(%rbp)
jmp 0x4023
testb $0x1, -0x1a(%rbp)
jne 0x4391
movl -0xa8(%rbp), %eax
movl %eax, -0x464(%rbp)
testl %eax, %eax
je 0x4064
jmp 0x403f
movl -0x464(%rbp), %eax
subl $0x1, %eax
je 0x4193
jmp 0x4050
movl -0x464(%rbp), %eax
subl $0x2, %eax
je 0x4273
jmp 0x4381
movq -0x10(%rbp), %rdi
callq 0x82c0
movl %eax, -0x468(%rbp)
jmp 0x4075
movl -0x468(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl $0x1, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jg 0x418e
testb $0x1, -0x19(%rbp)
je 0x410e
movl $0x2, %edi
callq 0x76c0
movl %eax, -0x46c(%rbp)
jmp 0x40b5
movl -0x46c(%rbp), %eax
movl %eax, -0x3fc(%rbp)
movl -0x3fc(%rbp), %esi
movq 0x7efa(%rip), %rdi      # 0xbfc8
callq 0x3260
movq %rax, -0x478(%rbp)
jmp 0x40dc
movq -0x478(%rbp), %rdi
movl -0x84(%rbp), %esi
callq 0x3370
movq %rax, -0x480(%rbp)
jmp 0x40f7
movq -0x480(%rbp), %rdi
leaq 0x52c8(%rip), %rsi      # 0x93cd
callq 0x31f0
jmp 0x410c
jmp 0x410e
movq -0x10(%rbp), %rdi
movl -0x84(%rbp), %edx
leaq -0x40(%rbp), %rsi
callq 0x8710
movb %al, -0x481(%rbp)
jmp 0x4129
movb -0x481(%rbp), %al
testb $0x1, %al
jne 0x4135
jmp 0x4165
movq 0x7e8c(%rip), %rdi      # 0xbfc8
leaq -0x40(%rbp), %rsi
callq 0x31d0
movq %rax, -0x490(%rbp)
jmp 0x414e
movq -0x490(%rbp), %rdi
movq 0x7e5c(%rip), %rsi      # 0xbfb8
callq 0x3210
jmp 0x4163
jmp 0x4178
movb $0x0, -0x1(%rbp)
movl $0x1, -0x400(%rbp)
jmp 0x439f
jmp 0x417a
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x408b
jmp 0x4381
testb $0x1, -0x19(%rbp)
je 0x4204
movl $0x2, %edi
callq 0x76c0
movl %eax, -0x494(%rbp)
jmp 0x41ab
movl -0x494(%rbp), %eax
movl %eax, -0x404(%rbp)
movl -0x404(%rbp), %esi
movq 0x7e04(%rip), %rdi      # 0xbfc8
callq 0x3260
movq %rax, -0x4a0(%rbp)
jmp 0x41d2
movq -0x4a0(%rbp), %rdi
movl -0x88(%rbp), %esi
callq 0x3370
movq %rax, -0x4a8(%rbp)
jmp 0x41ed
movq -0x4a8(%rbp), %rdi
leaq 0x51d2(%rip), %rsi      # 0x93cd
callq 0x31f0
jmp 0x4202
jmp 0x4204
movq -0x10(%rbp), %rdi
movl -0x88(%rbp), %edx
leaq -0x40(%rbp), %rsi
callq 0x8710
movb %al, -0x4a9(%rbp)
jmp 0x421f
movb -0x4a9(%rbp), %al
testb $0x1, %al
jne 0x422b
jmp 0x425b
movq 0x7d96(%rip), %rdi      # 0xbfc8
leaq -0x40(%rbp), %rsi
callq 0x31d0
movq %rax, -0x4b8(%rbp)
jmp 0x4244
movq -0x4b8(%rbp), %rdi
movq 0x7d66(%rip), %rsi      # 0xbfb8
callq 0x3210
jmp 0x4259
jmp 0x426e
movb $0x0, -0x1(%rbp)
movl $0x1, -0x400(%rbp)
jmp 0x439f
jmp 0x4381
movl -0x88(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jg 0x437f
testb $0x1, -0x19(%rbp)
je 0x4302
movl $0x2, %edi
callq 0x76c0
movl %eax, -0x4bc(%rbp)
jmp 0x42a9
movl -0x4bc(%rbp), %eax
movl %eax, -0x408(%rbp)
movl -0x408(%rbp), %esi
movq 0x7d06(%rip), %rdi      # 0xbfc8
callq 0x3260
movq %rax, -0x4c8(%rbp)
jmp 0x42d0
movq -0x4c8(%rbp), %rdi
movl -0x84(%rbp), %esi
callq 0x3370
movq %rax, -0x4d0(%rbp)
jmp 0x42eb
movq -0x4d0(%rbp), %rdi
leaq 0x50d5(%rip), %rsi      # 0x93ce
callq 0x31f0
jmp 0x4300
jmp 0x4302
movq -0x10(%rbp), %rdi
movl -0x84(%rbp), %edx
leaq -0x40(%rbp), %rsi
callq 0x8710
movb %al, -0x4d1(%rbp)
jmp 0x431d
movb -0x4d1(%rbp), %al
testb $0x1, %al
jne 0x4329
jmp 0x4359
movq 0x7c98(%rip), %rdi      # 0xbfc8
leaq -0x40(%rbp), %rsi
callq 0x31d0
movq %rax, -0x4e0(%rbp)
jmp 0x4342
movq -0x4e0(%rbp), %rdi
movq 0x7c68(%rip), %rsi      # 0xbfb8
callq 0x3210
jmp 0x4357
jmp 0x4369
movb $0x0, -0x1(%rbp)
movl $0x1, -0x400(%rbp)
jmp 0x439f
jmp 0x436b
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x427f
jmp 0x4381
movb $0x1, -0x1(%rbp)
movl $0x1, -0x400(%rbp)
jmp 0x439f
movb $0x0, -0x1(%rbp)
movl $0x1, -0x400(%rbp)
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 $0x4e0, %rsp            # imm = 0x4E0
popq %rbp
retq
leaq -0x80(%rbp), %rdi
callq 0x3160
leaq -0x60(%rbp), %rdi
callq 0x3160
leaq -0x40(%rbp), %rdi
callq 0x3160
movq -0xd0(%rbp), %rdi
callq 0x3380
nop