// File: /workspace/github_cpp_makefile_star2_O3/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, 0x10(%rsp)
movl %esi, %r14d
movq %rdi, %r15
movq (%rdi), %rax
movq 0x8(%rdi), %r12
movq %rax, %rcx
cmpq %r12, %rax
je 0x327b
leaq 0x8(%rax), %rcx
cmpq %r12, %rcx
je 0x328d
movl (%rax), %edx
movl %edx, %esi
movl (%rcx), %edx
cmpl %esi, %edx
jl 0x327b
jg 0x3270
movl 0x4(%rcx), %esi
cmpl -0x4(%rcx), %esi
jl 0x327b
addq $0x8, %rcx
cmpq %r12, %rcx
jne 0x325e
jmp 0x3284
cmpq %r12, %rcx
jne 0x3473
cmpq %r12, %rax
je 0x33fc
subq %rax, %r12
shrq $0x3, %r12
movslq %r12d, %r13
leaq 0x50(%rsp), %rdi
leaq 0x38(%rsp), %rdx
movq %r13, %rsi
callq 0x3f90
leaq 0x38(%rsp), %rdi
leaq 0xf(%rsp), %rdx
movq %r13, %rsi
callq 0x3f90
movq %rbx, 0x20(%rsp)
movq 0x10(%rsp), %rax
movl $0x0, (%rax)
testl %r12d, %r12d
movq %r15, 0x18(%rsp)
jle 0x341b
movq 0x50(%rsp), %rax
movq 0x38(%rsp), %rdx
movq (%r15), %rsi
leal -0x2(,%r14,2), %ecx
movq %rcx, 0x28(%rsp)
leal -0x2(%r14), %r9d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorl %r10d, %r10d
movq $-0x1, %rbx
movl $0x0, 0x8(%rsp)
movq %r12, 0x30(%rsp)
movl %r14d, (%rax,%r10,4)
movl $0xffffffff, (%rdx,%r10,4) # imm = 0xFFFFFFFF
testq %r10, %r10
je 0x33c6
movl (%rsi,%r10,8), %r8d
movl 0x4(%rsi,%r10,8), %ecx
movq 0x28(%rsp), %rdi
addl %r8d, %edi
subl %ecx, %r8d
addl %ecx, %edi
movl %edi, %r13d
shrl $0x1f, %r13d
addl %edi, %r13d
sarl %r13d
movq %rbx, %r11
movl %r11d, %r15d
movl (%rsi,%r15,8), %r12d
movl 0x4(%rsi,%r15,8), %ecx
leal (%r12,%r14), %edi
cmpl (%rsi,%r10,8), %edi
jg 0x3371
leal (%rcx,%r14), %ebp
cmpl 0x4(%rsi,%r10,8), %ebp
jle 0x33a1
subl %ecx, %r12d
cmpl %r12d, %r8d
jne 0x33b6
addl %r9d, %edi
addl %ecx, %edi
movl %edi, %r12d
shrl $0x1f, %r12d
addl %edi, %r12d
sarl %r12d
cmpl %r12d, %r13d
jle 0x33b6
movl %r13d, %ecx
subl %r12d, %ecx
cmpl %r14d, %ecx
jge 0x33b6
addl (%rax,%r15,4), %ecx
jmp 0x33a8
movl (%rax,%r15,4), %ecx
addl %r14d, %ecx
cmpl (%rax,%r10,4), %ecx
jle 0x33b6
movl %ecx, (%rax,%r10,4)
movl %r11d, (%rdx,%r10,4)
leaq -0x1(%r11), %rcx
incq %r11
cmpq $0x1, %r11
movq %rcx, %r11
jg 0x3350
movl (%rax,%r10,4), %ecx
movq 0x10(%rsp), %rdi
movl (%rdi), %r8d
cmpl %r8d, %ecx
jle 0x33e6
movq 0x10(%rsp), %rdi
movl %ecx, (%rdi)
movl %ecx, %r8d
movl %r10d, 0x8(%rsp)
incq %r10
incq %rbx
movq 0x30(%rsp), %r12
cmpq %r12, %r10
jne 0x3315
jmp 0x3426
movq 0x10(%rsp), %rax
movl $0x0, (%rax)
testq %rbx, %rbx
je 0x3464
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x3464
movq %rax, 0x8(%rbx)
jmp 0x3464
xorl %r8d, %r8d
movl $0x0, 0x8(%rsp)
movq 0x20(%rsp), %r9
testq %r9, %r9
je 0x3446
leaq 0x38(%rsp), %rdx
movq 0x18(%rsp), %rdi
movl %r14d, %esi
movl 0x8(%rsp), %ecx
callq 0x3dbc
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x3455
callq 0x2130
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x3464
callq 0x2130
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2f68(%rip), %rdi      # 0x63e2
leaq 0x2c2a(%rip), %rsi      # 0x60ab
leaq 0x2f84(%rip), %rcx      # 0x640c
movl $0xf9, %edx
callq 0x20b0
movq %rax, %rbx
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x34a9
callq 0x2130
jmp 0x34a9
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x34b8
callq 0x2130
movq %rbx, %rdi
callq 0x21f0