// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rdi, %rbx
testl %esi, %esi
movl %esi, 0x4(%rsp)
je 0x5635
cmpl $0x4e4, %esi            # imm = 0x4E4
jne 0x563d
movl $0xe0, %eax
jmp 0x563a
movl $0x60, %eax
movl %eax, 0x8(%rbx)
movslq 0x8(%rbx), %r12
movl $0x14, %esi
movq %r12, %rdi
callq 0x3270
movq %rbx, 0x10(%rsp)
movq %rax, 0x20(%rsp)
movq %rax, 0x10(%rbx)
testq %r12, %r12
jle 0x57c8
movq 0x10(%rsp), %rax
movq (%rax), %r15
movl 0x4(%r15), %r14d
addl $0x1f, %r12d
cmpl $0x21, %r12d
movl $0x20, %eax
cmovgel %r12d, %eax
incl %eax
movq %rax, 0x18(%rsp)
xorl %r12d, %r12d
movl $0x20, %edi
movl $0x1, %ecx
xorl %ebp, %ebp
xorl %eax, %eax
movl %r14d, 0x8(%rsp)
movl %ecx, (%rsp)
movq %rdi, 0x28(%rsp)
cmpl %r14d, %ebp
jge 0x56ec
movl (%r15), %ebx
movl %eax, %r13d
cmpl %ebx, %r13d
cmovgel %r12d, %r13d
movl %ebp, %eax
movl (%rsp), %edx
movl %edx, %ebp
cmovll %r12d, %ebp
movl $0x1, %ecx
cmovgel %ecx, %edx
movl %edx, (%rsp)
addl %eax, %ebp
movq %r15, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x5878
testl %eax, %eax
je 0x56ef
incl %r13d
cmpl %r14d, %ebp
jl 0x56b2
jmp 0x56ef
movl %eax, %r13d
cmpl %r14d, %ebp
jge 0x585c
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leal (%rbx,%r13), %esi
incl %esi
movq %r15, %rdi
movl %ebp, %edx
callq 0x5878
incl %ebx
testl %eax, %eax
je 0x56fd
leal (%rbx,%r13), %eax
movl %eax, 0xc(%rsp)
movl $0x1, %r14d
movl %ebp, %r12d
movq %r15, %rdi
movl %r13d, %esi
movl %r12d, %edx
callq 0x5878
decl %r14d
incl %r12d
testl %eax, %eax
je 0x5724
movq 0x28(%rsp), %rdi
movl %edi, %ecx
cmpq $0x80, %rdi
movl $0x0, %r12d
jb 0x576e
cmpl $0x4e4, 0x4(%rsp)       # imm = 0x4E4
jne 0x585c
leaq 0x6d39(%rip), %rax      # 0xc4a0
movl -0x200(%rax,%rdi,4), %ecx
leaq (%rdi,%rdi,4), %rax
movq 0x20(%rsp), %rsi
leaq (%rsi,%rax,4), %rdx
movl %r14d, %eax
negl %eax
movl %ecx, -0x280(%rdx)
movl %r13d, -0x27c(%rdx)
movl %ebp, -0x278(%rdx)
movl %ebx, -0x274(%rdx)
movl %eax, -0x270(%rdx)
addl 0x10(%rsi), %r14d
jne 0x585c
movl (%rsp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
incq %rdi
cmpq 0x18(%rsp), %rdi
movl 0x8(%rsp), %r14d
movl 0xc(%rsp), %eax
jne 0x569f
movl $0x1, %eax
movq 0x10(%rsp), %r11
cmpl $0x2, 0x8(%r11)
jl 0x5869
movl $0x1, %ecx
movl $0x2, %edx
movq 0x10(%r11), %rdi
leaq (%rcx,%rcx,4), %r8
movl (%rdi,%r8,4), %esi
movups 0x4(%rdi,%r8,4), %xmm0
movaps %xmm0, 0x30(%rsp)
movl %edx, %edi
leal -0x1(%rdi), %r8d
movq 0x10(%r11), %r9
leaq (%r8,%r8,4), %r10
cmpl %esi, -0x14(%r9,%r10,4)
jle 0x5832
leaq (%r9,%r10,4), %r8
movl -0x4(%r8), %r9d
movl %r9d, 0x10(%r8)
movups -0x14(%r8), %xmm0
movups %xmm0, (%r8)
decl %edi
cmpl $0x1, %edi
jg 0x5800
xorl %r8d, %r8d
movq 0x10(%r11), %rdi
movslq %r8d, %r8
leaq (%r8,%r8,4), %r8
movl %esi, (%rdi,%r8,4)
movaps 0x30(%rsp), %xmm0
movups %xmm0, 0x4(%rdi,%r8,4)
incq %rcx
movslq 0x8(%r11), %rsi
incl %edx
cmpq %rsi, %rcx
jl 0x57e7
jmp 0x5869
callq 0x3050
movl $0x16, (%rax)
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq