// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x128, %rsp            # imm = 0x128
movl $0x0, 0x124(%rsp)
movl %edi, 0x120(%rsp)
movq %rsi, 0x118(%rsp)
leaq 0xe498(%rip), %rax      # 0x1d1f0
movq %rax, 0x110(%rsp)
movl $0x30, 0x10c(%rsp)
movl $0x0, 0x108(%rsp)
movq $0x0, 0x100(%rsp)
movq $0x0, 0xf8(%rsp)
movl $0x1, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
cmpl 0x120(%rsp), %eax
jge 0xef74
movq 0x118(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xace0(%rip), %rsi      # 0x19aa8
callq 0x1100
cmpl $0x0, %eax
jne 0xee0d
movq 0x118(%rsp), %rax
movl 0xf4(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x11b0
movl %eax, 0x10c(%rsp)
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0xef5c
movq 0x118(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xac89(%rip), %rsi      # 0x19ab1
callq 0x1100
cmpl $0x0, %eax
jne 0xee69
movq 0x118(%rsp), %rax
movl 0xf4(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0xf8(%rsp)
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0xef5a
movq 0x118(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xac34(%rip), %rsi      # 0x19ab8
callq 0x1100
cmpl $0x0, %eax
jne 0xeec5
movq 0x118(%rsp), %rax
movl 0xf4(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x100(%rsp)
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0xef58
movq 0x118(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xabde(%rip), %rsi      # 0x19abe
callq 0x1100
cmpl $0x0, %eax
jne 0xeef7
movl $0x1, 0x108(%rsp)
jmp 0xef56
movq 0x118(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xabb5(%rip), %rsi      # 0x19ac7
callq 0x1100
cmpl $0x0, %eax
je 0xef41
movq 0x118(%rsp), %rax
movslq 0xf4(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xab97(%rip), %rsi      # 0x19ace
callq 0x1100
cmpl $0x0, %eax
je 0xef4d
movl $0x1, %edi
callq 0xec60
jmp 0xef54
xorl %edi, %edi
callq 0xec60
jmp 0xef56
jmp 0xef58
jmp 0xef5a
jmp 0xef5c
jmp 0xef5e
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0xed99
cmpq $0x0, 0xf8(%rsp)
je 0xef94
movq 0xf8(%rsp), %rdi
callq 0xeb70
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rsi
leaq 0x50(%rsp), %rdi
xorl %edx, %edx
callq 0xa8f0
cmpl $0x0, %eax
jne 0xefcf
movq 0xe044(%rip), %rax      # 0x1cff8
movq (%rax), %rdi
leaq 0xab13(%rip), %rsi      # 0x19ad1
movb $0x0, %al
callq 0x1110
movl $0x2, %edi
callq 0x11d0
movq $0x0, 0x48(%rsp)
movq $0x800, 0x40(%rsp)      # imm = 0x800
movq 0x40(%rsp), %rdi
callq 0x1150
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0xf01a
movq 0xdff9(%rip), %rax      # 0x1cff8
movq (%rax), %rdi
leaq 0xa955(%rip), %rsi      # 0x1995e
movb $0x0, %al
callq 0x1110
movl $0x2, %edi
callq 0x11d0
jmp 0xf01c
movq 0xdfb5(%rip), %rax      # 0x1cfd8
movq (%rax), %rdi
callq 0x1130
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xf03c
jmp 0xf0d6
movq 0x48(%rsp), %rax
cmpq 0x40(%rsp), %rax
jb 0xf09a
imulq $0x3, 0x40(%rsp), %rax
shrq %rax
movq %rax, 0x40(%rsp)
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rsi
shlq $0x0, %rsi
callq 0x1170
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0xf098
movq 0xdf7b(%rip), %rax      # 0x1cff8
movq (%rax), %rdi
leaq 0xa8d7(%rip), %rsi      # 0x1995e
movb $0x0, %al
callq 0x1110
movl $0x2, %edi
callq 0x11d0
jmp 0xf09a
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rdx
subq 0x48(%rsp), %rdx
movq 0xdf28(%rip), %rax      # 0x1cfd8
movq (%rax), %rcx
movl $0x1, %esi
callq 0x1040
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
addq 0x48(%rsp), %rax
movq %rax, 0x48(%rsp)
jmp 0xf01c
movl $0x0, 0x2c(%rsp)
movslq 0x2c(%rsp), %rax
cmpq 0x48(%rsp), %rax
jae 0xf13f
movq 0x38(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0xf10d
movq 0x38(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movb $0x20, (%rax,%rcx)
jmp 0xf130
movq 0x38(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jne 0xf12e
movq 0x38(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movb $0x20, (%rax,%rcx)
jmp 0xf130
jmp 0xf132
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xf0de
cvtsi2ssl 0x10c(%rsp), %xmm0
leaq 0x50(%rsp), %rdi
callq 0x7780
movss %xmm0, 0x28(%rsp)
movl 0x10c(%rsp), %eax
movl %eax, 0x20(%rsp)
movss 0x28(%rsp), %xmm0
movq 0x38(%rsp), %rcx
movq 0x48(%rsp), %r8
leaq 0x50(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x24(%rsp), %rdx
callq 0xe950
movl 0x24(%rsp), %eax
imull 0x20(%rsp), %eax
movslq %eax, %rdi
callq 0x1150
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0xf1c6
movq 0xde4d(%rip), %rax      # 0x1cff8
movq (%rax), %rdi
leaq 0xa7a9(%rip), %rsi      # 0x1995e
movb $0x0, %al
callq 0x1110
movl $0x2, %edi
callq 0x11d0
movq 0x18(%rsp), %rsi
movss 0x28(%rsp), %xmm0
movq 0x38(%rsp), %rcx
movq 0x48(%rsp), %r8
leaq 0x50(%rsp), %rdi
leaq 0x24(%rsp), %rdx
callq 0xe950
cmpl $0x0, 0x108(%rsp)
jne 0xf206
movq 0x18(%rsp), %rdi
movl 0x24(%rsp), %esi
movl 0x20(%rsp), %edx
callq 0xe8f0
movq 0xddc3(%rip), %rax      # 0x1cfd0
movq (%rax), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x100(%rsp)
je 0xf239
movq 0x100(%rsp), %rdi
leaq 0xa8bd(%rip), %rsi      # 0x19aec
callq 0x1190
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rsi
movl 0x24(%rsp), %edx
movl 0x20(%rsp), %ecx
movq 0x18(%rsp), %r9
movl 0x24(%rsp), %eax
leaq 0x6a(%rip), %rdi        # 0xf2c0
movl $0x1, %r8d
movl %eax, (%rsp)
callq 0x3d90
movq 0x10(%rsp), %rax
movq 0xdd60(%rip), %rcx      # 0x1cfd0
cmpq (%rcx), %rax
je 0xf27f
movq 0x10(%rsp), %rdi
callq 0x1060
leaq 0xdf6a(%rip), %rax      # 0x1d1f0
cmpq %rax, 0x110(%rsp)
je 0xf29d
movq 0x110(%rsp), %rdi
callq 0x1030
movq 0x38(%rsp), %rdi
callq 0x1030
movq 0x18(%rsp), %rdi
callq 0x1030
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
retq
nopl (%rax,%rax)