// File: /workspace/github_c_makefile_star2_O3/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 0x71e1
movl %esi, %ebx
movl %edi, %ebp
leaq 0x265e(%rip), %rdi      # 0x94c0
callq 0x71ff
movl %eax, %r14d
cmpl $0x1, %eax
jne 0x71e1
leaq 0x6c06(%rip), %r13      # 0xda80
leaq 0x40(%rsp), %rdi
movl $0x50, %edx
movq %r13, %rsi
callq 0x2070
leaq 0x6c3d(%rip), %rsi      # 0xdad0
movl $0x50, %edx
movq %r13, %rdi
callq 0x2070
leaq 0x2628(%rip), %rdi      # 0x94cf
callq 0x71ff
movl $0x2, %r14d
cmpl $0x2, %eax
je 0x71e1
testl %ebp, %ebp
setne 0x18(%rsp)
leaq 0x6c07(%rip), %r15      # 0xdad0
movl $0x50, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x2070
leaq 0x40(%rsp), %r12
movl $0x50, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x2070
movq %r13, %rdi
callq 0x20d0
movq %rax, %r14
movq %r15, %rdi
callq 0x20d0
movq %rax, %r15
movq %r14, 0x38(%rsp)
leal -0x1(%r14), %eax
cltq
movb (%r13,%rax), %al
movb %al, 0xb(%rsp)
leaq 0x25b6(%rip), %rsi      # 0x94d5
xorl %r14d, %r14d
movq %r12, %rdi
movq %r13, %rdx
leaq 0x6ba1(%rip), %rcx      # 0xdad0
xorl %eax, %eax
callq 0x2250
testl %ebx, %ebx
setle %al
testb %al, 0x18(%rsp)
je 0x6f5d
leaq 0x2616(%rip), %rdi      # 0x955e
movl %r14d, %esi
xorl %eax, %eax
callq 0x4246
movl $0x1, %r14d
jmp 0x71e1
leaq 0x6ae4(%rip), %rax      # 0xda48
movq (%rax), %rax
movq 0x18(%rax), %rcx
movq %rcx, 0x28(%rsp)
movq 0x20(%rax), %rax
movq %rax, 0x30(%rsp)
movl %r15d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x18(%rsp)
movl $0x1, %eax
leaq 0x6aee(%rip), %rdi      # 0xda80
xorl %r13d, %r13d
xorl %r14d, %r14d
cmpl $0x1, %eax
sete %r12b
movl $0x1, %esi
callq 0x7294
testl %eax, %eax
je 0x6f41
incl %r13d
leaq 0x6a91(%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, 0x20(%rsp)
testb $0x1, %r12b
je 0x7197
movl $0x0, 0xc(%rsp)
leaq 0x40(%rsp), %rdi
xorl %eax, %eax
callq 0x4246
xorl %eax, %eax
callq 0x370f
xorl %eax, %eax
leaq 0x6867(%rip), %rcx      # 0xd868
callq *0x20(%rcx)
movl %eax, %r12d
leaq 0x279c(%rip), %rdi      # 0x97aa
xorl %eax, %eax
callq 0x4246
movsbl %r12b, %eax
cmpl $0x3e, %eax
jle 0x7031
cmpl $0x6d, %eax
jg 0x7055
leaq 0x24ce(%rip), %rdi      # 0x94f8
cmpl $0x3f, %eax
je 0x6fea
jmp 0x7083
cmpl $0x1f, %eax
jle 0x7071
movq %r15, %r12
cmpl $0x20, %eax
je 0x70f2
cmpl $0x21, %eax
je 0x70fa
cmpl $0x2e, %eax
jne 0x7083
jmp 0x71b0
cmpl $0x79, %eax
movq %r15, %r12
je 0x70f2
cmpl $0x71, %eax
je 0x6f52
cmpl $0x6e, %eax
jne 0x7083
jmp 0x709b
cmpl $0x7, %eax
je 0x71d0
cmpl $0xd, %eax
je 0x6f52
xorl %eax, %eax
leaq 0x67dc(%rip), %rcx      # 0xd868
callq *0x50(%rcx)
leaq 0x2462(%rip), %rdi      # 0x94f8
jmp 0x6fea
movq 0x20(%rsp), %rax
cmpq %rax, 0x10(%rsp)
sete 0xc(%rsp)
cmpb $0xa, 0xb(%rsp)
sete 0x10(%rsp)
testl %ebp, %ebp
setne %r12b
xorl %edi, %edi
movl $0x1, %esi
callq 0x2470
cmpl %ebx, %r13d
setge %al
testb %al, %r12b
jne 0x6f41
movb $0x1, %r12b
movb 0x10(%rsp), %al
andb 0xc(%rsp), %al
leaq 0x6999(%rip), %rdi      # 0xda80
je 0x6f9f
jmp 0x6f41
movl $0x1, 0xc(%rsp)
movq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x5353
cmpl $0x1, %eax
jne 0x71f6
testl %r12d, %r12d
movl %r14d, %esi
jle 0x7150
xorl %r12d, %r12d
leaq 0x69af(%rip), %rax      # 0xdad0
movsbl (%rax,%r12), %esi
cmpl $0xa, %esi
jne 0x7134
xorl %eax, %eax
callq 0x524f
jmp 0x713e
movl $0x1, %edi
callq 0x5020
cmpl $0x1, %eax
jne 0x71a7
movl %r14d, %esi
incq %r12
cmpq %r12, 0x18(%rsp)
jne 0x711a
movq 0x20(%rsp), %rax
cmpq %rax, 0x10(%rsp)
sete %al
cmpb $0xa, 0xb(%rsp)
sete %cl
testl %ebp, %ebp
setne %dl
incl %esi
movl %esi, %r14d
cmpl %ebx, %r13d
setge %sil
testb %sil, %dl
jne 0x6f41
andb %al, %cl
leaq 0x68f8(%rip), %rdi      # 0xda80
movl 0xc(%rsp), %eax
je 0x6f98
jmp 0x6f41
movl $0x0, 0xc(%rsp)
movq %r15, %r12
jmp 0x70fa
leaq 0x2392(%rip), %rdi      # 0x9540
jmp 0x71d7
leaq 0x6891(%rip), %rax      # 0xda48
movq (%rax), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rax)
movslq 0x30(%rsp), %rcx
movq %rcx, 0x20(%rax)
orb $0x2, 0x3b(%rax)
leaq 0x2318(%rip), %rdi      # 0x94ef
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x4246
movl %r14d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x232d(%rip), %rdi      # 0x952a
jmp 0x71d7