// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/deark-font.c
// Function: fixup_codepoints
static void fixup_codepoints(deark *c, struct font_render_ctx *fctx)
{
	i64 i;
	i32 c1;
	i64 num_uncoded_chars = 0;
	u8 *used_codepoint_map = NULL;
	u8 codepoint_already_used;

	if(!fctx->render_as_unicode) {
		for(i=0; i<fctx->font->num_chars; i++) {
			fctx->codepoint_tmp[i] = fctx->font->char_array[i].codepoint_nonunicode;
		}
		goto done;
	}

	// An array of bits to remember if we've seen a codepoint before (BMP only).
	// A character with a duplicate codepoint will be moved to another
	// location, so that it doesn't get painted over the previous one.
	used_codepoint_map = de_malloc(c, 65536/8);

	for(i=0; i<fctx->font->num_chars; i++) {
		if(!is_valid_char(&fctx->font->char_array[i])) continue;
		c1 = fctx->font->char_array[i].codepoint_unicode;

		codepoint_already_used = 0;
		if(c1>=0 && c1<65536) {
			// Check if we've seen this codepoint before.
			codepoint_already_used = used_codepoint_map[c1/8] & (1<<(c1%8));

			// Remember that we've seen this codepoint.
			used_codepoint_map[c1/8] |= 1<<(c1%8);
		}

		if(codepoint_already_used || c1==DE_CODEPOINT_INVALID) {
			if(codepoint_already_used) {
				de_dbg2(c, "moving duplicate codepoint U+%04x at index %d to private use area",
					(unsigned int)c1, (int)i);
			}
			// Move uncoded characters to a Private Use area.
			// (Supplementary Private Use Area-A = U+F0000 - U+FFFFD)
			if(DE_CODEPOINT_MOVED + num_uncoded_chars <= DE_CODEPOINT_MOVED_MAX) {
				fctx->codepoint_tmp[i] = (i32)(DE_CODEPOINT_MOVED + num_uncoded_chars);
				num_uncoded_chars++;
			}
		}
		else {
			fctx->codepoint_tmp[i] = c1;
		}
	}

done:
	de_free(c, used_codepoint_map);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x170c1a
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
cmpq 0x18(%rcx), %rax
jge 0x170c15
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rax
imulq $0x28, -0x18(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %edx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x170bd2
jmp 0x170daa
movq -0x8(%rbp), %rdi
movl $0x2000, %esi           # imm = 0x2000
callq 0x15b780
movq %rax, -0x30(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
cmpq 0x18(%rcx), %rax
jge 0x170da8
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rdi
imulq $0x28, -0x18(%rbp), %rax
addq %rax, %rdi
callq 0x16f9f0
cmpl $0x0, %eax
jne 0x170c6b
jmp 0x170d97
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x20(%rax), %rax
imulq $0x28, -0x18(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x1c(%rbp)
movb $0x0, -0x31(%rbp)
cmpl $0x0, -0x1c(%rbp)
jl 0x170d15
cmpl $0x10000, -0x1c(%rbp)   # imm = 0x10000
jge 0x170d15
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x1c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x50(%rbp), %rax
movslq %ecx, %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x44(%rbp)
movl -0x1c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl -0x44(%rbp), %eax
movl %edx, %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
movb %al, -0x31(%rbp)
movl -0x1c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %edx, %ecx
movl $0x1, %esi
shll %cl, %esi
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movl -0x1c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x40(%rbp), %rax
movslq %ecx, %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
movzbl -0x31(%rbp), %eax
cmpl $0x0, %eax
jne 0x170d27
cmpl $0xfffffff, -0x1c(%rbp) # imm = 0xFFFFFFF
jne 0x170d83
cmpb $0x0, -0x31(%rbp)
je 0x170d48
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %edx
movq -0x18(%rbp), %rax
movl %eax, %ecx
leaq 0x64c25(%rip), %rsi     # 0x1d5966
movb $0x0, %al
callq 0x15b350
movq -0x28(%rbp), %rax
addq $0xfde00, %rax          # imm = 0xFDE00
cmpq $0xffffd, %rax          # imm = 0xFFFFD
jg 0x170d81
movq -0x28(%rbp), %rax
addq $0xfde00, %rax          # imm = 0xFDE00
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x170d95
movl -0x1c(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x170d97
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x170c34
jmp 0x170daa
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x15b820
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)