// File: /workspace/github_cpp_makefile_star2_O1/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 0x4a09
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 0x4b69
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x4b75
movl (%r12), %ecx
cmpl $0x22, %ecx
je 0x4b75
movq %rax, 0x38(%rsp)
testl %ecx, %ecx
jne 0x4914
movl %r14d, (%r12)
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x492a
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 0x4a1a
movq 0x48(%rsp), %rdx
testq %rdx, %rdx
je 0x4974
movq 0x40(%rsp), %rsi
movzbl (%rsi), %eax
cmpl $0x2c, %eax
je 0x4974
cmpl $0x3e, %eax
jne 0x49fa
testb $0x1, %bpl
jne 0x49d8
movq %r12, %rdi
leaq 0x18(%rsp), %rsi
callq 0x4c12
xorl %ebp, %ebp
movq 0x20(%rsp), %rdx
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
leaq 0x7e0(%rip), %rcx       # 0x51cd
xorl %r8d, %r8d
callq 0x21d0
jmp 0x4974
leaq 0x18(%rsp), %rdi
callq 0x2070
jmp 0x4974
leaq 0x87a(%rip), %rdi       # 0x528a
callq 0x21c0
jmp 0x4b55
movq %rsp, %rdi
leaq 0x18(%rsp), %rsi
callq 0x4c12
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
shrq $0x5, %rsi
leaq 0x77f(%rip), %rdi       # 0x51ba
xorl %eax, %eax
callq 0x2040
movq (%rsp), %r15
movq 0x8(%rsp), %rax
subq %r15, %rax
cmpq $0x20, %rax
ja 0x4a65
leaq 0x80f(%rip), %rdi       # 0x526a
callq 0x21c0
jmp 0x4b1d
leaq 0x7d4(%rip), %rdi       # 0x5240
callq 0x21c0
movl 0x8(%r15), %esi
leaq 0x752(%rip), %rdi       # 0x51ce
xorl %eax, %eax
callq 0x2040
movl 0x28(%r15), %esi
leaq 0x757(%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 0x3416
movq %r14, %r12
leaq 0x738(%rip), %rdi       # 0x51fc
movl %eax, %esi
xorl %eax, %eax
callq 0x2040
movq 0x18(%rbx), %rdi
movq 0x34e8(%rip), %rax      # 0x7fc0
movq (%rax), %rdx
leaq 0x72d(%rip), %rsi       # 0x520f
callq 0x2160
movq 0x60(%rsp), %rbx
movq 0x68(%rsp), %r14
cmpq %r14, %rbx
je 0x4b0b
movslq (%rbx), %rax
movq (%r15), %rcx
movsbl (%rcx,%rax), %edi
callq 0x2170
addq $0x8, %rbx
jmp 0x4af1
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x4b1a
callq 0x2130
movq %r12, %r14
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x4b31
callq 0x2130
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x4b40
callq 0x2130
movq %rsp, %rdi
callq 0x4c50
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 0x6a1(%rip), %rdi       # 0x5211
callq 0x20d0
leaq 0x695(%rip), %rdi       # 0x5211
callq 0x2190
jmp 0x4b86
movq %r14, %r12
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x4bd1
callq 0x2130
jmp 0x4bd1
jmp 0x4bcb
jmp 0x4bcb
jmp 0x4bcb
movq %rax, %rbx
jmp 0x4c09
movq %rax, %rbx
cmpl $0x0, (%r12)
jne 0x4bb3
movl %r14d, (%r12)
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4c09
callq 0x2130
jmp 0x4c09
movq %r14, %r12
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x4be5
callq 0x2130
movq 0x40(%rsp), %rdi
cmpq %r12, %rdi
je 0x4bf4
callq 0x2130
movq %rsp, %rdi
callq 0x4c50
leaq 0x80(%rsp), %rdi
callq 0x2050
movq %rbx, %rdi
callq 0x2200
nop