// File: /workspace/github_c_makefile_star2_O3/justinmeiners[P]stb-truetype-example/main.c
// Function: main
int main(int argc, const char * argv[])
{
    /* load font file */
    long size;
    unsigned char* fontBuffer;
    
    FILE* fontFile = fopen("font/cmunrm.ttf", "rb");
    fseek(fontFile, 0, SEEK_END);
    size = ftell(fontFile); /* how long is the file ? */
    fseek(fontFile, 0, SEEK_SET); /* reset */
    
    fontBuffer = malloc(size);
    
    fread(fontBuffer, size, 1, fontFile);
    fclose(fontFile);

    /* prepare font */
    stbtt_fontinfo info;
    if (!stbtt_InitFont(&info, fontBuffer, 0))
    {
        printf("failed\n");
    }
    
    int b_w = 512; /* bitmap width */
    int b_h = 128; /* bitmap height */
    int l_h = 64; /* line height */

    /* create a bitmap for the phrase */
    unsigned char* bitmap = calloc(b_w * b_h, sizeof(unsigned char));
    
    /* calculate font scaling */
    float scale = stbtt_ScaleForPixelHeight(&info, l_h);

    char* word = "the quick brown fox";
    
    int x = 0;
       
    int ascent, descent, lineGap;
    stbtt_GetFontVMetrics(&info, &ascent, &descent, &lineGap);
    
    ascent = roundf(ascent * scale);
    descent = roundf(descent * scale);
    
    int i;
    for (i = 0; i < strlen(word); ++i)
    {
        /* how wide is this character */
        int ax;
	int lsb;
        stbtt_GetCodepointHMetrics(&info, word[i], &ax, &lsb);
        /* (Note that each Codepoint call has an alternative Glyph version which caches the work required to lookup the character word[i].) */

        /* 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, word[i], scale, scale, &c_x1, &c_y1, &c_x2, &c_y2);
        
        /* compute y (different characters have different heights) */
        int y = ascent + c_y1;
        
        /* render character (stride and offset is important here) */
        int byteOffset = x + roundf(lsb * scale) + (y * b_w);
        stbtt_MakeCodepointBitmap(&info, bitmap + byteOffset, c_x2 - c_x1, c_y2 - c_y1, b_w, scale, scale, word[i]);

        /* advance x */
        x += roundf(ax * scale);
        
        /* add kerning */
        int kern;
        kern = stbtt_GetCodepointKernAdvance(&info, word[i], word[i + 1]);
        x += roundf(kern * scale);
    }
    
    /* save out a 1 channel image */
    stbi_write_png("out.png", b_w, b_h, 1, bitmap, b_w);
	
    /*
     Note that this example writes each character directly into the target image buffer.
     The "right thing" to do for fonts that have overlapping characters is
     MakeCodepointBitmap to a temporary buffer and then alpha blend that onto the target image.
     See the stb_truetype.h header for more info.
    */
    
    free(fontBuffer);
    free(bitmap);
    
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
leaq 0x5649(%rip), %rdi      # 0xede9
leaq 0x5652(%rip), %rsi      # 0xedf9
callq 0x11a0
movq %rax, %rbx
xorl %r13d, %r13d
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1170
movq %rbx, %rdi
callq 0x1120
movq %rax, %r15
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1170
movq %r15, %rdi
callq 0x1150
movq %rax, %r14
movl $0x1, %r12d
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
movq %rbx, %rcx
callq 0x1050
movq %rbx, %rdi
callq 0x1080
leaq -0x120(%rbp), %rdi
movq %r14, -0x78(%rbp)
movq %r14, %rsi
xorl %edx, %edx
callq 0x752f
testl %eax, %eax
jne 0x9829
leaq 0x5d89(%rip), %rdi      # 0xf5ad
callq 0x1040
movl $0x10000, %edi          # imm = 0x10000
movl $0x1, %esi
callq 0x1110
leaq -0x120(%rbp), %rdx
movq 0x8(%rdx), %rcx
movslq 0x24(%rdx), %rdx
movzbl 0x5(%rcx,%rdx), %esi
movzbl 0x4(%rcx,%rdx), %edi
shll $0x8, %edi
movswl %di, %edi
orl %esi, %edi
movzbl 0x7(%rcx,%rdx), %esi
movzbl 0x6(%rcx,%rdx), %ecx
shll $0x8, %ecx
movswl %cx, %ecx
orl %esi, %ecx
cvtsi2ss %edi, %xmm0
subl %ecx, %edi
cvtsi2ss %edi, %xmm1
movq %rax, -0x50(%rbp)
movss 0x5287(%rip), %xmm2     # 0xeb08
divss %xmm1, %xmm2
movss %xmm2, -0x2c(%rbp)
mulss %xmm2, %xmm0
callq 0x1060
cvttss2si %xmm0, %eax
movq %rax, -0x80(%rbp)
movb $0x74, %bl
movq %r12, -0x58(%rbp)
movsbl %bl, %r12d
leaq -0x120(%rbp), %rbx
movq %rbx, %rdi
movl %r12d, %esi
callq 0x356a
movq %rbx, %rdi
movl %eax, %esi
leaq -0x44(%rbp), %rdx
leaq -0x40(%rbp), %rcx
callq 0x44aa
movq %rbx, %rdi
movl %r12d, %esi
callq 0x356a
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
movq %rbx, %rdi
movl %eax, %esi
movss -0x2c(%rbp), %xmm0
movaps %xmm0, %xmm1
leaq -0x3c(%rbp), %rdx
leaq -0x38(%rbp), %rcx
leaq -0x34(%rbp), %r8
leaq -0x30(%rbp), %r9
callq 0x4d45
movl -0x38(%rbp), %r14d
movq -0x80(%rbp), %rax
leal (%r14,%rax), %ebx
xorps %xmm0, %xmm0
cvtsi2ss %r13d, %xmm0
movss %xmm0, -0x70(%rbp)
xorps %xmm0, %xmm0
cvtsi2ssl -0x40(%rbp), %xmm0
mulss -0x2c(%rbp), %xmm0
callq 0x1060
shll $0x9, %ebx
xorps %xmm1, %xmm1
cvtsi2ss %ebx, %xmm1
addss -0x70(%rbp), %xmm0
addss %xmm0, %xmm1
cvttss2si %xmm1, %eax
movslq %eax, %r13
addq -0x50(%rbp), %r13
movl -0x34(%rbp), %ebx
subl -0x3c(%rbp), %ebx
movl -0x30(%rbp), %r15d
subl %r14d, %r15d
leaq -0x120(%rbp), %r14
movq %r14, %rdi
movl %r12d, %esi
callq 0x356a
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
movq %r14, %rdi
movq %r13, %rsi
movl %ebx, %edx
movl %r15d, %ecx
movl $0x200, %r8d            # imm = 0x200
movss -0x2c(%rbp), %xmm0
movaps %xmm0, %xmm1
movl %eax, %r9d
callq 0x6131
xorps %xmm0, %xmm0
cvtsi2ssl -0x44(%rbp), %xmm0
mulss -0x2c(%rbp), %xmm0
callq 0x1060
addss -0x70(%rbp), %xmm0
movaps %xmm0, -0x70(%rbp)
movq -0x58(%rbp), %rax
leaq 0x544b(%rip), %rcx      # 0xedfc
movb (%rax,%rcx), %bl
movsbl %bl, %edx
movq %r14, %rdi
movl %r12d, %esi
movq -0x58(%rbp), %r12
callq 0x4a73
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss -0x2c(%rbp), %xmm0
callq 0x1060
cvttps2dq -0x70(%rbp), %xmm1
cvtdq2ps %xmm1, %xmm1
addss %xmm0, %xmm1
cvttss2si %xmm1, %r13d
incq %r12
cmpq $0x14, %r12
jne 0x989d
leaq 0x5414(%rip), %rdi      # 0xee10
movl $0x200, %esi            # imm = 0x200
movl $0x80, %edx
movl $0x1, %ecx
movq -0x50(%rbp), %rbx
movq %rbx, %r8
movl $0x200, %r9d            # imm = 0x200
callq 0x2cde
movq -0x78(%rbp), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq