// File: /workspace/github_cpp_makefile_star2_O2/ParkerLab[P]cta/src/cta.cpp
// Function: trim_pair(FASTQRecord&, FASTQRecord&, long long, long long, long long, long long, bool)
void trim_pair(FASTQRecord& rec1, FASTQRecord& rec2, long long int rc_length, long long int max_edit_distance, long long int fudge = 0, long long int trim_start = 0, bool verbose = false) {
    long long int alignment_start = -1;
    std::string rec2_rc = reverse_complement(rec2.sequence.substr(0, rc_length));
    if (verbose) {
        std::cout << rec1.sequence << '\n' << rec2.sequence << '\n' << rec2_rc << '\n';
    }

    size_t position_of_rec2_rc_in_rec1 = rec1.sequence.rfind(rec2_rc);
    long long int trim_end = rc_length - fudge;

    if (position_of_rec2_rc_in_rec1 != std::string::npos) {
        alignment_start = position_of_rec2_rc_in_rec1;

        if (verbose) {
            std::cout << "trim_pair: rec2 rc " << rec2_rc << " found at " << position_of_rec2_rc_in_rec1 << " in rec1 " << rec1.sequence  << '\n';
        }
    } else if (max_edit_distance > 0) {
        alignment_start = align(rec2_rc, rec1.sequence, max_edit_distance, verbose);

        if (verbose) {
            std::cout << "trim_pair: rec2 rc " << rec2_rc << " not found in " << rec1.sequence << "; alignment result: " << alignment_start << '\n';
        }
    }

    if (alignment_start > 0) {
        if (verbose) {
            std::cout << "trim_pair: best alignment at " << alignment_start << '\n';
        }
        if (alignment_start > 0) {
            if (verbose) {
                std::cout << "trim_pair: trimming pair " << rec1.name << '\n';
            }
            trim_fastq_record(rec1, trim_start, alignment_start + trim_end);
            trim_fastq_record(rec2, trim_start, alignment_start + trim_end);
        }
    } else if (verbose) {
        std::cout << "not touching pair " << rec1.name << '\n';
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, (%rsp)
movq %r8, 0x10(%rsp)
movq %rcx, %r14
movq %rdx, %r12
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
leaq 0x40(%rsi), %r13
leaq 0x38(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
xorl %edx, %edx
movq %r12, %rcx
callq 0xc500
leaq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0xcd98
movb 0x90(%rsp), %r15b
leaq 0x38(%rsp), %rdi
callq 0xc250
testb %r15b, %r15b
je 0xd4b1
leaq 0x40(%rbx), %rsi
movq 0x31b05(%rip), %rdi     # 0x3ef78
callq 0xc430
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0xc590
movq %rax, %rdi
movq %r13, %rsi
callq 0xc430
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0xc590
leaq 0x18(%rsp), %rsi
movq %rax, %rdi
callq 0xc430
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0xc590
leaq 0x40(%rbx), %r13
leaq 0x18(%rsp), %rsi
pushq $-0x1
popq %rdx
movq %r13, %rdi
callq 0xc3c0
cmpq $-0x1, %rax
je 0xd53b
movq %rax, %rbp
testb %r15b, %r15b
je 0xd5bf
movq 0x31a9a(%rip), %rdi     # 0x3ef78
leaq 0x18d2f(%rip), %rsi     # 0x26214
callq 0xc490
leaq 0x18(%rsp), %rsi
movq %rax, %rdi
callq 0xc430
leaq 0x18d2a(%rip), %rsi     # 0x26228
movq %rax, %rdi
callq 0xc490
movq %rax, %rdi
movq %rbp, %rsi
callq 0xc350
leaq 0x18d1b(%rip), %rsi     # 0x26233
movq %rax, %rdi
callq 0xc490
movq %rax, %rdi
movq %r13, %rsi
callq 0xc430
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0xc590
jmp 0xd5bf
testq %r14, %r14
jle 0xd649
movzbl %r15b, %ecx
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0xd0b3
movq %rax, %rbp
testb %r15b, %r15b
je 0xd5bf
movq 0x31a11(%rip), %rdi     # 0x3ef78
leaq 0x18ca6(%rip), %rsi     # 0x26214
callq 0xc490
leaq 0x18(%rsp), %rsi
movq %rax, %rdi
callq 0xc430
leaq 0x18cb6(%rip), %rsi     # 0x2623d
movq %rax, %rdi
callq 0xc490
movq %rax, %rdi
movq %r13, %rsi
callq 0xc430
leaq 0x18cab(%rip), %rsi     # 0x2624c
movq %rax, %rdi
callq 0xc490
movq %rax, %rdi
movq %rbp, %rsi
callq 0xc1d0
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0xc590
testq %rbp, %rbp
jle 0xd649
testb %r15b, %r15b
je 0xd61f
movq 0x319a4(%rip), %rdi     # 0x3ef78
leaq 0x18c86(%rip), %rsi     # 0x26261
callq 0xc490
movq %rax, %rdi
movq %rbp, %rsi
callq 0xc1d0
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0xc590
movq 0x3197b(%rip), %rdi     # 0x3ef78
leaq 0x18c7b(%rip), %rsi     # 0x2627f
callq 0xc490
movq %rax, %rdi
movq %rbx, %rsi
callq 0xc430
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0xc590
subq 0x10(%rsp), %r12
addq %r12, %rbp
movq %rbx, %rdi
movq (%rsp), %rsi
movq %rbp, %rdx
callq 0xd38c
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movq %rbp, %rdx
callq 0xd38c
jmp 0xd677
testb %r15b, %r15b
je 0xd677
movq 0x31923(%rip), %rdi     # 0x3ef78
leaq 0x18c3d(%rip), %rsi     # 0x26299
callq 0xc490
movq %rax, %rdi
movq %rbx, %rsi
callq 0xc430
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0xc590
leaq 0x18(%rsp), %rdi
callq 0xc250
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
jmp 0xd6a4
jmp 0xd69c
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0xc250
movq %rbx, %rdi
callq 0xc820