// File: /workspace/github_c_makefile_star2_O3/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
leaq 0x987c(%rip), %rbx      # 0x13140
xorl %r14d, %r14d
cmpl $0x2, %edi
jl 0x99ab
movq %rsi, %r15
movl %edi, %r12d
movl $0x1, %ebx
movl $0x30, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
movslq %ebx, %r13
movq (%r15,%r13,8), %rbp
movq %rbp, %rdi
leaq 0x570b(%rip), %rsi      # 0xf011
callq 0x1100
testl %eax, %eax
je 0x9953
movq %rbp, %rdi
leaq 0x5701(%rip), %rsi      # 0xf01a
callq 0x1100
testl %eax, %eax
je 0x9967
movq %rbp, %rdi
leaq 0x56f5(%rip), %rsi      # 0xf021
callq 0x1100
testl %eax, %eax
je 0x9977
movq %rbp, %rdi
leaq 0x56e8(%rip), %rsi      # 0xf027
callq 0x1100
movl $0x1, %r14d
testl %eax, %eax
je 0x9985
jmp 0x9c20
leal 0x1(%r13), %ebx
movq 0x8(%r15,%r13,8), %rdi
callq 0x11d0
movl %eax, 0x8(%rsp)
jmp 0x9985
leal 0x1(%r13), %ebx
movq 0x8(%r15,%r13,8), %rax
movq %rax, 0x10(%rsp)
jmp 0x9985
leal 0x1(%r13), %ebx
movq 0x8(%r15,%r13,8), %rax
movq %rax, 0x18(%rsp)
incl %ebx
cmpl %r12d, %ebx
jl 0x98f5
testl %r14d, %r14d
sete %r12b
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x99bf
callq 0x9795
movq %rax, %rbx
jmp 0x99c6
movb $0x1, %r12b
movl $0x30, %r14d
movq $0x0, 0x18(%rsp)
jmp 0x99cb
leaq 0x977a(%rip), %rbx      # 0x13140
movl 0x8(%rsp), %r14d
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x7364
testl %eax, %eax
je 0x9c54
movl $0x800, %edi            # imm = 0x800
callq 0x1160
testq %rax, %rax
je 0x9c59
movq %rax, %r15
movq 0x95d9(%rip), %r13      # 0x12fd8
movq (%r13), %rdi
callq 0x1130
testl %eax, %eax
movq %rbx, 0x10(%rsp)
je 0x9a18
xorl %ebp, %ebp
jmp 0x9ab8
movb %r12b, 0x7(%rsp)
movl %r14d, 0x8(%rsp)
movl $0x800, %ebx            # imm = 0x800
xorl %ebp, %ebp
movq %rbp, %r12
cmpq %rbx, %rbp
jb 0x9a4f
leaq (%rbx,%rbx,2), %rbx
shrq %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1190
movq %rax, %r15
testq %rax, %rax
je 0x9c1b
movq %rbx, %rdx
subq %r12, %rdx
movq (%r13), %rcx
movl $0x1, %esi
movq %r15, %rdi
callq 0x1040
movq %rax, %r14
leaq (%rax,%r12), %rbp
movq (%r13), %rdi
callq 0x1130
testl %eax, %eax
je 0x9a29
testq %rbp, %rbp
je 0x9aac
addq %r12, %r14
xorl %eax, %eax
movb 0x7(%rsp), %r12b
movzbl (%r15,%rax), %ecx
cmpl $0xd, %ecx
je 0x9a98
cmpl $0xa, %ecx
jne 0x9a9d
movb $0x20, (%r15,%rax)
incq %rax
cmpq %rax, %r14
jne 0x9a89
movl 0x8(%rsp), %r14d
jmp 0x9ab8
xorl %ebp, %ebp
movl 0x8(%rsp), %r14d
movb 0x7(%rsp), %r12b
cvtsi2ss %r14d, %xmm0
leaq 0x28(%rsp), %rdi
movq 0x8(%rdi), %rax
movslq 0x24(%rdi), %rcx
movzbl 0x5(%rax,%rcx), %edx
movzbl 0x4(%rax,%rcx), %esi
shll $0x8, %esi
movswl %si, %esi
orl %edx, %esi
movzbl 0x7(%rax,%rcx), %edx
movzbl 0x6(%rax,%rcx), %eax
shll $0x8, %eax
cwtl
orl %edx, %eax
subl %eax, %esi
cvtsi2ss %esi, %xmm1
divss %xmm1, %xmm0
leaq 0xc(%rsp), %rbx
xorl %esi, %esi
movq %rbx, %rdx
movss %xmm0, 0x8(%rsp)
movq %r15, %rcx
movq %rbp, %r8
callq 0x95c5
movslq (%rbx), %rax
movslq %r14d, %rdi
imulq %rax, %rdi
callq 0x1160
testq %rax, %rax
je 0x9c5e
movq %rax, %r13
leaq 0x28(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movq %rax, %rsi
movss 0x8(%rsp), %xmm0
movq %r15, %rcx
movq %rbp, %r8
callq 0x95c5
testb %r12b, %r12b
movq 0x18(%rsp), %rdi
je 0x9b71
movl 0xc(%rsp), %eax
imull %r14d, %eax
testl %eax, %eax
jle 0x9b71
movl %eax, %eax
xorl %ecx, %ecx
notb (%r13,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x9b64
movq 0x9458(%rip), %r12      # 0x12fd0
testq %rdi, %rdi
je 0x9b8e
leaq 0x54ce(%rip), %rsi      # 0xf052
callq 0x11b0
movq %rax, %rbx
jmp 0x9b92
movq (%r12), %rbx
movl 0xc(%rsp), %edx
leaq 0x24(%rsp), %r9
movq %r13, %rdi
movl %edx, %esi
movl %r14d, %ecx
movl $0x1, %r8d
callq 0x25c3
testq %rax, %rax
je 0x9bd3
movq %rax, %r14
movslq 0x24(%rsp), %rdx
movl $0x1, %esi
movq %rax, %rdi
movq %rbx, %rcx
callq 0x1200
movq %r14, %rdi
callq 0x1030
cmpq (%r12), %rbx
je 0x9be1
movq %rbx, %rdi
callq 0x1060
leaq 0x9558(%rip), %rax      # 0x13140
movq 0x10(%rsp), %rdi
cmpq %rax, %rdi
je 0x9bf7
callq 0x1030
movq %r15, %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 0x5409(%rip), %rsi      # 0xf030
movq %rbp, %rdi
callq 0x1100
testl %eax, %eax
je 0x9c4a
cmpb $0x2d, (%rbp)
jne 0x9c4a
cmpb $0x68, 0x1(%rbp)
jne 0x9c4a
cmpb $0x0, 0x2(%rbp)
jne 0x9c4a
callq 0x1289
movl $0x1, %edi
callq 0x981f
callq 0x1317
callq 0x12f2
callq 0x12cd