// File: /workspace/github_c_makefile_star2_O2/michaelforney[P]wld/intel.c
// Function: renderer_draw_text
void
renderer_draw_text(struct wld_renderer *base,
                   struct font *font, uint32_t color,
                   int32_t x, int32_t y, const char *text,
                   uint32_t length, struct wld_extents *extents)
{
	struct intel_renderer *renderer = intel_renderer(base);
	struct intel_buffer *dst = renderer->target;
	int ret;
	struct glyph *glyph;
	uint32_t row;
	FT_UInt glyph_index;
	uint32_t c;
	uint8_t immediate[512];
	uint8_t *byte;
	int32_t origin_x = x;

	xy_setup_blt(&renderer->batch, true, BLT_RASTER_OPERATION_SRC,
	             0, color, dst->bo, dst->base.base.pitch);

	if (length == -1)
		length = strlen(text);

	while ((ret = FcUtf8ToUcs4((FcChar8 *)text, &c, length)) > 0 && c != '\0') {
		text += ret;
		length -= ret;
		glyph_index = FT_Get_Char_Index(font->face, c);

		if (!font_ensure_glyph(font, glyph_index))
			continue;

		glyph = font->glyphs[glyph_index];

		if (glyph->bitmap.width == 0 || glyph->bitmap.rows == 0)
			goto advance;

		byte = immediate;

		/* XY_TEXT_IMMEDIATE requires a pitch with no extra bytes */
		for (row = 0; row < glyph->bitmap.rows; ++row) {
			memcpy(byte, glyph->bitmap.buffer + (row * glyph->bitmap.pitch),
			       (glyph->bitmap.width + 7) / 8);
			byte += (glyph->bitmap.width + 7) / 8;
		}

	retry:
		ret = xy_text_immediate_blt(&renderer->batch, dst->bo,
		                            origin_x + glyph->x, y + glyph->y,
		                            origin_x + glyph->x + glyph->bitmap.width,
		                            y + glyph->y + glyph->bitmap.rows,
		                            (byte - immediate + 3) / 4,
		                            (uint32_t *)immediate);

		if (ret == INTEL_BATCH_NO_SPACE) {
			intel_batch_flush(&renderer->batch);
			xy_setup_blt(&renderer->batch, true, BLT_RASTER_OPERATION_SRC,
			             0, color, dst->bo, dst->base.base.pitch);
			goto retry;
		}

	advance:
		origin_x += glyph->advance;
	}

	if (extents)
		extents->advance = origin_x - x;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movq %r9, %r14
movl %r8d, 0x24(%rsp)
movl %ecx, %r12d
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r13
movl 0x2a0(%rsp), %ebp
callq 0xb31c
movq 0x8028(%r13), %rax
movq %r13, 0x10(%rsp)
leaq 0x10(%r13), %rdi
movq 0x60(%rax), %rdx
movq %rax, 0x38(%rsp)
movzwl 0x10(%rax), %ecx
movq %rdi, 0x30(%rsp)
movl %ebx, 0x20(%rsp)
movl %ebx, %esi
callq 0xb40c
cmpl $-0x1, %ebp
jne 0xb01d
movq %r14, %rdi
callq 0x9250
movq %rax, %rbp
movq 0x2a8(%rsp), %r13
movl %r12d, 0xc(%rsp)
movq %r14, %rdi
leaq 0x28(%rsp), %rsi
movl %ebp, %edx
callq 0x92a0
testl %eax, %eax
jle 0xb235
movl 0x28(%rsp), %esi
testl %esi, %esi
je 0xb235
movl %eax, %ecx
addq %rcx, %r14
subl %eax, %ebp
movq 0x18(%r15), %rdi
callq 0x9640
movl %eax, %ebx
movq %r15, %rdi
movl %eax, %esi
callq 0xa181
testb %al, %al
je 0xb02a
movq %r15, 0x50(%rsp)
movq 0x20(%r15), %rax
movl %ebx, %ecx
movq (%rax,%rcx,8), %r13
movl 0x4(%r13), %r15d
testl %r15d, %r15d
je 0xb21a
movl (%r13), %esi
testl %esi, %esi
je 0xb21a
movl %r12d, 0x1c(%rsp)
xorl %r12d, %r12d
leaq 0x60(%rsp), %rbx
cmpl %esi, %r12d
jae 0xb0dd
movl 0x8(%r13), %esi
imull %r12d, %esi
addq 0x10(%r13), %rsi
addl $0x7, %r15d
shrl $0x3, %r15d
movq %rbx, %rdi
movq %r15, %rdx
callq 0x94c0
movl (%r13), %esi
movl 0x4(%r13), %r15d
leal 0x7(%r15), %eax
shrl $0x3, %eax
addq %rax, %rbx
incl %r12d
jmp 0xb0a2
leaq 0x60(%rsp), %rax
subq %rax, %rbx
addq $0x3, %rbx
movq %rbx, %rax
cqto
pushq $0x4
popq %rcx
idivq %rcx
movq %rax, 0x48(%rsp)
incl %eax
andl $0xfe, %eax
movq %rax, 0x40(%rsp)
leal 0x3(%rax), %r12d
movl 0x20(%rsp), %ebx
movl $0x1ffe, %eax           # imm = 0x1FFE
movq 0x10(%rsp), %rcx
subl 0x8020(%rcx), %eax
cmpl %r12d, %eax
jae 0xb150
movq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0xbbea
movq 0x38(%rsp), %rax
movq 0x60(%rax), %rdx
movzwl 0x10(%rax), %ecx
movq %r15, %rdi
movl %ebx, %esi
callq 0xb40c
movl (%r13), %esi
movl 0x4(%r13), %r15d
jmp 0xb10d
movq 0x38(%rsp), %rax
movq 0x60(%rax), %rdi
movzwl 0x28(%r13), %ebx
movzwl 0x2a(%r13), %r12d
addl 0x24(%rsp), %r12d
addl %r12d, %esi
movl %esi, 0x18(%rsp)
addl 0xc(%rsp), %ebx
addl %ebx, %r15d
leaq 0x2c(%rsp), %rsi
leaq 0x5c(%rsp), %rdx
callq 0x9350
xorl %edx, %edx
cmpl $0x0, 0x2c(%rsp)
setne %dl
shll $0xb, %edx
addl 0x40(%rsp), %edx
orl $0x4c410001, %edx       # imm = 0x4C410001
shll $0x10, %r12d
movswl %bx, %ecx
orl %r12d, %ecx
movl 0x18(%rsp), %eax
shll $0x10, %eax
movswl %r15w, %r8d
orl %eax, %r8d
movq 0x30(%rsp), %rdi
pushq $0x3
popq %rsi
xorl %eax, %eax
callq 0xb35e
movq 0x48(%rsp), %r9
movzwl %r9w, %eax
xorl %ecx, %ecx
movl 0x1c(%rsp), %r12d
cmpl %ecx, %eax
je 0xb1fb
movl 0x60(%rsp,%rcx,4), %edx
movq 0x10(%rsp), %r8
movl 0x8020(%r8), %esi
leal 0x1(%rsi), %edi
movl %edi, 0x8020(%r8)
movl %edx, 0x20(%r8,%rsi,4)
incq %rcx
jmp 0xb1d3
testb $0x1, %r9b
je 0xb21a
movq 0x10(%rsp), %rdx
movl 0x8020(%rdx), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x8020(%rdx)
andl $0x0, 0x20(%rdx,%rax,4)
movzwl 0x2c(%r13), %eax
addl %eax, 0xc(%rsp)
movq 0x50(%rsp), %r15
movq 0x2a8(%rsp), %r13
jmp 0xb02a
testq %r13, %r13
je 0xb245
movl 0xc(%rsp), %eax
subl %r12d, %eax
movl %eax, (%r13)
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq