// File: /workspace/github_c_makefile_star2_O1/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 0x127a1(%rip), %rbx     # 0x1e6f8
movq 0x1279a(%rip), %rdi     # 0x1e6f8
movl %r13d, %r15d
movq %r15, %rsi
callq 0x6890
testl %eax, %eax
je 0xbf75
movl %eax, %r12d
jmp 0xc21f
cmpl $0x0, 0x1298c(%rip)     # 0x1e908
jle 0xbfe3
movq %rbp, (%rsp)
movl %r13d, %ebp
xorl %r13d, %r13d
xorl %ebx, %ebx
movq 0x1297f(%rip), %rax     # 0x1e910
movq (%rax,%r13), %rdi
movq %r15, %rsi
callq 0x6890
movl %eax, %r12d
movq 0x12969(%rip), %rax     # 0x1e910
testl %r12d, %r12d
je 0xbfb5
cmpl %r14d, 0x18(%rax,%r13)
jne 0xbfc7
jmp 0xbfda
cmpl %r14d, 0x18(%rax,%r13)
jne 0xbfc7
cmpl %ebp, 0x1c(%rax,%r13)
je 0xc235
incq %rbx
movslq 0x12937(%rip), %rax     # 0x1e908
addq $0x20, %r13
cmpq %rax, %rbx
jl 0xbf8a
movl %ebp, %r13d
movq (%rsp), %rbp
jmp 0xbfe8
xorl %ebx, %ebx
xorl %r12d, %r12d
cmpl 0x1291a(%rip), %ebx     # 0x1e908
jl 0xc205
movl %r13d, 0xc(%rsp)
cmpq $0x0, 0x1270f(%rip)     # 0x1e710
jne 0xc024
movq 0x1270e(%rip), %rsi     # 0x1e718
leaq 0x1c(%rsp), %r8
xorl %edi, %edi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x66d0
movq %rax, 0x126ec(%rip)     # 0x1e710
movq 0x126ed(%rip), %rdi     # 0x1e718
callq 0x60e0
movq %rax, %r13
callq 0x62c0
movq %rax, %rbx
movq %rax, (%rsp)
movq %rax, %rdi
movl 0xc(%rsp), %esi
callq 0x6040
leaq 0x4e1f(%rip), %rsi      # 0x10e71
movq %r13, %rdi
movq %rbx, %rdx
callq 0x6ac0
leaq 0x4e15(%rip), %rsi      # 0x10e79
movq %r13, %rdi
movl $0x1, %edx
callq 0x6720
leaq 0x4cca(%rip), %rsi      # 0x10d42
movq %r13, %rdi
movl $0x1, %edx
callq 0x6720
xorl %edi, %edi
movq %r13, %rsi
xorl %edx, %edx
callq 0x68e0
movq %r13, %rdi
callq 0x6930
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 (%rsp), %rdi
callq 0x67b0
testq %rbx, %rbx
je 0xc218
movslq 0x12845(%rip), %rsi     # 0x1e918
cmpl %esi, 0x1282f(%rip)     # 0x1e908
jl 0xc0fc
addq $0x10, %rsi
movl %esi, 0x12833(%rip)     # 0x1e918
movq 0x12824(%rip), %rdi     # 0x1e910
shlq $0x5, %rsi
callq 0x7160
movq %rax, 0x12814(%rip)     # 0x1e910
cmpl $0x0, (%rbx)
jle 0xc19e
movq %rbp, (%rsp)
xorl %ebp, %ebp
movq 0x8(%rbx), %rax
movq (%rax,%rbp,8), %r13
movq %r13, %rdi
leaq 0x472a(%rip), %rsi      # 0x10847
xorl %edx, %edx
leaq 0x20(%rsp), %rcx
callq 0x64b0
movq 0x127f0(%rip), %rdi     # 0x1e920
movq 0x20(%rsp), %rsi
xorl %edx, %edx
leaq 0x10(%rsp), %rcx
callq 0x6800
testl %eax, %eax
je 0xc158
movq 0x20(%rsp), %rsi
leaq 0x4d31(%rip), %rdi      # 0x10e82
xorl %eax, %eax
callq 0x70c6
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x6890
testl %eax, %eax
jne 0xc18c
movq 0x10(%rsp), %rdi
callq 0x6980
movq $0x0, 0x10(%rsp)
incq %rbp
movslq (%rbx), %rax
cmpq %rax, %rbp
jl 0xc10b
xorl %r12d, %r12d
jmp 0xc19a
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 0xc215
movq 0x12759(%rip), %rax     # 0x1e910
movslq 0x1274a(%rip), %rcx     # 0x1e908
shlq $0x5, %rcx
movq %rdi, (%rax,%rcx)
movl $0x100000, %esi         # imm = 0x100000
callq 0x6100
movq 0x12739(%rip), %rcx     # 0x1e910
movslq 0x1272a(%rip), %rbx     # 0x1e908
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, 0x12703(%rip)     # 0x1e908
movslq %ebx, %rbx
shlq $0x5, %rbx
addq 0x126fd(%rip), %rbx     # 0x1e910
jmp 0xc21f
xorl %r12d, %r12d
leaq 0x124d9(%rip), %rbx     # 0x1e6f8
movq %rbx, (%rbp)
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
jmp 0xbfda