// File: /workspace/github_c_makefile_star2_O3/odknt[P]st/x.c
// Function: xselectfont
FT_UInt
xselectfont(Rune rune, int frcflags, MatchFont **ret)
{
	FT_UInt glyphidx;
	Font *font = &dc.font;
	FcFontSet *fonts, *fcsets[] = { NULL };
	FcCharSet *fccharset;
	FcPattern *fcpattern;
	FcResult fcres;
	FT_Face face;
	FcChar8 *path;
	int i, f;

	/* Lookup character index with default font. */
	glyphidx = FT_Get_Char_Index(font->match.face, rune);
	if (glyphidx) {
		*ret = &font->match;
		return glyphidx;
	}

	/* Fallback on font cache, search the font cache for match. */
	for (f = 0; f < frclen; f++) {
		glyphidx = FT_Get_Char_Index(frc[f].font.face, rune);
		/* Everything correct. */
		if (glyphidx && frc[f].flags == frcflags)
			break;
		/* We got a default font for a not found glyph. */
		if (!glyphidx && frc[f].flags == frcflags
				&& frc[f].unicodep == rune) {
			break;
		}
	}

	/* Nothing was found. Use fontconfig to find matching font. */
	if (f >= frclen) {
		if (!font->set)
			font->set = FcFontSort(0, font->pattern, 1, 0, &fcres);
		fcsets[0] = font->set;

		/*
		 * Nothing was found in the cache. Now use
		 * some dozen of Fontconfig calls to get the
		 * font for one single character.
		 *
		 * cairo and fontconfig are design failures.
		 */
		fcpattern = FcPatternDuplicate(font->pattern);
		fccharset = FcCharSetCreate();

		FcCharSetAddChar(fccharset, rune);
		FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
		FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
		FcPatternAddBool(fcpattern, FC_COLOR, 1);

		FcConfigSubstitute(0, fcpattern, FcMatchPattern);
		FcDefaultSubstitute(fcpattern);

		fonts = FcFontSort(NULL, fcpattern, 1, NULL, &fcres);
		FcPatternDestroy(fcpattern);
		FcCharSetDestroy(fccharset);
		if (!fonts) {
			*ret = &font->match;
			return glyphidx;
		}

		/* Allocate memory for the new cache entry. */
		if (frclen >= frccap) {
			frccap += 16;
			frc = xrealloc(frc, frccap * sizeof(Fontcache));
		}

		/* veirfy matched font */
		for (i = 0; i < fonts->nfont; i++) {
			fcpattern = fonts->fonts[i];
			FcPatternGetString(fcpattern, FC_FILE, 0, &path);
			if (FT_New_Face(ftlib, (const char *)path, 0, &face))
				die("FT_New_Face failed seeking fallback font: %s\n", path);
			if ((glyphidx = FT_Get_Char_Index(face, rune))) {
				fcpattern = FcPatternDuplicate(fcpattern);
				break;
			}
			FT_Done_Face(face);
			face = NULL;
		}
		FcFontSetDestroy(fonts);
		if (!face) {
			*ret = &font->match;
			return 0;
		}

		frc[frclen].font.face = face;
		frc[frclen].font.cairo = cairo_ft_font_face_create_for_ft_face(
		                         frc[frclen].font.face, FT_LOAD_COLOR);
		frc[frclen].font.pattern = fcpattern;
		frc[frclen].flags = frcflags;
		frc[frclen].unicodep = rune;

		f = frclen;
		frclen++;
	}
	*ret = &frc[f].font;
	return glyphidx;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %rbp
movl %esi, %r14d
movl %edi, %r13d
leaq 0x13139(%rip), %rbx     # 0x1f6e8
movq 0x13132(%rip), %rdi     # 0x1f6e8
movl %r13d, %r15d
movq %r15, %rsi
callq 0x6870
testl %eax, %eax
je 0xc5cd
movl %eax, %r12d
jmp 0xc87e
movl 0x13325(%rip), %eax     # 0x1f8f8
testl %eax, %eax
movq %rbp, (%rsp)
jle 0xc63e
movl %r13d, %ebp
movq 0x1331b(%rip), %rax     # 0x1f900
xorl %r13d, %r13d
xorl %ebx, %ebx
movq (%rax,%r13), %rdi
movq %r15, %rsi
callq 0x6870
movl %eax, %r12d
movq 0x13300(%rip), %rax     # 0x1f900
testl %r12d, %r12d
je 0xc60e
cmpl %r14d, 0x18(%rax,%r13)
jne 0xc61c
jmp 0xc62f
cmpl %r14d, 0x18(%rax,%r13)
jne 0xc61c
cmpl %ebp, 0x1c(%rax,%r13)
je 0xc62f
incq %rbx
movslq 0x132d2(%rip), %rcx     # 0x1f8f8
addq $0x20, %r13
cmpq %rcx, %rbx
jl 0xc5ea
movl 0x132c3(%rip), %eax     # 0x1f8f8
movl %ebp, %r13d
movq (%rsp), %rbp
jmp 0xc643
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpl %eax, %ebx
jge 0xc656
movq 0x132b2(%rip), %rcx     # 0x1f900
movslq %ebx, %rbx
jmp 0xc86b
movl %r13d, 0xc(%rsp)
cmpq $0x0, 0x1309d(%rip)     # 0x1f700
jne 0xc686
movq 0x1309c(%rip), %rsi     # 0x1f708
leaq 0x1c(%rsp), %r8
xorl %edi, %edi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x66d0
movq %rax, 0x1307a(%rip)     # 0x1f700
movq 0x1307b(%rip), %rdi     # 0x1f708
callq 0x60e0
movq %rax, %r13
callq 0x62c0
movq %rax, %rbp
movq %rax, %rdi
movl 0xc(%rsp), %esi
callq 0x6040
leaq 0x57e1(%rip), %rsi      # 0x11e91
movq %r13, %rdi
movq %rbp, %rdx
callq 0x6aa0
leaq 0x57d7(%rip), %rsi      # 0x11e99
movq %r13, %rdi
movl $0x1, %edx
callq 0x6720
leaq 0x568c(%rip), %rsi      # 0x11d62
movq %r13, %rdi
movl $0x1, %edx
callq 0x6720
xorl %edi, %edi
movq %r13, %rsi
xorl %edx, %edx
callq 0x68c0
movq %r13, %rdi
callq 0x6910
leaq 0x1c(%rsp), %r8
xorl %edi, %edi
movq %r13, %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x66d0
movq %rax, %rbx
movq %r13, %rdi
callq 0x6050
movq %rbp, %rdi
callq 0x67a0
testq %rbx, %rbx
je 0xc7e9
movslq 0x131d8(%rip), %rsi     # 0x1f908
cmpl %esi, 0x131c2(%rip)     # 0x1f8f8
movq (%rsp), %rbp
jl 0xc75d
addq $0x10, %rsi
movl %esi, 0x131c2(%rip)     # 0x1f908
movq 0x131b3(%rip), %rdi     # 0x1f900
shlq $0x5, %rsi
callq 0x7140
movq %rax, 0x131a3(%rip)     # 0x1f900
cmpl $0x0, (%rbx)
jle 0xc804
xorl %ebp, %ebp
movq 0x8(%rbx), %rax
movq (%rax,%rbp,8), %r13
movq %r13, %rdi
leaq 0x50fb(%rip), %rsi      # 0x11875
xorl %edx, %edx
leaq 0x20(%rsp), %rcx
callq 0x64b0
movq 0x13183(%rip), %rdi     # 0x1f910
movq 0x20(%rsp), %rsi
xorl %edx, %edx
leaq 0x10(%rsp), %rcx
callq 0x67f0
testl %eax, %eax
je 0xc7b5
movq 0x20(%rsp), %rsi
leaq 0x56f4(%rip), %rdi      # 0x11ea2
xorl %eax, %eax
callq 0x70a6
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x6870
testl %eax, %eax
jne 0xc7f2
movq 0x10(%rsp), %rdi
callq 0x6960
movq $0x0, 0x10(%rsp)
incq %rbp
movslq (%rbx), %rax
cmpq %rax, %rbp
jl 0xc768
xorl %r12d, %r12d
jmp 0xc800
movq (%rsp), %rbp
jmp 0xc877
movl %eax, %r12d
movq %r13, %rdi
callq 0x60e0
movq %rax, %r13
movq (%rsp), %rbp
movq %rbx, %rdi
callq 0x6600
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xc874
movq 0x130e3(%rip), %rax     # 0x1f900
movslq 0x130d4(%rip), %rcx     # 0x1f8f8
shlq $0x5, %rcx
movq %rdi, (%rax,%rcx)
movl $0x100000, %esi         # imm = 0x100000
callq 0x6100
movq 0x130c3(%rip), %rcx     # 0x1f900
movslq 0x130b4(%rip), %rbx     # 0x1f8f8
movq %rbx, %rdx
shlq $0x5, %rdx
movq %rax, 0x8(%rcx,%rdx)
movq %r13, 0x10(%rcx,%rdx)
movl %r14d, 0x18(%rcx,%rdx)
movl 0xc(%rsp), %eax
movl %eax, 0x1c(%rcx,%rdx)
leal 0x1(%rbx), %eax
movl %eax, 0x1308d(%rip)     # 0x1f8f8
shlq $0x5, %rbx
addq %rcx, %rbx
jmp 0xc87e
xorl %r12d, %r12d
leaq 0x12e6a(%rip), %rbx     # 0x1f6e8
movq %rbx, (%rbp)
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq