// File: /workspace/github_c_makefile_star2_O1/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
xorl %r14d, %r14d
cmpl $0x2, %edi
jl 0x9ff4
movq %rsi, %r15
movl %edi, %ebp
movl $0x1, %r12d
movl $0x30, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
movslq %r12d, %r13
movq (%r15,%r13,8), %rbx
movq %rbx, %rdi
leaq 0x609b(%rip), %rsi      # 0xfff1
callq 0x1100
testl %eax, %eax
je 0x9fa3
movq %rbx, %rdi
leaq 0x6091(%rip), %rsi      # 0xfffa
callq 0x1100
testl %eax, %eax
je 0x9fb7
movq %rbx, %rdi
leaq 0x6085(%rip), %rsi      # 0x10001
callq 0x1100
testl %eax, %eax
je 0x9fc7
movq %rbx, %rdi
leaq 0x6078(%rip), %rsi      # 0x10007
callq 0x1100
movl $0x1, %r14d
testl %eax, %eax
je 0x9fd5
jmp 0xa251
leal 0x1(%r13), %r12d
movq 0x8(%r15,%r13,8), %rdi
callq 0x11d0
movl %eax, 0x8(%rsp)
jmp 0x9fd5
leal 0x1(%r13), %r12d
movq 0x8(%r15,%r13,8), %rax
movq %rax, 0x10(%rsp)
jmp 0x9fd5
leal 0x1(%r13), %r12d
movq 0x8(%r15,%r13,8), %rax
movq %rax, 0x18(%rsp)
incl %r12d
cmpl %ebp, %r12d
jl 0x9f45
testl %r14d, %r14d
sete %bpl
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %rdi
jmp 0xa003
movl $0x30, 0x8(%rsp)
movb $0x1, %bpl
xorl %edi, %edi
xorl %ebx, %ebx
testq %rdi, %rdi
je 0xa012
callq 0x9dec
movq %rax, %r15
jmp 0xa019
leaq 0xa127(%rip), %r15      # 0x14140
leaq 0x28(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x7744
testl %eax, %eax
je 0xa286
movl $0x800, %edi            # imm = 0x800
callq 0x1160
testq %rax, %rax
je 0xa28b
movq %rax, %r12
movb %bpl, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movq 0x9f81(%rip), %rbx      # 0x13fd8
movq (%rbx), %rdi
callq 0x1130
testl %eax, %eax
je 0xa068
xorl %r14d, %r14d
jmp 0xa0bf
movl $0x800, %r13d           # imm = 0x800
xorl %ebp, %ebp
cmpq %r13, %rbp
jb 0xa094
leaq (%r13,%r13,2), %r13
shrq %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x1190
movq %rax, %r12
testq %rax, %rax
je 0xa24c
movq %r13, %rdx
subq %rbp, %rdx
movq (%rbx), %rcx
movl $0x1, %esi
movq %r12, %rdi
callq 0x1040
movq %rax, %r14
addq %rbp, %r14
movq (%rbx), %rdi
callq 0x1130
movq %r14, %rbp
testl %eax, %eax
je 0xa070
testq %r14, %r14
je 0xa0e2
xorl %eax, %eax
movzbl (%r12,%rax), %ecx
cmpl $0xd, %ecx
je 0xa0d5
cmpl $0xa, %ecx
jne 0xa0da
movb $0x20, (%r12,%rax)
incq %rax
cmpq %rax, %r14
jne 0xa0c6
movl 0x8(%rsp), %ebp
cvtsi2ss %ebp, %xmm0
leaq 0x28(%rsp), %rdi
movq 0x8(%rdi), %rax
movslq 0x24(%rdi), %rcx
movzbl 0x4(%rax,%rcx), %edx
shll $0x8, %edx
movzbl 0x5(%rax,%rcx), %esi
movswl %dx, %edx
orl %esi, %edx
movzbl 0x6(%rax,%rcx), %esi
shll $0x8, %esi
movzbl 0x7(%rax,%rcx), %eax
movswl %si, %ecx
orl %eax, %ecx
subl %ecx, %edx
cvtsi2ss %edx, %xmm1
divss %xmm1, %xmm0
leaq 0xc(%rsp), %rbx
xorl %esi, %esi
movq %rbx, %rdx
movss %xmm0, 0x20(%rsp)
movq %r12, %rcx
movq %r14, %r8
callq 0x9c1c
movslq (%rbx), %rax
movslq %ebp, %rdi
imulq %rax, %rdi
callq 0x1160
testq %rax, %rax
je 0xa290
movq %rax, %r13
leaq 0x28(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movq %rax, %rsi
movss 0x20(%rsp), %xmm0
movq %r12, %rcx
movq %r14, %r8
callq 0x9c1c
cmpb $0x0, 0x10(%rsp)
je 0xa19e
movl 0xc(%rsp), %eax
imull 0x8(%rsp), %eax
testl %eax, %eax
jle 0xa19e
movl %eax, %eax
xorl %ecx, %ecx
notb (%r13,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0xa191
movq 0x9e2b(%rip), %rbp      # 0x13fd0
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0xa1c0
leaq 0x5e7f(%rip), %rsi      # 0x10035
callq 0x11b0
movq %rax, %rbx
jmp 0xa1c4
movq (%rbp), %rbx
movl 0xc(%rsp), %edx
leaq 0x24(%rsp), %r9
movq %r13, %rdi
movl %edx, %esi
movl 0x8(%rsp), %ecx
movl $0x1, %r8d
callq 0x26dd
testq %rax, %rax
je 0xa206
movq %rax, %r14
movslq 0x24(%rsp), %rdx
movl $0x1, %esi
movq %rax, %rdi
movq %rbx, %rcx
callq 0x1200
movq %r14, %rdi
callq 0x1030
cmpq (%rbp), %rbx
je 0xa214
movq %rbx, %rdi
callq 0x1060
leaq 0x9f25(%rip), %rax      # 0x14140
cmpq %rax, %r15
je 0xa228
movq %r15, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x12a8
leaq 0x5db8(%rip), %rsi      # 0x10010
movq %rbx, %rdi
callq 0x1100
testl %eax, %eax
je 0xa27c
leaq 0x5dac(%rip), %rsi      # 0x10017
movq %rbx, %rdi
callq 0x1100
testl %eax, %eax
jne 0xa27c
callq 0x1289
movl $0x1, %edi
callq 0x9e76
callq 0x1317
callq 0x12f2
callq 0x12cd