// File: /workspace/github_c_makefile_star2_O3/hughbarney[P]pEmacs/bufmenu.c
// Function: buffermenu
int buffermenu(int f, int n)
{
	BUFFER *bp;
	BUFFER *org_bp = curbp;
	int c,k;
	int bufptr;
	int bufcount = 0;

	bufptr = 1;

start:
	listbuffers(f,n);
	swbuffer(blistp);
	onlywind(0,0);
	bufcount = count_buffers();

	if (bufptr > bufcount)
		bufptr = bufcount;

	if (bufcount > 0)
		forwline(0, bufptr + 1);
	else
		forwline(0, 2);

	for (;;)
	{
		mlwrite("Buffer Menu: 1,2,s,v,k,q ");
		update();
		c = ttgetc();

		/* if no buffers, only allow exit */
		if (bufcount == 0)
		{
			switch (c)
			{
			case 'q': case 'Q': case 'x': case 'X':
				break;
			default:
				(*term.t_beep) ();
				continue;
			}
		}

		/*
		 * pre process escape sequence to get up/down arrows
		 * convert to CTRL+N, CTRL+P 
		 */
		if (c == ESC)
		{
			k = getctl();
			if (k == '[')
			{
				k = getctl();
				switch(k)
				{
				case 'A': c = CTRL_P; break;
				case 'B': c = CTRL_N; break;
				default: 
					(*term.t_beep)();
					continue;
				}
			}
			else
			{
				k = getctl();
				(*term.t_beep) ();
				continue;
			}
		}  /* if ESC */

		switch (c)
		{
		case 'n':
		case 'N':
		case ' ':
		case CTRL_N:
			if (bufcount == bufptr)
			{
				(*term.t_beep) ();
				break;
			}
			forwline(0,1);
			bufptr++;
			break;

		case 'p':
		case 'P':
		case CTRL_P:
		case BACKSPACE:
			if (bufptr == 1)
			{
				(*term.t_beep) ();
				break;
			}
			backline(0,1);
			bufptr--;
			break;

		case '1':
		case CTRL_M:
			bp = get_buffer(bufptr);
			swbuffer(bp);
			onlywind(0,0);
			mlerase();
			return TRUE;

		case '2':
			bp = get_buffer(bufptr);
			swbuffer(bp);
			onlywind(0,0);
			/* need to check or is still valid */
			if (valid_buf(org_bp) == TRUE && bufcount > 1)
			{
				splitwind(0,0);
				swbuffer(org_bp);
				nextwind(0,0);
			}
			mlerase();
			return TRUE;

			/* save file */
		case 's':
		case 'S':
			bp = get_buffer(bufptr);
			if (bp != NULL)
			{
				curbp = bp;
				(void)filesave(0,0);
				curbp = blistp;
				goto start;
			}
			break;

			/* toggle read only */
		case 'v':
		case 'V':
		case '%':
			bp = get_buffer(bufptr);
			if (bp != NULL)  /* be defensive */
				bp->b_flag ^= BFRO;
			goto start;
			break;

			/* kill buffer */
		case 'd':
		case 'D':
		case 'k':
		case 'K':
			bp = get_buffer(bufptr);
			if (bp != NULL)
				zotbuf(bp);
			goto start;
			break;

			/* exit buffer menu */
		case 'q':
		case 'Q':
		case 'x':
		case 'X':
			if (bufcount == 0)
			{
				bp = get_scratch();
				swbuffer(bp);
				onlywind(0,0);
				mlerase();
				return TRUE;
			}

			if (valid_buf(org_bp) == TRUE)
				swbuffer(org_bp);
			else
			{
				bp = get_scratch();
				swbuffer(bp);
			}
			onlywind(0,0);
			mlerase();
			return TRUE;

			/* any other key */
		default:
			(*term.t_beep) ();
			break;
		}
	}

	mlerase();
	return TRUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
leaq 0xaa4b(%rip), %rax      # 0xda38
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movl $0x1, %r12d
leaq 0xaa56(%rip), %r14      # 0xda58
leaq 0xab27(%rip), %rbp      # 0xdb30
leaq 0x6118(%rip), %r15      # 0x9128
leaq 0xa851(%rip), %rbx      # 0xd868
movl %esi, 0xc(%rsp)
movl %edi, 0x8(%rsp)
callq 0x2c36
movq (%r14), %rdi
callq 0x2845
xorl %edi, %edi
xorl %esi, %esi
callq 0x7d79
movq (%rbp), %rax
testq %rax, %rax
je 0x306e
xorl %r14d, %r14d
movzbl 0x32(%rax), %ecx
notl %ecx
andl $0x1, %ecx
addl %ecx, %r14d
movq (%rax), %rax
testq %rax, %rax
jne 0x3041
cmpl %r14d, %r12d
cmovgel %r14d, %r12d
leal 0x1(%r12), %esi
testl %r14d, %r14d
movl $0x2, %eax
cmovlel %eax, %esi
jmp 0x307f
movl %r12d, %eax
sarl $0x1f, %eax
andl %eax, %r12d
xorl %r14d, %r14d
movl $0x2, %esi
xorl %edi, %edi
callq 0x259a
movq %r15, %rdi
xorl %eax, %eax
callq 0x4246
xorl %eax, %eax
callq 0x370f
xorl %eax, %eax
callq 0x7c80
testl %r14d, %r14d
je 0x30dc
cmpl $0x1b, %eax
jne 0x30fb
callq 0x5bb6
movl %eax, %r13d
callq 0x5bb6
cmpl $0x5b, %r13d
jne 0x30f4
cmpl $0x41, %eax
je 0x3161
cmpl $0x42, %eax
jne 0x30f4
cmpl %r12d, %r14d
jne 0x3193
xorl %eax, %eax
callq *0x50(%rbx)
movl %r14d, %r12d
jmp 0x3086
leal -0x51(%rax), %ecx
cmpl $0x27, %ecx
ja 0x30f4
movabsq $0x8100000081, %rdx     # imm = 0x8100000081
btq %rcx, %rdx
jb 0x30fb
xorl %eax, %eax
callq *0x50(%rbx)
jmp 0x3086
cmpl $0x63, %eax
jg 0x3121
cmpl $0x31, %eax
jle 0x3139
leal -0x4b(%rax), %ecx
cmpl $0xd, %ecx
ja 0x31bb
leaq 0x5fa0(%rip), %rdx      # 0x90b8
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
leal -0x6b(%rax), %ecx
cmpl $0xd, %ecx
ja 0x3157
leaq 0x5fc0(%rip), %rdx      # 0x90f0
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
cmpl $0x1f, %eax
jg 0x3177
cmpl $0xd, %eax
je 0x3258
cmpl $0xe, %eax
je 0x30c9
cmpl $0x10, %eax
jne 0x30f4
jmp 0x3161
cmpl $0x64, %eax
je 0x31cd
cmpl $0x7f, %eax
jne 0x30f4
cmpl $0x1, %r12d
jne 0x31a7
xorl %eax, %eax
callq *0x50(%rbx)
movl $0x1, %r12d
jmp 0x3086
cmpl $0x20, %eax
je 0x30c9
cmpl $0x25, %eax
je 0x31df
cmpl $0x31, %eax
jne 0x30f4
jmp 0x3258
xorl %edi, %edi
movl $0x1, %esi
callq 0x259a
incl %r12d
jmp 0x3086
xorl %edi, %edi
movl $0x1, %esi
callq 0x267a
decl %r12d
jmp 0x3086
cmpl $0x32, %eax
je 0x328a
cmpl $0x44, %eax
jne 0x30f4
movl %r12d, %edi
callq 0x3300
movq %rax, %rdi
callq 0x2b87
jmp 0x31eb
movl %r12d, %edi
callq 0x3300
xorb $0x4, 0x32(%rax)
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edi
leaq 0xa85e(%rip), %r14      # 0xda58
jmp 0x301f
movl %r12d, %edi
callq 0x3300
leaq 0xa82a(%rip), %r14      # 0xda38
movq %rax, (%r14)
xorl %edi, %edi
xorl %esi, %esi
callq 0x4ce4
leaq 0xa837(%rip), %rcx      # 0xda58
movq (%rcx), %rax
movq %rax, (%r14)
movq %rcx, %r14
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edi
jmp 0x301f
testl %r14d, %r14d
je 0x3251
movq 0x10(%rsp), %rdi
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x3251
cmpq %rdi, %rbp
jne 0x3241
jmp 0x3263
callq 0x2b5b
jmp 0x3260
movl %r12d, %edi
callq 0x3300
movq %rax, %rdi
callq 0x2845
xorl %edi, %edi
xorl %esi, %esi
callq 0x7d79
callq 0x3384
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r12d, %edi
callq 0x3300
movq %rax, %rdi
callq 0x2845
xorl %edi, %edi
xorl %esi, %esi
callq 0x7d79
leaq 0xa886(%rip), %rax      # 0xdb30
movq 0x10(%rsp), %rbx
movq (%rax), %rax
testq %rax, %rax
je 0x3271
cmpq %rbx, %rax
jne 0x32af
cmpl $0x2, %r14d
jl 0x3271
xorl %edi, %edi
xorl %esi, %esi
callq 0x7e67
movq %rbx, %rdi
callq 0x2845
xorl %edi, %edi
xorl %esi, %esi
callq 0x7cf5
jmp 0x3271