// File: /workspace/github_c_makefile_star2_O1/ggerganov[P]font-rasterizer/main.c
// Function: main
int main(int argc, const char * argv[]) {
    if (argc < 3) {
        printf("usage: %s font.ttf height [width]\n", argv[0]);
        printf("\n");
        printf("       rasterizes the provided ttf font into cells with specified height in pixels\n");
        printf("       if width is provided, it overrides the auto detected value\n");
        printf("       the generated raster data is printed on the standard output\n");
        return -1;
    }

    const char * fontFName = argv[1];
    int height = atoi(argv[2]);
    int width = argc > 3 ? atoi(argv[3]) : 0;

    unsigned char* fontBuffer;

    /* load font file */
    {
        long size;
        FILE* fontFile = fopen(argv[1], "rb");
        fseek(fontFile, 0, SEEK_END);
        size = ftell(fontFile);
        fseek(fontFile, 0, SEEK_SET);

        fontBuffer = malloc(size);

        fread(fontBuffer, size, 1, fontFile);
        fclose(fontFile);
    }

    /* prepare font */
    stbtt_fontinfo info;
    if (!stbtt_InitFont(&info, fontBuffer, 0)) {
        printf("failed to load ttf font\n");
        return -2;
    }

    int b_w = 0; /* bitmap width */
    int b_h = height; /* bitmap height */
    int c_w = width; /* char width */
    int c_h = height; /* char height */

    /* calculate font scaling */
    float scale = stbtt_ScaleForPixelHeight(&info, c_h);

    if (c_w == 0) {
        /* determine max char width */
        for (unsigned char ch = 0; ch < 128; ++ch) {
            int ax;
            int lsb;
            stbtt_GetCodepointHMetrics(&info, ch, &ax, &lsb);
            if (c_w < ax*scale) {
                c_w = ax*scale;
            }
        }
    }

    b_w = 128*c_w;

    /* create a bitmap for the phrase */
    unsigned char* bitmap = calloc(b_w*b_h, sizeof(unsigned char));

    int x = 0, maxx = 0;
    int ascent, descent, lineGap;
    stbtt_GetFontVMetrics(&info, &ascent, &descent, &lineGap);

    ascent *= scale;
    descent *= scale;

    /* begin C constants output */
    printf("static const int FONT_SIZE_X = %d;\n", c_w);
    printf("static const int FONT_SIZE_Y = %d;\n\n", c_h);
    printf("static const unsigned char kFontRaster[128][FONT_SIZE_Y*FONT_SIZE_X] = {\n");

    for (unsigned char ch = 0; ch < 128; ++ch) {
        /* how wide is this character */
        int ax;
        int lsb;
        stbtt_GetCodepointHMetrics(&info, ch, &ax, &lsb);

        /* get bounding box for character (may be offset to account for chars that dip above or below the line */
        int c_x1, c_y1, c_x2, c_y2;
        stbtt_GetCodepointBitmapBox(&info, ch, scale, scale, &c_x1, &c_y1, &c_x2, &c_y2);

        /* compute y (different characters have different heights */
        int y = ascent + c_y1;
        if (y < 0) y = 0;

        /* render character (stride and offset is important here) */
        int byteOffset = x + (lsb*scale) + (y*b_w);
        stbtt_MakeCodepointBitmap(&info, bitmap + byteOffset, c_x2 - c_x1, c_y2 - c_y1, b_w, scale, scale, ch);

        /* output C array data */
        printf("    {");
        for (int yy = 0; yy < c_h; ++yy) {
            for (int xx = 0; xx < c_w; ++xx) {
                int v = bitmap[byteOffset + (yy - y)*b_w + xx];
                printf(" %3d,", v);
                if (v > 0 && xx > maxx) {
                    maxx = xx;
                }
            }
        }
        printf(" }, // U+%04d\n", ch);

        /* advance x */
        //x += ax*scale; // use true character width
        x += c_w; // force mono-space width

        /* add kerning */
        if (ch < 127) {
            int kern;
            kern = stbtt_GetCodepointKernAdvance(&info, ch, ch + 1);
            x += kern*scale;
        }
    }

    printf("};\n");

    if (maxx + 1 < c_w) {
        printf("// warning : max character width was %d. consider lowering the width parameter to this value\n", maxx + 1);
    }

    /* save out a 1 channel image */
    {
        char tmp[128];
        snprintf(tmp, 128, "raster-h%d.png", c_h);
        stbi_write_png(tmp, b_w, b_h, 1, bitmap, b_w);
    }

    free(fontBuffer);
    free(bitmap);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rsi, %r14
cmpl $0x2, %edi
jg 0x9bcf
movq (%r14), %rsi
leaq 0x5259(%rip), %rdi      # 0xede9
xorl %eax, %eax
callq 0x10a0
movl $0xa, %edi
callq 0x1040
leaq 0x5b6f(%rip), %rdi      # 0xf717
callq 0x1050
leaq 0x5bb6(%rip), %rdi      # 0xf76a
callq 0x1050
leaq 0x5bec(%rip), %rdi      # 0xf7ac
callq 0x1050
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0xa059
movl %edi, %r15d
movq 0x10(%r14), %rdi
callq 0x11e0
movl %eax, -0x30(%rbp)
xorl %ebx, %ebx
cmpl $0x3, %r15d
je 0x9bf1
movq 0x18(%r14), %rdi
callq 0x11e0
movl %eax, %ebx
movq 0x8(%r14), %rdi
leaq 0x5210(%rip), %rsi      # 0xee0c
callq 0x11c0
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1190
movq %r14, %rdi
callq 0x1140
movq %rax, %r15
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1190
movq %r15, %rdi
callq 0x1170
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rcx
callq 0x1060
movq %r14, %rdi
callq 0x1080
leaq -0x140(%rbp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x7796
testl %eax, %eax
je 0xa048
movq %r12, -0x78(%rbp)
cvtsi2ssl -0x30(%rbp), %xmm1
movq -0x138(%rbp), %rdx
movslq -0x11c(%rbp), %rsi
movzbl 0x4(%rdx,%rsi), %eax
shll $0x8, %eax
movzbl 0x5(%rdx,%rsi), %ecx
cwtl
orl %ecx, %eax
movzbl 0x6(%rdx,%rsi), %ecx
shll $0x8, %ecx
movq %rdx, -0x48(%rbp)
movq %rsi, -0x40(%rbp)
movzbl 0x7(%rdx,%rsi), %edx
movswl %cx, %ecx
orl %edx, %ecx
subl %ecx, %eax
cvtsi2ss %eax, %xmm0
divss %xmm0, %xmm1
movss %xmm1, -0x2c(%rbp)
testl %ebx, %ebx
jne 0x9d1b
xorl %r14d, %r14d
leaq -0x140(%rbp), %r15
leaq -0x1c0(%rbp), %r12
leaq -0x50(%rbp), %r13
xorl %ebx, %ebx
movq %r15, %rdi
movl %r14d, %esi
callq 0x35dc
movq %r15, %rdi
movl %eax, %esi
movq %r12, %rdx
movq %r13, %rcx
callq 0x4595
xorps %xmm1, %xmm1
cvtsi2ss %ebx, %xmm1
xorps %xmm0, %xmm0
cvtsi2ssl -0x1c0(%rbp), %xmm0
mulss -0x2c(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x9d0f
cvttss2si %xmm0, %ebx
incl %r14d
cmpl $0x80, %r14d
jne 0x9cd4
movl %ebx, %r15d
shll $0x7, %r15d
movl %r15d, %eax
movl -0x30(%rbp), %r14d
imull %r14d, %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x1130
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %rdx
movzwl 0x4(%rcx,%rdx), %ecx
rolw $0x8, %cx
movswl %cx, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movq %rax, -0x58(%rbp)
mulss -0x2c(%rbp), %xmm0
cvttss2si %xmm0, %eax
movq %rax, -0x80(%rbp)
leaq 0x50a2(%rip), %rdi      # 0xee0f
movl %ebx, %esi
xorl %eax, %eax
callq 0x10a0
leaq 0x50b6(%rip), %rdi      # 0xee33
movl %r14d, %esi
xorl %eax, %eax
callq 0x10a0
leaq 0x593d(%rip), %rdi      # 0xf6cb
callq 0x1050
movl %r15d, -0x4c(%rbp)
movslq %r15d, %r15
movl %r14d, %eax
movq %rax, -0x98(%rbp)
movl %ebx, %r14d
xorl %r12d, %r12d
xorl %r13d, %r13d
movl $0x0, -0x34(%rbp)
movq %r14, -0x48(%rbp)
movq %r15, -0xa0(%rbp)
leaq -0x140(%rbp), %rbx
movq %rbx, %rdi
movl %r12d, %esi
callq 0x35dc
movq %rbx, %rdi
movl %eax, %esi
leaq -0x1c0(%rbp), %rdx
leaq -0x50(%rbp), %rcx
callq 0x4595
movq %rbx, %rdi
movl %r12d, %esi
callq 0x35dc
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
movq %rbx, %rdi
movl %eax, %esi
movss -0x2c(%rbp), %xmm0
movaps %xmm0, %xmm1
leaq -0x6c(%rbp), %rdx
leaq -0x68(%rbp), %rcx
leaq -0x64(%rbp), %r8
leaq -0x60(%rbp), %r9
callq 0x4f19
movl -0x68(%rbp), %eax
movq -0x80(%rbp), %rcx
leal (%rax,%rcx), %edx
xorps %xmm0, %xmm0
cvtsi2ssl -0x34(%rbp), %xmm0
testl %edx, %edx
xorps %xmm1, %xmm1
cvtsi2ssl -0x50(%rbp), %xmm1
movl $0x0, %ecx
cmovlel %ecx, %edx
mulss -0x2c(%rbp), %xmm1
movl %edx, -0x5c(%rbp)
movl %edx, %ecx
imull -0x4c(%rbp), %ecx
xorps %xmm2, %xmm2
cvtsi2ss %ecx, %xmm2
addss %xmm0, %xmm1
addss %xmm1, %xmm2
cvttss2si %xmm2, %ecx
movslq %ecx, %rcx
movq %rcx, -0x88(%rbp)
movq -0x58(%rbp), %rdx
addq %rdx, %rcx
movq %rcx, -0x40(%rbp)
movl -0x64(%rbp), %ebx
subl -0x6c(%rbp), %ebx
movl -0x60(%rbp), %r14d
subl %eax, %r14d
leaq -0x140(%rbp), %rdi
movq %r12, -0x90(%rbp)
movl %r12d, %esi
callq 0x35dc
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
leaq -0x140(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl %ebx, %edx
movl %r14d, %ecx
movl %r15d, %r8d
movss -0x2c(%rbp), %xmm0
movaps %xmm0, %xmm1
movl %eax, %r9d
callq 0x63ad
leaq 0x4f92(%rip), %rdi      # 0xee58
xorl %eax, %eax
callq 0x10a0
cmpl $0x0, -0x30(%rbp)
jle 0x9f5b
movl -0x5c(%rbp), %eax
imulq %r15, %rax
movq -0x88(%rbp), %r12
subq %rax, %r12
addq -0x58(%rbp), %r12
xorl %ecx, %ecx
movl %r13d, %ebx
movq -0x48(%rbp), %r14
movq %rcx, -0x40(%rbp)
testl %r14d, %r14d
jle 0x9f39
xorl %r15d, %r15d
movl %ebx, %r13d
movzbl (%r12,%r15), %r14d
leaq 0x4f4e(%rip), %rdi      # 0xee5e
movl %r14d, %esi
xorl %eax, %eax
callq 0x10a0
cmpl %ebx, %r15d
cmovgl %r15d, %r13d
testl %r14d, %r14d
movq -0x48(%rbp), %r14
cmovel %ebx, %r13d
incq %r15
movl %r13d, %ebx
cmpq %r15, %r14
jne 0x9f04
jmp 0x9f3c
movl %ebx, %r13d
movq -0x40(%rbp), %rcx
incq %rcx
movq -0xa0(%rbp), %r15
addq %r15, %r12
movl %r13d, %ebx
cmpq -0x98(%rbp), %rcx
jne 0x9ef5
jmp 0x9f5f
movq -0x48(%rbp), %r14
leaq 0x4efe(%rip), %rdi      # 0xee64
movq -0x90(%rbp), %r12
movl %r12d, %esi
xorl %eax, %eax
callq 0x10a0
movl -0x34(%rbp), %ebx
addl %r14d, %ebx
cmpl $0x7f, %r12d
jne 0x9f88
movl %ebx, -0x34(%rbp)
jmp 0x9fba
leal 0x1(%r12), %edx
leaq -0x140(%rbp), %rdi
movl %r12d, %esi
callq 0x4c2b
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %ebx, %xmm1
mulss -0x2c(%rbp), %xmm0
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x34(%rbp)
incl %r12d
cmpl $0x80, %r12d
jne 0x9dbf
leaq 0x5743(%rip), %rdi      # 0xf714
callq 0x1050
incl %r13d
cmpl %r14d, %r13d
movq -0x78(%rbp), %r12
jge 0x9ff3
leaq 0x4e8a(%rip), %rdi      # 0xee73
movl %r13d, %esi
xorl %eax, %eax
callq 0x10a0
leaq 0x4ed7(%rip), %rdx      # 0xeed1
xorl %ebx, %ebx
leaq -0x1c0(%rbp), %r14
movl $0x80, %esi
movq %r14, %rdi
movl -0x30(%rbp), %r15d
movl %r15d, %ecx
xorl %eax, %eax
callq 0x10b0
movq %r14, %rdi
movl -0x4c(%rbp), %esi
movl %r15d, %edx
movl $0x1, %ecx
movq -0x58(%rbp), %r14
movq %r14, %r8
movl %esi, %r9d
callq 0x2d91
movq %r12, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
jmp 0xa059
leaq 0x5664(%rip), %rdi      # 0xf6b3
callq 0x1050
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
movl %ebx, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq