// File: /workspace/github_c_makefile_star2_O1/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 0xaa2f(%rip), %rax      # 0xda38
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movl $0x1, %eax
movq %rax, (%rsp)
leaq 0xaa37(%rip), %r15      # 0xda58
leaq 0xab08(%rip), %rbp      # 0xdb30
leaq 0x60f9(%rip), %r12      # 0x9128
movabsq $0x8100000081, %r14     # imm = 0x8100000081
leaq 0xa828(%rip), %rbx      # 0xd868
movl %esi, 0xc(%rsp)
movl %edi, 0x8(%rsp)
callq 0x2c42
movq (%r15), %rdi
callq 0x2859
xorl %edi, %edi
xorl %esi, %esi
callq 0x7d05
movq (%rbp), %rax
movl $0x0, %r15d
testq %rax, %rax
je 0x3084
xorl %r15d, %r15d
movzbl 0x32(%rax), %ecx
notl %ecx
andl $0x1, %ecx
addl %ecx, %r15d
movq (%rax), %rax
testq %rax, %rax
jne 0x3070
movq (%rsp), %rax
cmpl %r15d, %eax
cmovgel %r15d, %eax
movq %rax, (%rsp)
leal 0x1(%rax), %esi
testl %r15d, %r15d
movl $0x2, %eax
cmovlel %eax, %esi
xorl %edi, %edi
callq 0x259a
movq %r12, %rdi
xorl %eax, %eax
callq 0x43cc
xorl %eax, %eax
callq 0x3705
xorl %eax, %eax
callq 0x7c0c
testl %r15d, %r15d
je 0x30ee
cmpl $0x1b, %eax
jne 0x30fe
callq 0x5cf6
movl %eax, %r13d
callq 0x5cf6
cmpl $0x5b, %r13d
jne 0x3154
cmpl $0x41, %eax
je 0x315e
cmpl $0x42, %eax
jne 0x3154
movl $0xe, %eax
jmp 0x30fe
movl %eax, %ecx
addl $-0x51, %ecx
cmpl $0x27, %ecx
ja 0x3154
btq %rcx, %r14
jae 0x3154
cmpl $0x4a, %eax
jle 0x3120
cmpl $0x6a, %eax
jg 0x3131
leal -0x4b(%rax), %ecx
cmpl $0xd, %ecx
ja 0x318d
leaq 0x5fa1(%rip), %rdx      # 0x90b8
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
cmpl $0x1f, %eax
jle 0x3149
cmpl $0x24, %eax
jg 0x3175
cmpl $0x20, %eax
je 0x314e
jmp 0x3154
leal -0x6b(%rax), %ecx
cmpl $0xd, %ecx
ja 0x3194
leaq 0x5fb0(%rip), %rdx      # 0x90f0
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
cmpl $0xe, %eax
jne 0x3165
cmpl (%rsp), %r15d
jne 0x31b5
xorl %eax, %eax
callq *0x50(%rbx)
jmp 0x30a8
movl $0x10, %eax
jmp 0x30fe
cmpl $0xd, %eax
je 0x3290
cmpl $0x10, %eax
jne 0x3154
jmp 0x3199
cmpl $0x31, %eax
jg 0x31d0
cmpl $0x25, %eax
je 0x3212
cmpl $0x31, %eax
jne 0x3154
jmp 0x3290
cmpl $0x64, %eax
jne 0x3154
jmp 0x31ff
cmpl $0x7f, %eax
jne 0x3154
movq (%rsp), %rax
cmpl $0x1, %eax
jne 0x31e4
xorl %eax, %eax
callq *0x50(%rbx)
movl $0x1, %eax
movq %rax, (%rsp)
jmp 0x30a8
xorl %edi, %edi
movl $0x1, %esi
callq 0x259a
movq (%rsp), %rax
incl %eax
movq %rax, (%rsp)
jmp 0x30a8
cmpl $0x32, %eax
je 0x32c3
cmpl $0x44, %eax
jne 0x3154
jmp 0x31ff
xorl %edi, %edi
movl $0x1, %esi
movq %rax, %r13
callq 0x267a
decl %r13d
movq %r13, (%rsp)
jmp 0x30a8
movq (%rsp), %rdi
callq 0x3345
movq %rax, %rdi
callq 0x2b9b
jmp 0x321f
movq (%rsp), %rdi
callq 0x3345
xorb $0x4, 0x32(%rax)
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edi
leaq 0xa82a(%rip), %r15      # 0xda58
jmp 0x3048
movq (%rsp), %rdi
callq 0x3345
leaq 0xa7f5(%rip), %r15      # 0xda38
movq %rax, (%r15)
xorl %edi, %edi
xorl %esi, %esi
callq 0x4e38
leaq 0xa802(%rip), %rcx      # 0xda58
movq (%rcx), %rax
movq %rax, (%r15)
movq %rcx, %r15
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edi
jmp 0x3048
testl %r15d, %r15d
je 0x3289
movq 0x10(%rsp), %rdi
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x3284
cmpq %rdi, %rbp
jne 0x3276
testq %rbp, %rbp
jne 0x329c
callq 0x2b6f
jmp 0x3299
movq (%rsp), %rdi
callq 0x3345
movq %rax, %rdi
callq 0x2859
xorl %edi, %edi
xorl %esi, %esi
callq 0x7d05
callq 0x33c8
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rdi
callq 0x3345
movq %rax, %rdi
callq 0x2859
xorl %edi, %edi
xorl %esi, %esi
callq 0x7d05
leaq 0xa84c(%rip), %rax      # 0xdb30
movq 0x10(%rsp), %rbx
movq (%rax), %rax
testq %rax, %rax
je 0x32f6
cmpq %rbx, %rax
jne 0x32e9
testq %rax, %rax
sete %al
cmpl $0x2, %r15d
setl %cl
orb %al, %cl
jne 0x32aa
xorl %edi, %edi
xorl %esi, %esi
callq 0x7df0
movq %rbx, %rdi
callq 0x2859
xorl %edi, %edi
xorl %esi, %esi
callq 0x7c81
jmp 0x32aa