// File: /workspace/github_c_makefile_star2_O2/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 0x67cd
movl %esi, %ebx
movl %edi, %ebp
leaq 0x1ff5(%rip), %rdi      # 0x84d8
callq 0x67eb
movl %eax, %r14d
cmpl $0x1, %eax
jne 0x67cd
leaq 0x6575(%rip), %r15      # 0xca70
leaq 0x40(%rsp), %r12
pushq $0x50
popq %r14
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2070
leaq 0x65a7(%rip), %r13      # 0xcac0
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x2070
leaq 0x1fb9(%rip), %rdi      # 0x84e7
callq 0x67eb
cmpl $0x2, %eax
jne 0x653f
pushq $0x2
jmp 0x67cb
pushq $0x50
popq %r14
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2070
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2070
movq %r15, %rdi
callq 0x20d0
movq %rax, %r14
movq %r13, %rdi
callq 0x20d0
movq %r13, %rcx
movq %rax, %r13
movq %r14, 0x38(%rsp)
leal -0x1(%r14), %eax
cltq
movb (%r15,%rax), %al
movb %al, 0xb(%rsp)
leaq 0x1f5b(%rip), %rsi      # 0x84ed
xorl %r14d, %r14d
movq %r12, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2240
leaq 0x648f(%rip), %rax      # 0xca38
movq (%rax), %rax
movq 0x18(%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x20(%rax), %rax
movq %rax, 0x28(%rsp)
testl %r13d, %r13d
cmovlel %r14d, %r13d
pushq $0x1
popq %r15
leaq 0x1f40(%rip), %r12      # 0x8510
movl $0x0, 0xc(%rsp)
movl $0x0, 0x14(%rsp)
movq $0x0, 0x18(%rsp)
movq %r13, 0x30(%rsp)
cmpl $0x1, %r15d
sete %r14b
cmpb $0xa, 0xb(%rsp)
sete %al
testl %ebp, %ebp
setne %cl
cmpl %ebx, 0x14(%rsp)
setge %dl
testb %dl, %cl
jne 0x67b7
andb 0x18(%rsp), %al
jne 0x67b7
leaq 0x644d(%rip), %rdi      # 0xca70
pushq $0x1
popq %rsi
callq 0x687e
testl %eax, %eax
je 0x67b7
movq %r12, %r13
incl 0x14(%rsp)
leaq 0x63f7(%rip), %rax      # 0xca38
movq (%rax), %rax
movq 0x8(%rax), %rcx
movq 0x18(%rax), %rax
movq (%rax), %rax
cmpq 0x28(%rcx), %rax
sete %al
movl $0x0, 0x10(%rsp)
testb $0x1, %r14b
movq %rax, 0x18(%rsp)
je 0x671c
leaq 0x40(%rsp), %rdi
leaq 0x61df(%rip), %r15      # 0xc858
movq %r13, %r12
xorl %eax, %eax
callq 0x3dec
xorl %eax, %eax
callq 0x34fc
xorl %eax, %eax
callq *0x20(%r15)
movl %eax, %r14d
leaq 0x2128(%rip), %rdi      # 0x87c2
xorl %eax, %eax
callq 0x3dec
movq %r12, %rdi
cmpb $0x3f, %r14b
je 0x667c
movsbl %r14b, %eax
cmpl $0x7, %eax
je 0x67a4
cmpl $0xd, %eax
leaq 0x6197(%rip), %r14      # 0xc858
je 0x67c9
cmpl $0x20, %eax
je 0x6713
cmpl $0x21, %eax
je 0x671c
cmpl $0x2e, %eax
je 0x6784
cmpl $0x79, %eax
je 0x6713
cmpl $0x71, %eax
je 0x67c9
movq %r13, %r12
cmpl $0x6e, %eax
je 0x6701
xorl %eax, %eax
callq *0x50(%r14)
movq %r12, %rdi
movq %r14, %r15
jmp 0x667c
xorl %edi, %edi
pushq $0x1
popq %rsi
callq 0x2453
movb $0x1, %r14b
jmp 0x65f6
pushq $0x1
popq %r15
movl %r15d, 0x10(%rsp)
movq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x4d58
cmpl $0x1, %eax
jne 0x67e2
xorl %r14d, %r14d
movq %r13, %r12
movq 0x30(%rsp), %r13
cmpq %r14, %r13
je 0x676d
leaq 0x6378(%rip), %rax      # 0xcac0
movsbl (%rax,%r14), %esi
cmpl $0xa, %esi
jne 0x675b
xorl %eax, %eax
callq 0x4c76
jmp 0x6763
pushq $0x1
popq %rdi
callq 0x4a7c
incq %r14
cmpl $0x1, %eax
je 0x673c
jmp 0x677b
incl 0xc(%rsp)
movl 0x10(%rsp), %r15d
jmp 0x65ee
leaq 0x1dd6(%rip), %rdi      # 0x8558
jmp 0x67ab
leaq 0x62ad(%rip), %rax      # 0xca38
movq (%rax), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x18(%rax)
movslq 0x28(%rsp), %rcx
movq %rcx, 0x20(%rax)
orb $0x2, 0x3b(%rax)
leaq 0x1d5c(%rip), %rdi      # 0x8507
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x3dec
jmp 0x67cd
leaq 0x1db8(%rip), %rdi      # 0x8576
movl 0xc(%rsp), %esi
xorl %eax, %eax
callq 0x3dec
pushq $0x1
popq %r14
movl %r14d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d59(%rip), %rdi      # 0x8542
jmp 0x67ab