// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movq %rdi, %rbx
movq 0x18(%rdi), %r15
movq %r15, %rdi
callq 0x3280
movq %rax, %r14
movq (%rbx), %rax
testq %rax, %rax
je 0x56ea
movswl 0x6(%rax), %ebp
jmp 0x56ef
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
movq %r15, %rdi
movq $-0x1, %rsi
callq 0x3180
cmpl %ebp, %eax
movq %r14, (%rsp)
jge 0x5710
movq $0x0, 0x40(%rbx)
jmp 0x5728
movq 0x38(%rbx), %rax
movq 0x40(%rbx), %rdi
movq %rax, %rsi
subq %rdi, %rsi
jae 0x57d1
movq %rax, 0x40(%rbx)
movq 0x40(%rbx), %rcx
xorl %r15d, %r15d
movq (%rsp), %r14
cmpq %r14, %rcx
jae 0x5772
xorl %eax, %eax
xorl %r12d, %r12d
xorl %r15d, %r15d
movl %r12d, %edx
movl %eax, %r12d
addl %edx, %r12d
cmpl %ebp, %r12d
jg 0x5772
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 0x5740
movq (%rbx), %rdi
callq 0x30e0
movq (%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3090
cmpl $-0x1, %eax
je 0x579c
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 $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5624
shlq $0x2, %rdi
addq 0x18(%rbx), %rdi
callq 0x3180
cmpl %ebp, %eax
jge 0x587b
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), %r13
xorl %eax, %eax
movq (%rsp), %rcx
cmpq %rcx, 0x38(%rbx)
sete %cl
testl %r13d, %r13d
jle 0x5728
movb %cl, %al
movl %ebp, %r14d
subl %eax, %r14d
xorl %eax, %eax
movl %r15d, %ecx
movl %eax, %r15d
addl %ecx, %r15d
cmpl %r14d, %r15d
jge 0x5728
movslq %r13d, %r13
movq %r13, %r12
decq %r13
movq 0x18(%rbx), %rax
movl -0x4(%rax,%r12,4), %edi
callq 0x3480
cmpq $0x2, %r12
jl 0x585f
testl %eax, %eax
jle 0x5841
leal (%rax,%r15), %ecx
cmpl %r14d, %ecx
jg 0x586c
movq %r13, 0x40(%rbx)
leaq 0x1(%r13), %rcx
cmpq $0x1, %rcx
jg 0x582c
jmp 0x5728
movq 0x18(%rbx), %rax
movq 0x40(%rbx), %rcx
movl (%rax,%rcx,4), %edi
callq 0x3480
testl %eax, %eax
jle 0x58b3
movq 0x38(%rbx), %rsi
movq 0x40(%rbx), %rax
leaq (,%rax,4), %rdi
addq 0x18(%rbx), %rdi
subq %rax, %rsi
callq 0x3180
cmpl %ebp, %eax
jl 0x5728
incq 0x40(%rbx)
jmp 0x587b