// File: /workspace/github_c_makefile_star2_O1/hughbarney[P]pEmacs/search.c
// Function: qreplace
int qreplace (int f, int n)
{
  LINE *origline;		/* original "." position */
  char tmpc;			/* temporary character */
  char c;			/* input char for query */
  char tpat[NPAT];		/* temporary to hold search pattern */
  int i;			/* loop index */
  int s;			/* success flag on pattern inputs */
  int slength, rlength;		/* length of search and replace strings */
  int numsub;			/* number of substitutions */
  int nummatch;			/* number of found matches */
  int nlflag;			/* last char of search string a <NL>? */
  int nlrepl;			/* was a replace done on the last line? */
  int origoff;			/* and offset (for . query option) */
  int ask = TRUE;

  /* check for negative repititions */
  if (f && n < 0)
    return (FALSE);

  /* ask the user for the text of a pattern */
  if ((s = readpattern ("Query replace ")) != TRUE)
    return (s);
  strncpy (&tpat[0], &pat[0], NPAT); /* salt it away */

  /* ask for the replacement string */
  strncpy (&pat[0], &rpat[0], NPAT); /* set up default string */
  if ((s = readpattern ("with ")) == ABORT)
    return (s);

  /* move everything to the right place and length them */
  strncpy (&rpat[0], &pat[0], NPAT);
  strncpy (&pat[0], &tpat[0], NPAT);
  slength = strlen (&pat[0]);
  rlength = strlen (&rpat[0]);

  /* set up flags so we can make sure not to do a recursive replace on the
   * last line */
  nlflag = (pat[slength - 1] == '\n');
  nlrepl = FALSE;

  /* build query replace question string */
  sprintf(tpat, "Replace '%s' with '%s' ? ", pat, rpat);

  /* save original . position */
  origline = curwp->w_dotp;
  origoff = curwp->w_doto;

  /* scan through the file */
  numsub = 0;
  nummatch = 0;
  while ((f == FALSE || n > nummatch) &&
	 (nlflag == FALSE || nlrepl == FALSE))
    {
      /* search for the pattern */
      if (forscan (&pat[0], PTBEG) != TRUE)
	break;			/* all done */
      ++nummatch;		/* increment # of matches */

      /* check if we are on the last line */
      nlrepl = (lforw (curwp->w_dotp) == curwp->w_bufp->b_linep);

	  if (ask == TRUE) {

		/* get the query */
		mlwrite (tpat);
	  qprompt:
		update ();		/* show the proposed place to change */
		c = (*term.t_getchar) (); /* and input */
		mlwrite ("");		/* and clear it */

		/* and respond appropriately */
		switch (c)
		  {
		  case 'y':		/* yes, substitute */
		  case ' ':
			break;
			
		  case 'n':		/* no, onword */
			forwchar (FALSE, 1);
			continue;
			
		  case '!':		/* yes/stop asking */
			ask = FALSE;
			break;
			
		  case '.':		/* abort! and return */
			/* restore old position */
			curwp->w_dotp = origline;
			curwp->w_doto = origoff;
			curwp->w_flag |= WFMOVE;

		  case BELL:		/* abort! and stay */
			mlwrite ("Aborted!");
			return (FALSE);

		  case 0x0d:		/* controlled exit */
		  case 'q':
			return (TRUE);

		  default:		/* bitch and beep */
			(*term.t_beep) ();

		  case '?':		/* help me */
			mlwrite ("(Y)es, (N)o, (!)Do the rest, (^G)Abort, (?)Help: ");
			goto qprompt;
		  }
	  }
      /* delete the sucker */
      if (ldelete (slength, FALSE) != TRUE)
	{
	  /* error while deleting */
	  mlwrite ("ERROR while deleteing");
	  return (FALSE);
	}
      /* and insert its replacement */
      for (i = 0; i < rlength; i++)
	{
	  tmpc = rpat[i];
	  s = (tmpc == '\n' ? lnewline () : linsert (1, tmpc));
	  if (s != TRUE)
	    {
	      /* error while inserting */
	      mlwrite ("Out of memory while inserting");
	      return (FALSE);
	    }
	}

      numsub++;			/* increment # of substitutions */
    }

  /* and report the results */
  mlwrite ("%d substitutions", numsub);
  return (TRUE);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
testl %edi, %edi
setne %al
testl %esi, %esi
sets %cl
xorl %r14d, %r14d
testb %cl, %al
jne 0x718b
movl %esi, %ebx
movl %edi, %ebp
leaq 0x26d3(%rip), %rdi      # 0x94d8
callq 0x71b3
movl %eax, %r14d
cmpl $0x1, %eax
jne 0x718b
leaq 0x6c63(%rip), %r13      # 0xda80
leaq 0x40(%rsp), %rdi
movl $0x50, %edx
movq %r13, %rsi
callq 0x2070
leaq 0x6c9a(%rip), %rsi      # 0xdad0
movl $0x50, %edx
movq %r13, %rdi
callq 0x2070
leaq 0x269d(%rip), %rdi      # 0x94e7
callq 0x71b3
movl $0x2, %r14d
cmpl $0x2, %eax
je 0x718b
leaq 0x6c6b(%rip), %r14      # 0xdad0
movl $0x50, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x2070
leaq 0x40(%rsp), %r15
movl $0x50, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x2070
movq %r13, %rdi
callq 0x20d0
movq %rax, %r12
movq %r14, %rdi
callq 0x20d0
movq %rax, %r14
movq %r12, 0x30(%rsp)
leal -0x1(%r12), %eax
cltq
movb (%r13,%rax), %al
movb %al, 0x7(%rsp)
leaq 0x2631(%rip), %rsi      # 0x94ed
xorl %r12d, %r12d
movq %r15, %rdi
movq %r13, %rdx
leaq 0x6c04(%rip), %rcx      # 0xdad0
xorl %eax, %eax
callq 0x2250
testl %ebp, %ebp
setne %al
testl %ebx, %ebx
setle %cl
testb %cl, %al
je 0x6efd
leaq 0x268e(%rip), %rdi      # 0x9576
movl %r12d, %esi
xorl %eax, %eax
callq 0x43cc
movl $0x1, %r14d
jmp 0x718b
leaq 0x6b44(%rip), %rax      # 0xda48
movq (%rax), %rax
movq 0x18(%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x20(%rax), %rax
movq %rax, 0x28(%rsp)
movl %r14d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x38(%rsp)
movl $0x1, %eax
leaq 0x6b4e(%rip), %rdi      # 0xda80
leaq 0x692f(%rip), %r15      # 0xd868
xorl %r13d, %r13d
xorl %r12d, %r12d
movl %r12d, 0xc(%rsp)
cmpl $0x1, %eax
sete %r12b
movl $0x1, %esi
callq 0x7248
testl %eax, %eax
je 0x71a0
incl %r13d
leaq 0x6ae1(%rip), %rax      # 0xda48
movq (%rax), %rax
movq 0x8(%rax), %rcx
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rcx), %rax
movq %rax, 0x18(%rsp)
testb $0x1, %r12b
je 0x7141
movl $0x0, 0x8(%rsp)
leaq 0x40(%rsp), %rdi
xorl %eax, %eax
callq 0x43cc
xorl %eax, %eax
callq 0x3705
xorl %eax, %eax
callq *0x20(%r15)
movl %eax, %r12d
leaq 0x280a(%rip), %rdi      # 0x97c2
xorl %eax, %eax
callq 0x43cc
movsbl %r12b, %eax
cmpl $0x3e, %eax
jle 0x6fdb
cmpl $0x6d, %eax
jg 0x6fff
leaq 0x253c(%rip), %rdi      # 0x9510
cmpl $0x3f, %eax
je 0x6f9a
jmp 0x702d
cmpl $0x1f, %eax
jle 0x701b
movq %r14, %r12
cmpl $0x20, %eax
je 0x7096
cmpl $0x21, %eax
je 0x709e
cmpl $0x2e, %eax
jne 0x702d
jmp 0x715a
cmpl $0x79, %eax
movq %r14, %r12
je 0x7096
cmpl $0x71, %eax
je 0x6ef2
cmpl $0x6e, %eax
jne 0x702d
jmp 0x703f
cmpl $0x7, %eax
je 0x717a
cmpl $0xd, %eax
je 0x6ef2
xorl %eax, %eax
callq *0x50(%r15)
leaq 0x24d6(%rip), %rdi      # 0x9510
jmp 0x6f9a
movq 0x18(%rsp), %rax
cmpq %rax, 0x10(%rsp)
sete 0x8(%rsp)
testl %ebp, %ebp
setne %r12b
cmpb $0xa, 0x7(%rsp)
sete 0x10(%rsp)
xorl %edi, %edi
movl $0x1, %esi
callq 0x2470
cmpl %ebx, %r13d
setge %al
testb %al, %r12b
jne 0x71a0
movb $0x1, %r12b
movb 0x10(%rsp), %al
andb 0x8(%rsp), %al
leaq 0x69f5(%rip), %rdi      # 0xda80
je 0x6f4b
jmp 0x71a0
movl $0x1, 0x8(%rsp)
movq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x548f
cmpl $0x1, %eax
jne 0x71aa
testl %r12d, %r12d
movl 0xc(%rsp), %esi
jle 0x70f6
xorl %r12d, %r12d
leaq 0x6a0a(%rip), %rax      # 0xdad0
movsbl (%rax,%r12), %esi
cmpl $0xa, %esi
jne 0x70d9
xorl %eax, %eax
callq 0x5390
jmp 0x70e3
movl $0x1, %edi
callq 0x516d
cmpl $0x1, %eax
movl 0xc(%rsp), %esi
jne 0x7151
incq %r12
cmpq %r12, 0x38(%rsp)
jne 0x70bf
movq 0x18(%rsp), %rax
cmpq %rax, 0x10(%rsp)
sete %al
testl %ebp, %ebp
setne %dl
movb 0x7(%rsp), %dil
cmpb $0xa, %dil
sete %cl
incl %esi
movl %esi, %r12d
cmpl %ebx, %r13d
setge %sil
testb %sil, %dl
jne 0x6ee1
andb %al, %cl
leaq 0x694e(%rip), %rdi      # 0xda80
movl 0x8(%rsp), %eax
je 0x6f3f
jmp 0x6ee1
movl $0x0, 0x8(%rsp)
movq %r14, %r12
jmp 0x709e
leaq 0x2400(%rip), %rdi      # 0x9558
jmp 0x7181
leaq 0x68e7(%rip), %rax      # 0xda48
movq (%rax), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x18(%rax)
movslq 0x28(%rsp), %rcx
movq %rcx, 0x20(%rax)
orb $0x2, 0x3b(%rax)
leaq 0x2386(%rip), %rdi      # 0x9507
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x43cc
movl %r14d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%rsp), %r12d
jmp 0x6ee1
leaq 0x2391(%rip), %rdi      # 0x9542
jmp 0x7181