// File: /workspace/github_c_makefile_star2_O0/cglindkamp[P]dfm/src/commandline.c
// Function: commandline_handlekey
int commandline_handlekey(struct commandline *commandline, wint_t key, bool iskeycode)
{
	size_t length = wcslen(commandline->buffer);
	size_t cursorpos = commandline->cursor_pos;

	if(iskeycode) {
		switch(key) {
		case KEY_BACKSPACE:
			if(commandline_copy_history_buffer(commandline) != 0)
				return ENOMEM;
			commandline_delete_character_before_cursor(commandline);
			break;
		case KEY_DC:
			if(commandline_copy_history_buffer(commandline) != 0)
				return ENOMEM;
			if(commandline->cursor_pos < length) {
				size_t distance = 0;
				do {
					distance++;
				} while(commandline->cursor_pos + distance < length && wcwidth(commandline->buffer[commandline->cursor_pos + distance]) < 1);
				wmemmove(commandline->buffer + cursorpos, commandline->buffer + cursorpos + distance, length - cursorpos - distance + 1);
			}
			break;
		case KEY_LEFT:
			if(commandline->cursor_pos > 0) {
				int width;
				do {
					width = wcwidth(commandline->buffer[commandline->cursor_pos - 1]);
					commandline->cursor_pos--;
				} while(commandline->cursor_pos > 0 && width < 1);
			}
			break;
		case KEY_RIGHT:
			if(commandline->cursor_pos < length) {
				do {
					commandline->cursor_pos++;
				} while(commandline->cursor_pos < length && wcwidth(commandline->buffer[commandline->cursor_pos]) < 1);
			}
			break;
		case KEY_HOME:
			commandline->cursor_pos = 0;
			break;
		case KEY_END:
			commandline->cursor_pos = length;
			break;
		case KEY_UP:
			if(commandline->history_position < list_length(commandline->history)) {
				if(commandline->history_position == 0)
					commandline->backup_buffer = commandline->buffer;
				commandline->history_position++;
				commandline->buffer = list_get_item(commandline->history, list_length(commandline->history) - commandline->history_position);
			}
			commandline->cursor_pos = wcslen(commandline->buffer);
			break;
		case KEY_DOWN:
			if(commandline->history_position > 0) {
				commandline->history_position--;
				if(commandline->history_position == 0) {
					commandline->buffer = commandline->backup_buffer;
					commandline->backup_buffer = NULL;
				} else
					commandline->buffer = list_get_item(commandline->history, list_length(commandline->history) - commandline->history_position);
			}
			commandline->cursor_pos = wcslen(commandline->buffer);
			break;
		}
	} else {
		if(key == 23) { /* Ctrl-W */
			if(commandline_copy_history_buffer(commandline) != 0)
				return ENOMEM;

			while(commandline->cursor_pos > 0 && commandline->buffer[commandline->cursor_pos - 1] == L' ') {
				commandline_delete_character_before_cursor(commandline);
			}
			while(commandline->cursor_pos > 0 && commandline->buffer[commandline->cursor_pos - 1] != L' ') {
				commandline_delete_character_before_cursor(commandline);
			}

			commandline_updateview(commandline);
			return 0;
		}

		if(key == 0 || wcwidth(key) < 0)
			return EINVAL;

		if(commandline->cursor_pos == 0 && wcwidth(key) == 0)
			return EINVAL;

		if(commandline_copy_history_buffer(commandline) != 0)
			return ENOMEM;

		if(length == commandline->allocated_size - 1)
			if(commandline_makeroom(commandline) != 0)
				return ENOMEM;

		wmemmove(commandline->buffer + cursorpos + 1, commandline->buffer + cursorpos, length - cursorpos);
		commandline->buffer[cursorpos] = key;
		commandline->buffer[length + 1] = L'\0';
		commandline->cursor_pos++;
	}

	commandline_updateview(commandline);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movb %dl, %al
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
andb $0x1, %al
movb %al, -0x15(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x3280
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x28(%rbp)
testb $0x1, -0x15(%rbp)
je 0x6db9
movl -0x14(%rbp), %eax
movl %eax, -0x38(%rbp)
subl $0x102, %eax            # imm = 0x102
je 0x6d1e
jmp 0x6a30
movl -0x38(%rbp), %eax
subl $0x103, %eax            # imm = 0x103
je 0x6c79
jmp 0x6a40
movl -0x38(%rbp), %eax
subl $0x104, %eax            # imm = 0x104
je 0x6b91
jmp 0x6a50
movl -0x38(%rbp), %eax
subl $0x105, %eax            # imm = 0x105
je 0x6bf5
jmp 0x6a60
movl -0x38(%rbp), %eax
subl $0x106, %eax            # imm = 0x106
je 0x6c57
jmp 0x6a70
movl -0x38(%rbp), %eax
subl $0x107, %eax            # imm = 0x107
je 0x6a9b
jmp 0x6a7c
movl -0x38(%rbp), %eax
subl $0x14a, %eax            # imm = 0x14A
je 0x6ac3
jmp 0x6a88
movl -0x38(%rbp), %eax
subl $0x168, %eax            # imm = 0x168
je 0x6c68
jmp 0x6db4
movq -0x10(%rbp), %rdi
callq 0x6f90
cmpl $0x0, %eax
je 0x6ab5
movl $0xc, -0x4(%rbp)
jmp 0x6f82
movq -0x10(%rbp), %rdi
callq 0x7050
jmp 0x6db4
movq -0x10(%rbp), %rdi
callq 0x6f90
cmpl $0x0, %eax
je 0x6add
movl $0xc, -0x4(%rbp)
jmp 0x6f82
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
cmpq -0x20(%rbp), %rax
jae 0x6b8c
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
addq -0x30(%rbp), %rcx
xorl %eax, %eax
cmpq -0x20(%rbp), %rcx
movb %al, -0x39(%rbp)
jae 0x6b3f
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x38(%rcx), %rcx
addq -0x30(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x3480
cmpl $0x1, %eax
setl %al
movb %al, -0x39(%rbp)
movb -0x39(%rbp), %al
testb $0x1, %al
jne 0x6af7
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x28(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movq -0x30(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movq -0x20(%rbp), %rdx
subq -0x28(%rbp), %rdx
subq -0x30(%rbp), %rdx
addq $0x1, %rdx
callq 0x35d0
jmp 0x6db4
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jbe 0x6bf0
jmp 0x6b9e
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x38(%rcx), %rcx
subq $0x1, %rcx
movl (%rax,%rcx,4), %edi
callq 0x3480
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x38(%rcx)
movb %al, -0x3a(%rbp)
jbe 0x6be7
cmpl $0x1, -0x34(%rbp)
setl %al
movb %al, -0x3a(%rbp)
movb -0x3a(%rbp), %al
testb $0x1, %al
jne 0x6b9e
jmp 0x6bf0
jmp 0x6db4
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
cmpq -0x20(%rbp), %rax
jae 0x6c52
jmp 0x6c05
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
xorl %eax, %eax
cmpq -0x20(%rbp), %rcx
movb %al, -0x3b(%rbp)
jae 0x6c49
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x38(%rcx), %rcx
movl (%rax,%rcx,4), %edi
callq 0x3480
cmpl $0x1, %eax
setl %al
movb %al, -0x3b(%rbp)
movb -0x3b(%rbp), %al
testb $0x1, %al
jne 0x6c05
jmp 0x6c52
jmp 0x6db4
movq -0x10(%rbp), %rax
movq $0x0, 0x38(%rax)
jmp 0x6db4
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0x6db4
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xa090
movq %rax, %rcx
movq -0x48(%rbp), %rax
cmpq %rcx, %rax
jae 0x6d01
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x6cb9
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xa090
movq -0x50(%rbp), %rdi
movq %rax, %rsi
movq -0x10(%rbp), %rax
subq 0x10(%rax), %rsi
callq 0xa430
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x3280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0x6db4
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jbe 0x6d9c
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x6d62
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x20(%rax)
jmp 0x6d9a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xa090
movq -0x58(%rbp), %rdi
movq %rax, %rsi
movq -0x10(%rbp), %rax
subq 0x10(%rax), %rsi
callq 0xa430
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0x6d9c
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x3280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0x6f72
cmpl $0x17, -0x14(%rbp)
jne 0x6e7a
movq -0x10(%rbp), %rdi
callq 0x6f90
cmpl $0x0, %eax
je 0x6ddd
movl $0xc, -0x4(%rbp)
jmp 0x6f82
jmp 0x6ddf
movq -0x10(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x38(%rcx)
movb %al, -0x59(%rbp)
jbe 0x6e0d
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x38(%rcx), %rcx
subq $0x1, %rcx
cmpl $0x20, (%rax,%rcx,4)
sete %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0x6e16
jmp 0x6e21
movq -0x10(%rbp), %rdi
callq 0x7050
jmp 0x6ddf
jmp 0x6e23
movq -0x10(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x38(%rcx)
movb %al, -0x5a(%rbp)
jbe 0x6e51
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x38(%rcx), %rcx
subq $0x1, %rcx
cmpl $0x20, (%rax,%rcx,4)
setne %al
movb %al, -0x5a(%rbp)
movb -0x5a(%rbp), %al
testb $0x1, %al
jne 0x6e5a
jmp 0x6e65
movq -0x10(%rbp), %rdi
callq 0x7050
jmp 0x6e23
movq -0x10(%rbp), %rdi
callq 0x6680
movl $0x0, -0x4(%rbp)
jmp 0x6f82
cmpl $0x0, -0x14(%rbp)
je 0x6e8d
movl -0x14(%rbp), %edi
callq 0x3480
cmpl $0x0, %eax
jge 0x6e99
movl $0x16, -0x4(%rbp)
jmp 0x6f82
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x6ebd
movl -0x14(%rbp), %edi
callq 0x3480
cmpl $0x0, %eax
jne 0x6ebd
movl $0x16, -0x4(%rbp)
jmp 0x6f82
movq -0x10(%rbp), %rdi
callq 0x6f90
cmpl $0x0, %eax
je 0x6ed7
movl $0xc, -0x4(%rbp)
jmp 0x6f82
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x6f05
movq -0x10(%rbp), %rdi
callq 0x7150
cmpl $0x0, %eax
je 0x6f03
movl $0xc, -0x4(%rbp)
jmp 0x6f82
jmp 0x6f05
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
addq $0x4, %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x28(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movq -0x20(%rbp), %rdx
subq -0x28(%rbp), %rdx
callq 0x35d0
movl -0x14(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x20(%rbp), %rcx
movl $0x0, 0x4(%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rdi
callq 0x6680
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)