// File: /workspace/github_cpp_makefile_star2_O3/ikatanic[P]lcskpp/main_fasta.cpp
// Function: main
int main(int argc, char** argv) {
  if (argc != 4) {
    printf(
      "Compute LCSk++ of two sequences given in FASTA format.\n\n"
      "Usage: ./main k input output\n\n"
      "Example: ./main 4 input.fa out\n"
      "finds LCS4++ of first two sequences given in `input.fa` and\n"
      "and writes it to `out`\n"
    );
    return 0;
  };

  int k = stoi(argv[1]);
  ifstream infile(argv[2]);

  vector<string> sequences;
  string line;
  string current_seq;
  bool first = true;
  while (getline(infile, line)) {
    if (!line.size()) continue;

    if (line[0] == '>') {
      if (!first) {
        sequences.push_back(current_seq);
      }
      current_seq = "";
      first = false;
    } else if (line[0] == ',') {
      continue;
    } else {
      current_seq += line;
    }
  }
  sequences.push_back(current_seq);

  printf("Read %d sequences.\n", (int)sequences.size());
  if (sequences.size() < 2) {
    printf("Not enough sequences. Aborting.\n");
    return 0;
  }

  string& A = sequences[0];
  string& B = sequences[1];

  printf("Computing LCSk++ of first two sequences..\n");
  printf("Sequence 1 length: %d\n", (int)A.size());
  printf("Sequence 2 length: %d\n", (int)B.size());

  vector<pair<int, int>> recon;
  int length = lcskpp(sequences[0], sequences[1], k, &recon);
  printf("LCSk++ length: %d\n", length);

  auto r = freopen(argv[3], "w", stdout);
  for (auto& p: recon) {
    putchar(A[p.first]);
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
cmpl $0x4, %edi
jne 0x49dd
movq %rsi, %rbx
movq 0x8(%rsi), %rsi
leaq 0x80(%rsp), %r15
leaq 0x18(%rsp), %rdx
movq %r15, %rdi
callq 0x2090
movq 0x80(%rsp), %r13
callq 0x2030
movq %rax, %r12
movl (%rax), %r14d
movl $0x0, (%rax)
leaq 0x40(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
movl $0xa, %edx
callq 0x2180
cmpq %r13, (%rbp)
je 0x4b43
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x4b4f
movl (%r12), %ecx
cmpl $0x22, %ecx
je 0x4b4f
movq %rax, 0x38(%rsp)
testl %ecx, %ecx
jne 0x48e8
movl %r14d, (%r12)
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x48fe
callq 0x2130
movq 0x10(%rbx), %rsi
movq %r15, %rdi
movl $0x8, %edx
callq 0x2220
xorl %eax, %eax
movq %rsp, %r12
movq %rax, 0x10(%r12)
xorps %xmm0, %xmm0
movaps %xmm0, (%r12)
leaq 0x50(%rsp), %r14
movq %r14, -0x10(%r14)
movq %rax, -0x8(%r14)
movb %al, (%r14)
leaq 0x28(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movb $0x1, %bpl
leaq 0x40(%rsp), %r13
movq 0x80(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x2230
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x49ee
movq 0x48(%rsp), %rdx
testq %rdx, %rdx
je 0x4948
movq 0x40(%rsp), %rsi
movzbl (%rsi), %eax
cmpl $0x2c, %eax
je 0x4948
cmpl $0x3e, %eax
jne 0x49ce
testb $0x1, %bpl
jne 0x49ac
movq %r12, %rdi
leaq 0x18(%rsp), %rsi
callq 0x4bec
xorl %ebp, %ebp
movq 0x20(%rsp), %rdx
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
leaq 0x80c(%rip), %rcx       # 0x51cd
xorl %r8d, %r8d
callq 0x21d0
jmp 0x4948
leaq 0x18(%rsp), %rdi
callq 0x2070
jmp 0x4948
leaq 0x8a6(%rip), %rdi       # 0x528a
callq 0x21c0
jmp 0x4b2f
movq %rsp, %rdi
leaq 0x18(%rsp), %rsi
callq 0x4bec
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
shrq $0x5, %rsi
leaq 0x7ab(%rip), %rdi       # 0x51ba
xorl %eax, %eax
callq 0x2040
movq (%rsp), %r15
movq 0x8(%rsp), %rax
subq %r15, %rax
cmpq $0x20, %rax
ja 0x4a39
leaq 0x83b(%rip), %rdi       # 0x526a
callq 0x21c0
jmp 0x4af7
leaq 0x800(%rip), %rdi       # 0x5240
callq 0x21c0
movl 0x8(%r15), %esi
leaq 0x77e(%rip), %rdi       # 0x51ce
xorl %eax, %eax
callq 0x2040
movl 0x28(%r15), %esi
leaq 0x783(%rip), %rdi       # 0x51e5
xorl %eax, %eax
callq 0x2040
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rcx
movaps %xmm0, (%rcx)
movq $0x0, 0x10(%rcx)
movq (%rsp), %rdi
leaq 0x20(%rdi), %rsi
movq 0x38(%rsp), %rdx
callq 0x33fa
movq %r14, %r12
leaq 0x764(%rip), %rdi       # 0x51fc
movl %eax, %esi
xorl %eax, %eax
callq 0x2040
movq 0x18(%rbx), %rdi
movq 0x3514(%rip), %rax      # 0x7fc0
movq (%rax), %rdx
leaq 0x759(%rip), %rsi       # 0x520f
callq 0x2160
movq 0x60(%rsp), %rbx
movq 0x68(%rsp), %r14
cmpq %r14, %rbx
je 0x4ae7
movslq (%rbx), %rax
movq (%r15), %rcx
movsbl (%rcx,%rax), %edi
callq 0x2170
addq $0x8, %rbx
cmpq %r14, %rbx
jne 0x4aca
movq 0x60(%rsp), %rbx
testq %rbx, %rbx
movq %r12, %r14
je 0x4af7
movq %rbx, %rdi
callq 0x2130
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x4b0b
callq 0x2130
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x4b1a
callq 0x2130
movq %rsp, %rdi
callq 0x4c2a
leaq 0x80(%rsp), %rdi
callq 0x2050
xorl %eax, %eax
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6c7(%rip), %rdi       # 0x5211
callq 0x20d0
leaq 0x6bb(%rip), %rdi       # 0x5211
callq 0x2190
jmp 0x4b60
movq %r14, %r12
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x4bab
callq 0x2130
jmp 0x4bab
jmp 0x4ba5
jmp 0x4ba5
jmp 0x4ba5
movq %rax, %rbx
jmp 0x4be3
movq %rax, %rbx
cmpl $0x0, (%r12)
jne 0x4b8d
movl %r14d, (%r12)
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4be3
callq 0x2130
jmp 0x4be3
movq %r14, %r12
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x4bbf
callq 0x2130
movq 0x40(%rsp), %rdi
cmpq %r12, %rdi
je 0x4bce
callq 0x2130
movq %rsp, %rdi
callq 0x4c2a
leaq 0x80(%rsp), %rdi
callq 0x2050
movq %rbx, %rdi
callq 0x2200
nop