// File: /workspace/github_c_makefile_star2_O3/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, %r14d
movl %edi, 0x14(%rsp)
xorl %eax, %eax
leaq 0x7524(%rip), %rbx      # 0xdb50
movl $0xfffffffd, (%rax,%rbx) # imm = 0xFFFFFFFD
addq $0x18, %rax
cmpq $0xc00, %rax            # imm = 0xC00
jne 0x662c
movl $0x80, 0x74f7(%rip)     # 0xdb40
leaq 0x7430(%rip), %rsi      # 0xda80
leaq 0x30(%rsp), %rdi
movl $0x50, %edx
callq 0x65bc
leaq 0x4(%rsp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
leaq 0x73d7(%rip), %rax      # 0xda48
movq (%rax), %rax
movq 0x18(%rax), %rcx
movq 0x20(%rax), %rax
movq %rax, 0x28(%rsp)
movl %eax, 0x74f1(%rip)      # 0xdb78
movq %rcx, 0x20(%rsp)
movq %rcx, 0x74dd(%rip)      # 0xdb70
movl $0x1, 0x74a3(%rip)      # 0xdb40
movl $0x0, 0x74c1(%rip)      # 0xdb68
movl %r14d, %edi
movl $0x1, %esi
movl $0x1, %edx
callq 0x7722
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
movl %r14d, 0xc(%rsp)
movl %r14d, 0x10(%rsp)
movl %r14d, %ebp
movl $0x1, %r12d
movl 0x4(%rsp), %eax
movl %eax, 0x8(%rsp)
xorl %eax, %eax
callq 0x370f
xorl %eax, %eax
callq 0x7c80
cmpl $0x12, %eax
jne 0x6797
cmpl $-0x1, %ebp
je 0x6aad
testl %r12d, %r12d
jne 0x6b1e
cmpl $-0x2, %ebp
jne 0x6b1e
leaq 0x731e(%rip), %rax      # 0xda38
movq (%rax), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rax
leaq 0x731c(%rip), %rcx      # 0xda48
movq (%rcx), %rcx
movq %rax, 0x18(%rcx)
movslq 0x14(%rax), %rax
movq %rax, 0x20(%rcx)
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
callq 0x77f6
testl %eax, %eax
je 0x6775
movl 0x73f1(%rip), %eax      # 0xdb40
leal 0x1(%rax), %ecx
cmpl $0x7f, %eax
cmovgel %r13d, %ecx
movl %ecx, 0x73e1(%rip)      # 0xdb40
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
movl $0xfffffffb, (%rbx,%rax,8) # imm = 0xFFFFFFFB
movl $0x1, %r12d
jmp 0x6778
xorl %r12d, %r12d
leaq 0x7301(%rip), %rsi      # 0xda80
leaq 0x2dfb(%rip), %rdi      # 0x9581
xorl %eax, %eax
callq 0x4246
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0x66e1
movl %eax, %r15d
jg 0x67bc
leal -0x7(%r15), %eax
cmpl $0x6, %eax
leaq 0x72d6(%rip), %rbp      # 0xda80
ja 0x67dd
leaq 0x2cd9(%rip), %rcx      # 0x948c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x13, %r15d
leaq 0x72b9(%rip), %rbp      # 0xda80
je 0x6829
cmpl $0x7f, %r15d
je 0x69dd
cmpl $0x1b, %r15d
je 0x6d53
cmpl $0x1f, %r15d
jbe 0x6dee
movq 0x18(%rsp), %rax
cmpl $-0x1, %eax
je 0x68b0
testl %eax, %eax
movl $0x1, %eax
cmovel %eax, %r12d
movq 0x18(%rsp), %rax
cmpl $0x4f, %eax
jne 0x68b8
xorl %eax, %eax
leaq 0x7051(%rip), %rcx      # 0xd868
callq *0x50(%rcx)
movl $0x4f, %eax
movq %rax, 0x18(%rsp)
jmp 0x68d6
cmpl $-0x2, 0x10(%rsp)
je 0x6bfc
testl %r12d, %r12d
jne 0x6c60
cmpl $-0x1, 0xc(%rsp)
jne 0x6c60
leaq 0x71e9(%rip), %rax      # 0xda38
movq (%rax), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
leaq 0x71e8(%rip), %rcx      # 0xda48
movq (%rcx), %rcx
movq %rax, 0x18(%rcx)
movq $0x0, 0x20(%rcx)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x77f6
testl %eax, %eax
je 0x69af
movl 0x72b9(%rip), %eax      # 0xdb40
leal 0x1(%rax), %ecx
cmpl $0x7f, %eax
cmovgel %r13d, %ecx
movl %ecx, 0x72a9(%rip)      # 0xdb40
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
movl $0xfffffffb, (%rbx,%rax,8) # imm = 0xFFFFFFFB
movl $0x1, %r12d
jmp 0x69b2
xorl %eax, %eax
movl $0x1, %r12d
movq %rax, %rcx
movslq %ecx, %rax
incl %ecx
movb %r15b, (%rbp,%rax)
movslq %ecx, %rax
movb $0x0, (%rbp,%rax)
movq %rcx, 0x18(%rsp)
movl %ecx, 0x8(%rsp)
movl 0x7264(%rip), %ecx      # 0xdb40
leal 0x1(%rcx), %eax
cmpl $0x7f, %ecx
cmovgel %r13d, %eax
movslq %eax, %rcx
leaq (%rcx,%rcx,2), %rcx
movl $0xfffffffd, (%rbx,%rcx,8) # imm = 0xFFFFFFFD
leaq 0x714d(%rip), %rdx      # 0xda48
movq (%rdx), %rdx
movl 0x20(%rdx), %esi
movl %esi, 0x10(%rbx,%rcx,8)
movq 0x18(%rdx), %rdx
movq %rdx, 0x8(%rbx,%rcx,8)
testl %r12d, %r12d
je 0x6948
movl %r14d, %edi
callq 0x77f6
testl %eax, %eax
je 0x695d
movl 0x721b(%rip), %eax      # 0xdb40
leal 0x1(%rax), %ecx
cmpl $0x7f, %eax
cmovgel %r13d, %ecx
movl %ecx, 0x720b(%rip)      # 0xdb40
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
movl %r15d, (%rbx,%rax,8)
movl $0x1, %r12d
jmp 0x6990
leaq (%rbx,%rcx,8), %rcx
movl %eax, 0x71ee(%rip)      # 0xdb40
movl $0xfffffffc, (%rcx)     # imm = 0xFFFFFFFC
xorl %r12d, %r12d
jmp 0x6990
xorl %r12d, %r12d
xorl %eax, %eax
leaq 0x6eff(%rip), %rcx      # 0xd868
callq *0x50(%rcx)
movl 0x71ce(%rip), %eax      # 0xdb40
leal 0x1(%rax), %ecx
cmpl $0x7f, %eax
cmovgel %r12d, %ecx
movl %ecx, 0x71be(%rip)      # 0xdb40
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
movl $0xfffffffc, (%rbx,%rax,8) # imm = 0xFFFFFFFC
movl %r14d, %edi
xorl %esi, %esi
movl %r12d, %edx
callq 0x7722
movl %r14d, 0xc(%rsp)
movl %r14d, 0x10(%rsp)
movl %r14d, %ebp
jmp 0x66e1
xorl %r12d, %r12d
leaq 0x2bc8(%rip), %rdi      # 0x9581
movq %rbp, %rsi
xorl %eax, %eax
callq 0x4246
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x66e1
movl 0x8(%rsp), %eax
movl %eax, 0x4(%rsp)
leaq 0x4(%rsp), %rdi
leaq 0x14(%rsp), %rsi
callq 0x7772
movslq 0x7145(%rip), %rax      # 0xdb40
leaq (%rax,%rax,2), %rax
cmpl $-0x4, (%rbx,%rax,8)
movl $0x1, %eax
cmovnel %eax, %r12d
movl 0x14(%rsp), %r14d
movl 0x4(%rsp), %eax
movq %rax, 0x18(%rsp)
movl %eax, %esi
shrl $0x1f, %esi
movl %r14d, %edi
movl %r12d, %edx
jmp 0x6a96
movl 0x8(%rsp), %eax
movl %eax, 0x4(%rsp)
cmpl $0x1, %r12d
je 0x6d9e
movslq 0x7100(%rip), %rax      # 0xdb40
leaq (%rax,%rax,2), %rax
cmpl $-0x4, (%rbx,%rax,8)
movq 0x18(%rsp), %rax
jne 0x6a7e
leaq 0x4(%rsp), %r14
leaq 0x14(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x7772
movslq 0x70d5(%rip), %rax      # 0xdb40
leaq (%rax,%rax,2), %rax
cmpl $-0x4, (%rbx,%rax,8)
je 0x6a59
movl 0x14(%rsp), %r14d
movl 0x4(%rsp), %eax
movq %rax, 0x18(%rsp)
movl %eax, %esi
shrl $0x1f, %esi
movl $0x1, %r12d
movl %r14d, %edi
movl $0x1, %edx
callq 0x7722
movl %r14d, 0xc(%rsp)
movl %r14d, 0x10(%rsp)
movl %r14d, %ebp
jmp 0x66d9
movl 0x8(%rsp), %eax
movl %eax, 0x4(%rsp)
movl $0xfffffffe, %r14d      # imm = 0xFFFFFFFE
movl %r14d, 0x14(%rsp)
movl 0x707a(%rip), %ecx      # 0xdb40
leal 0x1(%rcx), %eax
cmpl $0x7f, %ecx
movl $0x0, %r8d
cmovgel %r8d, %eax
movslq %eax, %rsi
leaq (%rsi,%rsi,2), %rdi
leaq 0x6f64(%rip), %rcx      # 0xda48
movq (%rcx), %rdx
movl 0x20(%rdx), %ecx
movl %ecx, 0x10(%rbx,%rdi,8)
movq 0x18(%rdx), %rdx
movq %rdx, 0x8(%rbx,%rdi,8)
movl %esi, 0x7043(%rip)      # 0xdb40
movl %r14d, (%rbx,%rdi,8)
movl $0x1, %r12d
movl $0xfffffffe, 0xc(%rsp)  # imm = 0xFFFFFFFE
movl $0xfffffffe, 0x10(%rsp) # imm = 0xFFFFFFFE
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0x6b40
movl 0x8(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x7014(%rip), %eax      # 0xdb40
leaq 0x6f15(%rip), %rcx      # 0xda48
movq (%rcx), %rdx
movl 0x20(%rdx), %ecx
movq 0x18(%rdx), %rdx
xorl %r8d, %r8d
leal 0x1(%rax), %esi
cmpl $0x7f, %eax
cmovgel %r8d, %esi
movslq %esi, %rax
leaq (%rax,%rax,2), %rax
movl $0xfffffffd, (%rbx,%rax,8) # imm = 0xFFFFFFFD
movl %ecx, 0x10(%rbx,%rax,8)
movq %rdx, 0x8(%rbx,%rax,8)
leaq 0x6f18(%rip), %rdi      # 0xda80
callq 0x20d0
xorl %r15d, %r15d
movq %rax, 0x18(%rsp)
movl %eax, 0x4(%rsp)
movl $0x8, %edi
movl $0x1, %esi
callq 0x2412
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
callq 0x77f6
testl %eax, %eax
je 0x6bc2
movl 0x6fa4(%rip), %eax      # 0xdb40
leal 0x1(%rax), %ecx
cmpl $0x7f, %eax
movl $0x0, %r13d
cmovgel %r15d, %ecx
movl %ecx, 0x6f8e(%rip)      # 0xdb40
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
movl $0xfffffffb, (%rbx,%rax,8) # imm = 0xFFFFFFFB
jmp 0x6be3
xorl %r13d, %r13d
movl $0x8, %edi
movl $0x1, %esi
callq 0x2470
xorl %r12d, %r12d
xorl %eax, %eax
leaq 0x6c88(%rip), %rcx      # 0xd868
callq *0x50(%rcx)
movq 0x18(%rsp), %rax
movl %eax, %esi
shrl $0x1f, %esi
movl %ebp, %edi
movl %r12d, %edx
callq 0x7722
jmp 0x66d9
movl 0x8(%rsp), %eax
movl %eax, 0x4(%rsp)
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl 0x6f30(%rip), %ecx      # 0xdb40
leal 0x1(%rcx), %eax
cmpl $0x7f, %ecx
movl $0x0, %r8d
cmovgel %r8d, %eax
movl %r14d, 0x14(%rsp)
movslq %eax, %rsi
leaq (%rsi,%rsi,2), %rdi
leaq 0x6e15(%rip), %rcx      # 0xda48
movq (%rcx), %rdx
movl 0x20(%rdx), %ecx
movl %ecx, 0x10(%rbx,%rdi,8)
movq 0x18(%rdx), %rdx
movq %rdx, 0x8(%rbx,%rdi,8)
movl %esi, 0x6ef4(%rip)      # 0xdb40
movl %r14d, (%rbx,%rdi,8)
movl $0x1, %r12d
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
jmp 0x6c82
movl 0x8(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x6ed2(%rip), %eax      # 0xdb40
leaq 0x6dd3(%rip), %rcx      # 0xda48
movq (%rcx), %rdx
movl 0x20(%rdx), %ecx
movq 0x18(%rdx), %rdx
xorl %r8d, %r8d
leal 0x1(%rax), %esi
cmpl $0x7f, %eax
cmovgel %r8d, %esi
movslq %esi, %rax
leaq (%rax,%rax,2), %rax
movl $0xfffffffd, (%rbx,%rax,8) # imm = 0xFFFFFFFD
movl %ecx, 0x10(%rbx,%rax,8)
movq %rdx, 0x8(%rbx,%rax,8)
movq %rbp, %rdi
callq 0x20d0
xorl %r15d, %r15d
movq %rax, 0x18(%rsp)
movl %eax, 0x4(%rsp)
movl $0x8, %edi
movl $0x1, %esi
callq 0x2470
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x77f6
testl %eax, %eax
je 0x6d00
movl 0x6e66(%rip), %eax      # 0xdb40
leal 0x1(%rax), %ecx
cmpl $0x7f, %eax
movl $0x0, %r13d
cmovgel %r15d, %ecx
movl %ecx, 0x6e50(%rip)      # 0xdb40
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
movl $0xfffffffb, (%rbx,%rax,8) # imm = 0xFFFFFFFB
jmp 0x6d32
xorl %r13d, %r13d
movl $0x8, %edi
movl $0x1, %esi
callq 0x2412
xorl %r12d, %r12d
xorl %eax, %eax
leaq 0x6b4a(%rip), %rcx      # 0xd868
callq *0x50(%rcx)
leaq 0x2872(%rip), %rdi      # 0x959a
movq %rbp, %rsi
xorl %eax, %eax
callq 0x4246
movq 0x18(%rsp), %rax
movl %eax, %esi
shrl $0x1f, %esi
movl 0xc(%rsp), %ebp
movl %ebp, %edi
movl %r12d, %edx
callq 0x7722
movl %ebp, 0x10(%rsp)
jmp 0x66d9
movl 0x8(%rsp), %eax
movl %eax, 0x4(%rsp)
movl %r14d, 0x6afe(%rip)     # 0xd860
leaq 0x6cdf(%rip), %rax      # 0xda48
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 0x27fb
movl $0x1, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6ca3(%rip), %rax      # 0xda48
movq (%rax), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x18(%rax)
movslq 0x28(%rsp), %rcx
movq %rcx, 0x20(%rax)
orb $0x2, 0x3b(%rax)
movl %r14d, 0x6a9b(%rip)     # 0xd860
leaq 0x6cb4(%rip), %rdi      # 0xda80
leaq 0x30(%rsp), %rsi
movl $0x50, %edx
callq 0x65bc
movl $0x8, %edi
xorl %esi, %esi
callq 0x5d72
movl $0x2, %eax
jmp 0x6d8c
movl 0x8(%rsp), %eax
movl %eax, 0x4(%rsp)
leaq 0x6c4b(%rip), %rbx      # 0xda48
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 0x27fb
movq (%rbx), %rax
orb $0x2, 0x3b(%rax)
jmp 0x6d87