// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
leaq 0xcd03(%rip), %rax      # 0x10a48
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x1, -0x2c(%rbp)
movl -0x8(%rbp), %edi
movl -0xc(%rbp), %esi
callq 0x35f0
leaq 0xccfc(%rip), %rax      # 0x10a68
movq (%rax), %rdi
callq 0x2d50
xorl %esi, %esi
movl %esi, %edi
callq 0xb0c0
callq 0x40c0
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jle 0x3d93
movl -0x30(%rbp), %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x30(%rbp)
jle 0x3da8
movl -0x2c(%rbp), %esi
addl $0x1, %esi
xorl %edi, %edi
callq 0x2790
jmp 0x3db4
xorl %edi, %edi
movl $0x2, %esi
callq 0x2790
jmp 0x3db6
leaq 0x84c7(%rip), %rdi      # 0xc284
movb $0x0, %al
callq 0x56d0
movb $0x0, %al
callq 0x46b0
movb $0x0, %al
callq 0xaf00
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x3e18
movl -0x24(%rbp), %eax
movl %eax, -0x34(%rbp)
subl $0x51, %eax
je 0x3e06
jmp 0x3de8
movl -0x34(%rbp), %eax
subl $0x58, %eax
je 0x3e06
jmp 0x3df2
movl -0x34(%rbp), %eax
subl $0x71, %eax
je 0x3e06
jmp 0x3dfc
movl -0x34(%rbp), %eax
subl $0x78, %eax
jne 0x3e08
jmp 0x3e06
jmp 0x3e16
leaq 0xca69(%rip), %rcx      # 0x10878
movb $0x0, %al
callq *0x50(%rcx)
jmp 0x3db6
jmp 0x3e18
cmpl $0x1b, -0x24(%rbp)
jne 0x3e8b
callq 0x8100
movl %eax, -0x28(%rbp)
cmpl $0x5b, -0x28(%rbp)
jne 0x3e70
callq 0x8100
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x38(%rbp)
subl $0x41, %eax
je 0x3e4b
jmp 0x3e41
movl -0x38(%rbp), %eax
subl $0x42, %eax
je 0x3e54
jmp 0x3e5d
movl $0x10, -0x24(%rbp)
jmp 0x3e6e
movl $0xe, -0x24(%rbp)
jmp 0x3e6e
leaq 0xca14(%rip), %rcx      # 0x10878
movb $0x0, %al
callq *0x50(%rcx)
jmp 0x3db6
jmp 0x3e89
callq 0x8100
movl %eax, -0x28(%rbp)
leaq 0xc9f9(%rip), %rcx      # 0x10878
movb $0x0, %al
callq *0x50(%rcx)
jmp 0x3db6
jmp 0x3e8b
movl -0x24(%rbp), %eax
addl $-0xd, %eax
movl %eax, %ecx
movq %rcx, -0x40(%rbp)
subl $0x72, %eax
ja 0x40a5
movq -0x40(%rbp), %rax
leaq 0x820d(%rip), %rcx      # 0xc0b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jne 0x3ecd
leaq 0xc9b5(%rip), %rcx      # 0x10878
movb $0x0, %al
callq *0x50(%rcx)
jmp 0x40b1
xorl %edi, %edi
movl $0x1, %esi
callq 0x2790
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x40b1
cmpl $0x1, -0x2c(%rbp)
jne 0x3efe
leaq 0xc984(%rip), %rcx      # 0x10878
movb $0x0, %al
callq *0x50(%rcx)
jmp 0x40b1
xorl %edi, %edi
movl $0x1, %esi
callq 0x2960
movl -0x2c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x40b1
movl -0x2c(%rbp), %edi
callq 0x4120
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2d50
xorl %esi, %esi
movl %esi, %edi
callq 0xb0c0
callq 0x4260
movl $0x1, -0x4(%rbp)
jmp 0x40b6
movl -0x2c(%rbp), %edi
callq 0x4120
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2d50
xorl %esi, %esi
movl %esi, %edi
callq 0xb0c0
movq -0x20(%rbp), %rdi
callq 0x41b0
cmpl $0x1, %eax
jne 0x3f94
cmpl $0x1, -0x30(%rbp)
jle 0x3f94
xorl %esi, %esi
movl %esi, %edi
callq 0xb300
movq -0x20(%rbp), %rdi
callq 0x2d50
xorl %esi, %esi
movl %esi, %edi
callq 0xafc0
callq 0x4260
movl $0x1, -0x4(%rbp)
jmp 0x40b6
movl -0x2c(%rbp), %edi
callq 0x4120
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x3fe8
movq -0x18(%rbp), %rcx
leaq 0xca85(%rip), %rax      # 0x10a48
movq %rcx, (%rax)
xorl %esi, %esi
movl %esi, %edi
callq 0x6790
leaq 0xca92(%rip), %rax      # 0x10a68
movq (%rax), %rcx
leaq 0xca68(%rip), %rax      # 0x10a48
movq %rcx, (%rax)
jmp 0x3d5a
jmp 0x40b1
movl -0x2c(%rbp), %edi
callq 0x4120
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x400e
movq -0x18(%rbp), %rax
movsbl 0x32(%rax), %ecx
xorl $0x4, %ecx
movb %cl, 0x32(%rax)
jmp 0x3d5a
movl -0x2c(%rbp), %edi
callq 0x4120
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x402f
movq -0x18(%rbp), %rdi
callq 0x3460
jmp 0x3d5a
cmpl $0x0, -0x30(%rbp)
jne 0x4063
callq 0x3400
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2d50
xorl %esi, %esi
movl %esi, %edi
callq 0xb0c0
callq 0x4260
movl $0x1, -0x4(%rbp)
jmp 0x40b6
movq -0x20(%rbp), %rdi
callq 0x41b0
cmpl $0x1, %eax
jne 0x407c
movq -0x20(%rbp), %rdi
callq 0x2d50
jmp 0x408e
callq 0x3400
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2d50
xorl %esi, %esi
movl %esi, %edi
callq 0xb0c0
callq 0x4260
movl $0x1, -0x4(%rbp)
jmp 0x40b6
leaq 0xc7cc(%rip), %rcx      # 0x10878
movb $0x0, %al
callq *0x50(%rcx)
jmp 0x3db6
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop