// File: /workspace/github_c_makefile_star2_O2/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 $0x98, %rsp
testl %esi, %esi
setne %al
testl %edx, %edx
sets %cl
xorl %r14d, %r14d
testb %cl, %al
jne 0x682f
movl %edx, %ebx
movl %esi, %ebp
movl %edi, %r13d
testl %edi, %edi
leaq 0x1fcb(%rip), %rax      # 0x84be
leaq 0x1fcc(%rip), %rdi      # 0x84c6
cmoveq %rax, %rdi
callq 0x605d
movl %eax, %r14d
cmpl $0x1, %eax
jne 0x682f
leaq 0x636a(%rip), %r15      # 0xc880
leaq 0x40(%rsp), %rdi
pushq $0x50
popq %r14
movq %r15, %rsi
movq %r14, %rdx
callq 0x2060
leaq 0x639f(%rip), %r12      # 0xc8d0
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2060
leaq 0x1f8e(%rip), %rdi      # 0x84d4
callq 0x605d
cmpl $0x2, %eax
jne 0x6559
pushq $0x2
popq %r14
jmp 0x682f
pushq $0x50
popq %r14
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2060
leaq 0x40(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x2060
movq %r15, %rdi
callq 0x20c0
movq %rax, %r14
leaq 0x6340(%rip), %rdi      # 0xc8d0
callq 0x20c0
movl %r13d, 0x10(%rsp)
movq %rax, %r13
movq %r14, 0x30(%rsp)
leal -0x1(%r14), %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 $0x40, %rsi
pushq $0x1a
popq %r14
movq %r15, %rdi
movl %r14d, %edx
callq 0x6441
movq %r12, %rdi
callq 0x20c0
movabsq $0x2720687469772027, %rcx # imm = 0x2720687469772027
movq %rcx, 0x40(%rsp,%rax)
movb $0x0, 0x48(%rsp,%rax)
movq %r12, %rdi
callq 0x20c0
leaq (%rsp,%rax), %rsi
addq $0x40, %rsi
leaq 0x62b8(%rip), %rdi      # 0xc8d0
movl %r14d, %edx
callq 0x6441
movq %r12, %rdi
callq 0x20c0
movl $0x203f27, 0x40(%rsp,%rax) # imm = 0x203F27
leaq 0x6211(%rip), %rax      # 0xc848
movq (%rax), %rax
movq 0x18(%rax), %rcx
movq %rcx, 0x28(%rsp)
xorl %ecx, %ecx
testl %r13d, %r13d
cmovlel %ecx, %r13d
movq %r13, 0x38(%rsp)
movl 0x10(%rsp), %r13d
movq 0x20(%rax), %rax
movq %rax, 0x20(%rsp)
pushq $0x1
popq %r14
movl $0x0, 0x14(%rsp)
xorl %r12d, %r12d
movq $0x0, 0x18(%rsp)
cmpb $0xa, 0xf(%rsp)
sete %al
testl %ebp, %ebp
setne %cl
cmpl %ebx, %r12d
setge %dl
testb %dl, %cl
jne 0x6844
andb 0x18(%rsp), %al
jne 0x6844
leaq 0x61dd(%rip), %rdi      # 0xc880
movl %r14d, %esi
callq 0x6107
testl %eax, %eax
je 0x6844
incl %r12d
leaq 0x618b(%rip), %rax      # 0xc848
movq (%rax), %rax
movq 0x8(%rax), %rcx
movq 0x18(%rax), %rax
movq (%rax), %rax
cmpq 0x28(%rcx), %rax
sete %al
movl $0x0, 0x10(%rsp)
testl %r13d, %r13d
movq %rax, 0x18(%rsp)
je 0x678c
leaq 0x40(%rsp), %rdi
leaq 0x618c(%rip), %rsi      # 0xc880
leaq 0x61d5(%rip), %rdx      # 0xc8d0
xorl %eax, %eax
callq 0x3e83
xorl %eax, %eax
callq 0x3270
xorl %eax, %eax
leaq 0x606e(%rip), %rcx      # 0xc780
callq *0x20(%rcx)
movl %eax, %r15d
leaq 0x1f1d(%rip), %rdi      # 0x863c
xorl %eax, %eax
callq 0x3e83
cmpb $0x3f, %r15b
je 0x6774
movsbl %r15b, %eax
cmpl $0x7, %eax
je 0x681e
cmpl $0xd, %eax
je 0x682f
cmpl $0x20, %eax
je 0x6787
cmpl $0x21, %eax
je 0x678c
cmpl $0x2e, %eax
je 0x67fe
cmpl $0x79, %eax
je 0x6787
cmpl $0x71, %eax
je 0x682f
cmpl $0x6e, %eax
je 0x67e6
xorl %eax, %eax
leaq 0x600f(%rip), %rcx      # 0xc780
callq *0x50(%rcx)
leaq 0x1d7a(%rip), %rdi      # 0x84f5
xorl %eax, %eax
callq 0x3e83
jmp 0x6702
movl %r13d, 0x10(%rsp)
movq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x4dec
cmpl $0x1, %eax
leaq 0x612e(%rip), %r13      # 0xc8d0
jne 0x6858
xorl %r15d, %r15d
cmpq %r15, 0x38(%rsp)
je 0x67d8
movsbl (%r13,%r15), %esi
cmpl $0xa, %esi
jne 0x67c6
xorl %eax, %eax
callq 0x4d0a
jmp 0x67ce
movl %r14d, %edi
callq 0x4b10
incq %r15
cmpl $0x1, %eax
je 0x67ab
jmp 0x67f5
incl 0x14(%rsp)
movl 0x10(%rsp), %r13d
jmp 0x6677
xorl %edi, %edi
movl %r14d, %esi
callq 0x23b3
jmp 0x6677
leaq 0x1d56(%rip), %rdi      # 0x8552
jmp 0x6825
leaq 0x6043(%rip), %rax      # 0xc848
movq (%rax), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rax)
movslq 0x20(%rsp), %rcx
movq %rcx, 0x20(%rax)
orb $0x2, 0x3b(%rax)
leaq 0x1cc7(%rip), %rdi      # 0x84ec
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x3e83
movl %r14d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d25(%rip), %rdi      # 0x8570
movl 0x14(%rsp), %esi
xorl %eax, %eax
callq 0x3e83
jmp 0x682f
leaq 0x1cdd(%rip), %rdi      # 0x853c
jmp 0x6825