// File: /workspace/github_c_makefile_star2_O2/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, %ebx
leaq 0x130fd(%rip), %r13     # 0x1e6f8
movq 0x130f6(%rip), %rdi     # 0x1e6f8
movl %ebx, %r12d
movq %r12, %rsi
callq 0x6880
testl %eax, %eax
je 0xb619
movl %eax, %r15d
jmp 0xb8ac
movl %ebx, 0x4(%rsp)
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %r15d, %r15d
movslq 0x132dc(%rip), %rax     # 0x1e908
cmpq %rax, %rbx
jge 0xb67a
movq 0x132d8(%rip), %rax     # 0x1e910
movq (%rax,%r13), %rdi
movq %r12, %rsi
callq 0x6880
movl %eax, %r15d
movq 0x132c2(%rip), %rax     # 0x1e910
testl %r15d, %r15d
je 0xb65c
cmpl %r14d, 0x18(%rax,%r13)
jne 0xb66e
jmp 0xb67a
cmpl %r14d, 0x18(%rax,%r13)
jne 0xb66e
movl 0x4(%rsp), %ecx
cmpl %ecx, 0x1c(%rax,%r13)
je 0xb677
incq %rbx
addq $0x20, %r13
jmp 0xb625
xorl %r15d, %r15d
cmpl %ebx, 0x13288(%rip)     # 0x1e908
jle 0xb68e
movq 0x13287(%rip), %rcx     # 0x1e910
jmp 0xb891
cmpq $0x0, 0x1307a(%rip)     # 0x1e710
jne 0xb6b7
movq 0x13079(%rip), %rsi     # 0x1e718
pushq $0x1
popq %rdx
leaq 0x1c(%rsp), %r8
xorl %edi, %edi
xorl %ecx, %ecx
callq 0x66d0
movq %rax, 0x13059(%rip)     # 0x1e710
movq 0x1305a(%rip), %rdi     # 0x1e718
callq 0x60e0
movq %rax, %r13
callq 0x62c0
movq %rax, %rbx
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movl 0x4(%rsp), %esi
callq 0x6040
leaq 0x577b(%rip), %rsi      # 0x10e61
movq %r13, %rdi
movq %rbx, %rdx
callq 0x6ab0
leaq 0x5771(%rip), %rsi      # 0x10e69
pushq $0x1
popq %rbx
movq %r13, %rdi
movl %ebx, %edx
callq 0x6720
leaq 0x5626(%rip), %rsi      # 0x10d32
movq %r13, %rdi
movl %ebx, %edx
callq 0x6720
xorl %edi, %edi
movq %r13, %rsi
xorl %edx, %edx
callq 0x68d0
movq %r13, %rdi
callq 0x6920
leaq 0x1c(%rsp), %r8
xorl %edi, %edi
movq %r13, %rsi
movl %ebx, %edx
xorl %ecx, %ecx
callq 0x66d0
movq %rax, %rbx
movq %r13, %rdi
callq 0x6050
movq 0x8(%rsp), %rdi
callq 0x67a0
testq %rbx, %rbx
je 0xb8a5
movq %rbp, 0x8(%rsp)
movslq 0x131b1(%rip), %rsi     # 0x1e918
cmpl %esi, 0x1319b(%rip)     # 0x1e908
jl 0xb790
addq $0x10, %rsi
movl %esi, 0x1319f(%rip)     # 0x1e918
movq 0x13190(%rip), %rdi     # 0x1e910
shlq $0x5, %rsi
callq 0x6c8e
movq %rax, 0x13180(%rip)     # 0x1e910
xorl %ebp, %ebp
movslq (%rbx), %rax
cmpq %rax, %rbp
jge 0xb825
movq 0x8(%rbx), %rax
movq (%rax,%rbp,8), %r13
movq %r13, %rdi
leaq 0x5095(%rip), %rsi      # 0x10845
xorl %edx, %edx
leaq 0x20(%rsp), %rcx
callq 0x64b0
movq 0x1315d(%rip), %rdi     # 0x1e920
movq 0x20(%rsp), %rsi
xorl %edx, %edx
leaq 0x10(%rsp), %rcx
callq 0x67f0
testl %eax, %eax
je 0xb7eb
movq 0x20(%rsp), %rsi
leaq 0x568e(%rip), %rdi      # 0x10e72
xorl %eax, %eax
callq 0x6bf6
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x6880
testl %eax, %eax
jne 0xb817
movq 0x10(%rsp), %rdi
callq 0x6970
andq $0x0, 0x10(%rsp)
incq %rbp
xorl %r15d, %r15d
jmp 0xb792
movl %eax, %r15d
movq %r13, %rdi
callq 0x60e0
movq %rax, %r13
movq %rbx, %rdi
callq 0x6600
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xb89d
movq 0x130d2(%rip), %rax     # 0x1e910
movslq 0x130c3(%rip), %rcx     # 0x1e908
shlq $0x5, %rcx
movq %rdi, (%rax,%rcx)
movl $0x100000, %esi         # imm = 0x100000
callq 0x6100
movq 0x130b2(%rip), %rcx     # 0x1e910
movslq 0x130a3(%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 0x4(%rsp), %eax
movl %eax, 0x1c(%rcx,%rdx)
leal 0x1(%rbx), %eax
movl %eax, 0x1307c(%rip)     # 0x1e908
movq 0x8(%rsp), %rbp
shlq $0x5, %rbx
addq %rcx, %rbx
movq %rbx, %r13
jmp 0xb8ac
xorl %r15d, %r15d
movq 0x8(%rsp), %rbp
leaq 0x12e4c(%rip), %r13     # 0x1e6f8
movq %r13, (%rbp)
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq