// File: 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
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdx, -0x1a8(%rbp)
movq %rsi, -0x1a0(%rbp)
movq %rdi, -0x198(%rbp)
movb %r8b, %al
movq %rdi, %r8
movq %r8, -0x190(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
andb $0x1, %al
movb %al, -0x1d(%rbp)
movb $0x0, -0x1e(%rbp)
callq 0x6c460
leaq -0x59(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x8980
movq -0x188(%rbp), %rdx
leaq 0x5c85(%rip), %rsi      # 0x7076d
leaq -0x58(%rbp), %rdi
callq 0x35e90
jmp 0x6aaf3
movq -0x1a0(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x8280
jmp 0x6ab08
leaq -0x38(%rbp), %rdi
leaq -0x58(%rbp), %rsi
leaq -0x90(%rbp), %rdx
callq 0x6a520
jmp 0x6ab1e
leaq -0x90(%rbp), %rdi
callq 0x8a98
leaq -0x58(%rbp), %rdi
callq 0x8a98
leaq -0x59(%rbp), %rdi
callq 0x85e0
leaq -0x38(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
movl $0x1, %esi
callq 0x69140
movq -0x1b8(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x94(%rbp)
movl $0x2, %esi
callq 0x69140
movl (%rax), %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
addl $0x9, %eax
movl %eax, -0x9c(%rbp)
movl -0x98(%rbp), %eax
incl %eax
movl %eax, -0xa0(%rbp)
movl $0x0, -0xa8(%rbp)
movl -0x94(%rbp), %eax
addl $-0xb, %eax
movl %eax, -0xac(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xac(%rbp), %rsi
callq 0x68cd0
movq %rax, -0x1b0(%rbp)
jmp 0x6abc4
movq -0x1b0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
movb $0x1, -0xad(%rbp)
leaq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x69140
movl (%rax), %eax
cmpl -0x1c(%rbp), %eax
jle 0x6ac6c
movq -0x198(%rbp), %rax
movb $0x0, -0xad(%rbp)
movl $0x2, (%rax)
jmp 0x6acd0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x6ac44
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x6ac3b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x8a98
leaq -0x58(%rbp), %rdi
callq 0x8a98
leaq -0x59(%rbp), %rdi
callq 0x85e0
jmp 0x6af64
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x36b20
jmp 0x6af64
movq -0x1a0(%rbp), %rdi
movl -0x9c(%rbp), %eax
addl $0x6, %eax
cltq
movq %rax, -0x1c0(%rbp)
callq 0x8850
movq %rax, %rcx
movq -0x1c0(%rbp), %rax
cmpq %rcx, %rax
jb 0x6acaf
movq -0x198(%rbp), %rax
movl $0x3, (%rax)
movb $0x0, -0xad(%rbp)
jmp 0x6acce
cmpl $0x8, -0x94(%rbp)
jge 0x6accc
movq -0x198(%rbp), %rax
movl $0x3, (%rax)
movb $0x0, -0xad(%rbp)
jmp 0x6acce
jmp 0x6acd0
testb $0x1, -0xad(%rbp)
je 0x6af35
movq -0x198(%rbp), %rax
movl $0x0, (%rax)
movslq -0xa4(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movl $0xb, -0xd4(%rbp)
leaq -0x94(%rbp), %rdi
leaq -0xd4(%rbp), %rsi
callq 0x6c6c0
movq %rax, -0x1c8(%rbp)
jmp 0x6ad1e
movq -0x1d0(%rbp), %rdx
movq -0x1a0(%rbp), %rsi
movq -0x1c8(%rbp), %rax
movslq (%rax), %rcx
leaq -0xd0(%rbp), %rdi
callq 0x8590
jmp 0x6ad44
movq -0x198(%rbp), %rdi
addq $0x8, %rdi
leaq -0xd0(%rbp), %rsi
movq %rsi, -0x1e8(%rbp)
callq 0x86c0
movq -0x1e8(%rbp), %rdi
callq 0x8a98
movslq -0xa4(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movl $0xb, -0xfc(%rbp)
leaq -0x94(%rbp), %rdi
leaq -0xfc(%rbp), %rsi
callq 0x6c6c0
movq %rax, -0x1d8(%rbp)
jmp 0x6ada2
movq -0x1e0(%rbp), %rdx
movq -0x1a8(%rbp), %rsi
movq -0x1d8(%rbp), %rax
movslq (%rax), %rcx
leaq -0xf8(%rbp), %rdi
callq 0x8590
jmp 0x6adc8
movq -0x198(%rbp), %rdi
addq $0x28, %rdi
leaq -0xf8(%rbp), %rsi
movq %rsi, -0x1f0(%rbp)
callq 0x86c0
movq -0x1f0(%rbp), %rdi
callq 0x8a98
movq -0x1a0(%rbp), %rsi
movslq -0xa0(%rbp), %rdx
leaq -0x120(%rbp), %rdi
movl $0x8, %ecx
callq 0x8590
jmp 0x6ae13
movq -0x198(%rbp), %rdi
addq $0x48, %rdi
leaq -0x120(%rbp), %rsi
movq %rsi, -0x1f8(%rbp)
callq 0x86c0
movq -0x1f8(%rbp), %rdi
callq 0x8a98
movq -0x1a8(%rbp), %rsi
movslq -0xa0(%rbp), %rdx
leaq -0x140(%rbp), %rdi
movl $0x8, %ecx
callq 0x8590
jmp 0x6ae5e
movq -0x198(%rbp), %rdi
addq $0x68, %rdi
leaq -0x140(%rbp), %rsi
movq %rsi, -0x200(%rbp)
callq 0x86c0
movq -0x200(%rbp), %rdi
callq 0x8a98
movq -0x1a0(%rbp), %rsi
movslq -0x9c(%rbp), %rdx
leaq -0x160(%rbp), %rdi
movl $0x6, %ecx
callq 0x8590
jmp 0x6aea9
movq -0x198(%rbp), %rdi
addq $0x88, %rdi
leaq -0x160(%rbp), %rsi
movq %rsi, -0x208(%rbp)
callq 0x86c0
movq -0x208(%rbp), %rdi
callq 0x8a98
movq -0x1a8(%rbp), %rsi
movslq -0x9c(%rbp), %rdx
leaq -0x180(%rbp), %rdi
movl $0x6, %ecx
callq 0x8590
jmp 0x6aef7
movq -0x198(%rbp), %rdi
addq $0xa8, %rdi
leaq -0x180(%rbp), %rsi
callq 0x86c0
leaq -0x180(%rbp), %rdi
callq 0x8a98
testb $0x1, -0x1d(%rbp)
je 0x6af33
movq -0x198(%rbp), %rdi
callq 0x6af80
jmp 0x6af31
jmp 0x6af33
jmp 0x6af35
movb $0x1, -0x1e(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x36b20
testb $0x1, -0x1e(%rbp)
jne 0x6af54
movq -0x198(%rbp), %rdi
callq 0x368f0
movq -0x190(%rbp), %rax
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
movq -0x198(%rbp), %rdi
callq 0x368f0
movq -0x68(%rbp), %rdi
callq 0x8970
nopl (%rax)