// File: /workspace/github_c_makefile_star2_O2/hughbarney[P]pEmacs/search.c
// Function: isearch
static int
isearch(int dir)
{
	LINE 	*clp;		/* Saved line pointer */
	int		 c;
	int		 cbo;		/* Saved offset */
	int		 success;
	int		 pptr;
	char	 opat[NPAT];

	for (cip = 0; cip < NSRCH; cip++)
		cmds[cip].s_code = SRCH_NOPR;

	(void)strlcpy(opat, pat, sizeof(opat));
	cip = 0;
	pptr = -1;
	clp = curwp->w_dotp;
	cbo = curwp->w_doto;
	is_lpush();
	is_cpush(SRCH_BEGIN);
	success = TRUE;
	is_prompt(dir, TRUE, success);

	for (;;) {
		update();
		c = ttgetc();

		switch (c) {
		case ESC:
			/*
			 * If new characters come in the next 300 msec,
			 * we can assume that they belong to a longer
			 * escaped sequence so we should ungetkey the
			 * ESC to avoid writing out garbage.
			 *
			 * BUG: The Mg code had these lines, however
			 * I was not able to get ttwait() to indicate that
			 * chars had arrived in the input buffer as it 
			 * always returned TRUE. So it made no difference.
			 *
			 * if (ttwait(300) == FALSE)
			 *   ungetkey(c);
			 *
			 * What will happen is that if the user hits
			 * up-arrow during an isearch then it terminates
			 * and [A gets output at the last search position.
			 * The same issue can also be observed in uEmacs/Pk
			 */

			/* FALLTHRU */
		case CTRL_M:
			srch_lastdir = dir;
			curwp->w_markp = clp;
			curwp->w_marko = cbo;
			setmark(0,0);
			return (TRUE);

		case CTRL_G:
			if (success != TRUE) {
				while (is_peek() == SRCH_ACCM)
					is_undo(&pptr, &dir);
				success = TRUE;
				is_prompt(dir, pptr < 0, success);
				break;
			}
			curwp->w_dotp = clp;
			curwp->w_doto = cbo;
			curwp->w_flag |= WFMOVE;
			srch_lastdir = dir;
			(void)strlcpy(pat, opat, NPAT);
			(void)ctrlg(FFRAND, 0);
			return (ABORT);
		case CTRL_S:
			if (dir == SRCH_BACK) {
				dir = SRCH_FORW;
				is_lpush();
				is_cpush(SRCH_FORW);
				success = TRUE;
			}
			if (success == FALSE && dir == SRCH_FORW) {
				/* wrap the search to beginning */
				curwp->w_dotp = bfirstlp(curbp);
				curwp->w_doto = 0;
				if (is_find(dir) != FALSE) {
					is_cpush(SRCH_MARK);
					success = TRUE;
				}
				mlwrite("Overwrapped I-search: %s", pat);
				break;
			}

			is_lpush();
			pptr = strlen(pat);
			(void)forwchar(FFRAND, 1);
			if (is_find(SRCH_FORW) != FALSE)
				is_cpush(SRCH_MARK);
			else {
				(void)backchar(FFRAND, 1);
				(*term.t_beep) ();
				success = FALSE;
				mlwrite("Failed I-search: %s", pat);
			}
			is_prompt(dir, pptr < 0, success);
			break;
		case CTRL_R:
			if (dir == SRCH_FORW) {
				dir = SRCH_BACK;
				is_lpush();
				is_cpush(SRCH_BACK);
				success = TRUE;
			}
			if (success == FALSE && dir == SRCH_BACK) {
				/* wrap the search to end */
				curwp->w_dotp = blastlp(curbp);
				curwp->w_doto = llength(curwp->w_dotp);
				if (is_find(dir) != FALSE) {
					is_cpush(SRCH_MARK);
					success = TRUE;
				}
				mlwrite("Overwrapped I-search: %s", pat);
				break;
			}
			is_lpush();
			pptr = strlen(pat);
			(void)backchar(FFRAND, 1);
			if (is_find(SRCH_BACK) != FALSE)
				is_cpush(SRCH_MARK);
			else {
				(void)forwchar(FFRAND, 1);
				(*term.t_beep) ();
				success = FALSE;
			}
			is_prompt(dir, pptr < 0, success);
			break;
		case CTRL_H:
		case BACKSPACE:
			is_undo(&pptr, &dir);
			if (is_peek() != SRCH_ACCM)
				success = TRUE;
			is_prompt(dir, pptr < 0, success);
			break;
		default:
			if (ISCTRL(c)) {
			  /* ungetkey(c);  not required as we allow any CTRL key to exit isearch */
				curwp->w_markp = clp;
				curwp->w_marko = cbo;
				setmark(0,0);
				curwp->w_flag |= WFMOVE;
				return (TRUE);
			}
			/* FALLTHRU */
		case CTRL_I:
		  //addchar:
			if (pptr == -1)
				pptr = 0;
			if (pptr == 0)
				success = TRUE;
			if (pptr == NPAT - 1)
				(*term.t_beep) ();
			else {
				pat[pptr++] = c;
				pat[pptr] = '\0';
			}
			is_lpush();
			if (success != FALSE) {
				if (is_find(dir) != FALSE)
					is_cpush(c);
				else {
					success = FALSE;
					(*term.t_beep) ();
					is_cpush(SRCH_ACCM);
				}
			} else
				is_cpush(SRCH_ACCM);
			is_prompt(dir, FALSE, success);
		}
	}
	/* NOTREACHED */
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %edi, 0xc(%rsp)
movl %edi, 0x14(%rsp)
xorl %eax, %eax
leaq 0x6c2d(%rip), %rbx      # 0xcb40
cmpq $0xc00, %rax            # imm = 0xC00
je 0x5f28
movl $0xfffffffd, (%rax,%rbx) # imm = 0xFFFFFFFD
addq $0x18, %rax
jmp 0x5f13
movl $0x80, 0x6bfe(%rip)     # 0xcb30
leaq 0x6b37(%rip), %rsi      # 0xca70
leaq 0x30(%rsp), %rdi
pushq $0x50
popq %rdx
callq 0x5ea4
andl $0x0, 0x6be3(%rip)      # 0xcb30
leaq 0x4(%rsp), %rax
orl $-0x1, (%rax)
leaq 0x6adc(%rip), %rax      # 0xca38
movq (%rax), %rax
movq 0x18(%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x20(%rax), %rax
movq %rax, 0x28(%rsp)
callq 0x6c38
xorl %edi, %edi
callq 0x6c79
pushq $0x1
popq %rbp
movl 0xc(%rsp), %r15d
movl %r15d, %edi
movl %ebp, %esi
movl %ebp, %edx
callq 0x6ca2
pushq $-0x1
popq %rax
leaq 0x24f1(%rip), %r12      # 0x848c
pushq $0x8
popq %r13
movq %rax, 0x18(%rsp)
movl %r15d, 0x8(%rsp)
movl 0x4(%rsp), %eax
movl %eax, 0x10(%rsp)
xorl %eax, %eax
callq 0x34fc
xorl %eax, %eax
callq 0x71cd
movl %eax, %r14d
leal -0x7(%r14), %eax
cmpl $0xc, %eax
ja 0x6055
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
cmpl $-0x1, %r15d
je 0x6256
testl %ebp, %ebp
jne 0x6290
cmpl $-0x2, %r15d
jne 0x6290
leaq 0x6a2d(%rip), %rax      # 0xca28
movq (%rax), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rax
leaq 0x6a2b(%rip), %rcx      # 0xca38
movq (%rcx), %rcx
movq %rax, 0x18(%rcx)
movslq 0x14(%rax), %rax
movq %rax, 0x20(%rcx)
pushq $-0x2
popq %r15
movl %r15d, %edi
callq 0x6d7b
testl %eax, %eax
je 0x6039
pushq $-0x5
popq %rdi
callq 0x6c79
pushq $0x1
popq %rbp
jmp 0x603b
xorl %ebp, %ebp
leaq 0x6a2e(%rip), %rsi      # 0xca70
leaq 0x2550(%rip), %rdi      # 0x8599
xorl %eax, %eax
callq 0x3dec
jmp 0x5fb1
cmpl $0x7f, %r14d
je 0x61c2
cmpl $0x1b, %r14d
je 0x63d9
cmpl $0x1f, %r14d
jbe 0x6425
movq 0x18(%rsp), %rax
cmpl $-0x1, %eax
je 0x6121
testl %eax, %eax
pushq $0x1
popq %r15
cmovel %r15d, %ebp
cmpl $0x4f, %eax
leaq 0x69db(%rip), %rcx      # 0xca70
jne 0x6131
xorl %eax, %eax
leaq 0x67b4(%rip), %rcx      # 0xc858
callq *0x50(%rcx)
pushq $0x4f
popq %rax
movq %rax, 0x18(%rsp)
jmp 0x614d
cmpl $-0x2, 0x8(%rsp)
je 0x631a
testl %ebp, %ebp
leaq 0x69a8(%rip), %r14      # 0xca70
pushq $0x1
popq %r15
jne 0x6352
cmpl $-0x1, 0x8(%rsp)
jne 0x6352
leaq 0x6944(%rip), %rax      # 0xca28
movq (%rax), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
leaq 0x6943(%rip), %rcx      # 0xca38
movq (%rcx), %rcx
movq %rax, 0x18(%rcx)
andq $0x0, 0x20(%rcx)
pushq $-0x1
popq %rdi
callq 0x6d7b
testl %eax, %eax
je 0x61a1
pushq $-0x5
popq %rdi
callq 0x6c79
movl %r15d, %ebp
jmp 0x61a3
xorl %eax, %eax
pushq $0x1
popq %r15
movl %r15d, %ebp
leaq 0x693f(%rip), %rcx      # 0xca70
movq %rax, %rdx
movslq %edx, %rax
incl %edx
movb %r14b, (%rcx,%rax)
movslq %edx, %rax
movb $0x0, (%rcx,%rax)
movq %rdx, 0x18(%rsp)
movl %edx, 0x10(%rsp)
callq 0x6c38
testl %ebp, %ebp
je 0x6176
movl 0xc(%rsp), %edi
callq 0x6d7b
movl %r15d, %ebp
testl %eax, %eax
jne 0x617e
xorl %ebp, %ebp
xorl %eax, %eax
leaq 0x66e7(%rip), %rcx      # 0xc858
callq *0x50(%rcx)
jmp 0x6178
xorl %ebp, %ebp
pushq $-0x4
popq %rax
movl %eax, %r14d
movl %r14d, %edi
callq 0x6c79
movl 0xc(%rsp), %r15d
movl %r15d, %edi
xorl %esi, %esi
movl %ebp, %edx
callq 0x6ca2
movl %r15d, 0x8(%rsp)
jmp 0x5fb1
xorl %ebp, %ebp
leaq 0x23ef(%rip), %rdi      # 0x8599
movq %r14, %rsi
xorl %eax, %eax
callq 0x3dec
pushq $-0x1
popq %r15
movl %r15d, 0x8(%rsp)
jmp 0x5fb1
movl 0x10(%rsp), %eax
movl %eax, 0x4(%rsp)
leaq 0x4(%rsp), %rdi
leaq 0x14(%rsp), %rsi
callq 0x6cf2
movslq 0x6950(%rip), %rax      # 0xcb30
imulq $0x18, %rax, %rax
cmpl $-0x4, (%rax,%rbx)
pushq $0x1
popq %rax
cmovnel %eax, %ebp
movl 0x14(%rsp), %r15d
movl 0x4(%rsp), %eax
movq %rax, 0x18(%rsp)
movl %eax, %esi
shrl $0x1f, %esi
movl %r15d, %edi
movl %ebp, %edx
callq 0x6ca2
movl %r15d, 0x8(%rsp)
movl %r15d, 0xc(%rsp)
jmp 0x5fa9
movl 0x10(%rsp), %eax
movl %eax, 0x4(%rsp)
cmpl $0x1, %ebp
je 0x645b
leaq 0x4(%rsp), %r14
pushq $0x1
popq %rbp
leaq 0x14(%rsp), %r15
movslq 0x68f1(%rip), %rax      # 0xcb30
imulq $0x18, %rax, %rax
cmpl $-0x4, (%rax,%rbx)
jne 0x61ee
movq %r14, %rdi
movq %r15, %rsi
callq 0x6cf2
jmp 0x6238
movl %r13d, %r12d
movl 0x10(%rsp), %eax
movl %eax, 0x4(%rsp)
movl $0xfffffffe, 0x14(%rsp) # imm = 0xFFFFFFFE
callq 0x6c38
pushq $-0x2
popq %r13
movl %r13d, %edi
callq 0x6c79
pushq $0x1
popq %r14
movl %r14d, %ebp
movl %r13d, %r15d
movl %r13d, 0x8(%rsp)
movl %r13d, 0xc(%rsp)
jmp 0x62a3
movl %r13d, %r12d
movl 0x10(%rsp), %eax
movl %eax, 0x4(%rsp)
pushq $0x1
popq %r14
pushq $-0x2
popq %r13
callq 0x6c38
leaq 0x67c1(%rip), %rdi      # 0xca70
callq 0x20d0
movq %rax, 0x18(%rsp)
movl %eax, 0x4(%rsp)
movl %r12d, %edi
movl %r14d, %esi
callq 0x23f7
movl %r13d, %edi
callq 0x6d7b
testl %eax, %eax
je 0x62de
pushq $-0x5
popq %rdi
callq 0x6c79
jmp 0x62f7
movl %r12d, %edi
movl %r14d, %esi
callq 0x2453
xorl %ebp, %ebp
xorl %eax, %eax
leaq 0x6564(%rip), %rcx      # 0xc858
callq *0x50(%rcx)
movl %r12d, %r13d
movq 0x18(%rsp), %rax
movl %eax, %esi
shrl $0x1f, %esi
movl %r15d, %edi
movl %ebp, %edx
callq 0x6ca2
leaq 0x2177(%rip), %r12      # 0x848c
jmp 0x5fa9
movl 0x10(%rsp), %eax
movl %eax, 0x4(%rsp)
orl $-0x1, 0x14(%rsp)
callq 0x6c38
pushq $-0x1
popq %r14
movl %r14d, %edi
callq 0x6c79
pushq $0x1
popq %r15
movl %r15d, %ebp
movl %r14d, 0x8(%rsp)
movl %r14d, 0xc(%rsp)
leaq 0x6720(%rip), %r14      # 0xca70
jmp 0x635a
movl 0x10(%rsp), %eax
movl %eax, 0x4(%rsp)
callq 0x6c38
movq %r14, %rdi
callq 0x20d0
movq %rax, 0x18(%rsp)
movl %eax, 0x4(%rsp)
movl %r13d, %edi
movl %r15d, %esi
callq 0x2453
pushq $-0x1
popq %rdi
callq 0x6d7b
testl %eax, %eax
je 0x6391
pushq $-0x5
popq %rdi
callq 0x6c79
jmp 0x63bb
movl %r13d, %edi
movl %r15d, %esi
callq 0x23f7
xorl %ebp, %ebp
xorl %eax, %eax
leaq 0x64b1(%rip), %rcx      # 0xc858
callq *0x50(%rcx)
leaq 0x2201(%rip), %rdi      # 0x85b2
movq %r14, %rsi
xorl %eax, %eax
callq 0x3dec
movq 0x18(%rsp), %rax
movl %eax, %esi
shrl $0x1f, %esi
movl 0x8(%rsp), %r15d
movl %r15d, %edi
movl %ebp, %edx
callq 0x6ca2
jmp 0x5fa9
movl 0x10(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x6465(%rip)      # 0xc850
leaq 0x6646(%rip), %rax      # 0xca38
movq (%rax), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x28(%rax)
movslq 0x28(%rsp), %rcx
movq %rcx, 0x30(%rax)
xorl %edi, %edi
xorl %esi, %esi
callq 0x27a6
pushq $0x1
popq %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x10(%rsp), %eax
movl %eax, 0x4(%rsp)
leaq 0x6604(%rip), %rbx      # 0xca38
movq (%rbx), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x28(%rax)
movslq 0x28(%rsp), %rcx
movq %rcx, 0x30(%rax)
xorl %edi, %edi
xorl %esi, %esi
callq 0x27a6
movq (%rbx), %rax
orb $0x2, 0x3b(%rax)
jmp 0x6410
leaq 0x65d6(%rip), %rax      # 0xca38
movq (%rax), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x18(%rax)
movslq 0x28(%rsp), %rcx
movq %rcx, 0x20(%rax)
orb $0x2, 0x3b(%rax)
movl 0xc(%rsp), %eax
movl %eax, 0x63cb(%rip)      # 0xc850
leaq 0x65e4(%rip), %rdi      # 0xca70
leaq 0x30(%rsp), %rsi
pushq $0x50
popq %rdx
callq 0x5ea4
pushq $0x8
popq %rdi
xorl %esi, %esi
callq 0x5694
pushq $0x2
jmp 0x6412