// File: /workspace/github_c_makefile_star2_O2/nkanaev[P]tipsy/src/tigr.c
// Function: tigrLoadGlyphs
int tigrLoadGlyphs(TigrFont* font, int codepage) {
    int i, x = 0, y = 0, w, h, rowh = 1;
    TigrGlyph* g;
    switch (codepage) {
        case 0:
            font->numGlyphs = 128 - 32;
            break;
        case 1252:
            font->numGlyphs = 256 - 32;
            break;
    }

    font->glyphs = (TigrGlyph*)calloc(font->numGlyphs, sizeof(TigrGlyph));
    for (i = 32; i < font->numGlyphs + 32; i++) {
        // Find the next glyph.
        scan(font->bitmap, &x, &y, &rowh);
        if (y >= font->bitmap->h) {
            errno = EINVAL;
            return 0;
        }

        // Scan the width and height
        w = h = 0;
        while (!border(font->bitmap, x + w, y))
            w++;
        while (!border(font->bitmap, x, y + h))
            h++;

        // Look up the Unicode code point.
        g = &font->glyphs[i - 32];
        if (i < 128)
            g->code = i;  // ASCII
        else if (codepage == 1252)
            g->code = cp1252[i - 128];
        else {
            errno = EINVAL;
            return 0;
        }

        g->x = x;
        g->y = y;
        g->w = w;
        g->h = h;
        x += w;
        if (h != font->glyphs[0].h) {
            errno = EINVAL;
            return 0;
        }

        if (h > rowh)
            rowh = h;
    }

    // Sort by code point.
    for (i = 1; i < font->numGlyphs; i++) {
        int j = i;
        TigrGlyph g = font->glyphs[i];
        while (j > 0 && font->glyphs[j - 1].code > g.code) {
            font->glyphs[j] = font->glyphs[j - 1];
            j--;
        }
        font->glyphs[j] = g;
    }

    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
testl %esi, %esi
movl %esi, 0x24(%rsp)
je 0x5834
cmpl $0x4e4, %esi            # imm = 0x4E4
jne 0x5840
movl $0xe0, 0x8(%rbx)
movl $0xe0, %eax
jmp 0x5843
movl $0x60, 0x8(%rbx)
pushq $0x60
popq %rax
jmp 0x5843
movl 0x8(%rbx), %eax
movl %eax, 0x20(%rsp)
movslq %eax, %r14
pushq $0x14
popq %rsi
movq %r14, %rdi
callq 0x3270
movq %rax, 0x30(%rsp)
movq %rax, 0x10(%rbx)
addl $0x20, %r14d
movslq %r14d, %rax
movq %rax, 0x38(%rsp)
pushq $0x1
popq %r15
pushq $0x20
popq %rax
xorl %r12d, %r12d
xorl %r14d, %r14d
movl %r15d, %ecx
movq %rbx, 0x28(%rsp)
cmpq 0x38(%rsp), %rax
jge 0x59cd
movq %rax, 0x18(%rsp)
movq (%rbx), %r13
movl 0x4(%r13), %ebp
xorl %ebx, %ebx
cmpl %ebp, %r14d
jge 0x58d0
cmpl (%r13), %r12d
cmovgel %ebx, %r12d
movl %ecx, %eax
cmovll %ebx, %eax
cmovgel %r15d, %ecx
movl %ecx, 0xc(%rsp)
addl %eax, %r14d
movq %r13, %rdi
movl %r12d, %esi
movl %r14d, %edx
callq 0x5a47
testl %eax, %eax
je 0x58d4
incl %r12d
movl 0xc(%rsp), %ecx
jmp 0x5898
movl %ecx, 0xc(%rsp)
movl %r12d, %eax
movq %rax, 0x10(%rsp)
cmpl %ebp, %r14d
jge 0x59b1
pushq $-0x1
popq %rax
movl %eax, %ebp
movq 0x10(%rsp), %rbx
leal (%rbx,%rbp), %esi
incl %esi
movq %r13, %rdi
movl %r14d, %edx
callq 0x5a47
incl %ebp
testl %eax, %eax
je 0x58ef
leal (%rbx,%rbp), %r12d
movl %r14d, %r15d
movq %rbx, 0x10(%rsp)
pushq $0x1
popq %rbx
movq %r13, %rdi
movq 0x10(%rsp), %rsi
movl %r15d, %edx
callq 0x5a47
decl %ebx
incl %r15d
testl %eax, %eax
je 0x5914
movq 0x18(%rsp), %rax
movl %eax, %ecx
cmpq $0x80, %rax
pushq $0x1
popq %r15
jb 0x595d
cmpl $0x4e4, 0x24(%rsp)      # imm = 0x4E4
jne 0x59b1
movq 0x18(%rsp), %rax
leaq 0x5b4a(%rip), %rcx      # 0xb4a0
movl -0x200(%rcx,%rax,4), %ecx
imulq $0x14, %rax, %rdx
movq 0x30(%rsp), %rsi
addq %rsi, %rdx
movl %ebx, %eax
negl %eax
movl %ecx, -0x280(%rdx)
movq 0x10(%rsp), %rcx
movl %ecx, -0x27c(%rdx)
movl %r14d, -0x278(%rdx)
movl %ebp, -0x274(%rdx)
movl %eax, -0x270(%rdx)
addl 0x10(%rsi), %ebx
jne 0x59b1
movl 0xc(%rsp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movq 0x18(%rsp), %rax
incq %rax
movq 0x28(%rsp), %rbx
jmp 0x587f
callq 0x3050
movl $0x16, (%rax)
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rcx
pushq $0x1
popq %rax
movl 0x20(%rsp), %edx
movslq %edx, %rdx
cmpq %rdx, %rcx
jge 0x59be
movq 0x10(%rbx), %rsi
imulq $0x14, %rcx, %rdi
movl (%rsi,%rdi), %edx
movups 0x4(%rsi,%rdi), %xmm0
movaps %xmm0, 0x40(%rsp)
movl %ecx, %edi
testl %edi, %edi
jle 0x5a2a
movl %edi, %r8d
imulq $0x14, %r8, %r8
cmpl %edx, -0x14(%rsi,%r8)
jle 0x5a2c
leaq (%rsi,%r8), %r9
addq %r8, %rsi
addq $-0x14, %rsi
movl 0x10(%rsi), %r8d
movl %r8d, 0x10(%r9)
movups (%rsi), %xmm0
movups %xmm0, (%r9)
decl %edi
movq 0x10(%rbx), %rsi
jmp 0x59f6
xorl %edi, %edi
movl %edi, %edi
imulq $0x14, %rdi, %rdi
movl %edx, (%rsi,%rdi)
movaps 0x40(%rsp), %xmm0
movups %xmm0, 0x4(%rsi,%rdi)
incq %rcx
movl 0x8(%rbx), %edx
jmp 0x59d7