// File: /workspace/github_c_makefile_star2_O2/justinmeiners[P]text2image/text2image.c
// Function: main
int main(int argc, const char * argv[])
{
    unsigned char* font_buffer = font_cmunrm_ttf;
    int line_height = 48;
    int invert = 0;
    const char* out_path = NULL;
    const char* font_path = NULL;

    for (int i = 1; i < argc; ++i)
    {
        if (strcmp(argv[i], "--height") == 0)
        {
            line_height = atoi(argv[i + 1]);
            ++i;
        }
        else if (strcmp(argv[i], "--font") == 0)
        {
            font_path  = argv[i + 1];
            ++i;
        }
        else if (strcmp(argv[i], "--out") == 0)
        {
            out_path = argv[i + 1];
            ++i;
        }
        else if (strcmp(argv[i], "--invert") == 0)
        {
            invert = 1;
        }
        else if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h"))
        {
            show_usage(1);
        }
        else
        {
            show_usage(0);
        }
    }

    if (font_path)
    {
        font_buffer = read_font_file(font_path);
    }

    /* prepare font */
    stbtt_fontinfo font_info;
    if (!stbtt_InitFont(&font_info, font_buffer, 0))
    {
        fprintf(stderr, "failed to initialize font.");
        exit(2);
    }

    /* read stdin into a string.
       we have to do this, because it needs two passes
    */
    size_t text_size = 0;
    size_t text_capacity = 2048;
    char* text = malloc(text_capacity);

    if (!text)
    {
        fprintf(stderr, "allocation failed. out of memory?");
        exit(2);
    }

    while (!feof(stdin))
    {
        if (text_size >= text_capacity)
        {
            text_capacity = text_capacity * 3 / 2;
            text = realloc(text, sizeof(char) * text_capacity);

            if (!text)
            {
                fprintf(stderr, "allocation failed. out of memory?");
                exit(2);
            }
        }

        size_t read = fread(text, sizeof(char), (text_capacity - text_size), stdin);
        text_size += read;
    }
    
    /* cleanup characters */

    for (int i = 0; i < text_size; ++i)
    {
        if (text[i] == '\n')
        {
            text[i] = ' ';
        }
        else if (text[i] == '\r')
        {
            text[i] = ' ';
        }
    }

    float scale = stbtt_ScaleForPixelHeight(&font_info, line_height);

    /* first pass */
    int image_w;  
    int image_h = line_height;

    render_text(&font_info, NULL, &image_w, scale, text, text_size);

    /* second pass */
    unsigned char* bitmap = malloc(image_w * image_h);
    if (!bitmap)
    {
        fprintf(stderr, "allocation failed. out of memory?");
        exit(2);
    }

    render_text(&font_info, bitmap, &image_w, scale, text, text_size);
    
    if (!invert)
    {
        invert_image(bitmap, image_w, image_h);
    }
    
    /* save out a 1 channel image */

    FILE* out_file = stdout;
    if (out_path)
    {
        out_file = fopen(out_path, "wb");
    }

    stbi_write_png_to_func(image_stdio_write, out_file, image_w, image_h, 1, bitmap, image_w);

    if (out_file != stdout)
    {
        fclose(out_file);
    }
    
    if (font_buffer != font_cmunrm_ttf)
    {
        free(font_buffer);
    }

    free(text);
    free(bitmap);
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %r15
movl %edi, %ebp
pushq $0x30
popq %rax
movl %eax, 0x14(%rsp)
movq $0x0, 0x18(%rsp)
pushq $0x1
popq %r12
xorl %r13d, %r13d
movl $0x0, 0x10(%rsp)
cmpl %ebp, %r12d
jge 0x8f76
movslq %r12d, %r14
movq (%r15,%r14,8), %rbx
movq %rbx, %rdi
leaq 0x50d3(%rip), %rsi      # 0xdfc9
callq 0x1100
testl %eax, %eax
je 0x8f44
movq %rbx, %rdi
leaq 0x50c9(%rip), %rsi      # 0xdfd2
callq 0x1100
testl %eax, %eax
je 0x8f57
movq %rbx, %rdi
leaq 0x50bd(%rip), %rsi      # 0xdfd9
callq 0x1100
testl %eax, %eax
je 0x8f66
movq %rbx, %rdi
leaq 0x50b0(%rip), %rsi      # 0xdfdf
callq 0x1100
pushq $0x1
popq %rcx
movl %ecx, 0x10(%rsp)
testl %eax, %eax
je 0x8f6e
jmp 0x9173
incl %r12d
movq 0x8(%r15,%r14,8), %rdi
callq 0x11d0
movl %eax, 0x14(%rsp)
jmp 0x8f6e
incl %r12d
movq 0x8(%r15,%r14,8), %rax
movq %rax, 0x18(%rsp)
jmp 0x8f6e
incl %r12d
movq 0x8(%r15,%r14,8), %r13
incl %r12d
jmp 0x8edc
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x8f8a
callq 0x8d60
movq %rax, %r15
jmp 0x8f91
leaq 0x91af(%rip), %r15      # 0x12140
leaq 0x28(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x6c6f
testl %eax, %eax
je 0x91a6
movq %r13, 0x18(%rsp)
movl $0x800, %ebx            # imm = 0x800
movl $0x800, %edi            # imm = 0x800
callq 0x1160
testq %rax, %rax
je 0x914f
movq %rax, %r12
xorl %r13d, %r13d
movq 0x9006(%rip), %rbp      # 0x11fd8
pushq $0x1
popq %r14
movq (%rbp), %rdi
callq 0x1130
testl %eax, %eax
jne 0x9020
cmpq %rbx, %r13
jb 0x9006
leaq (%rbx,%rbx,2), %rbx
shrq %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1190
movq %rax, %r12
testq %rax, %rax
je 0x914f
movq %rbx, %rdx
subq %r13, %rdx
movq (%rbp), %rcx
movq %r12, %rdi
movq %r14, %rsi
callq 0x1040
addq %rax, %r13
jmp 0x8fd6
xorl %eax, %eax
movl 0x14(%rsp), %ebp
cmpq %rax, %r13
je 0x9044
movzbl (%r12,%rax), %ecx
cmpl $0xd, %ecx
je 0x903a
cmpl $0xa, %ecx
jne 0x903f
movb $0x20, (%r12,%rax)
incq %rax
jmp 0x9026
cvtsi2ss %ebp, %xmm0
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x48c1
leaq 0xc(%rsp), %r14
movq %rbx, %rdi
xorl %esi, %esi
movq %r14, %rdx
movss %xmm0, 0x24(%rsp)
movq %r12, %rcx
movq %r13, %r8
callq 0x8be9
movslq (%r14), %rax
movslq %ebp, %rdi
imulq %rax, %rdi
callq 0x1160
testq %rax, %rax
je 0x914f
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movq %rax, %rsi
movss 0x24(%rsp), %xmm0
movq %r12, %rcx
movq %r13, %r8
callq 0x8be9
cmpl $0x0, 0x10(%rsp)
movq 0x18(%rsp), %r14
jne 0x90c6
movl 0xc(%rsp), %esi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x8bd1
movq 0x8f03(%rip), %r13      # 0x11fd0
testq %r14, %r14
je 0x90e6
leaq 0x4f31(%rip), %rsi      # 0xe00a
movq %r14, %rdi
callq 0x11b0
movq %rax, %r14
jmp 0x90ea
movq (%r13), %r14
movl 0xc(%rsp), %edx
movl %edx, (%rsp)
leaq 0xc3(%rip), %rdi        # 0x91bb
pushq $0x1
popq %r8
movq %r14, %rsi
movl %ebp, %ecx
movq %rbx, %r9
callq 0x2b82
cmpq (%r13), %r14
je 0x9117
movq %r14, %rdi
callq 0x1060
leaq 0x9022(%rip), %rax      # 0x12140
cmpq %rax, %r15
je 0x912b
movq %r15, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8ea2(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x4d1f(%rip), %rdi      # 0xde7f
pushq $0x21
popq %rsi
pushq $0x1
popq %rdx
callq 0x1200
pushq $0x2
popq %rdi
callq 0x11f0
leaq 0x4e6e(%rip), %rsi      # 0xdfe8
movq %rbx, %rdi
callq 0x1100
testl %eax, %eax
je 0x919e
cmpb $0x2d, (%rbx)
jne 0x919e
cmpb $0x68, 0x1(%rbx)
jne 0x919e
cmpb $0x0, 0x2(%rbx)
jne 0x919e
xorl %edi, %edi
callq 0x8e1c
pushq $0x1
popq %rdi
callq 0x8e1c
movq 0x8e4b(%rip), %rax      # 0x11ff8
movq (%rax), %rcx
leaq 0x4e38(%rip), %rdi      # 0xdfef
pushq $0x1a
jmp 0x9162