// File: /workspace/github_c_makefile_star2_O3/seanpringle[P]xoat/./textbox.c
// Function: textbox_draw
void textbox_draw(Textbox *tb)
{
	XGlyphInfo extents;

	GC context    = XCreateGC(display, tb->window, 0, 0);
	Pixmap canvas = XCreatePixmap(display, tb->window, tb->w, tb->h, DefaultDepth(display, DefaultScreen(display)));
	XftDraw *draw = XftDrawCreate(display, canvas, DefaultVisual(display, DefaultScreen(display)), DefaultColormap(display, DefaultScreen(display)));

	// clear canvas
	XftDrawRect(draw, &tb->color_bg, 0, 0, tb->w, tb->h);

	char *line  = tb->text,
		*text   = tb->text ? tb->text: "",
		*prompt = tb->prompt ? tb->prompt: "",
		*eline  = NULL;

	int text_len    = strlen(text);
	int length      = text_len;
	int line_height = tb->font->ascent + tb->font->descent;

	int cursor_x      = 0;
	int cursor_offset = 0;
	int cursor_width  = MAX(2, line_height/10);

	if (tb->flags & TB_EDITABLE)
	{
		int prompt_len = strlen(prompt);
		length = text_len + prompt_len;
		cursor_offset = MIN(tb->cursor + prompt_len, length);

		eline = calloc(length+10, sizeof(char)); line = eline;
		sprintf(line, "%s%s", prompt, text);

		// replace trailing space so XftTextExtents8 includes its width
		if (length > 0 && isspace(line[length-1])) line[length-1] = '.';

		// calc cursor position
		XftTextExtentsUtf8(display, tb->font, (unsigned char*)line, cursor_offset, &extents);
		cursor_x = extents.width;

		// restore correct text string
		sprintf(line, "%s%s", prompt, text);
	}

	// calc full input text width
	XftTextExtentsUtf8(display, tb->font, (unsigned char*)line, length, &extents);
	int line_width = extents.width;

	int x = 0, y = tb->font->ascent;
	if (tb->flags & TB_RIGHT)  x = tb->w - line_width;
	if (tb->flags & TB_CENTER) x = MAX(0, (tb->w - line_width) / 2);

	// draw the text, including any prompt in edit mode
	XftDrawStringUtf8(draw, &tb->color_fg, tb->font, x, y, (unsigned char*)line, length);

	// draw the cursor
	if (tb->flags & TB_EDITABLE)
		XftDrawRect(draw, &tb->color_fg, cursor_x, 2, cursor_width, line_height-4);

	// flip canvas to window
	XCopyArea(display, canvas, tb->window, context, 0, 0, tb->w, tb->h, 0, 0);

	XFreeGC(display, context);
	XftDrawDestroy(draw);
	XFreePixmap(display, canvas);

	free(eline);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r15
movq 0xa53c(%rip), %rdi      # 0xebb0
movq 0x8(%r15), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x33c0
movq %rax, 0x30(%rsp)
movq 0xa523(%rip), %rdi      # 0xebb0
movq 0x8(%r15), %rsi
movswl 0x1c(%r15), %edx
movswl 0x1e(%r15), %ecx
movq 0xe8(%rdi), %rax
movslq 0xe0(%rdi), %r8
shlq $0x7, %r8
movl 0x38(%rax,%r8), %r8d
callq 0x3650
movq %rax, %rsi
movq 0xa4ef(%rip), %rdi      # 0xebb0
movq 0xe8(%rdi), %rax
movslq 0xe0(%rdi), %rcx
shlq $0x7, %rcx
movq 0x40(%rax,%rcx), %rdx
movq 0x50(%rax,%rcx), %rcx
movq %rsi, 0x28(%rsp)
callq 0x3670
leaq 0x40(%r15), %rsi
movswl 0x1c(%r15), %r8d
movswl 0x1e(%r15), %r9d
movq %rax, 0x20(%rsp)
movq %rax, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x32c0
movq 0x50(%r15), %r14
testq %r14, %r14
leaq 0x5ff6(%rip), %rax      # 0xa70a
movq %r14, %rbp
cmoveq %rax, %rbp
movq 0x58(%r15), %rbx
testq %rbx, %rbx
cmoveq %rax, %rbx
movq %rbp, %rdi
callq 0x3250
movq %rax, %r12
movq 0x28(%r15), %rsi
movl 0x4(%rsi), %eax
addl (%rsi), %eax
movl %eax, 0x14(%rsp)
cltq
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x22, %rcx
addl %edx, %ecx
cmpl $0x14, %eax
movl $0x2, %eax
cmovgel %ecx, %eax
movl %eax, 0x10(%rsp)
testb $0x8, 0x2(%r15)
jne 0x477d
movl $0x0, 0xc(%rsp)
movl %r12d, %ebp
xorl %r13d, %r13d
jmp 0x483d
movq %rbx, %rdi
callq 0x3250
leal (%rax,%r12), %r14d
movswl 0x20(%r15), %ecx
leal (%rcx,%rax), %edx
cmpl %r12d, %ecx
cmovgel %r14d, %edx
movl %edx, 0xc(%rsp)
addl %r12d, %eax
addl $0xa, %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x3500
movq %rax, %r13
leaq 0x5bf1(%rip), %rsi      # 0xa3aa
movq %rax, %rdi
movq %rbx, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x32d0
movq %r14, 0x18(%rsp)
testl %r14d, %r14d
jle 0x47f2
callq 0x3290
movq (%rax), %rcx
movl 0x18(%rsp), %eax
movsbq -0x1(%r13,%rax), %rdx
testb $0x20, 0x1(%rcx,%rdx,2)
je 0x47f2
movb $0x2e, -0x1(%r13,%rax)
movq 0xa3b7(%rip), %rdi      # 0xebb0
movq 0x28(%r15), %rsi
leaq 0x3c(%rsp), %r12
movq %r13, %rdx
movl 0xc(%rsp), %ecx
movq %r12, %r8
callq 0x3520
movzwl (%r12), %eax
movl %eax, 0xc(%rsp)
leaq 0x5b89(%rip), %rsi      # 0xa3aa
movq %r13, %rdi
movq %rbx, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x32d0
movq 0x28(%r15), %rsi
movq %r13, %r14
movq 0x18(%rsp), %rbp
movq 0xa36c(%rip), %rdi      # 0xebb0
leaq 0x3c(%rsp), %rbx
movq %r14, %rdx
movl %ebp, %ecx
movq %rbx, %r8
callq 0x3520
movzwl (%rbx), %eax
movq (%r15), %rsi
movq 0x28(%r15), %rdx
btl $0x11, %esi
movq 0x20(%rsp), %r12
movl $0x0, %ecx
jae 0x4877
movswl 0x1c(%r15), %ecx
subl %eax, %ecx
movl (%rdx), %r8d
btl $0x12, %esi
jae 0x4898
movswl 0x1c(%r15), %esi
subl %eax, %esi
movl %esi, %eax
shrl $0x1f, %eax
addl %esi, %eax
sarl %eax
xorl %ecx, %ecx
cmpl $-0x1, %esi
cmovgel %eax, %ecx
leaq 0x30(%r15), %rbx
subq $0x8, %rsp
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %r9
pushq %rbp
callq 0x3170
addq $0x10, %rsp
testb $0x8, 0x2(%r15)
je 0x48dc
movl 0x14(%rsp), %r9d
addl $-0x4, %r9d
movq %r12, %rdi
movq %rbx, %rsi
movl 0xc(%rsp), %edx
movl $0x2, %ecx
movl 0x10(%rsp), %r8d
callq 0x32c0
movq 0xa2cd(%rip), %rdi      # 0xebb0
movq 0x8(%r15), %rdx
movswl 0x1c(%r15), %eax
movswl 0x1e(%r15), %r10d
xorl %r11d, %r11d
movq 0x28(%rsp), %r14
movq %r14, %rsi
movq 0x30(%rsp), %rbx
movq %rbx, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r11
pushq %r11
pushq %r10
pushq %rax
callq 0x3240
addq $0x20, %rsp
movq 0xa28f(%rip), %rdi      # 0xebb0
movq %rbx, %rsi
callq 0x32f0
movq %r12, %rdi
callq 0x3230
movq 0xa278(%rip), %rdi      # 0xebb0
movq %r14, %rsi
callq 0x3600
movq %r13, %rdi
callq 0x3220
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq