// File: /workspace/github_c_makefile_star2_O3/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
movl %esi, %ebp
movq %rdi, %rbx
testl %esi, %esi
je 0x5751
cmpl $0x4e4, %ebp            # imm = 0x4E4
jne 0x5760
movl $0xe0, 0x8(%rbx)
movl $0xe0, %r14d
jmp 0x5764
movl $0x60, 0x8(%rbx)
movl $0x60, %r14d
jmp 0x5764
movl 0x8(%rbx), %r14d
movslq %r14d, %r15
movl $0x14, %esi
movq %r15, %rdi
callq 0x3270
movq %rax, 0x30(%rsp)
movq %rax, 0x10(%rbx)
testl %r15d, %r15d
jle 0x598a
movl %ebp, 0xc(%rsp)
movq %rbx, 0x18(%rsp)
movq (%rbx), %r15
movl 0x4(%r15), %r12d
movq %r14, 0x20(%rsp)
leal 0x1f(%r14), %eax
cmpl $0x21, %eax
movl $0x20, %ecx
cmovgel %eax, %ecx
incl %ecx
movq %rcx, 0x28(%rsp)
xorl %r14d, %r14d
movl $0x20, %edi
movl $0x1, %ecx
xorl %ebp, %ebp
xorl %eax, %eax
movl %r12d, 0x10(%rsp)
movl %ecx, 0x8(%rsp)
movq %rdi, 0x38(%rsp)
cmpl %r12d, %ebp
jge 0x5817
movl (%r15), %ebx
movl %eax, %r13d
cmpl %ebx, %r13d
cmovgel %r14d, %r13d
movl %ebp, %eax
movl 0x8(%rsp), %edx
movl %edx, %ebp
cmovll %r14d, %ebp
movl $0x1, %ecx
cmovgel %ecx, %edx
movl %edx, 0x8(%rsp)
addl %eax, %ebp
movq %r15, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x599e
testl %eax, %eax
je 0x581a
incl %r13d
cmpl %r12d, %ebp
jl 0x57db
jmp 0x581a
movl %eax, %r13d
cmpl %r12d, %ebp
jge 0x597b
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leal (%rbx,%r13), %esi
incl %esi
movq %r15, %rdi
movl %ebp, %edx
callq 0x599e
incl %ebx
testl %eax, %eax
je 0x5828
leal (%rbx,%r13), %eax
movl %eax, 0x14(%rsp)
movl $0x1, %r14d
movl %ebp, %r12d
movq %r15, %rdi
movl %r13d, %esi
movl %r12d, %edx
callq 0x599e
decl %r14d
incl %r12d
testl %eax, %eax
je 0x584f
movq 0x38(%rsp), %rdi
movl %edi, %ecx
cmpq $0x80, %rdi
movl 0x10(%rsp), %r12d
jb 0x5898
cmpl $0x4e4, 0xc(%rsp)       # imm = 0x4E4
jne 0x597b
leaq 0x6cbf(%rip), %rax      # 0xc550
movl -0x200(%rax,%rdi,4), %ecx
leaq (%rdi,%rdi,4), %rax
movq 0x30(%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 0x597b
movl 0x8(%rsp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
incq %rdi
cmpq 0x28(%rsp), %rdi
movl $0x0, %r14d
movl 0x14(%rsp), %eax
jne 0x57c7
movl $0x1, %eax
cmpl $0x1, 0x20(%rsp)
je 0x598f
movl $0x1, %ecx
movq 0x18(%rsp), %r9
movq 0x10(%r9), %rsi
leaq (%rcx,%rcx,4), %rdi
movl (%rsi,%rdi,4), %edx
movups 0x4(%rsi,%rdi,4), %xmm0
movaps %xmm0, 0x40(%rsp)
movq %rcx, %rsi
movq 0x10(%r9), %rdi
movl %esi, %r8d
leaq (%r8,%r8,4), %r8
cmpl %edx, -0x14(%rdi,%r8,4)
jle 0x595c
leaq (%rdi,%r8,4), %rdi
movl -0x4(%rdi), %r8d
movl %r8d, 0x10(%rdi)
movups -0x14(%rdi), %xmm0
movups %xmm0, (%rdi)
decq %rsi
leal 0x1(%rsi), %edi
cmpl $0x1, %edi
jg 0x5926
movq 0x10(%r9), %rdi
xorl %esi, %esi
leaq (%rsi,%rsi,4), %rsi
movl %edx, (%rdi,%rsi,4)
movaps 0x40(%rsp), %xmm0
movups %xmm0, 0x4(%rdi,%rsi,4)
incq %rcx
movslq 0x8(%r9), %rdx
cmpq %rdx, %rcx
jl 0x590e
jmp 0x598f
callq 0x3050
movl $0x16, (%rax)
xorl %eax, %eax
jmp 0x598f
movl $0x1, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq