// File: /workspace/github_c_makefile_star2_O3/cglindkamp[P]dfm/src/commandline.c
// Function: commandline_updateview
static void commandline_updateview(struct commandline *commandline)
{
	size_t length = wcslen(commandline->buffer);
	int promptlength = 1;
	int windowwidth = getmaxx(commandline->window);

	if(wcswidth(commandline->buffer, -1) < windowwidth - promptlength)
		commandline->first = 0;
	else if(commandline->cursor_pos < commandline->first)
		commandline->first = commandline->cursor_pos;
	else if(wcswidth(commandline->buffer + commandline->first, commandline->cursor_pos - commandline->first) >= windowwidth - promptlength) {
		while(wcwidth(*(commandline->buffer + commandline->first)) < 1 ||
		      wcswidth(commandline->buffer + commandline->first, commandline->cursor_pos - commandline->first) >= windowwidth - promptlength) {
			commandline->first++;
		}
	} else {
		int currentwidth = wcswidth(commandline->buffer + commandline->first, commandline->cursor_pos - commandline->first + 1);
		int targetwidth = windowwidth - promptlength - (commandline->cursor_pos == length ? 1 : 0);
		int first = commandline->first;
		int firstwidth = 0;

		while(first > 0 && currentwidth + firstwidth < targetwidth) {
			currentwidth += firstwidth;

			do {
				first--;
				firstwidth = wcwidth(commandline->buffer[first]);
			} while(first > 0 && firstwidth < 1);

			if(currentwidth + firstwidth <= targetwidth)
				commandline->first = first;
		}
	}

	size_t distance = 0;
	int width = 0;
	int currentwidth = 0;
	while(commandline->first + distance < length && width + currentwidth <= windowwidth - promptlength) {
		width += currentwidth;
		currentwidth = wcwidth(*(commandline->buffer + commandline->first + distance));
		distance++;
	}

	werase(commandline->window);
	mvwaddwstr(commandline->window, 0, 0, commandline->prompt);
	waddnwstr(commandline->window, commandline->buffer + commandline->first, distance);
	wrefresh(commandline->window);
	commandline_updatecursor(commandline);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %r15
movq %r15, %rdi
callq 0x3280
movq (%rbx), %rcx
testq %rcx, %rcx
movq %rax, 0x10(%rsp)
je 0x5688
movswl 0x6(%rcx), %r13d
jmp 0x568e
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
movq %r15, %rdi
movq $-0x1, %rsi
callq 0x3180
cmpl %r13d, %eax
jge 0x56ac
movq $0x0, 0x40(%rbx)
jmp 0x56c4
movq 0x38(%rbx), %rax
movq 0x40(%rbx), %rdi
movq %rax, %rsi
subq %rdi, %rsi
jae 0x576e
movq %rax, 0x40(%rbx)
movq 0x40(%rbx), %rcx
xorl %r15d, %r15d
movq 0x10(%rsp), %r14
cmpq %r14, %rcx
jae 0x570f
xorl %eax, %eax
xorl %r12d, %r12d
xorl %r15d, %r15d
movl %r12d, %edx
movl %eax, %r12d
addl %edx, %r12d
cmpl %r13d, %r12d
jg 0x570f
shlq $0x2, %rcx
addq 0x18(%rbx), %rcx
movl (%rcx,%r15,4), %edi
callq 0x3480
movq 0x40(%rbx), %rcx
leaq (%rcx,%r15), %rdx
incq %rdx
incq %r15
cmpq %r14, %rdx
jb 0x56dd
movq (%rbx), %rdi
callq 0x30e0
movq (%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3090
cmpl $-0x1, %eax
je 0x5739
movq (%rbx), %rdi
leaq 0x30(%rbx), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x32d0
movq (%rbx), %rdi
movq 0x40(%rbx), %rsi
shlq $0x2, %rsi
addq 0x18(%rbx), %rsi
movl %r15d, %edx
callq 0x32d0
movq (%rbx), %rdi
callq 0x3390
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x55bc
shlq $0x2, %rdi
addq 0x18(%rbx), %rdi
callq 0x3180
cmpl %r13d, %eax
jge 0x5833
movq 0x38(%rbx), %rsi
movq 0x40(%rbx), %rax
leaq (,%rax,4), %rdi
addq 0x18(%rbx), %rdi
subq %rax, %rsi
incq %rsi
callq 0x3180
movl %eax, %r15d
movq 0x40(%rbx), %rax
xorl %ecx, %ecx
movq 0x10(%rsp), %rdx
cmpq %rdx, 0x38(%rbx)
sete %dl
testl %eax, %eax
jle 0x56c4
movb %dl, %cl
movl %r13d, %edx
subl %ecx, %edx
movl %eax, %ebp
xorl %eax, %eax
movl %r13d, 0xc(%rsp)
movl %edx, 0x8(%rsp)
movl %r15d, %ecx
movl %eax, %r15d
addl %ecx, %r15d
cmpl %edx, %r15d
jge 0x56c4
movl %ebp, %eax
incl %ebp
movl %ebp, %r12d
movq %rax, %rbp
movq %rbp, %r13
decq %rbp
movq 0x18(%rbx), %rax
movl %ebp, %r14d
movl (%rax,%r14,4), %edi
callq 0x3480
decl %r12d
cmpq $0x2, %r13
jb 0x5813
testl %eax, %eax
jle 0x57f0
leal (%rax,%r15), %ecx
movl 0x8(%rsp), %edx
cmpl %edx, %ecx
jg 0x5823
movq %r14, 0x40(%rbx)
cmpl $0x1, %r12d
movl 0xc(%rsp), %r13d
jg 0x57d4
jmp 0x56c4
movq 0x40(%rbx), %rax
movq 0x18(%rbx), %rcx
movl (%rcx,%rax,4), %edi
callq 0x3480
testl %eax, %eax
jle 0x586c
movq 0x38(%rbx), %rsi
movq 0x40(%rbx), %rax
leaq (,%rax,4), %rdi
addq 0x18(%rbx), %rdi
subq %rax, %rsi
callq 0x3180
cmpl %r13d, %eax
jl 0x56c4
movq 0x40(%rbx), %rax
incq %rax
movq %rax, 0x40(%rbx)
jmp 0x5837