// File: /workspace/github_cpp_makefile_star2_O2/margual56[P]Markdown-CLI/src/markdown.cpp
// Function: markdown(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
std::string markdown(std::string markdown){
    std::smatch match;  // Match object (stores match information)

    ////////////////////////////// HEADERS //////////////////////////////////////
    std::regex headers("#+[^\\r\\n]+(\\r|\\n|\\r\\n)"); // Regex for the headers

    while (std::regex_search (markdown, match, headers)) {

        int match_begin = match.position();
        int match_end   = match_begin + match.length();

        printf("Processing match: %s", markdown.substr(match_begin, match.length()).c_str());

        int j;
        for(j = 0; markdown[j+match_begin]=='#'; j++){}     // So count the number of '#' in a row,


        markdown.insert(match_end, "</h" + std::to_string(j) + "><hr/>");
        markdown.replace(match_begin, j, "<h" + std::to_string(j) + ">");
    }
    
    //////////////////////////////// LINKS //////////////////////////////////////
    std::regex links("\\[[^\\n\\r]*\\]\\([^\\n\\r]*\\)"); // Regex for the headers
    std::regex linkHref("\\([^\\n\\r]*\\)");

    std::smatch match2;  // Match object (stores match information)
    while (std::regex_search (markdown, match, links)) {

        int match_begin = match.position();
        int match_end   = match_begin + match.length();
        
        std::string text = "";
        std::string link = "";

        int i, count;
        for(i = match_begin+1, count = 1; count > 0; i++){
            char c = markdown[i];

            if(c == '[') count++;
            else if(c == ']') count--;

            if(count > 0) text += c;
        }

        for(i += 1, count = 1; count > 0; i++){
            char c = markdown[i];

            if(c == '(') count++;
            else if(c == ')') count--;

            if(count > 0) link += c;
        }

        std::string finalLink = "<a href=\"" + link + "\">" + text + "</a>";

        printf("Processing link: %s", finalLink.c_str());
        markdown.replace(match_begin, match.length(), finalLink);
    }

    ////////////////////////// OTHER TOKENS ////////////////////////////////////
    //token{"[^\\r\\n]+((\\r|\\n|\\r\\n)[^\\r\\n]+)*",0,2,  "p"}       // Paragraphs regex

    std::list<token> tokens = {
        token{"`[^`\\n]+`",                             1,1,  "code"},   // Code tag regex
        token{"(\\*){2}[^(\\*){2}\\r\\n]+(\\*){2}",     2,2,  "b"},      // Bold tag regex    (**)
        token{"(_){2}[^(_){2}\\r\\n]+(_){2}",           2,2,  "b"},      // Bold tag regex    (__)
        token{"\\*[^\\*\\r\\n]+\\*",                    1,1,  "i"},      // Italic tag regex  (*)
        token{"_[^\\*\\r\\n]+_",                        1,1,  "i"}      // Italic tag regex  (_)
    };

    for(token tok: tokens){
        printf("Checking for %s\n", tok.tag.c_str());
        std::regex tmp(tok.rex);
        
        int match_end, match_begin;
        while (std::regex_search (markdown, match, tmp)) {
            match_begin = match.position();
            match_end   = match_begin + match.length();

            printf("Processing match: %s\n", markdown.substr(match_begin, match.length()).c_str());

            markdown.replace(match_end-tok.length2, tok.length2, "</" + tok.tag + ">");
            markdown.replace(match_begin, tok.length1, "<" + tok.tag + ">");
        }

        printf("Done checking %s\n", tok.tag.c_str());
    }

    ////////////////////////////// LINE BREAKS ///////////////////////////////////
    for(int i = 0; i<markdown.length(); i++){
        if(markdown[i] == '\n'){
            markdown.insert(i, "<br/>");
            i+=6;
        }
    }

    return markdown;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2d8, %rsp            # imm = 0x2D8
movq %rsi, %rbx
movq %rdi, -0xa8(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movaps %xmm0, -0x60(%rbp)
leaq 0xd9c8(%rip), %rsi      # 0x13278
leaq -0x190(%rbp), %rdi
pushq $0x10
popq %rdx
callq 0x6208
leaq -0x60(%rbp), %r14
movq %rbx, -0x30(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
leaq -0x190(%rbp), %rdx
xorl %ecx, %ecx
callq 0x6272
testb %al, %al
je 0x5a52
movq %r14, %rdi
xorl %esi, %esi
callq 0x6292
movq %r14, %rbx
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
callq 0x62ae
movq %rax, %r15
movq %rbx, %r12
movq %rbx, %rdi
xorl %esi, %esi
callq 0x62ae
movq -0x30(%rbp), %r13
leaq -0x2f8(%rbp), %rbx
movslq %r14d, %rdx
movq %rbx, %rdi
movq %r13, %rsi
movq %rdx, -0x40(%rbp)
movq %rax, %rcx
callq 0x4330
addq %r14, %r15
movq -0x2f8(%rbp), %rsi
leaq 0xd951(%rip), %rdi      # 0x1328f
xorl %eax, %eax
callq 0x4050
movq %rbx, %rdi
callq 0x41a0
movslq %r14d, %rax
addq (%r13), %rax
pushq $-0x1
popq %r13
cmpb $0x23, 0x1(%rax,%r13)
leaq 0x1(%r13), %r13
je 0x5958
leaq -0x110(%rbp), %rbx
movq %rbx, %rdi
movl %r13d, %esi
callq 0x62fb
movq %r12, %r14
leaq -0xa0(%rbp), %r12
movq %r12, %rdi
leaq 0xd91a(%rip), %rsi      # 0x132a4
movq %rbx, %rdx
callq 0x62cd
leaq -0x2f8(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
leaq 0xd902(%rip), %rdx      # 0x132a8
callq 0x4b2f
movslq %r15d, %rsi
movq -0x30(%rbp), %rdi
movq %rbx, %rdx
callq 0x4470
movq %rbx, %rdi
callq 0x41a0
leaq -0xa0(%rbp), %r15
movq %r15, %rdi
callq 0x41a0
leaq -0x110(%rbp), %rbx
movq %rbx, %rdi
callq 0x41a0
movq %rbx, %rdi
movl %r13d, %esi
callq 0x62fb
movq %r15, %rdi
leaq 0xd8ba(%rip), %rsi      # 0x132af
movq %rbx, %rdx
callq 0x62cd
leaq -0x2f8(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
leaq 0xd658(%rip), %rdx      # 0x13069
callq 0x4b2f
movl %r13d, %edx
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq %rbx, %rcx
callq 0x4130
movq %rbx, %rdi
callq 0x41a0
leaq -0xa0(%rbp), %rdi
callq 0x41a0
leaq -0x110(%rbp), %rdi
callq 0x41a0
movq -0x30(%rbp), %rbx
jmp 0x58c7
leaq 0xd859(%rip), %rsi      # 0x132b2
leaq -0xa0(%rbp), %rdi
pushq $0x10
popq %rdx
callq 0x6208
leaq 0xd84f(%rip), %rsi      # 0x132be
leaq -0x110(%rbp), %rdi
pushq $0x10
popq %rdx
callq 0x6208
xorps %xmm0, %xmm0
movaps %xmm0, -0x140(%rbp)
movaps %xmm0, -0x150(%rbp)
leaq -0x60(%rbp), %r15
leaq -0x2f8(%rbp), %rbx
leaq -0x130(%rbp), %r12
movq -0x30(%rbp), %rdi
movq %r15, %rsi
leaq -0xa0(%rbp), %rdx
xorl %ecx, %ecx
callq 0x6272
testb %al, %al
je 0x5c58
movq %r15, %rdi
xorl %esi, %esi
callq 0x6292
movq %rax, %r14
movq %r15, %rdi
xorl %esi, %esi
callq 0x62ae
movq %rbx, %rdi
leaq 0xd8fa(%rip), %r15      # 0x133d9
movq %r15, %rsi
movq %r12, %rdx
callq 0x4af4
movq %r12, %rdi
movq %r15, %rsi
leaq -0x80(%rbp), %rdx
callq 0x4af4
movslq %r14d, %rax
movslq %r14d, %r14
movq %rax, -0x40(%rbp)
leal 0x2(%rax), %r13d
pushq $0x1
popq %r15
testl %r15d, %r15d
jle 0x5b49
movq -0x30(%rbp), %rax
movq (%rax), %rax
movzbl 0x1(%rax,%r14), %eax
cmpl $0x5d, %eax
je 0x5b2c
cmpl $0x5b, %eax
jne 0x5b31
incl %r15d
jmp 0x5b31
decl %r15d
je 0x5b44
movsbl %al, %esi
movq %rbx, %rdi
callq 0x42b0
incl %r13d
incq %r14
jmp 0x5b0b
xorl %r15d, %r15d
jmp 0x5b3c
movslq %r13d, %r14
pushq $0x1
popq %r15
testl %r15d, %r15d
jle 0x5b8a
movq -0x30(%rbp), %rax
movq (%rax), %rax
movzbl (%rax,%r14), %eax
cmpl $0x29, %eax
je 0x5b70
cmpl $0x28, %eax
jne 0x5b75
incl %r15d
jmp 0x5b75
decl %r15d
je 0x5b85
movsbl %al, %esi
movq %r12, %rdi
callq 0x42b0
incq %r14
jmp 0x5b50
xorl %r15d, %r15d
jmp 0x5b80
leaq -0x170(%rbp), %r14
movq %r14, %rdi
leaq 0xd730(%rip), %rsi      # 0x132cb
movq %r12, %rdx
callq 0x4b58
leaq -0xd0(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
leaq 0xd4b1(%rip), %rdx      # 0x13068
callq 0x4b2f
leaq -0x60(%rbp), %r15
leaq -0xf0(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x635b
leaq -0x80(%rbp), %rdi
movq %r14, %rsi
leaq 0xd6f2(%rip), %rdx      # 0x132d5
callq 0x4b2f
movq %r14, %rdi
callq 0x41a0
leaq -0xd0(%rbp), %rdi
callq 0x41a0
leaq -0x170(%rbp), %rdi
callq 0x41a0
movq -0x80(%rbp), %rsi
leaq 0xd6c7(%rip), %rdi      # 0x132da
xorl %eax, %eax
callq 0x4050
movq %r15, %rdi
xorl %esi, %esi
callq 0x62ae
leaq -0x80(%rbp), %r14
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq %rax, %rdx
movq %r14, %rcx
callq 0x4130
movq %r14, %rdi
callq 0x41a0
movq %r12, %rdi
callq 0x41a0
movq %rbx, %rdi
callq 0x41a0
jmp 0x5aa1
leaq 0xd68f(%rip), %rsi      # 0x132ee
leaq -0x2f8(%rbp), %r15
leaq -0x130(%rbp), %rdx
movq %r15, %rdi
callq 0x4af4
movabsq $0x100000001, %rbx      # imm = 0x100000001
leaq -0x2d0(%rbp), %rdi
movq %rbx, -0x8(%rdi)
leaq 0xd667(%rip), %rsi      # 0x132f8
leaq -0x80(%rbp), %rdx
callq 0x4af4
leaq -0x2b0(%rbp), %r12
leaq 0xd655(%rip), %rsi      # 0x132fd
leaq -0xf0(%rbp), %rdx
movq %r12, %rdi
callq 0x4af4
movabsq $0x200000002, %r14      # imm = 0x200000002
leaq -0x288(%rbp), %rdi
movq %r14, -0x8(%rdi)
leaq 0xdc23(%rip), %rsi      # 0x138f6
leaq -0x170(%rbp), %rdx
callq 0x4af4
leaq -0x268(%rbp), %r12
leaq 0xd62e(%rip), %rsi      # 0x1331b
leaq -0x37(%rbp), %rdx
movq %r12, %rdi
callq 0x4af4
leaq -0x240(%rbp), %rdi
movq %r14, -0x8(%rdi)
leaq 0xdbeb(%rip), %rsi      # 0x138f6
leaq -0x36(%rbp), %rdx
callq 0x4af4
leaq -0x220(%rbp), %r12
leaq 0xd614(%rip), %rsi      # 0x13336
leaq -0x35(%rbp), %rdx
movq %r12, %rdi
callq 0x4af4
leaq -0x1f8(%rbp), %rdi
movq %rbx, -0x8(%rdi)
leaq 0xd605(%rip), %rsi      # 0x13345
leaq -0x34(%rbp), %rdx
callq 0x4af4
leaq -0x1d8(%rbp), %r12
leaq 0xd5f0(%rip), %rsi      # 0x13347
leaq -0x33(%rbp), %rdx
movq %r12, %rdi
callq 0x4af4
leaq -0x1b0(%rbp), %rdi
movq %rbx, -0x8(%rdi)
leaq 0xd5d0(%rip), %rsi      # 0x13345
leaq -0x32(%rbp), %rdx
callq 0x4af4
leaq -0xd0(%rbp), %rdi
leaq -0x2f8(%rbp), %rsi
pushq $0x5
popq %rdx
leaq -0x31(%rbp), %rcx
callq 0x6384
movl $0x120, %ebx            # imm = 0x120
leaq (%rbx,%rbp), %rdi
addq $-0x2f8, %rdi           # imm = 0xFD08
callq 0x63be
addq $-0x48, %rbx
cmpq $-0x48, %rbx
jne 0x5d9d
leaq -0x80(%rbp), %r12
leaq -0xd0(%rbp), %rcx
movq -0x30(%rbp), %rbx
movq (%rcx), %rcx
leaq -0xd0(%rbp), %rax
cmpq %rax, %rcx
je 0x5f91
movq %rcx, -0xb0(%rbp)
leaq 0x10(%rcx), %rsi
leaq -0x2f8(%rbp), %r14
movq %r14, %rdi
callq 0x63de
movq -0x2d0(%rbp), %rsi
leaq 0xd553(%rip), %rdi      # 0x13354
xorl %eax, %eax
callq 0x4050
leaq -0x130(%rbp), %rdi
movq %r14, %rsi
pushq $0x10
popq %rdx
callq 0x6420
leaq -0x60(%rbp), %rsi
movq %rbx, %rdi
leaq -0x130(%rbp), %rdx
xorl %ecx, %ecx
callq 0x6272
testb %al, %al
je 0x5f58
leaq -0x60(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x6292
movq %rax, %r14
movq %r15, %rdi
xorl %esi, %esi
callq 0x62ae
movq %rax, %r13
movq %r15, %rdi
xorl %esi, %esi
callq 0x62ae
movslq %r14d, %rdx
movq %r12, %rdi
movq %rbx, %rsi
movq %rdx, -0x40(%rbp)
movq %rax, %rcx
callq 0x4330
movq -0x80(%rbp), %rsi
leaq 0xd4e6(%rip), %rdi      # 0x13365
xorl %eax, %eax
callq 0x4050
movq %r12, %rdi
callq 0x41a0
movslq -0x2d4(%rbp), %rbx
movq %r12, %r15
leaq -0xf0(%rbp), %r12
movq %r12, %rdi
leaq 0xd160(%rip), %rsi      # 0x13009
leaq -0x2d0(%rbp), %rdx
callq 0x4b58
movq %r15, %rdi
movq %r12, %rsi
leaq 0xd1a7(%rip), %rdx      # 0x13069
callq 0x4b2f
addl %r14d, %r13d
movslq %r13d, %rsi
subq %rbx, %rsi
movq -0x30(%rbp), %rdi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x4130
movq %r15, %r12
movq %r15, %rdi
callq 0x41a0
leaq -0xf0(%rbp), %r14
movq %r14, %rdi
callq 0x41a0
movslq -0x2d8(%rbp), %rbx
movq %r14, %rdi
leaq 0xd0fa(%rip), %rsi      # 0x13004
leaq -0x2d0(%rbp), %rdx
callq 0x4b58
movq %r12, %rdi
movq %r14, %rsi
leaq 0xd146(%rip), %rdx      # 0x13069
callq 0x4b2f
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x4130
movq %r12, %rdi
callq 0x41a0
leaq -0xf0(%rbp), %rdi
callq 0x41a0
movq -0x30(%rbp), %rbx
jmp 0x5e1a
movq -0x2d0(%rbp), %rsi
leaq 0xd415(%rip), %rdi      # 0x1337b
xorl %eax, %eax
callq 0x4050
leaq -0x130(%rbp), %rdi
callq 0x6488
leaq -0x2f8(%rbp), %rdi
callq 0x63be
movq -0xb0(%rbp), %rcx
jmp 0x5dc6
xorl %r14d, %r14d
leaq 0xd3f2(%rip), %rbx      # 0x1338d
movslq %r14d, %rsi
movq -0x30(%rbp), %rcx
cmpq %rsi, 0x8(%rcx)
jbe 0x5fc5
movq (%rcx), %rax
cmpb $0xa, (%rax,%rsi)
jne 0x5fc0
movq %rcx, %rdi
movq %rbx, %rdx
callq 0x40d0
addl $0x6, %r14d
incl %r14d
jmp 0x5f9b
movq -0xa8(%rbp), %rbx
movq %rbx, %rdi
movq %rcx, %rsi
callq 0x43d0
leaq -0xd0(%rbp), %rdi
callq 0x6600
leaq -0x150(%rbp), %rdi
callq 0x6640
leaq -0x110(%rbp), %rdi
callq 0x6488
leaq -0xa0(%rbp), %rdi
callq 0x6488
leaq -0x190(%rbp), %rdi
callq 0x6488
leaq -0x60(%rbp), %rdi
callq 0x6640
movq %rbx, %rax
addq $0x2d8, %rsp            # imm = 0x2D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x120, %r14d           # imm = 0x120
leaq (%r14,%rbp), %rdi
addq $-0x2f8, %rdi           # imm = 0xFD08
callq 0x63be
addq $-0x48, %r14
cmpq $-0x48, %r14
jne 0x603a
jmp 0x61c7
jmp 0x6065
jmp 0x6072
jmp 0x6065
jmp 0x6072
jmp 0x6065
jmp 0x6072
movq %rax, %rbx
movq %r12, %rdi
callq 0x41a0
jmp 0x6075
movq %rax, %rbx
addq $-0x48, %r12
movq %r12, %rdi
callq 0x63be
cmpq %r15, %r12
jne 0x6075
jmp 0x61c7
jmp 0x60ed
jmp 0x6138
movq %rax, %rbx
jmp 0x61df
jmp 0x6150
movq %rax, %rbx
jmp 0x61f7
jmp 0x60a9
movq %rax, %rbx
jmp 0x61bb
movq %rax, %rbx
jmp 0x61af
movq %rax, %rbx
jmp 0x60d2
movq %rax, %rbx
jmp 0x60de
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0x41a0
leaq -0xd0(%rbp), %rdi
callq 0x41a0
leaq -0x170(%rbp), %rdi
jmp 0x6149
jmp 0x615a
jmp 0x6138
jmp 0x6138
movq %rax, %rbx
jmp 0x6169
jmp 0x6138
jmp 0x60fa
jmp 0x610a
jmp 0x6101
movq %rax, %rbx
jmp 0x6125
jmp 0x6150
movq %rax, %rbx
jmp 0x6119
jmp 0x6150
jmp 0x6150
movq %rax, %rbx
leaq -0x2f8(%rbp), %rdi
callq 0x41a0
leaq -0xa0(%rbp), %rdi
callq 0x41a0
leaq -0x110(%rbp), %rdi
callq 0x41a0
jmp 0x61eb
jmp 0x6150
movq %rax, %rbx
jmp 0x61c7
jmp 0x6150
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x41a0
jmp 0x615d
movq %rax, %rbx
jmp 0x61eb
jmp 0x615a
movq %rax, %rbx
leaq -0x130(%rbp), %rdi
callq 0x41a0
leaq -0x2f8(%rbp), %rdi
callq 0x41a0
jmp 0x61c7
jmp 0x6179
movq %rax, %rbx
jmp 0x618e
jmp 0x61a0
jmp 0x6182
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x41a0
leaq -0xf0(%rbp), %rdi
callq 0x41a0
jmp 0x61a3
jmp 0x61a0
jmp 0x61a0
movq %rax, %rbx
leaq -0x130(%rbp), %rdi
callq 0x6488
leaq -0x2f8(%rbp), %rdi
callq 0x63be
leaq -0xd0(%rbp), %rdi
callq 0x6600
leaq -0x150(%rbp), %rdi
callq 0x6640
leaq -0x110(%rbp), %rdi
callq 0x6488
leaq -0xa0(%rbp), %rdi
callq 0x6488
leaq -0x190(%rbp), %rdi
callq 0x6488
leaq -0x60(%rbp), %rdi
callq 0x6640
movq %rbx, %rdi
callq 0x44f0