// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movl %edi, %ebp
movl %edi, 0x8(%rsp)
xorl %eax, %eax
leaq 0x73b8(%rip), %rbx      # 0xdb50
movl $0xfffffffd, (%rax,%rbx) # imm = 0xFFFFFFFD
addq $0x18, %rax
cmpq $0xc00, %rax            # imm = 0xC00
jne 0x6798
movl $0x80, 0x738b(%rip)     # 0xdb40
leaq 0x72c4(%rip), %rsi      # 0xda80
leaq 0x20(%rsp), %rdi
movl $0x50, %edx
callq 0x671c
xorl %r12d, %r12d
movl %r12d, 0x736b(%rip)     # 0xdb40
leaq 0xc(%rsp), %r14
movl $0xffffffff, (%r14)     # imm = 0xFFFFFFFF
leaq 0x7260(%rip), %rax      # 0xda48
movq (%rax), %rax
movq 0x18(%rax), %rcx
movq 0x20(%rax), %rax
movq %rax, 0x18(%rsp)
movl %eax, 0x737a(%rip)      # 0xdb78
movq %rcx, 0x10(%rsp)
movq %rcx, 0x7366(%rip)      # 0xdb70
movl $0x1, 0x732c(%rip)      # 0xdb40
movl %r12d, 0x734d(%rip)     # 0xdb68
movl %ebp, %edi
movl $0x1, %esi
movl $0x1, %edx
callq 0x76b1
leaq 0x8(%rsp), %r15
movl $0x1, %ebp
xorl %eax, %eax
callq 0x3705
xorl %eax, %eax
callq 0x7c0c
movl %eax, %r13d
leal -0x7(%r13), %eax
cmpl $0xc, %eax
ja 0x68a2
leaq 0x2c35(%rip), %rcx      # 0x948c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x1, %ebp
jne 0x6875
jmp 0x6d6a
movq %r14, %rdi
movq %r15, %rsi
callq 0x7701
movslq 0x72c4(%rip), %rax      # 0xdb40
leaq (%rax,%rax,2), %rax
cmpl $-0x4, (%rbx,%rax,8)
je 0x686a
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %esi
shrl $0x1f, %esi
movl $0x1, %ebp
movl $0x1, %edx
callq 0x76b1
jmp 0x6836
cmpl $0x7f, %r13d
je 0x68e4
cmpl $0x1b, %r13d
je 0x6d06
cmpl $0x1f, %r13d
jbe 0x6d37
xorl %eax, %eax
movl 0xc(%rsp), %r12d
cmpl $-0x1, %r12d
cmovel %eax, %r12d
movl %r12d, 0xc(%rsp)
cmpl $0x4f, %r12d
jne 0x6916
xorl %eax, %eax
leaq 0x6f89(%rip), %rcx      # 0xd868
callq *0x50(%rcx)
jmp 0x6931
movq %r14, %rdi
movq %r15, %rsi
callq 0x7701
movslq 0x724a(%rip), %rax      # 0xdb40
leaq (%rax,%rax,2), %rax
cmpl $-0x4, (%rbx,%rax,8)
movl $0x1, %eax
cmovnel %eax, %ebp
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %esi
shrl $0x1f, %esi
jmp 0x6ca8
movslq %r12d, %rax
leaq 0x1(%rax), %rcx
movl %ecx, 0xc(%rsp)
leaq 0x7158(%rip), %rcx      # 0xda80
movb %r13b, (%rcx,%rax)
movb $0x0, 0x1(%rcx,%rax)
testl %r12d, %r12d
setne %dl
movl 0x7203(%rip), %ecx      # 0xdb40
leal 0x1(%rcx), %eax
cmpl $0x7f, %ecx
movl $0x0, %r12d
cmovgel %r12d, %eax
movslq %eax, %rcx
leaq (%rcx,%rcx,2), %rcx
movl $0xfffffffd, (%rbx,%rcx,8) # imm = 0xFFFFFFFD
leaq 0x70e6(%rip), %rsi      # 0xda48
movq (%rsi), %rsi
movl 0x20(%rsi), %edi
movl %edi, 0x10(%rbx,%rcx,8)
movq 0x18(%rsi), %rsi
movq %rsi, 0x8(%rbx,%rcx,8)
testl %ebp, %ebp
sete %sil
testb %dl, %sil
je 0x6997
leaq (%rbx,%rcx,8), %rcx
movl %eax, 0x71b6(%rip)      # 0xdb40
movl $0xfffffffc, (%rcx)     # imm = 0xFFFFFFFC
xorl %ebp, %ebp
jmp 0x6ca2
movl 0x8(%rsp), %edi
callq 0x7785
testl %eax, %eax
je 0x6c71
movl 0x7192(%rip), %eax      # 0xdb40
leal 0x1(%rax), %ecx
cmpl $0x7f, %eax
cmovgel %r12d, %ecx
movl %ecx, 0x7182(%rip)      # 0xdb40
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
movl %r13d, (%rbx,%rax,8)
movl $0x1, %ebp
jmp 0x6ca2
cmpl $-0x2, 0x8(%rsp)
jne 0x6a29
movl 0x7160(%rip), %eax      # 0xdb40
leal 0x1(%rax), %ecx
cmpl $0x7f, %eax
cmovgel %r12d, %ecx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %edi, 0x8(%rsp)
movslq %ecx, %rax
leaq (%rax,%rax,2), %rcx
movl $0xfffffffd, (%rbx,%rcx,8) # imm = 0xFFFFFFFD
leaq 0x7040(%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)
movl %eax, 0x711f(%rip)      # 0xdb40
movl %edi, (%rbx,%rcx,8)
movl $0x1, %ebp
movl 0x8(%rsp), %r13d
movl %r13d, %eax
notl %eax
orl %ebp, %eax
jne 0x6b36
leaq 0x6ff6(%rip), %rax      # 0xda38
movq (%rax), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
leaq 0x6ff5(%rip), %rcx      # 0xda48
movq (%rcx), %rcx
movq %rax, 0x18(%rcx)
movq $0x0, 0x20(%rcx)
jmp 0x6af8
cmpl $-0x1, 0x8(%rsp)
jne 0x6abd
movl 0x70cc(%rip), %eax      # 0xdb40
leal 0x1(%rax), %ecx
cmpl $0x7f, %eax
cmovgel %r12d, %ecx
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
movl %edi, 0x8(%rsp)
movslq %ecx, %rax
leaq (%rax,%rax,2), %rcx
movl $0xfffffffd, (%rbx,%rcx,8) # imm = 0xFFFFFFFD
leaq 0x6fac(%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)
movl %eax, 0x708b(%rip)      # 0xdb40
movl %edi, (%rbx,%rcx,8)
movl $0x1, %ebp
movl 0x8(%rsp), %r13d
movl %r13d, %eax
xorl $-0x2, %eax
orl %ebp, %eax
jne 0x6bda
leaq 0x6f61(%rip), %rax      # 0xda38
movq (%rax), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rax
leaq 0x6f5f(%rip), %rcx      # 0xda48
movq (%rcx), %rcx
movq %rax, 0x18(%rcx)
movslq 0x14(%rax), %rax
movq %rax, 0x20(%rcx)
movl %r13d, %edi
callq 0x7785
testl %eax, %eax
je 0x6cb4
movl 0x7032(%rip), %eax      # 0xdb40
leal 0x1(%rax), %ecx
cmpl $0x7f, %eax
cmovgel %r12d, %ecx
movl %ecx, 0x7022(%rip)      # 0xdb40
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
movl $0xfffffffb, (%rbx,%rax,8) # imm = 0xFFFFFFFB
movl $0x1, %ebp
jmp 0x6cb6
movl 0x7004(%rip), %eax      # 0xdb40
leal 0x1(%rax), %ecx
cmpl $0x7f, %eax
cmovgel %r12d, %ecx
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
movl $0xfffffffd, (%rbx,%rax,8) # imm = 0xFFFFFFFD
leaq 0x6eed(%rip), %rcx      # 0xda48
movq (%rcx), %rcx
movl 0x20(%rcx), %edx
movl %edx, 0x10(%rbx,%rax,8)
movq 0x18(%rcx), %rcx
movq %rcx, 0x8(%rbx,%rax,8)
leaq 0x6f0b(%rip), %rdi      # 0xda80
callq 0x20d0
movq %rax, %r12
movl %r12d, 0xc(%rsp)
movl $0x8, %edi
movl $0x1, %esi
callq 0x2470
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x7785
testl %eax, %eax
jne 0x6c47
movl $0x8, %edi
movl $0x1, %esi
callq 0x2412
xorl %ebp, %ebp
xorl %eax, %eax
leaq 0x6cab(%rip), %rcx      # 0xd868
callq *0x50(%rcx)
leaq 0x29eb(%rip), %rdi      # 0x95b2
leaq 0x6eb2(%rip), %rsi      # 0xda80
xorl %eax, %eax
callq 0x43cc
jmp 0x6ced
movl 0x6f60(%rip), %eax      # 0xdb40
leal 0x1(%rax), %ecx
cmpl $0x7f, %eax
cmovgel %r12d, %ecx
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
movl $0xfffffffd, (%rbx,%rax,8) # imm = 0xFFFFFFFD
leaq 0x6e49(%rip), %rcx      # 0xda48
movq (%rcx), %rcx
movl 0x20(%rcx), %edx
movl %edx, 0x10(%rbx,%rax,8)
movq 0x18(%rcx), %rcx
movq %rcx, 0x8(%rbx,%rax,8)
leaq 0x6e67(%rip), %rdi      # 0xda80
callq 0x20d0
movq %rax, %r12
movl %r12d, 0xc(%rsp)
movl $0x8, %edi
movl $0x1, %esi
callq 0x2412
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
callq 0x7785
testl %eax, %eax
je 0x6cd0
movl 0x6ef3(%rip), %eax      # 0xdb40
leal 0x1(%rax), %ecx
cmpl $0x7f, %eax
movl $0x0, %eax
cmovgel %eax, %ecx
movl %ecx, 0x6edf(%rip)      # 0xdb40
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
movl $0xfffffffb, (%rbx,%rax,8) # imm = 0xFFFFFFFB
jmp 0x6ced
xorl %ebp, %ebp
xorl %eax, %eax
leaq 0x6bec(%rip), %rcx      # 0xd868
callq *0x50(%rcx)
movl 0x6ebb(%rip), %eax      # 0xdb40
leal 0x1(%rax), %ecx
cmpl $0x7f, %eax
cmovgel %ebp, %ecx
movl %ecx, 0x6eac(%rip)      # 0xdb40
movslq %ecx, %rax
leaq (%rax,%rax,2), %rax
movl $0xfffffffc, (%rbx,%rax,8) # imm = 0xFFFFFFFC
movl 0x8(%rsp), %edi
xorl %esi, %esi
movl %ebp, %edx
callq 0x76b1
jmp 0x6836
xorl %ebp, %ebp
leaq 0x6dc3(%rip), %rsi      # 0xda80
leaq 0x28d5(%rip), %rdi      # 0x9599
xorl %eax, %eax
callq 0x43cc
jmp 0x6836
movl $0x8, %edi
movl $0x1, %esi
callq 0x2470
xorl %ebp, %ebp
xorl %eax, %eax
leaq 0x6b7e(%rip), %rcx      # 0xd868
callq *0x50(%rcx)
shrl $0x1f, %r12d
movl %r13d, %edi
movl %r12d, %esi
movl %ebp, %edx
callq 0x76b1
xorl %r12d, %r12d
jmp 0x6836
movl 0x8(%rsp), %eax
movl %eax, 0x6b50(%rip)      # 0xd860
leaq 0x6d31(%rip), %rax      # 0xda48
movq (%rax), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x28(%rax)
movslq 0x18(%rsp), %rcx
movq %rcx, 0x30(%rax)
xorl %edi, %edi
xorl %esi, %esi
callq 0x280d
jmp 0x6d63
leaq 0x6d0a(%rip), %rbx      # 0xda48
movq (%rbx), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x28(%rax)
movslq 0x18(%rsp), %rcx
movq %rcx, 0x30(%rax)
xorl %edi, %edi
xorl %esi, %esi
callq 0x280d
movq (%rbx), %rax
orb $0x2, 0x3b(%rax)
movl $0x1, %eax
jmp 0x6dbb
leaq 0x6cd7(%rip), %rax      # 0xda48
movq (%rax), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x18(%rax)
movslq 0x18(%rsp), %rcx
movq %rcx, 0x20(%rax)
orb $0x2, 0x3b(%rax)
movl 0x8(%rsp), %eax
movl %eax, 0x6acc(%rip)      # 0xd860
leaq 0x6ce5(%rip), %rdi      # 0xda80
leaq 0x20(%rsp), %rsi
movl $0x50, %edx
callq 0x671c
movl $0x8, %edi
xorl %esi, %esi
callq 0x5eb7
movl $0x2, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq