// File: /workspace/github_cpp_makefile_star2_O2/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 $0x298, %rsp            # imm = 0x298
cmpl $0x4, %edi
jne 0x479d
movq %rsi, %rbx
movq 0x8(%rsi), %rsi
leaq 0x90(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x2090
leaq 0x90(%rsp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x494a
movl %eax, 0xc(%rsp)
leaq 0x90(%rsp), %r14
movq %r14, %rdi
callq 0x20b0
movq 0x10(%rbx), %rsi
leaq 0x90(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x21f0
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r15
movaps %xmm0, (%r15)
andq $0x0, 0x10(%r15)
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0x80(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
movb $0x1, %r13b
leaq 0x30(%rsp), %r12
leaq 0x70(%rsp), %rbp
movq %r14, %rdi
movq %r12, %rsi
callq 0x2050
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x47ae
cmpq $0x0, 0x38(%rsp)
je 0x4738
movq 0x30(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x2c, %eax
je 0x4738
cmpl $0x3e, %eax
jne 0x4790
testb $0x1, %r13b
jne 0x477c
movq %r15, %rdi
movq %rbp, %rsi
callq 0x4966
xorl %r13d, %r13d
movq %rbp, %rdi
leaq 0xa08(%rip), %rsi       # 0x5191
callq 0x2200
jmp 0x4738
movq %rbp, %rdi
movq %r12, %rsi
callq 0x2220
jmp 0x4738
leaq 0xa80(%rip), %rdi       # 0x5224
callq 0x21b0
jmp 0x48d2
leaq 0x10(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x4966
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
shrq $0x5, %rsi
leaq 0x9ac(%rip), %rdi       # 0x517e
xorl %eax, %eax
callq 0x2040
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %rax
subq %r14, %rax
cmpq $0x20, %rax
ja 0x47fd
leaq 0xa11(%rip), %rdi       # 0x5204
callq 0x21b0
jmp 0x48a7
leaq 0x9d6(%rip), %rdi       # 0x51da
callq 0x21b0
movl 0x8(%r14), %esi
leaq 0x97e(%rip), %rdi       # 0x5192
xorl %eax, %eax
callq 0x2040
movl 0x28(%r14), %esi
leaq 0x983(%rip), %rdi       # 0x51a9
xorl %eax, %eax
callq 0x2040
leaq 0x50(%rsp), %rcx
andq $0x0, 0x10(%rcx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rcx)
movq 0x10(%rsp), %rdi
leaq 0x20(%rdi), %rsi
movl 0xc(%rsp), %edx
callq 0x313f
leaq 0x96a(%rip), %rdi       # 0x51c0
movl %eax, %esi
xorl %eax, %eax
callq 0x2040
movq 0x18(%rbx), %rdi
movq 0x374e(%rip), %rax      # 0x7fb8
movq (%rax), %rdx
leaq 0x95f(%rip), %rsi       # 0x51d3
callq 0x2140
movq 0x50(%rsp), %rbx
movq 0x58(%rsp), %r15
cmpq %r15, %rbx
je 0x489d
movslq (%rbx), %rax
movq (%r14), %rcx
movsbl (%rcx,%rax), %edi
callq 0x2170
addq $0x8, %rbx
jmp 0x4883
leaq 0x50(%rsp), %rdi
callq 0x4616
leaq 0x70(%rsp), %rdi
callq 0x20b0
leaq 0x30(%rsp), %rdi
callq 0x20b0
leaq 0x10(%rsp), %rdi
callq 0x4992
leaq 0x90(%rsp), %rdi
callq 0x2060
xorl %eax, %eax
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x48e8
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4616
jmp 0x4917
jmp 0x4914
jmp 0x4914
jmp 0x4914
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x20b0
jmp 0x4942
movq %rax, %rbx
jmp 0x4942
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x20b0
leaq 0x30(%rsp), %rdi
callq 0x20b0
leaq 0x10(%rsp), %rdi
callq 0x4992
leaq 0x90(%rsp), %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x21e0