// File: /workspace/github_cpp_makefile_star2_O1/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, %r14d
movl %ecx, %r12d
movq %rdx, 0x20(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
callq 0x2f788
leaq 0x78(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x46f5(%rip), %rsi      # 0x326c9
leaq 0x4704(%rip), %rdx      # 0x326df
leaq 0x68(%rsp), %rdi
callq 0x1be64
leaq 0x58(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %rdi
callq 0x1c224
leaq 0x30(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x2dae7
movq 0x48(%rsp), %rdi
cmpq %rbp, %rdi
je 0x2e02d
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x63c0
movq 0x68(%rsp), %rdi
cmpq %r13, %rdi
je 0x2e044
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x63c0
movq 0x30(%rsp), %rdx
movl 0x4(%rdx), %eax
cmpl $0xc, %eax
movl $0xb, %ecx
cmovgel %eax, %ecx
movl $0x2, %esi
cmpl %r12d, (%rdx)
jg 0x2e082
movslq 0x8(%rdx), %r12
leaq 0xf(%r12), %rdx
cmpq %rdx, 0x8(%r15)
setbe %dl
cmpl $0x8, %eax
setl %sil
orb %dl, %sil
movl $0x3, %esi
je 0x2e0b0
movl %esi, (%rbx)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x2e09b
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x63c0
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r14d, 0x2c(%rsp)
addl $-0xb, %ecx
movl $0x0, (%rbx)
movl %ecx, %r13d
cmpl $0xb, %eax
movl $0xb, %ebp
cmovll %eax, %ebp
movq %rsp, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x63f0
leaq 0x8(%rbx), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0x64d0
movq (%r14), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2e106
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x63c0
movq %rsp, %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x63f0
leaq 0x1(%r12), %r13
leaq 0x28(%rbx), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0x64d0
movq (%r14), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2e147
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x63c0
movq %rsp, %rdi
movl $0x8, %ecx
movq %r15, %rsi
movq %r13, %rdx
callq 0x63f0
leaq 0x48(%rbx), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0x64d0
movq (%r14), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2e183
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x63c0
movq %rsp, %rdi
movl $0x8, %ecx
movq 0x20(%rsp), %rsi
movq %r13, %rdx
callq 0x63f0
addq $0x9, %r12
leaq 0x68(%rbx), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0x64d0
movq (%r14), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2e1c5
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x63c0
movq %rsp, %rdi
movl $0x6, %ecx
movq %r15, %rsi
movq %r12, %rdx
callq 0x63f0
leaq 0x88(%rbx), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0x64d0
movq (%r14), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2e204
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x63c0
movq %rsp, %rdi
movl $0x6, %ecx
movq 0x20(%rsp), %rsi
movq %r12, %rdx
callq 0x63f0
leaq 0xa8(%rbx), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0x64d0
movq (%r14), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2e245
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x63c0
cmpb $0x0, 0x2c(%rsp)
je 0x2e084
movq %rbx, %rdi
callq 0x2e2cb
jmp 0x2e084
jmp 0x2e269
jmp 0x2e269
jmp 0x2e269
jmp 0x2e269
jmp 0x2e269
jmp 0x2e269
movq %rax, %r14
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x2e2bb
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
jmp 0x2e2b1
movq %rax, %r14
movq 0x48(%rsp), %rdi
cmpq %rbp, %rdi
je 0x2e29f
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x63c0
jmp 0x2e29f
movq %rax, %r14
movq 0x68(%rsp), %rdi
cmpq %r13, %rdi
je 0x2e2bb
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x63c0
jmp 0x2e2bb
movq %rax, %r14
movq %rbx, %rdi
callq 0x1a776
movq %r14, %rdi
callq 0x6660