// File: /workspace/github_c_makefile_star2_O3/ThomasMadappattu[P]Sandmacs/search.c
// Function: replaces
int replaces (int kind, 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) */

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

  /* ask the user for the text of a pattern */
  if ((s = readpattern ((kind == FALSE ? "Replace" : "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 */
  strncpy (tpat, "Replace '", 10);
  expandp (&pat[0], &tpat[strlen (tpat)], NPAT / 3);
  strncat (tpat, "' with '", 9);
  expandp (&rpat[0], &tpat[strlen (tpat)], NPAT / 3);
  strncat (tpat, "'? ", 4);

  /* 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);

      /* check for query */
      if (kind)
	{
	  /* get the query */
	  mlwrite (&tpat[0], &pat[0], &rpat[0]);
	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 */
	      kind = 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,RET,q)Abort, (.)Abort back, (?)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 $0xa8, %rsp
testl %esi, %esi
setne %al
testl %edx, %edx
sets %cl
xorl %r12d, %r12d
testb %cl, %al
jne 0x7021
movl %edx, %ebx
movl %esi, %ebp
movl %edi, %r14d
testl %edi, %edi
leaq 0x286e(%rip), %rax      # 0x94be
leaq 0x286f(%rip), %rdi      # 0x94c6
cmoveq %rax, %rdi
callq 0x6794
movl %eax, %r12d
cmpl $0x1, %eax
jne 0x7021
leaq 0x6c1d(%rip), %r15      # 0xd890
leaq 0x50(%rsp), %rdi
movl $0x50, %edx
movq %r15, %rsi
callq 0x2060
leaq 0x6c54(%rip), %rsi      # 0xd8e0
movl $0x50, %edx
movq %r15, %rdi
callq 0x2060
leaq 0x2834(%rip), %rdi      # 0x94d4
callq 0x6794
movl $0x2, %r12d
cmpl $0x2, %eax
je 0x7021
movl %r14d, 0x14(%rsp)
testl %ebp, %ebp
setne 0x28(%rsp)
leaq 0x6c19(%rip), %r14      # 0xd8e0
movl $0x50, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2060
leaq 0x50(%rsp), %r12
movl $0x50, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2060
movq %r15, %rdi
callq 0x20c0
movq %rax, %r13
movq %r14, %rdi
callq 0x20c0
movq %rax, %r14
movq %r13, 0x48(%rsp)
leal -0x1(%r13), %eax
cltq
movb (%r15,%rax), %al
movb %al, 0xf(%rsp)
movabsq $0x206563616c706552, %rax # imm = 0x206563616C706552
movq %rax, (%r12)
movw $0x27, 0x8(%r12)
movq %r12, %rdi
callq 0x20c0
leaq (%rsp,%rax), %rsi
addq $0x50, %rsi
movq %r15, %rdi
movl $0x1a, %edx
callq 0x6bae
movq %r12, %rdi
callq 0x20c0
movabsq $0x2720687469772027, %rcx # imm = 0x2720687469772027
movq %rcx, 0x50(%rsp,%rax)
movb $0x0, 0x58(%rsp,%rax)
movq %r12, %rdi
callq 0x20c0
leaq (%rsp,%rax), %rsi
addq $0x50, %rsi
leaq 0x6b65(%rip), %rdi      # 0xd8e0
movl $0x1a, %edx
callq 0x6bae
movq %r12, %rdi
callq 0x20c0
movl $0x203f27, 0x50(%rsp,%rax) # imm = 0x203F27
testl %ebx, %ebx
setle %al
xorl %r13d, %r13d
testb %al, 0x28(%rsp)
je 0x6dbf
leaq 0x27c6(%rip), %rdi      # 0x9570
movl %r13d, %esi
xorl %eax, %eax
callq 0x4352
movl $0x1, %r12d
jmp 0x7021
leaq 0x6a92(%rip), %rax      # 0xd858
movq (%rax), %rax
movq 0x18(%rax), %rcx
movq %rcx, 0x30(%rsp)
movq 0x20(%rax), %rax
movq %rax, 0x38(%rsp)
movl %r14d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x28(%rsp)
leaq 0x69a1(%rip), %r12      # 0xd790
xorl %r15d, %r15d
xorl %r13d, %r13d
movq %r14, 0x40(%rsp)
movl 0x14(%rsp), %r14d
movl %r13d, 0x10(%rsp)
leaq 0x6a85(%rip), %rdi      # 0xd890
movl $0x1, %esi
callq 0x684a
testl %eax, %eax
je 0x6da3
incl %r15d
leaq 0x6a35(%rip), %rax      # 0xd858
movq (%rax), %rax
movq 0x8(%rax), %rcx
movq 0x18(%rax), %rax
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x28(%rcx), %rax
movq %rax, 0x18(%rsp)
testl %r14d, %r14d
je 0x6f4c
leaq 0x50(%rsp), %rdi
leaq 0x6a3c(%rip), %rsi      # 0xd890
leaq 0x6a85(%rip), %rdx      # 0xd8e0
xorl %eax, %eax
callq 0x4352
xorl %eax, %eax
callq 0x3493
xorl %eax, %eax
callq *0x20(%r12)
movl %eax, %r13d
leaq 0x27c2(%rip), %rdi      # 0x963c
xorl %eax, %eax
callq 0x4352
movsbl %r13b, %eax
cmpl $0x3e, %eax
jle 0x6e96
cmpl $0x6d, %eax
jg 0x6eb7
cmpl $0x3f, %eax
je 0x6ee9
jmp 0x6ee2
cmpl $0x1f, %eax
jle 0x6ed0
cmpl $0x20, %eax
je 0x6f4f
cmpl $0x21, %eax
je 0x6f4c
cmpl $0x2e, %eax
jne 0x6ee2
jmp 0x6ff0
cmpl $0x79, %eax
je 0x6f4f
cmpl $0x71, %eax
je 0x6db4
cmpl $0x6e, %eax
jne 0x6ee2
jmp 0x6efc
cmpl $0x7, %eax
je 0x7010
cmpl $0xd, %eax
je 0x6db4
xorl %eax, %eax
callq *0x50(%r12)
leaq 0x2605(%rip), %rdi      # 0x94f5
xorl %eax, %eax
callq 0x4352
jmp 0x6e62
movq 0x18(%rsp), %rax
cmpq %rax, 0x20(%rsp)
sete 0x18(%rsp)
cmpb $0xa, 0xf(%rsp)
sete %r13b
testl %ebp, %ebp
setne 0x20(%rsp)
xorl %edi, %edi
movl $0x1, %esi
callq 0x23d0
cmpl %ebx, %r15d
setge %al
testb %al, 0x20(%rsp)
jne 0x703f
andb 0x18(%rsp), %r13b
movl 0x10(%rsp), %r13d
je 0x6e04
jmp 0x6da3
xorl %r14d, %r14d
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x53f3
cmpl $0x1, %eax
jne 0x7036
movq 0x40(%rsp), %rdi
testl %edi, %edi
jle 0x6fa3
xorl %r13d, %r13d
leaq 0x6969(%rip), %rax      # 0xd8e0
movsbl (%rax,%r13), %esi
cmpl $0xa, %esi
jne 0x6f8a
xorl %eax, %eax
callq 0x52ef
jmp 0x6f94
movl $0x1, %edi
callq 0x50c0
cmpl $0x1, %eax
jne 0x6fe7
incq %r13
cmpq %r13, 0x28(%rsp)
jne 0x6f70
movq 0x18(%rsp), %rax
cmpq %rax, 0x20(%rsp)
sete %al
cmpb $0xa, 0xf(%rsp)
sete %cl
testl %ebp, %ebp
setne %dl
movl 0x10(%rsp), %r13d
incl %r13d
cmpl %ebx, %r15d
setge %sil
testb %sil, %dl
jne 0x6da3
movl %r14d, 0x14(%rsp)
andb %al, %cl
je 0x6dfa
jmp 0x6da3
leaq 0x2564(%rip), %rdi      # 0x9552
jmp 0x7017
leaq 0x6861(%rip), %rax      # 0xd858
movq (%rax), %rax
movq 0x30(%rsp), %rcx
movq %rcx, 0x18(%rax)
movslq 0x38(%rsp), %rcx
movq %rcx, 0x20(%rax)
orb $0x2, 0x3b(%rax)
leaq 0x24d5(%rip), %rdi      # 0x94ec
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x4352
movl %r12d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x24ff(%rip), %rdi      # 0x953c
jmp 0x7017
movl 0x10(%rsp), %r13d
jmp 0x6da3