// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0xa0dc
cmpl $0x0, -0x10(%rbp)
jge 0xa0dc
movl $0x0, -0x4(%rbp)
jmp 0xa59f
movl -0x8(%rbp), %ecx
leaq 0x34cc(%rip), %rdi      # 0xd5b2
leaq 0x34bd(%rip), %rax      # 0xd5aa
cmpl $0x0, %ecx
cmoveq %rax, %rdi
callq 0x98e0
movl %eax, -0x78(%rbp)
cmpl $0x1, %eax
je 0xa10c
movl -0x78(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xa59f
leaq -0x70(%rbp), %rdi
leaq 0x7799(%rip), %rsi      # 0x118b0
movl $0x50, %edx
callq 0x2060
leaq 0x7788(%rip), %rdi      # 0x118b0
leaq 0x77d1(%rip), %rsi      # 0x11900
movl $0x50, %edx
callq 0x2060
leaq 0x3480(%rip), %rdi      # 0xd5c0
callq 0x98e0
movl %eax, -0x78(%rbp)
cmpl $0x2, %eax
jne 0xa158
movl -0x78(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xa59f
leaq 0x77a1(%rip), %rdi      # 0x11900
leaq 0x774a(%rip), %rsi      # 0x118b0
movl $0x50, %edx
callq 0x2060
leaq -0x70(%rbp), %rsi
leaq 0x7735(%rip), %rdi      # 0x118b0
movl $0x50, %edx
callq 0x2060
leaq 0x7724(%rip), %rdi      # 0x118b0
callq 0x20c0
movl %eax, -0x7c(%rbp)
leaq 0x7765(%rip), %rdi      # 0x11900
callq 0x20c0
movl %eax, -0x80(%rbp)
movl -0x7c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x76fd(%rip), %rax      # 0x118b0
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 0x33e8(%rip), %rsi      # 0xd5c5
movl $0xa, %edx
callq 0x2060
leaq -0x70(%rbp), %rdi
callq 0x20c0
leaq -0x70(%rbp), %rsi
addq %rax, %rsi
leaq 0x76b2(%rip), %rdi      # 0x118b0
movl $0x1a, %edx
callq 0x9f20
leaq -0x70(%rbp), %rdi
leaq 0x33bc(%rip), %rsi      # 0xd5cf
movl $0x9, %edx
callq 0x2100
leaq -0x70(%rbp), %rdi
callq 0x20c0
leaq -0x70(%rbp), %rsi
addq %rax, %rsi
leaq 0x76cc(%rip), %rdi      # 0x11900
movl $0x1a, %edx
callq 0x9f20
leaq -0x70(%rbp), %rdi
leaq 0x338f(%rip), %rsi      # 0xd5d8
movl $0x4, %edx
callq 0x2100
leaq 0x761e(%rip), %rax      # 0x11878
movq (%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x18(%rbp)
leaq 0x760c(%rip), %rax      # 0x11878
movq (%rax), %rax
movq 0x20(%rax), %rax
movl %eax, -0x94(%rbp)
movl $0x0, -0x84(%rbp)
movl $0x0, -0x88(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0xa2a6
movl -0x10(%rbp), %ecx
xorl %eax, %eax
cmpl -0x88(%rbp), %ecx
movb %al, -0x95(%rbp)
jle 0xa2d3
movb $0x1, %al
cmpl $0x0, -0x8c(%rbp)
movb %al, -0x96(%rbp)
je 0xa2c7
cmpl $0x0, -0x90(%rbp)
sete %al
movb %al, -0x96(%rbp)
movb -0x96(%rbp), %al
movb %al, -0x95(%rbp)
movb -0x95(%rbp), %al
testb $0x1, %al
jne 0xa2e2
jmp 0xa584
leaq 0x75c7(%rip), %rdi      # 0x118b0
movl $0x1, %esi
callq 0x99b0
cmpl $0x1, %eax
je 0xa2fd
jmp 0xa584
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
leaq 0x7565(%rip), %rax      # 0x11878
movq (%rax), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
leaq 0x7554(%rip), %rcx      # 0x11878
movq (%rcx), %rcx
movq 0x8(%rcx), %rcx
cmpq 0x28(%rcx), %rax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x90(%rbp)
cmpl $0x0, -0x8(%rbp)
je 0xa4c8
leaq -0x70(%rbp), %rdi
leaq 0x755e(%rip), %rsi      # 0x118b0
leaq 0x75a7(%rip), %rdx      # 0x11900
movb $0x0, %al
callq 0x5970
xorl %eax, %eax
movb %al, -0x9d(%rbp)
callq 0x43e0
movb -0x9d(%rbp), %al
leaq 0x7436(%rip), %rcx      # 0x117b0
movq 0x20(%rcx), %rcx
callq *%rcx
movl %eax, %ecx
movb -0x9d(%rbp), %al
movb %cl, -0x1a(%rbp)
leaq 0x339a(%rip), %rdi      # 0xd72c
callq 0x5970
movsbl -0x1a(%rbp), %eax
movl %eax, -0x9c(%rbp)
subl $0x7, %eax
je 0xa481
jmp 0xa3ac
movl -0x9c(%rbp), %eax
subl $0xd, %eax
je 0xa49b
jmp 0xa3bd
movl -0x9c(%rbp), %eax
subl $0x20, %eax
je 0xa424
jmp 0xa3ca
movl -0x9c(%rbp), %eax
subl $0x21, %eax
je 0xa43a
jmp 0xa3d7
movl -0x9c(%rbp), %eax
subl $0x2e, %eax
je 0xa446
jmp 0xa3e4
movl -0x9c(%rbp), %eax
subl $0x3f, %eax
je 0xa4b3
jmp 0xa3f5
movl -0x9c(%rbp), %eax
subl $0x6e, %eax
je 0xa429
jmp 0xa402
movl -0x9c(%rbp), %eax
subl $0x71, %eax
je 0xa49b
jmp 0xa413
movl -0x9c(%rbp), %eax
subl $0x79, %eax
jne 0xa4a7
jmp 0xa424
jmp 0xa4c6
xorl %edi, %edi
movl $0x1, %esi
callq 0x24f0
jmp 0xa28d
movl $0x0, -0x8(%rbp)
jmp 0xa4c6
movq -0x18(%rbp), %rcx
leaq 0x7427(%rip), %rax      # 0x11878
movq (%rax), %rax
movq %rcx, 0x18(%rax)
movslq -0x94(%rbp), %rcx
leaq 0x7412(%rip), %rax      # 0x11878
movq (%rax), %rax
movq %rcx, 0x20(%rax)
leaq 0x7404(%rip), %rax      # 0x11878
movq (%rax), %rax
movsbl 0x3b(%rax), %ecx
orl $0x2, %ecx
movb %cl, 0x3b(%rax)
leaq 0x3154(%rip), %rdi      # 0xd5dc
movb $0x0, %al
callq 0x5970
movl $0x0, -0x4(%rbp)
jmp 0xa59f
movl $0x1, -0x4(%rbp)
jmp 0xa59f
leaq 0x7302(%rip), %rcx      # 0x117b0
movb $0x0, %al
callq *0x50(%rcx)
leaq 0x312b(%rip), %rdi      # 0xd5e5
movb $0x0, %al
callq 0x5970
jmp 0xa360
jmp 0xa4c8
movl -0x7c(%rbp), %edi
xorl %esi, %esi
callq 0x7750
cmpl $0x1, %eax
je 0xa4f1
leaq 0x314e(%rip), %rdi      # 0xd62c
movb $0x0, %al
callq 0x5970
movl $0x0, -0x4(%rbp)
jmp 0xa59f
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x80(%rbp), %eax
jge 0xa570
movslq -0x74(%rbp), %rcx
leaq 0x73f5(%rip), %rax      # 0x11900
movb (%rax,%rcx), %al
movb %al, -0x19(%rbp)
movsbl -0x19(%rbp), %eax
cmpl $0xa, %eax
jne 0xa529
movb $0x0, %al
callq 0x7540
movl %eax, -0xa4(%rbp)
jmp 0xa53d
movsbl -0x19(%rbp), %esi
movl $0x1, %edi
callq 0x7150
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
movl %eax, -0x78(%rbp)
cmpl $0x1, -0x78(%rbp)
je 0xa563
leaq 0x30ef(%rip), %rdi      # 0xd642
movb $0x0, %al
callq 0x5970
movl $0x0, -0x4(%rbp)
jmp 0xa59f
jmp 0xa565
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0xa4f8
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xa28d
movl -0x84(%rbp), %esi
leaq 0x30cf(%rip), %rdi      # 0xd660
movb $0x0, %al
callq 0x5970
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax,%rax)