// File: /workspace/github_cpp_makefile_star2_O2/fpavetic[P]lcskpp/lcskpp.cpp
// Function: lcskpp_sparse_slow(std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>> const&, int, int*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>*)
static void lcskpp_sparse_slow(
    const vector<pair<int, int> >& matches,
    const int k, int* lcskpp_length,
    vector<pair<int, int> >* lcskpp_reconstruction) {
  assert(is_sorted(matches.begin(), matches.end()));

  if (matches.empty()) {
    *lcskpp_length = 0;
    if (lcskpp_reconstruction) lcskpp_reconstruction->clear();
  } else {
    int n = matches.size();
    vector<int> dp(n);
    vector<int> recon(n);
    int best_idx = 0;
    *lcskpp_length = 0;
    
    for (int i = 0; i < n; ++i) {
      dp[i] = k;
      recon[i] = -1;

      int end_row_i = matches[i].first+k-1;
      int end_col_i = matches[i].second+k-1;

      int primary_diagonal_i = end_row_i - end_col_i;
      int secondary_diagonal_i = (end_row_i + end_col_i)/2;

      for (int j = i-1; j >= 0; --j) {
        if (matches[j].first + k <= matches[i].first &&
            matches[j].second + k <= matches[i].second) {
          // 1) Taking the entire match interval and continuing
          // another match which 'ended'.
          if (dp[j] + k > dp[i]) {
            dp[i] = dp[j] + k;
            recon[i] = j;
          }
        } else {
          // 2) Continuation on the same primary diagonal.
          int end_row_j = matches[j].first+k-1;
          int end_col_j = matches[j].second+k-1;
          int primary_diagonal_j = end_row_j - end_col_j;
          int secondary_diagonal_j = (end_row_j + end_col_j)/2;
	  
          if (primary_diagonal_i == primary_diagonal_j &&
              secondary_diagonal_i > secondary_diagonal_j &&
              secondary_diagonal_i - secondary_diagonal_j < k) {
            if (dp[j] + secondary_diagonal_i - secondary_diagonal_j 
                > dp[i]) {
              dp[i] = 
                dp[j] + secondary_diagonal_i - secondary_diagonal_j;
              recon[i] = j;
            }
          }
        }
      }

      if (dp[i] > *lcskpp_length) {
        best_idx = i;
        *lcskpp_length = dp[i];
      }
    }

    if (lcskpp_reconstruction) {
      fill_lcskpp_reconstruction(matches, k, recon, best_idx, 
                                 *lcskpp_length, 
                                 lcskpp_reconstruction);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, %rbx
movq %rdx, 0x8(%rsp)
movl %esi, %r14d
movq %rdi, %r15
movq (%rdi), %rdi
movq 0x8(%r15), %rsi
callq 0x499c
testb %al, %al
je 0x31e4
movq (%r15), %rax
movq 0x8(%r15), %rcx
cmpq %rcx, %rax
je 0x31c8
movq %rbx, 0x20(%rsp)
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %r13
leaq 0x50(%rsp), %rdi
movq %rcx, %rbx
leaq 0x38(%rsp), %rdx
movq %r13, %rsi
callq 0x3b98
leaq 0x38(%rsp), %rdi
leaq 0x3(%rsp), %rdx
movq %r13, %rsi
callq 0x3b98
movq 0x8(%rsp), %rax
andl $0x0, (%rax)
movq 0x50(%rsp), %rdi
movq 0x38(%rsp), %r9
movq %r15, 0x18(%rsp)
movq (%r15), %r10
leal -0x2(,%r14,2), %eax
movq %rax, 0x28(%rsp)
leal -0x2(%r14), %r13d
xorl %r8d, %r8d
testl %ebx, %ebx
cmovlel %r8d, %ebx
pushq $-0x1
popq %rax
movq %rax, 0x10(%rsp)
pushq $0x2
popq %r15
xorl %r11d, %r11d
movl $0x0, 0x4(%rsp)
movq %rbx, 0x30(%rsp)
cmpq %rbx, %r11
je 0x3185
movl %r14d, (%rdi,%r11,4)
orl $-0x1, (%r9,%r11,4)
movl (%r10,%r11,8), %r8d
movl 0x4(%r10,%r11,8), %edx
movq 0x28(%rsp), %rax
addl %r8d, %eax
subl %edx, %r8d
addl %edx, %eax
cltd
idivl %r15d
movl %eax, %esi
movq 0x10(%rsp), %rbx
leaq 0x1(%rbx), %rax
testq %rax, %rax
jle 0x3153
movl %ebx, %ebp
movl (%r10,%rbp,8), %r12d
movl 0x4(%r10,%rbp,8), %edx
leal (%r12,%r14), %eax
cmpl (%r10,%r11,8), %eax
jg 0x3117
leal (%rdx,%r14), %ecx
cmpl 0x4(%r10,%r11,8), %ecx
jle 0x313a
subl %edx, %r12d
addl %r13d, %eax
addl %edx, %eax
cltd
idivl %r15d
cmpl %r12d, %r8d
jne 0x314e
cmpl %eax, %esi
jle 0x314e
movl %esi, %edx
subl %eax, %edx
cmpl %r14d, %edx
jge 0x314e
addl (%rdi,%rbp,4), %edx
jmp 0x3140
movl (%rdi,%rbp,4), %edx
addl %r14d, %edx
cmpl (%rdi,%r11,4), %edx
jle 0x314e
movl %edx, (%rdi,%r11,4)
movl %ebx, (%r9,%r11,4)
decq %rbx
jmp 0x30ee
movl (%rdi,%r11,4), %eax
movq 0x8(%rsp), %rcx
movl (%rcx), %r8d
cmpl %r8d, %eax
jle 0x3173
movq 0x8(%rsp), %rcx
movl %eax, (%rcx)
movl %eax, %r8d
movl %r11d, 0x4(%rsp)
incq %r11
incq 0x10(%rsp)
movq 0x30(%rsp), %rbx
jmp 0x30bb
movq 0x20(%rsp), %r9
testq %r9, %r9
je 0x31a5
leaq 0x38(%rsp), %rdx
movq 0x18(%rsp), %rdi
movl %r14d, %esi
movl 0x4(%rsp), %ecx
callq 0x3a30
leaq 0x38(%rsp), %rdi
callq 0x55fc
leaq 0x50(%rsp), %rdi
callq 0x55fc
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
andl $0x0, (%rax)
testq %rbx, %rbx
je 0x31b9
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x31b9
movq %rax, 0x8(%rbx)
jmp 0x31b9
leaq 0x3197(%rip), %rdi      # 0x6382
leaq 0x2ebe(%rip), %rsi      # 0x60b0
leaq 0x31b3(%rip), %rcx      # 0x63ac
movl $0xf9, %edx
callq 0x20c0
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x55fc
jmp 0x3215
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x55fc
movq %rbx, %rdi
callq 0x21f0