// File: 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
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movb 0x10(%rbp), %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
andb $0x1, %al
movb %al, -0x31(%rbp)
movq $-0x1, -0x40(%rbp)
movq -0x10(%rbp), %rsi
addq $0x40, %rsi
movq -0x18(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x80(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0xe670
movq -0xa8(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0xf380
jmp 0x10105
leaq -0x80(%rbp), %rdi
callq 0xe400
testb $0x1, -0x31(%rbp)
je 0x101f4
movq -0x8(%rbp), %rsi
addq $0x40, %rsi
movq 0x81e51(%rip), %rdi     # 0x91f78
callq 0xe580
movq %rax, -0xb0(%rbp)
jmp 0x10135
movq -0xb0(%rbp), %rdi
movl $0xa, %esi
callq 0xe780
movq %rax, -0xb8(%rbp)
jmp 0x1014f
movq -0xb8(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x40, %rsi
callq 0xe580
movq %rax, -0xc0(%rbp)
jmp 0x1016c
movq -0xc0(%rbp), %rdi
movl $0xa, %esi
callq 0xe780
movq %rax, -0xc8(%rbp)
jmp 0x10186
movq -0xc8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0xe580
movq %rax, -0xd0(%rbp)
jmp 0x1019f
movq -0xd0(%rbp), %rdi
movl $0xa, %esi
callq 0xe780
jmp 0x101b2
jmp 0x101f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xe400
jmp 0x1055e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0xe400
jmp 0x1055e
movq -0x8(%rbp), %rdi
addq $0x40, %rdi
leaq -0x60(%rbp), %rsi
movq $-0x1, %rdx
callq 0xe4e0
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
subq -0x28(%rbp), %rax
movq %rax, -0xa0(%rbp)
cmpq $-0x1, -0x98(%rbp)
je 0x10305
movq -0x98(%rbp), %rax
movq %rax, -0x40(%rbp)
testb $0x1, -0x31(%rbp)
je 0x10300
movq 0x81d2c(%rip), %rdi     # 0x91f78
leaq 0x51001(%rip), %rsi     # 0x61254
callq 0xe610
movq %rax, -0xd8(%rbp)
jmp 0x10261
movq -0xd8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0xe580
movq %rax, -0xe0(%rbp)
jmp 0x1027a
movq -0xe0(%rbp), %rdi
leaq 0x50fe0(%rip), %rsi     # 0x61268
callq 0xe610
movq %rax, -0xe8(%rbp)
jmp 0x10296
movq -0xe8(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq 0xe140
movq %rax, -0xf0(%rbp)
jmp 0x102b2
movq -0xf0(%rbp), %rdi
leaq 0x50fb3(%rip), %rsi     # 0x61273
callq 0xe610
movq %rax, -0xf8(%rbp)
jmp 0x102ce
movq -0xf8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x40, %rsi
callq 0xe580
movq %rax, -0x100(%rbp)
jmp 0x102eb
movq -0x100(%rbp), %rdi
movl $0xa, %esi
callq 0xe780
jmp 0x102fe
jmp 0x10300
jmp 0x10406
cmpq $0x0, -0x20(%rbp)
jle 0x10404
movq -0x8(%rbp), %rsi
addq $0x40, %rsi
movq -0x20(%rbp), %rdx
movzbl -0x31(%rbp), %ecx
andl $0x1, %ecx
leaq -0x60(%rbp), %rdi
callq 0xfad0
movq %rax, -0x108(%rbp)
jmp 0x10335
movq -0x108(%rbp), %rax
movq %rax, -0x40(%rbp)
testb $0x1, -0x31(%rbp)
je 0x10402
movq 0x81c27(%rip), %rdi     # 0x91f78
leaq 0x50efc(%rip), %rsi     # 0x61254
callq 0xe610
movq %rax, -0x110(%rbp)
jmp 0x10366
movq -0x110(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0xe580
movq %rax, -0x118(%rbp)
jmp 0x1037f
movq -0x118(%rbp), %rdi
leaq 0x50ef0(%rip), %rsi     # 0x6127d
callq 0xe610
movq %rax, -0x120(%rbp)
jmp 0x1039b
movq -0x120(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x40, %rsi
callq 0xe580
movq %rax, -0x128(%rbp)
jmp 0x103b8
movq -0x128(%rbp), %rdi
leaq 0x50ec6(%rip), %rsi     # 0x6128c
callq 0xe610
movq %rax, -0x130(%rbp)
jmp 0x103d4
movq -0x130(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0xe360
movq %rax, -0x138(%rbp)
jmp 0x103ed
movq -0x138(%rbp), %rdi
movl $0xa, %esi
callq 0xe780
jmp 0x10400
jmp 0x10402
jmp 0x10404
jmp 0x10406
cmpq $0x0, -0x40(%rbp)
jle 0x104fa
testb $0x1, -0x31(%rbp)
je 0x10461
movq 0x81b5a(%rip), %rdi     # 0x91f78
leaq 0x50e7c(%rip), %rsi     # 0x612a1
callq 0xe610
movq %rax, -0x140(%rbp)
jmp 0x10433
movq -0x140(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0xe360
movq %rax, -0x148(%rbp)
jmp 0x1044c
movq -0x148(%rbp), %rdi
movl $0xa, %esi
callq 0xe780
jmp 0x1045f
jmp 0x10461
cmpq $0x0, -0x40(%rbp)
jle 0x104f8
testb $0x1, -0x31(%rbp)
je 0x104bc
movq 0x81aff(%rip), %rdi     # 0x91f78
leaq 0x50e3f(%rip), %rsi     # 0x612bf
callq 0xe610
movq %rax, -0x150(%rbp)
jmp 0x1048e
movq -0x150(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0xe580
movq %rax, -0x158(%rbp)
jmp 0x104a7
movq -0x158(%rbp), %rdi
movl $0xa, %esi
callq 0xe780
jmp 0x104ba
jmp 0x104bc
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0xa0(%rbp), %rax
addq %rax, %rdx
callq 0x10020
jmp 0x104d9
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0xa0(%rbp), %rax
addq %rax, %rdx
callq 0x10020
jmp 0x104f6
jmp 0x104f8
jmp 0x1054c
testb $0x1, -0x31(%rbp)
je 0x1054a
movq 0x81a71(%rip), %rdi     # 0x91f78
leaq 0x50dcb(%rip), %rsi     # 0x612d9
callq 0xe610
movq %rax, -0x160(%rbp)
jmp 0x1051c
movq -0x160(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0xe580
movq %rax, -0x168(%rbp)
jmp 0x10535
movq -0x168(%rbp), %rdi
movl $0xa, %esi
callq 0xe780
jmp 0x10548
jmp 0x1054a
jmp 0x1054c
leaq -0x60(%rbp), %rdi
callq 0xe400
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x88(%rbp), %rdi
callq 0xeb80
nopw (%rax,%rax)