// File: /workspace/github_c_makefile_star2_O0/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
subq $0x140, %rsp            # imm = 0x140
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x9a98(%rip), %rdi      # 0x16938
leaq 0x9aa1(%rip), %rsi      # 0x16948
callq 0x1180
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x1150
movq -0x28(%rbp), %rdi
callq 0x1120
movq %rax, -0x18(%rbp)
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1150
movq -0x18(%rbp), %rdi
callq 0x1140
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rcx
movl $0x1, %edx
callq 0x1040
movq -0x28(%rbp), %rdi
callq 0x1070
movq -0x20(%rbp), %rsi
leaq -0xc8(%rbp), %rdi
xorl %edx, %edx
callq 0x97b0
cmpl $0x0, %eax
jne 0xcf2f
leaq 0x9a23(%rip), %rdi      # 0x1694b
movb $0x0, %al
callq 0x1090
movl $0x200, -0xcc(%rbp)     # imm = 0x200
movl $0x80, -0xd0(%rbp)
movl $0x40, -0xd4(%rbp)
movl -0xcc(%rbp), %eax
imull -0xd0(%rbp), %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x1110
movq %rax, -0xe0(%rbp)
cvtsi2ssl -0xd4(%rbp), %xmm0
leaq -0xc8(%rbp), %rdi
callq 0x6c30
movss %xmm0, -0xe4(%rbp)
leaq 0x99c2(%rip), %rax      # 0x16953
movq %rax, -0xf0(%rbp)
movl $0x0, -0xf4(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
leaq -0xfc(%rbp), %rdx
leaq -0x100(%rbp), %rcx
callq 0x6900
cvtsi2ssl -0xf8(%rbp), %xmm0
mulss -0xe4(%rbp), %xmm0
callq 0x1050
cvttss2si %xmm0, %eax
movl %eax, -0xf8(%rbp)
cvtsi2ssl -0xfc(%rbp), %xmm0
mulss -0xe4(%rbp), %xmm0
callq 0x1050
cvttss2si %xmm0, %eax
movl %eax, -0xfc(%rbp)
movl $0x0, -0x104(%rbp)
movslq -0x104(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0xf0(%rbp), %rdi
callq 0x1080
movq %rax, %rcx
movq -0x130(%rbp), %rax
cmpq %rcx, %rax
jae 0xd21d
movq -0xf0(%rbp), %rax
movslq -0x104(%rbp), %rcx
movsbl (%rax,%rcx), %esi
leaq -0xc8(%rbp), %rdi
leaq -0x108(%rbp), %rdx
leaq -0x10c(%rbp), %rcx
callq 0x68b0
movq -0xf0(%rbp), %rax
movslq -0x104(%rbp), %rcx
movsbl (%rax,%rcx), %esi
movss -0xe4(%rbp), %xmm0
movss -0xe4(%rbp), %xmm1
leaq -0xc8(%rbp), %rdi
leaq -0x110(%rbp), %rdx
leaq -0x114(%rbp), %rcx
leaq -0x118(%rbp), %r8
leaq -0x11c(%rbp), %r9
callq 0x6f70
movl -0xf8(%rbp), %eax
addl -0x114(%rbp), %eax
movl %eax, -0x120(%rbp)
cvtsi2ssl -0xf4(%rbp), %xmm0
movss %xmm0, -0x134(%rbp)
cvtsi2ssl -0x10c(%rbp), %xmm0
mulss -0xe4(%rbp), %xmm0
callq 0x1050
movaps %xmm0, %xmm1
movss -0x134(%rbp), %xmm0
addss %xmm1, %xmm0
movl -0x120(%rbp), %eax
imull -0xcc(%rbp), %eax
cvtsi2ss %eax, %xmm1
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x124(%rbp)
movq -0xe0(%rbp), %rsi
movslq -0x124(%rbp), %rax
addq %rax, %rsi
movl -0x118(%rbp), %edx
subl -0x110(%rbp), %edx
movl -0x11c(%rbp), %ecx
subl -0x114(%rbp), %ecx
movl -0xcc(%rbp), %r8d
movss -0xe4(%rbp), %xmm0
movss -0xe4(%rbp), %xmm1
movq -0xf0(%rbp), %rax
movslq -0x104(%rbp), %rdi
movsbl (%rax,%rdi), %r9d
leaq -0xc8(%rbp), %rdi
callq 0x7fb0
cvtsi2ssl -0x108(%rbp), %xmm0
mulss -0xe4(%rbp), %xmm0
callq 0x1050
movaps %xmm0, %xmm1
cvtsi2ssl -0xf4(%rbp), %xmm0
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0xf4(%rbp)
movq -0xf0(%rbp), %rax
movslq -0x104(%rbp), %rcx
movsbl (%rax,%rcx), %esi
movq -0xf0(%rbp), %rax
movl -0x104(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %edx
leaq -0xc8(%rbp), %rdi
callq 0x6840
movl %eax, -0x128(%rbp)
cvtsi2ssl -0x128(%rbp), %xmm0
mulss -0xe4(%rbp), %xmm0
callq 0x1050
movaps %xmm0, %xmm1
cvtsi2ssl -0xf4(%rbp), %xmm0
addss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0xf4(%rbp)
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0xd00b
movl -0xcc(%rbp), %esi
movl -0xd0(%rbp), %edx
movq -0xe0(%rbp), %r8
movl -0xcc(%rbp), %r9d
leaq 0x9729(%rip), %rdi      # 0x16967
movl $0x1, %ecx
callq 0x37f0
movq -0x20(%rbp), %rdi
callq 0x1030
movq -0xe0(%rbp), %rdi
callq 0x1030
xorl %eax, %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopl (%rax,%rax)