// File: /workspace/github_c_makefile_star2_O1/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 0x7115
movl %edx, %ebx
movl %esi, %ebp
movl %edi, %r14d
testl %edi, %edi
leaq 0x2770(%rip), %rax      # 0x94be
leaq 0x2771(%rip), %rdi      # 0x94c6
cmoveq %rax, %rdi
callq 0x68ac
movl %eax, %r12d
cmpl $0x1, %eax
jne 0x7115
leaq 0x6b1f(%rip), %r15      # 0xd890
leaq 0x50(%rsp), %rdi
movl $0x50, %edx
movq %r15, %rsi
callq 0x2060
leaq 0x6b56(%rip), %rsi      # 0xd8e0
movl $0x50, %edx
movq %r15, %rdi
callq 0x2060
leaq 0x2736(%rip), %rdi      # 0x94d4
callq 0x68ac
movl $0x2, %r12d
cmpl $0x2, %eax
je 0x7115
movl %r14d, 0x14(%rsp)
leaq 0x6b22(%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, 0x40(%rsp)
leal -0x1(%r13), %eax
cltq
movb (%r15,%rax), %r13b
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 0x6cac
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 0x6a72(%rip), %rdi      # 0xd8e0
movl $0x1a, %edx
callq 0x6cac
movq %r12, %rdi
callq 0x20c0
movl $0x203f27, 0x50(%rsp,%rax) # imm = 0x203F27
testl %ebp, %ebp
setne %al
testl %ebx, %ebx
setle %cl
xorl %esi, %esi
testb %cl, %al
je 0x6eb1
leaq 0x26d1(%rip), %rdi      # 0x9570
xorl %eax, %eax
callq 0x446e
movl $0x1, %r12d
jmp 0x7115
leaq 0x69a0(%rip), %rax      # 0xd858
movq (%rax), %rax
movq 0x18(%rax), %rcx
movq %rcx, 0x28(%rsp)
movq 0x20(%rax), %rax
movq %rax, 0x30(%rsp)
movl %r14d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x48(%rsp)
leaq 0x68af(%rip), %r12      # 0xd790
xorl %r15d, %r15d
xorl %esi, %esi
movq %r14, 0x38(%rsp)
movb %r13b, 0xf(%rsp)
movl %esi, 0x10(%rsp)
movl 0x14(%rsp), %r14d
leaq 0x6990(%rip), %rdi      # 0xd890
movl $0x1, %esi
callq 0x6962
testl %eax, %eax
je 0x712a
incl %r15d
leaq 0x693c(%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 0x7040
leaq 0x50(%rsp), %rdi
leaq 0x6943(%rip), %rsi      # 0xd890
leaq 0x698c(%rip), %rdx      # 0xd8e0
xorl %eax, %eax
callq 0x446e
xorl %eax, %eax
callq 0x346d
xorl %eax, %eax
callq *0x20(%r12)
movl %eax, %r13d
leaq 0x26c9(%rip), %rdi      # 0x963c
xorl %eax, %eax
callq 0x446e
movsbl %r13b, %eax
cmpl $0x3e, %eax
jle 0x6f8f
cmpl $0x6d, %eax
jg 0x6fb0
cmpl $0x3f, %eax
je 0x6fe2
jmp 0x6fdb
cmpl $0x1f, %eax
jle 0x6fc9
cmpl $0x20, %eax
je 0x7043
cmpl $0x21, %eax
je 0x7040
cmpl $0x2e, %eax
jne 0x6fdb
jmp 0x70e4
cmpl $0x79, %eax
je 0x7043
cmpl $0x71, %eax
je 0x6ea6
cmpl $0x6e, %eax
jne 0x6fdb
jmp 0x6ff5
cmpl $0x7, %eax
je 0x7104
cmpl $0xd, %eax
je 0x6ea6
xorl %eax, %eax
callq *0x50(%r12)
leaq 0x250c(%rip), %rdi      # 0x94f5
xorl %eax, %eax
callq 0x446e
jmp 0x6f5b
movq 0x18(%rsp), %rax
cmpq %rax, 0x20(%rsp)
sete 0x18(%rsp)
testl %ebp, %ebp
setne 0x20(%rsp)
cmpb $0xa, 0xf(%rsp)
sete %r13b
xorl %edi, %edi
movl $0x1, %esi
callq 0x23d0
cmpl %ebx, %r15d
setge %al
testb %al, 0x20(%rsp)
jne 0x712a
andb 0x18(%rsp), %r13b
je 0x6ef9
jmp 0x712a
xorl %r14d, %r14d
movq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x54cf
cmpl $0x1, %eax
jne 0x7133
movq 0x38(%rsp), %rdi
testl %edi, %edi
jle 0x7097
xorl %r13d, %r13d
leaq 0x6875(%rip), %rax      # 0xd8e0
movsbl (%rax,%r13), %esi
cmpl $0xa, %esi
jne 0x707e
xorl %eax, %eax
callq 0x53d0
jmp 0x7088
movl $0x1, %edi
callq 0x51ad
cmpl $0x1, %eax
jne 0x70db
incq %r13
cmpq %r13, 0x48(%rsp)
jne 0x7064
movq 0x18(%rsp), %rax
cmpq %rax, 0x20(%rsp)
sete %al
testl %ebp, %ebp
setne %dl
movb 0xf(%rsp), %r13b
cmpb $0xa, %r13b
sete %cl
incl 0x10(%rsp)
cmpl %ebx, %r15d
setge %sil
testb %sil, %dl
jne 0x712a
movl %r14d, 0x14(%rsp)
andb %al, %cl
movl 0x10(%rsp), %esi
je 0x6ef0
jmp 0x6e98
leaq 0x2470(%rip), %rdi      # 0x9552
jmp 0x710b
leaq 0x676d(%rip), %rax      # 0xd858
movq (%rax), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rax)
movslq 0x30(%rsp), %rcx
movq %rcx, 0x20(%rax)
orb $0x2, 0x3b(%rax)
leaq 0x23e1(%rip), %rdi      # 0x94ec
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x446e
movl %r12d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x10(%rsp), %esi
jmp 0x6e98
leaq 0x2402(%rip), %rdi      # 0x953c
jmp 0x710b