// File: /workspace/github_cpp_makefile_star2_O2/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
leaq -0xc8(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rsi, -0x38(%rbp)
movl %edi, -0x2c(%rbp)
xorl %ecx, %ecx
leaq -0x68(%rbp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
leaq -0xa8(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq -0x88(%rbp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
leaq 0xdcae(%rip), %r15      # 0x130a7
leaq 0x18ef0(%rip), %r12     # 0x1e2f0
leaq -0x340(%rbp), %r13
leaq -0xd8(%rbp), %rbx
movq 0x18b7b(%rip), %r14     # 0x1df90
andl $0x0, -0x340(%rbp)
movl -0x2c(%rbp), %edi
movq -0x38(%rbp), %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r13, %r8
callq 0x44b0
cmpl $0x69, %eax
je 0x5445
movq %rbx, %rdi
cmpl $0x73, %eax
je 0x5449
cmpl $0x76, %eax
je 0x5415
jmp 0x5453
leaq -0x78(%rbp), %rdi
movq (%r14), %rsi
callq 0x4550
jmp 0x5415
cmpl $-0x1, %eax
je 0x5472
cmpl $0x68, %eax
jne 0x54a2
leaq 0xdc4a(%rip), %rdi      # 0x130ae
xorl %eax, %eax
callq 0x4050
xorl %edi, %edi
callq 0x4390
leaq 0xdc5b(%rip), %rdi      # 0x130d4
callq 0x4450
movq 0x18b33(%rip), %rbx     # 0x1dfb8
movl (%rbx), %eax
incl %eax
cmpl -0x2c(%rbp), %eax
jge 0x54a7
leaq 0xdc53(%rip), %rdi      # 0x130e8
callq 0x42a0
pushq $0x2
popq %rdi
callq 0x4390
callq 0x4210
cmpq $0x0, -0x70(%rbp)
jne 0x566a
leaq 0xdc84(%rip), %rdi      # 0x1313d
callq 0x4450
leaq -0x330(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x18b08(%rip), %r15     # 0x1dfe0
leaq -0x340(%rbp), %r12
leaq 0xdef2(%rip), %r14      # 0x133d8
leaq -0x58(%rbp), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x4090
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x4bce
leaq -0x98(%rbp), %rdi
movq %r13, %rsi
callq 0x4570
movq %r13, %rdi
callq 0x41a0
movq (%r15), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x20(%r15,%rax)
je 0x54ea
leaq -0x340(%rbp), %rdi
callq 0x41a0
pushq $0x78
popq %rdi
callq 0x42f0
movq %rax, %r15
leaq -0xf8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x43e0
leaq -0x118(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x5880
leaq -0x118(%rbp), %rsi
movq %r15, %rdi
callq 0x4f40
leaq -0x118(%rbp), %rdi
callq 0x41a0
leaq -0xf8(%rbp), %rdi
callq 0x41a0
cmpq $0x0, -0xd0(%rbp)
je 0x55ac
movq -0xd8(%rbp), %rsi
leaq 0xdb81(%rip), %rdi      # 0x13126
xorl %eax, %eax
callq 0x4050
movslq (%rbx), %rax
cmpl -0x2c(%rbp), %eax
jge 0x5756
movq -0x38(%rbp), %rcx
movq (%rcx,%rax,8), %rsi
leaq -0x340(%rbp), %rdi
leaq -0x58(%rbp), %rdx
callq 0x4af4
leaq -0xb8(%rbp), %rbx
leaq -0x340(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x43a0
movq %r14, %rdi
callq 0x41a0
movq (%rbx), %rsi
leaq 0xdb36(%rip), %rdi      # 0x13131
xorl %eax, %eax
callq 0x4050
leaq -0x340(%rbp), %rdi
pushq $0x10
popq %rdx
movq %rbx, %rsi
callq 0x4560
leaq -0x2d0(%rbp), %rdi
callq 0x40c0
testb %al, %al
je 0x5737
leaq -0x58(%rbp), %rdi
movq %r15, %rsi
callq 0x48b6
leaq -0x340(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x42d0
leaq -0x58(%rbp), %rdi
callq 0x41a0
leaq -0x340(%rbp), %rdi
callq 0x4410
leaq -0x340(%rbp), %rdi
callq 0x4430
jmp 0x546b
leaq -0x340(%rbp), %rdi
leaq -0x78(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0x4480
leaq -0x2c8(%rbp), %rdi
callq 0x40c0
testb %al, %al
je 0x5737
leaq -0x48(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x340(%rbp), %r14
leaq -0x58(%rbp), %r12
leaq -0x138(%rbp), %r15
leaq -0x98(%rbp), %r13
movq %r14, %rdi
movq %r12, %rsi
callq 0x4090
movq %r15, %rdi
movq %r12, %rsi
leaq 0xdd06(%rip), %rdx      # 0x133d8
callq 0x4bce
movq %r13, %rdi
movq %r15, %rsi
callq 0x4570
movq %r15, %rdi
callq 0x41a0
movq -0x340(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x2, -0x320(%rbp,%rax)
je 0x56ba
leaq -0x340(%rbp), %rdi
callq 0x40a0
leaq -0x58(%rbp), %rdi
callq 0x41a0
movq -0x78(%rbp), %rsi
leaq 0xd9fc(%rip), %rdi      # 0x1311b
xorl %eax, %eax
callq 0x4050
leaq -0x340(%rbp), %rdi
callq 0x40e0
jmp 0x5535
movq -0xb8(%rbp), %rdi
callq 0x4450
leaq 0xd9b2(%rip), %rdi      # 0x130fc
callq 0x42a0
pushq $0x3
jmp 0x549c
leaq 0xda19(%rip), %rdi      # 0x13176
callq 0x4450
leaq -0x340(%rbp), %rdi
movq %r15, %rsi
callq 0x48b6
leaq -0x340(%rbp), %rbx
movq (%rbx), %rdi
callq 0x4450
movq %rbx, %rdi
callq 0x41a0
jmp 0x546b
jmp 0x579d
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x41a0
jmp 0x57a0
movq %rax, %rbx
leaq -0x340(%rbp), %rdi
callq 0x4430
jmp 0x5849
jmp 0x5846
jmp 0x5846
jmp 0x5846
movq %rax, %rbx
leaq -0x118(%rbp), %rdi
callq 0x41a0
jmp 0x57d6
jmp 0x57f7
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x41a0
jmp 0x5814
movq %rax, %rbx
leaq -0x138(%rbp), %rdi
callq 0x41a0
jmp 0x57fa
jmp 0x57f7
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x41a0
leaq -0x340(%rbp), %rdi
callq 0x40e0
jmp 0x5849
movq %rax, %rbx
pushq $0x78
popq %rsi
movq %r15, %rdi
callq 0x4300
jmp 0x5849
jmp 0x5846
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x41a0
jmp 0x5836
jmp 0x5833
movq %rax, %rbx
leaq -0x340(%rbp), %rdi
callq 0x41a0
jmp 0x5849
jmp 0x5846
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x41a0
leaq -0xb8(%rbp), %rdi
callq 0x41a0
leaq -0x78(%rbp), %rdi
callq 0x41a0
leaq -0xd8(%rbp), %rdi
callq 0x41a0
movq %rbx, %rdi
callq 0x44f0
nop