// File: /workspace/github_cpp_makefile_star2_O3/margual56[P]Markdown-CLI/src/main.cpp
// Function: main
int main(int argc, char **argv) {
    int c;
    std::string styleFile, inputFile, outputFile, stdinMarkdown;
    bool verbose_flag;

    while (1) {
        static struct option long_options[] = {
            {"verbose",     no_argument,        0, 'v'},
            {"help",        no_argument,        0, 'h'},
            {"style",       required_argument,  0, 's'},
            {"input",       required_argument,  0, 'i'}
        };

        /* getopt_long stores the option index here. */
        int option_index = 0;

        c = getopt_long(argc, argv, "vhs:i:", long_options, &option_index);

        /* Detect the end of the options. */
        if (c == -1)
            break;

        switch (c) {
            case 'v':
                verbose_flag = true;
            break;

            case 'h':
                printf("Gotta show the man page (or the help)");
                exit(0);
            break;

            case 's':
                styleFile = optarg;
            break;

            case 'i':
                inputFile = optarg;
            break;

            default:
                abort();
        }
    }

    if (verbose_flag)
        puts("Verbose flag is set");

    if (optind+1 < argc) {
        perror("Too many arguments\n");
        exit(2);
    }

    if(inputFile.empty()){
        printf("Input: stdin\nPress Ctrl+D (or Ctrl+Z on Windows) to end.\n");

        // Read from stdin and load it into a variable
        std::string line;
        do{
            std::getline(std::cin, line);
            stdinMarkdown += line + "\n";
        }while(!std::cin.eof());
    }else{
        std::ifstream inputStream(inputFile);

        if(inputStream.is_open()){
            std::string line;
            do{
                std::getline(inputStream, line);
                stdinMarkdown += line + "\n";
            }while(!inputStream.eof());

            inputStream.close();
        }else{
            printf("%s\n", outputFile.c_str());
            perror("Unable to open the output file");
            exit(3);
        }

        if(verbose_flag)
            printf("Input: %s\n", inputFile.c_str());
    }

    //printf("%s\n", stdinMarkdown.c_str());

    HTML *output = new HTML(markdown(stdinMarkdown));

    if(!styleFile.empty() && verbose_flag){
        printf("Style: %s\n", styleFile.c_str());
    }

    if(optind < argc){
        outputFile = std::string(argv[optind]);
        if(verbose_flag)
            printf("Output: %s\n", outputFile.c_str());

        std::ofstream outputStream (outputFile);
        if(outputStream.is_open()){
            outputStream << output->render();
            outputStream.close();
        }else{
            printf("%s\n", outputFile.c_str());
            perror("Unable to open the output file");
            exit(3);
        }
    }else{
        if(verbose_flag)
            printf("Output: stdout\n\n");

        printf("%s\n", output->render().c_str());
    }


    exit(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x318, %rsp            # imm = 0x318
movq %rsi, -0x58(%rbp)
movl %edi, -0x2c(%rbp)
leaq -0x108(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0xc8(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0xa8(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x88(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x18888(%rip), %r12     # 0x1e2c0
leaq -0x340(%rbp), %r13
movl $0x0, -0x340(%rbp)
movl -0x2c(%rbp), %edi
movq -0x58(%rbp), %rsi
leaq 0x10650(%rip), %rdx     # 0x160a7
movq %r12, %rcx
movq %r13, %r8
callq 0x4470
cmpl $0x72, %eax
jle 0x5a7d
leaq -0x118(%rbp), %rbx
cmpl $0x73, %eax
je 0x5a89
cmpl $0x76, %eax
je 0x5a3f
jmp 0x5b05
cmpl $0x69, %eax
jne 0x5ab4
leaq -0xd8(%rbp), %rbx
movq 0x184f0(%rip), %rax     # 0x1df80
movq (%rax), %r14
movq 0x8(%rbx), %r15
movq %r14, %rdi
callq 0x4160
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x4420
jmp 0x5a3f
cmpl $-0x1, %eax
je 0x5ad3
cmpl $0x68, %eax
jne 0x5b05
leaq 0x105e9(%rip), %rdi     # 0x160ae
xorl %eax, %eax
callq 0x4050
xorl %edi, %edi
callq 0x4360
leaq 0x105fa(%rip), %rdi     # 0x160d4
callq 0x4410
movq 0x184ca(%rip), %rbx     # 0x1dfb0
movl (%rbx), %eax
incl %eax
cmpl -0x2c(%rbp), %eax
jge 0x5b0a
leaq 0x105f2(%rip), %rdi     # 0x160e8
callq 0x4250
movl $0x2, %edi
callq 0x4360
callq 0x41c0
cmpq $0x0, -0xd0(%rbp)
jne 0x5d96
leaq 0x1061e(%rip), %rdi     # 0x1613d
callq 0x4410
leaq -0x330(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x40(%rbp), %r14
movq 0x18493(%rip), %r15     # 0x1dfd8
movq (%r15), %rax
movq -0x18(%rax), %rdi
leaq -0x50(%rbp), %r13
leaq -0x98(%rbp), %r12
addq %r15, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %edx
movq %r15, %rdi
leaq -0x340(%rbp), %rsi
callq 0x44d0
movq %r14, -0x50(%rbp)
movq -0x340(%rbp), %rsi
movq -0x338(%rbp), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x4f62
movq %r13, %rdi
leaq 0x1083b(%rip), %rsi     # 0x163d8
callq 0x4510
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %r12, %rdi
callq 0x40e0
movq -0x50(%rbp), %rdi
cmpq %r14, %rdi
je 0x5bc7
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x42b0
movq (%r15), %rax
movq -0x18(%rax), %rdi
testb $0x2, 0x20(%r15,%rdi)
je 0x5b57
movq -0x340(%rbp), %rdi
leaq -0x330(%rbp), %rax
cmpq %rax, %rdi
je 0x5bf8
movq -0x330(%rbp), %rsi
incq %rsi
callq 0x42b0
movl $0x78, %edi
callq 0x42a0
movq %rax, %r15
leaq -0xe8(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rdx
addq %rsi, %rdx
leaq -0xf8(%rbp), %rdi
callq 0x4f62
leaq -0x138(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x6134
leaq -0x138(%rbp), %rsi
movq %r15, %rdi
callq 0x52cc
leaq -0x128(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5c6e
movq -0x128(%rbp), %rsi
incq %rsi
callq 0x42b0
movq -0xf8(%rbp), %rdi
cmpq %r14, %rdi
je 0x5c89
movq -0xe8(%rbp), %rsi
incq %rsi
callq 0x42b0
cmpq $0x0, -0x110(%rbp)
je 0x5ca8
movq -0x118(%rbp), %rsi
leaq 0x10485(%rip), %rdi     # 0x16126
xorl %eax, %eax
callq 0x4050
movslq (%rbx), %rax
cmpl -0x2c(%rbp), %eax
jge 0x5ef0
movq -0x58(%rbp), %rcx
movq (%rcx,%rax,8), %rsi
leaq -0x340(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x60f6
leaq -0xb8(%rbp), %rdi
leaq -0x340(%rbp), %rbx
movq %rbx, %rsi
callq 0x4370
movq (%rbx), %rdi
leaq -0x330(%rbp), %rax
cmpq %rax, %rdi
je 0x5d00
movq -0x330(%rbp), %rsi
incq %rsi
callq 0x42b0
leaq -0xb8(%rbp), %rbx
movq (%rbx), %rsi
leaq 0x10420(%rip), %rdi     # 0x16131
xorl %eax, %eax
callq 0x4050
leaq -0x340(%rbp), %rdi
movq %rbx, %rsi
movl $0x10, %edx
callq 0x4520
leaq -0x2d0(%rbp), %rdi
callq 0x40a0
testb %al, %al
je 0x5ece
leaq -0x50(%rbp), %rdi
movq %r15, %rsi
callq 0x4ad8
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x340(%rbp), %rdi
callq 0x4330
leaq -0x40(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5d79
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x42b0
leaq -0x340(%rbp), %rdi
callq 0x43c0
leaq -0x340(%rbp), %rdi
callq 0x43f0
jmp 0x5acc
leaq -0x340(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
movl $0x8, %edx
callq 0x4440
leaq -0x2c8(%rbp), %rdi
callq 0x40a0
testb %al, %al
je 0x5ece
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x68(%rbp), %r14
leaq -0x340(%rbp), %rax
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rax
leaq -0x78(%rbp), %r13
leaq 0x105e3(%rip), %r15     # 0x163d8
leaq -0x98(%rbp), %r12
leaq (%rax,%rbp), %rdi
addq $-0x340, %rdi           # imm = 0xFCC0
movl $0xa, %esi
callq 0x4290
movsbl %al, %edx
movq %rbx, %rdi
leaq -0x50(%rbp), %rsi
callq 0x44d0
movq %r14, -0x78(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x4f62
movq %r13, %rdi
movq %r15, %rsi
callq 0x4510
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq %r12, %rdi
callq 0x40e0
movq -0x78(%rbp), %rdi
cmpq %r14, %rdi
je 0x5e67
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x42b0
movq -0x340(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x2, -0x320(%rbp,%rax)
je 0x5dfc
leaq -0x340(%rbp), %rdi
callq 0x4090
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x5ea1
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x42b0
movq -0xd8(%rbp), %rsi
leaq 0x1026c(%rip), %rdi     # 0x1611b
xorl %eax, %eax
callq 0x4050
leaq -0x340(%rbp), %rdi
callq 0x40b0
movq 0x180e7(%rip), %rbx     # 0x1dfb0
jmp 0x5bf8
movq -0xb8(%rbp), %rdi
callq 0x4410
leaq 0x1021b(%rip), %rdi     # 0x160fc
callq 0x4250
movl $0x3, %edi
callq 0x4360
leaq 0x1027f(%rip), %rdi     # 0x16176
callq 0x4410
leaq -0x340(%rbp), %rdi
movq %r15, %rsi
callq 0x4ad8
leaq -0x330(%rbp), %rbx
movq -0x10(%rbx), %rdi
callq 0x4410
movq -0x10(%rbx), %rdi
cmpq %rbx, %rdi
je 0x5acc
movq -0x330(%rbp), %rsi
incq %rsi
callq 0x42b0
jmp 0x5acc
jmp 0x5f5c
movq %rax, %rbx
leaq -0x40(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5f5f
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x42b0
jmp 0x5f5f
movq %rax, %rbx
leaq -0x340(%rbp), %rdi
callq 0x43f0
jmp 0x6066
jmp 0x6063
jmp 0x6063
jmp 0x6063
movq %rax, %rbx
leaq -0x128(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5fa8
movq -0x128(%rbp), %rsi
incq %rsi
callq 0x42b0
jmp 0x5fa8
jmp 0x5ff0
movq %rax, %rbx
movq -0xf8(%rbp), %rdi
cmpq %r14, %rdi
je 0x5fe6
movq -0xe8(%rbp), %rsi
incq %rsi
callq 0x42b0
jmp 0x5fe6
jmp 0x5fc7
movq %rax, %rbx
movq -0x78(%rbp), %rdi
cmpq %r14, %rdi
je 0x5ff3
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x42b0
jmp 0x5ff3
jmp 0x5ff0
movq %rax, %rbx
movl $0x78, %esi
movq %r15, %rdi
jmp 0x605a
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x600c
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x42b0
leaq -0x340(%rbp), %rdi
callq 0x40b0
jmp 0x6066
jmp 0x6063
jmp 0x601e
movq %rax, %rbx
movq -0x50(%rbp), %rdi
cmpq %r14, %rdi
je 0x603d
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x42b0
jmp 0x603d
jmp 0x603a
movq %rax, %rbx
movq -0x340(%rbp), %rdi
leaq -0x330(%rbp), %rax
cmpq %rax, %rdi
je 0x6066
movq -0x330(%rbp), %rsi
incq %rsi
callq 0x42b0
jmp 0x6066
jmp 0x6063
movq %rax, %rbx
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x6088
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x42b0
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x60aa
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x42b0
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x60cc
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x42b0
movq -0x118(%rbp), %rdi
leaq -0x108(%rbp), %rax
cmpq %rax, %rdi
je 0x60ee
movq -0x108(%rbp), %rsi
incq %rsi
callq 0x42b0
movq %rbx, %rdi
callq 0x44a0