// File: /workspace/github_cpp_makefile_star2_O1/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 0x198a0(%rip), %r12     # 0x1f2c0
leaq -0x340(%rbp), %r13
movl $0x0, -0x340(%rbp)
movl -0x2c(%rbp), %edi
movq -0x58(%rbp), %rsi
leaq 0x10668(%rip), %rdx     # 0x160a7
movq %r12, %rcx
movq %r13, %r8
callq 0x4470
cmpl $0x72, %eax
jle 0x5a65
leaq -0x118(%rbp), %rbx
cmpl $0x73, %eax
je 0x5a71
cmpl $0x76, %eax
je 0x5a27
jmp 0x5aed
cmpl $0x69, %eax
jne 0x5a9c
leaq -0xd8(%rbp), %rbx
movq 0x19508(%rip), %rax     # 0x1ef80
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 0x5a27
cmpl $-0x1, %eax
je 0x5abb
cmpl $0x68, %eax
jne 0x5aed
leaq 0x10601(%rip), %rdi     # 0x160ae
xorl %eax, %eax
callq 0x4050
xorl %edi, %edi
callq 0x4360
leaq 0x10612(%rip), %rdi     # 0x160d4
callq 0x4410
movq 0x194e2(%rip), %rbx     # 0x1efb0
movl (%rbx), %eax
incl %eax
cmpl -0x2c(%rbp), %eax
jge 0x5af2
leaq 0x1060a(%rip), %rdi     # 0x160e8
callq 0x4250
movl $0x2, %edi
callq 0x4360
callq 0x41c0
cmpq $0x0, -0xd0(%rbp)
jne 0x5d82
leaq 0x10636(%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 0x194ab(%rip), %r15     # 0x1efd8
leaq -0x50(%rbp), %r13
leaq -0x98(%rbp), %r12
movq (%r15), %rax
movq -0x18(%rax), %rdi
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 0x4f68
movq %r13, %rdi
leaq 0x10853(%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 0x5baf
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x42b0
movq (%r15), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x20(%r15,%rax)
je 0x5b38
movq -0x340(%rbp), %rdi
leaq -0x330(%rbp), %rax
cmpq %rax, %rdi
je 0x5be4
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 0x4f68
leaq -0x138(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x6110
leaq -0x138(%rbp), %rsi
movq %r15, %rdi
callq 0x52c8
leaq -0x128(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5c5a
movq -0x128(%rbp), %rsi
incq %rsi
callq 0x42b0
movq -0xf8(%rbp), %rdi
cmpq %r14, %rdi
je 0x5c75
movq -0xe8(%rbp), %rsi
incq %rsi
callq 0x42b0
cmpq $0x0, -0x110(%rbp)
je 0x5c94
movq -0x118(%rbp), %rsi
leaq 0x10499(%rip), %rdi     # 0x16126
xorl %eax, %eax
callq 0x4050
movslq (%rbx), %rax
cmpl -0x2c(%rbp), %eax
jge 0x5ecb
movq -0x58(%rbp), %rcx
movq (%rcx,%rax,8), %rsi
leaq -0x340(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x60d2
leaq -0xb8(%rbp), %rdi
leaq -0x340(%rbp), %rbx
movq %rbx, %rsi
callq 0x4370
movq (%rbx), %rdi
leaq -0x330(%rbp), %rax
cmpq %rax, %rdi
je 0x5cec
movq -0x330(%rbp), %rsi
incq %rsi
callq 0x42b0
leaq -0xb8(%rbp), %rbx
movq (%rbx), %rsi
leaq 0x10434(%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 0x5ea9
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 0x5d65
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x42b0
leaq -0x340(%rbp), %rdi
callq 0x43c0
leaq -0x340(%rbp), %rdi
callq 0x43f0
jmp 0x5ab4
leaq -0x340(%rbp), %r15
leaq -0xd8(%rbp), %rsi
movq %r15, %rdi
movl $0x8, %edx
callq 0x4440
leaq -0x2c8(%rbp), %rdi
callq 0x40a0
testb %al, %al
je 0x5ea9
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x68(%rbp), %r14
leaq -0x78(%rbp), %r13
leaq -0x98(%rbp), %r12
movq -0x340(%rbp), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %edx
movq %r15, %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 0x4f68
movq %r13, %rdi
leaq 0x105bd(%rip), %rsi     # 0x163d8
callq 0x4510
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq %r12, %rdi
callq 0x40e0
movq -0x78(%rbp), %rdi
cmpq %r14, %rdi
je 0x5e45
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x42b0
movq -0x340(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x2, -0x320(%rbp,%rax)
je 0x5dd3
leaq -0x340(%rbp), %rdi
callq 0x4090
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x5e83
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x42b0
movq -0xd8(%rbp), %rsi
leaq 0x1028a(%rip), %rdi     # 0x1611b
xorl %eax, %eax
callq 0x4050
leaq -0x340(%rbp), %rdi
callq 0x40b0
jmp 0x5be4
movq -0xb8(%rbp), %rdi
callq 0x4410
leaq 0x10240(%rip), %rdi     # 0x160fc
callq 0x4250
movl $0x3, %edi
callq 0x4360
leaq 0x102a4(%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 0x5ab4
movq -0x330(%rbp), %rsi
incq %rsi
callq 0x42b0
jmp 0x5ab4
jmp 0x5f37
movq %rax, %rbx
leaq -0x40(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5f3a
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x42b0
jmp 0x5f3a
movq %rax, %rbx
leaq -0x340(%rbp), %rdi
callq 0x43f0
jmp 0x6041
jmp 0x603e
jmp 0x603e
jmp 0x603e
movq %rax, %rbx
leaq -0x128(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5f83
movq -0x128(%rbp), %rsi
incq %rsi
callq 0x42b0
jmp 0x5f83
jmp 0x5fcb
movq %rax, %rbx
movq -0xf8(%rbp), %rdi
cmpq %r14, %rdi
je 0x5fc1
movq -0xe8(%rbp), %rsi
incq %rsi
callq 0x42b0
jmp 0x5fc1
jmp 0x5fa2
movq %rax, %rbx
movq -0x78(%rbp), %rdi
cmpq %r14, %rdi
je 0x5fce
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x42b0
jmp 0x5fce
jmp 0x5fcb
movq %rax, %rbx
movl $0x78, %esi
movq %r15, %rdi
jmp 0x6035
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x5fe7
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x42b0
leaq -0x340(%rbp), %rdi
callq 0x40b0
jmp 0x6041
jmp 0x603e
jmp 0x5ff9
movq %rax, %rbx
movq -0x50(%rbp), %rdi
cmpq %r14, %rdi
je 0x6018
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x42b0
jmp 0x6018
jmp 0x6015
movq %rax, %rbx
movq -0x340(%rbp), %rdi
leaq -0x330(%rbp), %rax
cmpq %rax, %rdi
je 0x6041
movq -0x330(%rbp), %rsi
incq %rsi
callq 0x42b0
jmp 0x6041
jmp 0x603e
movq %rax, %rbx
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x6063
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x42b0
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x6085
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x42b0
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x60a7
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x42b0
movq -0x118(%rbp), %rdi
leaq -0x108(%rbp), %rax
cmpq %rax, %rdi
je 0x60c9
movq -0x108(%rbp), %rsi
incq %rsi
callq 0x42b0
movq %rbx, %rdi
callq 0x44a0
nop