// File: /workspace/github_cpp_makefile_star2_O2/VanAndelInstitute[P]intent/src/seq_tools.cpp
// Function: extract_v2_barcodes(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, bool)
ParsedBarcode extract_v2_barcodes(std::string read, 
                                  std::string read_qual, 
                                  int max_distance, 
                                  bool fix) {
    ParsedBarcode barcode;
    std::vector<int> coords = getEnds(W1, read);
    int start = coords[1];
    int end = coords[2];
    int umiStart = end + 1 + 8;
    int bc2Start = end + 1;
    int bc1Start = std::max(0, start-11);
    bool ok = true;
    if(coords[0] > max_distance) { // poor W1 match
        ok = false;
        barcode.status = BC_ERROR_BAD_W1;
    } else if(umiStart + 6 >= read.length()) { // incomplete UMI
        barcode.status = BC_ERROR_SHORTREAD;
        ok = false; 
    } else if(start < 8) { // Barcode 2 too short
        barcode.status = BC_ERROR_SHORTREAD;
        ok = false;
    } 
    if(ok) {
        barcode.status = BC_OK;
        barcode.barcode1 = read.substr(bc1Start, std::min(start, 11));
        barcode.barcode1_quality = read_qual.substr(bc1Start, std::min(start, 11));
        barcode.barcode2 = read.substr(bc2Start, 8);
        barcode.barcode2_quality = read_qual.substr(bc2Start, 8);
        barcode.UMI = read.substr(umiStart, 6);
        barcode.UMI_quality = read_qual.substr(umiStart, 6);
        if(fix) {
          fix_barcodes(barcode);
        }
    }
    return(barcode);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %r8d, %ebp
movl %ecx, %r12d
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
callq 0x25f76
leaq 0x4352(%rip), %rsi      # 0x296c9
leaq 0x68(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x1463a
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x6580
leaq 0x30(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x25067
leaq 0x48(%rsp), %rdi
callq 0x6768
leaq 0x68(%rsp), %rdi
callq 0x6768
movq 0x30(%rsp), %rdx
movl 0x4(%rdx), %eax
cmpl $0xc, %eax
pushq $0xb
popq %r13
movl %r13d, %ecx
cmovgel %eax, %ecx
cmpl %r12d, (%rdx)
jle 0x253d9
pushq $0x2
jmp 0x253f7
movslq 0x8(%rdx), %r12
leaq 0xf(%r12), %rdx
cmpq %rdx, 0x8(%r15)
setbe %dl
cmpl $0x8, %eax
setl %sil
orb %dl, %sil
je 0x25419
pushq $0x3
popq %rax
movl %eax, (%rbx)
leaq 0x30(%rsp), %rdi
callq 0x22326
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, (%rsp)
movl %ebp, 0xc(%rsp)
addl $-0xb, %ecx
andl $0x0, (%rbx)
movl %ecx, %ebp
cmpl $0xb, %eax
cmovbl %eax, %r13d
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x6410
leaq 0x8(%rbx), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x64d0
movq %r14, %rdi
callq 0x6768
leaq 0x10(%rsp), %rdi
movq (%rsp), %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x6410
leaq 0x1(%r12), %r13
leaq 0x28(%rbx), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x64d0
movq %r14, %rdi
callq 0x6768
leaq 0x10(%rsp), %rdi
pushq $0x8
popq %rcx
movq %r15, %rsi
movq %r13, %rdx
callq 0x6410
leaq 0x48(%rbx), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x64d0
movq %r14, %rdi
callq 0x6768
leaq 0x10(%rsp), %rdi
pushq $0x8
popq %rcx
movq (%rsp), %rsi
movq %r13, %rdx
callq 0x6410
addq $0x9, %r12
leaq 0x68(%rbx), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x64d0
movq %r14, %rdi
callq 0x6768
leaq 0x10(%rsp), %rdi
pushq $0x6
popq %rcx
movq %r15, %rsi
movq %r12, %rdx
callq 0x6410
leaq 0x88(%rbx), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x64d0
movq %r14, %rdi
callq 0x6768
leaq 0x10(%rsp), %rdi
pushq $0x6
popq %rcx
movq (%rsp), %rsi
movq %r12, %rdx
callq 0x6410
leaq 0xa8(%rbx), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x64d0
movq %r14, %rdi
callq 0x6768
cmpb $0x0, 0xc(%rsp)
je 0x253fa
movq %rbx, %rdi
callq 0x255ae
jmp 0x253fa
jmp 0x2556e
jmp 0x2556e
jmp 0x2556e
jmp 0x2556e
jmp 0x2556e
jmp 0x2556e
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x22326
jmp 0x2559e
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x6768
jmp 0x2558f
movq %rax, %r14
leaq 0x68(%rsp), %rdi
callq 0x6768
jmp 0x2559e
movq %rax, %r14
movq %rbx, %rdi
callq 0x14aa0
movq %r14, %rdi
callq 0x66a0