// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0x1, -0x98(%rbp)
cmpl $0x0, -0x8(%rbp)
je 0x9933
cmpl $0x0, -0xc(%rbp)
jge 0x9933
movl $0x0, -0x4(%rbp)
jmp 0x9d7c
leaq 0x2cf2(%rip), %rdi      # 0xc62c
callq 0x9d90
movl %eax, -0x78(%rbp)
cmpl $0x1, %eax
je 0x9952
movl -0x78(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x9d7c
leaq -0x70(%rbp), %rdi
leaq 0x7133(%rip), %rsi      # 0x10a90
movl $0x50, %edx
callq 0x2070
leaq 0x7122(%rip), %rdi      # 0x10a90
leaq 0x716b(%rip), %rsi      # 0x10ae0
movl $0x50, %edx
callq 0x2070
leaq 0x2cb5(%rip), %rdi      # 0xc63b
callq 0x9d90
movl %eax, -0x78(%rbp)
cmpl $0x2, %eax
jne 0x999e
movl -0x78(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x9d7c
leaq 0x713b(%rip), %rdi      # 0x10ae0
leaq 0x70e4(%rip), %rsi      # 0x10a90
movl $0x50, %edx
callq 0x2070
leaq -0x70(%rbp), %rsi
leaq 0x70cf(%rip), %rdi      # 0x10a90
movl $0x50, %edx
callq 0x2070
leaq 0x70be(%rip), %rdi      # 0x10a90
callq 0x20e0
movl %eax, -0x7c(%rbp)
leaq 0x70ff(%rip), %rdi      # 0x10ae0
callq 0x20e0
movl %eax, -0x80(%rbp)
movl -0x7c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x7097(%rip), %rax      # 0x10a90
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x8c(%rbp)
movl $0x0, -0x90(%rbp)
leaq -0x70(%rbp), %rdi
leaq 0x2c1e(%rip), %rsi      # 0xc641
leaq 0x7066(%rip), %rdx      # 0x10a90
leaq 0x70af(%rip), %rcx      # 0x10ae0
movb $0x0, %al
callq 0x2270
leaq 0x7019(%rip), %rax      # 0x10a58
movq (%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x18(%rbp)
leaq 0x7007(%rip), %rax      # 0x10a58
movq (%rax), %rax
movq 0x20(%rax), %rax
movl %eax, -0x94(%rbp)
movl $0x0, -0x84(%rbp)
movl $0x0, -0x88(%rbp)
cmpl $0x0, -0x8(%rbp)
je 0x9a8b
movl -0xc(%rbp), %ecx
xorl %eax, %eax
cmpl -0x88(%rbp), %ecx
movb %al, -0x99(%rbp)
jle 0x9ab8
movb $0x1, %al
cmpl $0x0, -0x8c(%rbp)
movb %al, -0x9a(%rbp)
je 0x9aac
cmpl $0x0, -0x90(%rbp)
sete %al
movb %al, -0x9a(%rbp)
movb -0x9a(%rbp), %al
movb %al, -0x99(%rbp)
movb -0x99(%rbp), %al
testb $0x1, %al
jne 0x9ac7
jmp 0x9d61
leaq 0x6fc2(%rip), %rdi      # 0x10a90
movl $0x1, %esi
callq 0x9e90
cmpl $0x1, %eax
je 0x9ae2
jmp 0x9d61
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
leaq 0x6f60(%rip), %rax      # 0x10a58
movq (%rax), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
leaq 0x6f4f(%rip), %rcx      # 0x10a58
movq (%rcx), %rcx
movq 0x8(%rcx), %rcx
cmpq 0x28(%rcx), %rax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x90(%rbp)
cmpl $0x1, -0x98(%rbp)
jne 0x9ca5
leaq -0x70(%rbp), %rdi
movb $0x0, %al
callq 0x56d0
xorl %eax, %eax
movb %al, -0xa1(%rbp)
callq 0x46b0
movb -0xa1(%rbp), %al
leaq 0x6d24(%rip), %rcx      # 0x10878
movq 0x20(%rcx), %rcx
callq *%rcx
movl %eax, %ecx
movb -0xa1(%rbp), %al
movb %cl, -0x1a(%rbp)
leaq 0x2d6d(%rip), %rdi      # 0xc8d9
callq 0x56d0
movsbl -0x1a(%rbp), %eax
movl %eax, -0xa0(%rbp)
subl $0x7, %eax
je 0x9c5e
jmp 0x9b86
movl -0xa0(%rbp), %eax
subl $0xd, %eax
je 0x9c78
jmp 0x9b97
movl -0xa0(%rbp), %eax
subl $0x20, %eax
je 0x9bfe
jmp 0x9ba4
movl -0xa0(%rbp), %eax
subl $0x21, %eax
je 0x9c14
jmp 0x9bb1
movl -0xa0(%rbp), %eax
subl $0x2e, %eax
je 0x9c23
jmp 0x9bbe
movl -0xa0(%rbp), %eax
subl $0x3f, %eax
je 0x9c90
jmp 0x9bcf
movl -0xa0(%rbp), %eax
subl $0x6e, %eax
je 0x9c03
jmp 0x9bdc
movl -0xa0(%rbp), %eax
subl $0x71, %eax
je 0x9c78
jmp 0x9bed
movl -0xa0(%rbp), %eax
subl $0x79, %eax
jne 0x9c84
jmp 0x9bfe
jmp 0x9ca3
xorl %edi, %edi
movl $0x1, %esi
callq 0x25b0
jmp 0x9a72
movl $0x0, -0x98(%rbp)
jmp 0x9ca3
movq -0x18(%rbp), %rcx
leaq 0x6e2a(%rip), %rax      # 0x10a58
movq (%rax), %rax
movq %rcx, 0x18(%rax)
movslq -0x94(%rbp), %rcx
leaq 0x6e15(%rip), %rax      # 0x10a58
movq (%rax), %rax
movq %rcx, 0x20(%rax)
leaq 0x6e07(%rip), %rax      # 0x10a58
movq (%rax), %rax
movsbl 0x3b(%rax), %ecx
orl $0x2, %ecx
movb %cl, 0x3b(%rax)
leaq 0x29f6(%rip), %rdi      # 0xc65b
movb $0x0, %al
callq 0x56d0
movl $0x0, -0x4(%rbp)
jmp 0x9d7c
movl $0x1, -0x4(%rbp)
jmp 0x9d7c
leaq 0x6bed(%rip), %rcx      # 0x10878
movb $0x0, %al
callq *0x50(%rcx)
leaq 0x29cd(%rip), %rdi      # 0xc664
movb $0x0, %al
callq 0x56d0
jmp 0x9b3a
jmp 0x9ca5
movl -0x7c(%rbp), %edi
xorl %esi, %esi
callq 0x7380
cmpl $0x1, %eax
je 0x9cce
leaq 0x29db(%rip), %rdi      # 0xc696
movb $0x0, %al
callq 0x56d0
movl $0x0, -0x4(%rbp)
jmp 0x9d7c
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x80(%rbp), %eax
jge 0x9d4d
movslq -0x74(%rbp), %rcx
leaq 0x6df8(%rip), %rax      # 0x10ae0
movb (%rax,%rcx), %al
movb %al, -0x19(%rbp)
movsbl -0x19(%rbp), %eax
cmpl $0xa, %eax
jne 0x9d06
movb $0x0, %al
callq 0x7170
movl %eax, -0xa8(%rbp)
jmp 0x9d1a
movsbl -0x19(%rbp), %esi
movl $0x1, %edi
callq 0x6d80
movl %eax, -0xa8(%rbp)
movl -0xa8(%rbp), %eax
movl %eax, -0x78(%rbp)
cmpl $0x1, -0x78(%rbp)
je 0x9d40
leaq 0x297c(%rip), %rdi      # 0xc6ac
movb $0x0, %al
callq 0x56d0
movl $0x0, -0x4(%rbp)
jmp 0x9d7c
jmp 0x9d42
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x9cd5
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x9a72
movl -0x84(%rbp), %esi
leaq 0x295c(%rip), %rdi      # 0xc6ca
movb $0x0, %al
callq 0x56d0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax,%rax)