// File: /workspace/github_c_makefile_star2_O3/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 $0x188, %rsp            # imm = 0x188
movq %rsi, %r14
cmpl $0x2, %edi
jg 0x981a
movq (%r14), %rsi
leaq 0x560e(%rip), %rdi      # 0xede9
xorl %eax, %eax
callq 0x10a0
movl $0xa, %edi
callq 0x1040
leaq 0x5ee6(%rip), %rdi      # 0xf6d9
callq 0x1050
leaq 0x5f2d(%rip), %rdi      # 0xf72c
callq 0x1050
leaq 0x5f63(%rip), %rdi      # 0xf76e
callq 0x1050
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x9c97
movl %edi, %r15d
movq 0x10(%r14), %rdi
callq 0x11e0
movl %eax, %r13d
xorl %ebx, %ebx
cmpl $0x3, %r15d
je 0x983c
movq 0x18(%r14), %rdi
callq 0x11e0
movl %eax, %ebx
movq 0x8(%r14), %rdi
leaq 0x55c5(%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 -0x130(%rbp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x755f
testl %eax, %eax
je 0x9c86
movq %r12, -0x80(%rbp)
cvtsi2ss %r13d, %xmm1
movq -0x128(%rbp), %rdx
movslq -0x10c(%rbp), %rsi
movzbl 0x5(%rdx,%rsi), %eax
movzbl 0x4(%rdx,%rsi), %ecx
shll $0x8, %ecx
movswl %cx, %r14d
orl %eax, %r14d
movzbl 0x7(%rdx,%rsi), %eax
movzbl 0x6(%rdx,%rsi), %ecx
shll $0x8, %ecx
movswl %cx, %ecx
orl %eax, %ecx
movl %r14d, %eax
subl %ecx, %eax
cvtsi2ss %eax, %xmm0
divss %xmm0, %xmm1
testl %ebx, %ebx
movss %xmm1, -0x2c(%rbp)
movl %r13d, -0x30(%rbp)
jne 0x9988
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
xorl %r14d, %r14d
leaq -0x130(%rbp), %r15
leaq -0x1b0(%rbp), %r12
leaq -0x48(%rbp), %r13
xorl %ebx, %ebx
movq %r15, %rdi
movl %r14d, %esi
callq 0x359a
movq %r15, %rdi
movl %eax, %esi
movq %r12, %rdx
movq %r13, %rcx
callq 0x44da
xorps %xmm1, %xmm1
cvtsi2ss %ebx, %xmm1
xorps %xmm0, %xmm0
cvtsi2ssl -0x1b0(%rbp), %xmm0
mulss -0x2c(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x9965
cvttss2si %xmm0, %ebx
incl %r14d
cmpl $0x80, %r14d
jne 0x992a
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
movzwl 0x4(%rax,%rcx), %r14d
rolw $0x8, %r14w
movl -0x30(%rbp), %r13d
movl %ebx, %r15d
shll $0x7, %r15d
movl %r15d, %eax
imull %r13d, %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x1130
movswl %r14w, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movq %rax, %r14
mulss -0x2c(%rbp), %xmm0
cvttss2si %xmm0, %eax
movq %rax, -0x88(%rbp)
leaq 0x5447(%rip), %rdi      # 0xee0f
movl %ebx, %esi
xorl %eax, %eax
callq 0x10a0
leaq 0x545b(%rip), %rdi      # 0xee33
movl %r13d, %esi
xorl %eax, %eax
callq 0x10a0
leaq 0x5ca4(%rip), %rdi      # 0xf68d
callq 0x1050
movl %r15d, -0x44(%rbp)
movslq %r15d, %rax
movq %rax, -0x78(%rbp)
movl %r13d, %eax
movq %rax, -0x90(%rbp)
movl %ebx, %eax
movq %rax, -0x40(%rbp)
leaq -0x130(%rbp), %rbx
xorl %eax, %eax
xorl %r15d, %r15d
movq %r14, -0x50(%rbp)
xorl %r13d, %r13d
movq %rbx, %rdi
movq %rax, %r12
movl %r12d, %esi
movq %rax, -0x70(%rbp)
callq 0x359a
movq %rbx, %rdi
movl %eax, %esi
leaq -0x1b0(%rbp), %rdx
leaq -0x48(%rbp), %rcx
callq 0x44da
movq %rbx, %rdi
movl %r12d, %esi
callq 0x359a
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
movq %rbx, %rdi
movl %eax, %esi
movss -0x2c(%rbp), %xmm0
movaps %xmm0, %xmm1
leaq -0x68(%rbp), %rdx
leaq -0x64(%rbp), %rcx
leaq -0x60(%rbp), %r8
leaq -0x5c(%rbp), %r9
callq 0x4d75
movl -0x64(%rbp), %eax
movq -0x88(%rbp), %rcx
leal (%rax,%rcx), %edx
movl %r13d, -0x58(%rbp)
xorps %xmm0, %xmm0
cvtsi2ss %r13d, %xmm0
testl %edx, %edx
xorps %xmm1, %xmm1
cvtsi2ssl -0x48(%rbp), %xmm1
movl $0x0, %ecx
cmovlel %ecx, %edx
mulss -0x2c(%rbp), %xmm1
movl %edx, -0x54(%rbp)
movl %edx, %ecx
imull -0x44(%rbp), %ecx
xorps %xmm2, %xmm2
cvtsi2ss %ecx, %xmm2
addss %xmm0, %xmm1
addss %xmm1, %xmm2
cvttss2si %xmm2, %ecx
movq %rbx, %r13
movslq %ecx, %rbx
leaq (%r14,%rbx), %rcx
movq %rcx, -0x38(%rbp)
movl -0x60(%rbp), %r14d
subl -0x68(%rbp), %r14d
movl -0x5c(%rbp), %r12d
subl %eax, %r12d
movq %r13, %rdi
movq -0x70(%rbp), %rsi
callq 0x359a
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
movq %r13, %rdi
movq -0x38(%rbp), %rsi
movl %r14d, %edx
movl %r12d, %ecx
movq -0x78(%rbp), %r14
movl %r14d, %r8d
movss -0x2c(%rbp), %xmm0
movaps %xmm0, %xmm1
movl %eax, %r9d
callq 0x6161
leaq 0x533c(%rip), %rdi      # 0xee58
xorl %eax, %eax
callq 0x10a0
cmpl $0x0, -0x30(%rbp)
jle 0x9b9d
movl -0x54(%rbp), %eax
imulq %r14, %rax
subq %rax, %rbx
addq -0x50(%rbp), %rbx
xorl %ecx, %ecx
movq -0x40(%rbp), %r14
movq %rcx, -0x38(%rbp)
testl %r14d, %r14d
jle 0x9b81
xorl %r13d, %r13d
movl %r15d, %r12d
movzbl (%rbx,%r13), %r14d
leaq 0x5306(%rip), %rdi      # 0xee5e
movl %r14d, %esi
xorl %eax, %eax
callq 0x10a0
cmpl %r15d, %r13d
cmovgl %r13d, %r12d
testl %r14d, %r14d
movq -0x40(%rbp), %r14
cmovel %r15d, %r12d
incq %r13
movl %r12d, %r15d
cmpq %r13, %r14
jne 0x9b4c
jmp 0x9b84
movl %r15d, %r12d
movq -0x38(%rbp), %rcx
incq %rcx
addq -0x78(%rbp), %rbx
movl %r12d, %r15d
cmpq -0x90(%rbp), %rcx
jne 0x9b3d
jmp 0x9ba4
movl %r15d, %r12d
movq -0x40(%rbp), %r14
leaq 0x52b9(%rip), %rdi      # 0xee64
movq -0x70(%rbp), %r13
movl %r13d, %esi
xorl %eax, %eax
callq 0x10a0
cmpl $0x7f, %r13d
je 0x9c0b
movl -0x58(%rbp), %r15d
addl %r14d, %r15d
leal 0x1(%r13), %r14d
leaq -0x130(%rbp), %rbx
movq %rbx, %rdi
movl %r13d, %esi
movl %r14d, %edx
callq 0x4aa3
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %r15d, %xmm1
mulss -0x2c(%rbp), %xmm0
addss %xmm1, %xmm0
cvttss2si %xmm0, %r13d
movl %r14d, %eax
movl %r12d, %r15d
movq -0x50(%rbp), %r14
jmp 0x9a1c
leaq 0x5ac4(%rip), %rdi      # 0xf6d6
callq 0x1050
incl %r12d
cmpl %r14d, %r12d
movl -0x30(%rbp), %r15d
jge 0x9c34
leaq 0x5249(%rip), %rdi      # 0xee73
movl %r12d, %esi
xorl %eax, %eax
callq 0x10a0
leaq 0x5296(%rip), %rdx      # 0xeed1
xorl %ebx, %ebx
leaq -0x1b0(%rbp), %r14
movl $0x80, %esi
movq %r14, %rdi
movl %r15d, %ecx
xorl %eax, %eax
callq 0x10b0
movq %r14, %rdi
movl -0x44(%rbp), %esi
movl %r15d, %edx
movl $0x1, %ecx
movq -0x50(%rbp), %r14
movq %r14, %r8
movl %esi, %r9d
callq 0x2d0e
movq -0x80(%rbp), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
jmp 0x9c97
leaq 0x59e8(%rip), %rdi      # 0xf675
callq 0x1050
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
movl %ebx, %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq