// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/tools/gt_kmer_database.c
// Function: gt_kmer_database_runner
static int gt_kmer_database_runner(GT_UNUSED int argc, const char **argv,
                                   int parsed_args, void *tool_arguments,
                                   GtError *err)
{
  GtKmerDatabaseArguments *arguments = tool_arguments;
  int had_err = 0;
  GtEncseq       *es;
  GtUword        es_length,
                 nu_kmer_codes = 0;
  GtKmerDatabase *compare_db = NULL,
                 *db = NULL;
  GtLogger *logger;
  FILE *fp = NULL;
  GtHashmap *kmer_hash = NULL;
  GtTimer *timer = NULL;

  gt_error_check(err);
  gt_assert(arguments);

  if (arguments->use_hash)
    kmer_hash = gt_hashmap_new(GT_HASH_DIRECT, NULL,
                               (GtFree) gt_kmer_database_delete_hash_value);
  if (arguments->bench)
    timer = gt_timer_new_with_progress_description("loading encoded sequence");

  logger = gt_logger_new(arguments->verbose, GT_LOGGER_DEFLT_PREFIX, stderr);

  if (arguments->verbose && gt_str_length(arguments->print_filename) > 0UL) {
    fp = gt_fa_fopen(gt_str_get(arguments->print_filename), "w", err);
    gt_logger_set_target(logger, fp);
  }

  if (!had_err) {
    GtEncseqLoader *es_l;
    if (arguments->bench)
      gt_timer_start(timer);
    es_l = gt_encseq_loader_new();
    es = gt_encseq_loader_load(es_l, argv[parsed_args], err);
    if (arguments->bench)
      gt_timer_show_progress(timer, "saving kmers (+iterating over file)",
                             stdout);
    if (es == NULL) {
      had_err = -1;
    }
    gt_encseq_loader_delete(es_l);
  }
  if (!had_err) {
    es_length = gt_encseq_total_length(es);
    if (es_length < (GtUword) arguments->kmersize) {
      gt_error_set(err, "Input is too short for used kmersize. File length: "
                   GT_WU " kmersize: %u", es_length, arguments->kmersize);
      had_err = -1;
    }
  }
  if (!had_err) {
    GtAlphabet *alphabet;
    alphabet = gt_encseq_alphabet(es);
    if (arguments->bench)
    nu_kmer_codes = gt_power_for_small_exponents(
                                            gt_alphabet_num_of_chars(alphabet),
                                            arguments->kmersize);
    if (!arguments->merge_only && !arguments->use_hash && !arguments->bench) {
      compare_db = gt_kmer_database_new(gt_alphabet_num_of_chars(alphabet),
                                        arguments->kmersize,
                                        arguments->sb_size, es);
    }
    if (!arguments->use_hash) {
      db = gt_kmer_database_new(gt_alphabet_num_of_chars(alphabet),
                                arguments->kmersize,
                                arguments->sb_size, es);
      if (arguments->cutoff) {
        if (arguments->mean_cutoff)
          gt_kmer_database_use_mean_cutoff(db, (GtUword) 2,
                                           arguments->cutoff_value);
        else
          gt_kmer_database_set_cutoff(db, arguments->cutoff_value);
        if (!arguments->prune)
          gt_kmer_database_set_prune(db);
      }
    }
  }

  if (!had_err) {
    GtUword startpos = 0,
            endpos,
            interval_id = 0;
    GtKmercodeiterator *iter;
    const GtKmercode *kmercode = NULL;
    iter = gt_kmercodeiterator_encseq_new(es, GT_READMODE_FORWARD,
                                          arguments->kmersize, 0);
    while (!had_err && startpos < es_length - (arguments->kmersize - 1)) {
      GtUword startpos_add_kmer = startpos;
      if (arguments->merge_only) {
        endpos = startpos + (arguments->kmersize - 1) +
                 (gt_rand_max((arguments->sb_size - 1) * 2));
        if (endpos > es_length)
          endpos = es_length;
      }
      else {
        endpos = startpos + (arguments->kmersize - 1) +
                 (gt_rand_max(arguments->sb_size - 1));
      }
      gt_kmercodeiterator_reset(iter, GT_READMODE_FORWARD, startpos);
      while ((kmercode = gt_kmercodeiterator_encseq_next(iter)) != NULL &&
             startpos_add_kmer <= endpos - (arguments->kmersize - 1)) {
        if (!arguments->merge_only && !arguments->use_hash &&
            !kmercode->definedspecialposition && !arguments->bench) {
          gt_kmer_database_add_kmer(compare_db, kmercode->code,
                                    startpos_add_kmer, interval_id);
        }
        if (arguments->use_hash && !kmercode->definedspecialposition) {
          gt_kmer_database_add_to_hash(kmer_hash, kmercode->code,
                                       startpos_add_kmer);
        }
        startpos_add_kmer++;
      }
      if (!arguments->use_hash) {
        gt_kmer_database_add_interval(db, startpos, endpos, interval_id++);
        gt_kmer_database_print_buffer(db, logger);
        if (!arguments->bench)
          had_err = gt_kmer_database_check_consistency(db, err);
      }
      startpos = endpos + 1;
    }
    if (!arguments->use_hash) {
      gt_kmer_database_flush(db);
      gt_kmer_database_print_buffer(db, logger);
      if (!had_err && !arguments->bench)
        had_err = gt_kmer_database_check_consistency(db, err);
      if (!arguments->merge_only && !had_err && !arguments->bench)
        had_err = gt_kmer_database_check_consistency(compare_db, err);
      if (!arguments->merge_only && !arguments->bench)
        gt_kmer_database_print(compare_db, logger, true);
      if (!arguments->merge_only && !had_err && !arguments->bench)
        had_err = gt_kmer_database_compare(compare_db, db, err);
      gt_kmer_database_print(db, logger, true);
    }
    gt_kmercodeiterator_delete(iter);
  }

  if (arguments->bench) {
    GtKmerStartpos pos;
    GtArrayGtUword *pos_hash;
    GtUword rand_access = (GtUword) 50000000,
            rand_code,
            i,
            sum = 0;
    gt_timer_show_progress(timer, "random access", stdout);
    for (i = 0; i < rand_access; i++) {
      rand_code = gt_rand_max(nu_kmer_codes - 1);
      if (arguments->use_hash) {
        pos_hash = gt_hashmap_get(kmer_hash, (const void *) rand_code);
        if (pos_hash != NULL)
          sum += pos_hash->spaceGtUword[pos_hash->nextfreeGtUword - 1];
      }
      else {
        pos = gt_kmer_database_get_startpos(db, rand_code);
        if (pos.no_positions > 0)
          sum += pos.startpos[pos.no_positions - 1];
      }
    }
    printf("sum: " GT_WU "\n", sum);

    gt_timer_show_progress(timer, "", stdout);
    gt_timer_stop(timer);
    gt_timer_delete(timer);
  }
  if (arguments->use_hash)
    gt_hashmap_delete(kmer_hash);
  gt_encseq_delete(es);
  if (!arguments->use_hash)
    gt_kmer_database_delete(db);
  if (!arguments->merge_only && !arguments->bench)
    gt_kmer_database_delete(compare_db);
  gt_logger_delete(logger);
  gt_fa_fclose(fp);

  return had_err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r8, %r15
movq %rcx, %rbx
movl %edx, %ebp
movq %rsi, %r14
testq %r8, %r8
je 0xaf536
movq %r15, %rdi
callq 0x101a9a
testb %al, %al
jne 0xafc6e
testq %rbx, %rbx
je 0xafc69
cmpb $0x1, 0x1d(%rbx)
jne 0xaf55c
leaq 0x727(%rip), %rdx       # 0xafc73
xorl %edi, %edi
xorl %esi, %esi
callq 0x105830
movq %rax, 0x20(%rsp)
jmp 0xaf565
movq $0x0, 0x20(%rsp)
cmpb $0x1, 0x1e(%rbx)
jne 0xaf57c
leaq 0x2a943e(%rip), %rdi    # 0x3589b0
callq 0x1226d6
movq %rax, %r13
jmp 0xaf57f
xorl %r13d, %r13d
movzbl 0x18(%rbx), %edi
movq 0x4149f6(%rip), %rax    # 0x4c3f80
movq (%rax), %rdx
leaq 0x2a0340(%rip), %rsi    # 0x34f8d4
callq 0x108ed0
movq %rax, 0x10(%rsp)
cmpb $0x1, 0x18(%rbx)
jne 0xaf5eb
movq 0x20(%rbx), %rdi
callq 0x120d4c
testq %rax, %rax
je 0xaf5eb
movq 0x20(%rbx), %rdi
callq 0x120d23
leaq 0x2ed46f(%rip), %rsi    # 0x39ca31
leaq 0x2a8fd7(%rip), %rdx    # 0x3585a0
movq %rax, %rdi
movl $0xf5, %ecx
movq %r15, %r8
callq 0x101c23
movq %rax, %r12
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x108f6a
jmp 0xaf5ee
xorl %r12d, %r12d
movq %r15, 0x30(%rsp)
cmpb $0x1, 0x1e(%rbx)
jne 0xaf601
movq %r13, %rdi
callq 0x122733
callq 0xf36ef
movq %rax, %r15
movslq %ebp, %rax
movq (%r14,%rax,8), %rsi
movq %r15, %rdi
movq 0x30(%rsp), %rdx
callq 0xf374b
movq %rax, %rbp
cmpb $0x1, 0x1e(%rbx)
jne 0xaf63f
movq 0x4148bb(%rip), %rax    # 0x4c3ee8
movq (%rax), %rdx
leaq 0x2a9392(%rip), %rsi    # 0x3589c9
movq %r13, %rdi
callq 0x1229c8
movq %r15, %rdi
callq 0xf424d
testq %rbp, %rbp
je 0xaf67e
movq %rbp, %rdi
callq 0xec1d8
movl (%rbx), %ecx
movb $0x1, %r15b
cmpq %rcx, %rax
movq %rax, 0x18(%rsp)
jae 0xaf681
leaq 0x2a9383(%rip), %rsi    # 0x3589ed
xorl %r15d, %r15d
movq 0x30(%rsp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1019cf
jmp 0xaf681
xorl %r15d, %r15d
testb %r15b, %r15b
movq %rbp, 0x50(%rsp)
je 0xaf6f1
movq %rbp, %rdi
callq 0xf02da
movq %rax, %r14
movq $-0x1, 0x48(%rsp)
cmpb $0x1, 0x1e(%rbx)
jne 0xaf6be
movq %r14, %rdi
callq 0xd3713
movl (%rbx), %esi
movl %eax, %edi
callq 0x10af8d
decq %rax
movq %rax, 0x48(%rsp)
cmpb $0x0, 0x19(%rbx)
jne 0xaf6d4
cmpb $0x0, 0x1d(%rbx)
jne 0xaf6d4
cmpb $0x0, 0x1e(%rbx)
je 0xafa80
movq $0x0, 0x8(%rsp)
cmpb $0x0, 0x1d(%rbx)
je 0xaf9d8
movq $0x0, (%rsp)
jmp 0xaf70b
movq $-0x1, 0x48(%rsp)
movq $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testb %r15b, %r15b
movq %r12, 0x28(%rsp)
je 0xafb30
movq %r13, 0x58(%rsp)
movl (%rbx), %edx
xorl %ebp, %ebp
movq 0x50(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x1f7dc2
movq %rax, %r13
movl (%rbx), %r15d
decl %r15d
movb $0x1, %r12b
cmpq %r15, 0x18(%rsp)
je 0xaf9c8
xorl %ecx, %ecx
movq $0x0, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
addq %rcx, %r15
cmpb $0x1, 0x19(%rbx)
jne 0xaf787
movq 0x8(%rbx), %rax
leaq -0x2(,%rax,2), %rdi
callq 0x10ae3c
addq %rax, %r15
movq 0x18(%rsp), %rax
cmpq %rax, %r15
cmovaeq %rax, %r15
jmp 0xaf796
movq 0x8(%rbx), %rdi
decq %rdi
callq 0x10ae3c
addq %rax, %r15
movq %r15, %r14
movq %r13, %rdi
xorl %esi, %esi
movq 0x40(%rsp), %rdx
callq 0x1f7b10
movq %r13, %rdi
callq 0x1f7f9f
testq %rax, %rax
je 0xaf957
movq %rax, %r12
movq 0x40(%rsp), %rbp
movl (%rbx), %eax
decl %eax
movq %r14, %rcx
subq %rax, %rcx
cmpq %rcx, %rbp
ja 0xaf957
cmpb $0x0, 0x19(%rbx)
jne 0xaf804
cmpb $0x0, 0x1d(%rbx)
jne 0xaf804
cmpb $0x0, (%r12)
jne 0xaf804
cmpb $0x0, 0x1e(%rbx)
jne 0xaf804
movq 0x8(%r12), %rsi
movq 0x8(%rsp), %rdi
movq %rbp, %rdx
movq 0x38(%rsp), %rcx
callq 0x15f608
cmpb $0x1, 0x1d(%rbx)
jne 0xaf940
cmpb $0x0, (%r12)
jne 0xaf940
movq 0x8(%r12), %r15
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x105a11
movq %rax, %r12
testq %rax, %rax
jne 0xaf86c
movl $0x18, %edi
leaq 0x2a8d61(%rip), %rsi    # 0x3585a0
movl $0xcb, %edx
callq 0x1092b2
movq %rax, %r12
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x20(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x105a4c
movq 0x8(%r12), %rax
testq %rax, %rax
je 0xaf8fc
cmpq %rax, 0x10(%r12)
jb 0xaf8ee
movq %rax, %xmm0
punpckldq 0x2a1052(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x2a105a(%rip), %xmm0   # 0x3508f0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x2a7956(%rip), %xmm1   # 0x357200
cvttsd2si %xmm1, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
subsd 0x2a1462(%rip), %xmm1   # 0x350d20
cvttsd2si %xmm1, %rsi
andq %rdx, %rsi
orq %rcx, %rsi
addq %rax, %rsi
movq %rsi, 0x8(%r12)
movq (%r12), %rdi
shlq $0x3, %rsi
leaq 0x2a8cc0(%rip), %rdx    # 0x3585a0
movl $0xd6, %ecx
callq 0x1094e6
movq %rax, (%r12)
movq (%r12), %rax
testq %rax, %rax
jne 0xaf92c
jmp 0xafc5f
movq $0x14, 0x8(%r12)
movq (%r12), %rdi
movl $0xa0, %esi
leaq 0x2a8c8b(%rip), %rdx    # 0x3585a0
movl $0xd2, %ecx
callq 0x1094e6
movq %rax, (%r12)
testq %rax, %rax
je 0xafc64
addq $0x10, %r12
movq (%r12), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r12)
movq %rbp, (%rax,%rcx,8)
incq %rbp
movq %r13, %rdi
callq 0x1f7f9f
movq %rax, %r12
testq %rax, %rax
jne 0xaf7c1
xorl %ebp, %ebp
cmpb $0x0, 0x1d(%rbx)
jne 0xaf9a3
movq 0x38(%rsp), %rcx
leaq 0x1(%rcx), %r15
movq (%rsp), %r12
movq %r12, %rdi
movq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0x15f35d
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq 0x15fd4d
cmpb $0x0, 0x1e(%rbx)
jne 0xaf99e
movq %r12, %rdi
movq 0x30(%rsp), %rsi
callq 0x15fa59
movl %eax, %ebp
movq %r15, 0x38(%rsp)
movq 0x18(%rsp), %rax
testl %ebp, %ebp
sete %r12b
jne 0xaf9c8
incq %r14
movl (%rbx), %r15d
decl %r15d
subq %r15, %rax
movq %r14, %rcx
cmpq %rax, %r14
jb 0xaf757
cmpb $0x0, 0x1d(%rbx)
je 0xafa1f
movq 0x28(%rsp), %r12
jmp 0xafb23
movq %r14, %rdi
callq 0xd3713
movl (%rbx), %esi
movq 0x8(%rbx), %rdx
movl %eax, %edi
movq %rbp, %rcx
callq 0x15e918
movq %rax, (%rsp)
cmpb $0x1, 0x1a(%rbx)
jne 0xaf70b
movq 0x10(%rbx), %rdx
cmpb $0x1, 0x1c(%rbx)
jne 0xafaa2
movl $0x2, %esi
movq (%rsp), %rdi
callq 0x15f81c
jmp 0xafaae
movq (%rsp), %r14
movq %r14, %rdi
callq 0x15ec22
movq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x15fd4d
testb %r12b, %r12b
movq 0x30(%rsp), %r14
je 0xafa58
xorl %ebp, %ebp
cmpb $0x0, 0x1e(%rbx)
jne 0xafa58
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x15fa59
movl %eax, %ebp
cmpb $0x0, 0x19(%rbx)
jne 0xafac6
testl %ebp, %ebp
movq 0x28(%rsp), %r12
jne 0xafacb
xorl %ebp, %ebp
cmpb $0x0, 0x1e(%rbx)
jne 0xafacb
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x15fa59
movl %eax, %ebp
jmp 0xafacb
movq %r14, %rdi
callq 0xd3713
movl (%rbx), %esi
movq 0x8(%rbx), %rdx
movl %eax, %edi
movq %rbp, %rcx
callq 0x15e918
movq %rax, 0x8(%rsp)
jmp 0xaf6dd
movq (%rsp), %rdi
movq %rdx, %rsi
callq 0x15f7ed
cmpb $0x0, 0x1b(%rbx)
jne 0xaf70b
movq (%rsp), %rdi
callq 0x15f84b
jmp 0xaf70b
movq 0x28(%rsp), %r12
cmpb $0x0, 0x19(%rbx)
jne 0xafaeb
cmpb $0x0, 0x1e(%rbx)
jne 0xafaeb
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl $0x1, %edx
callq 0x15fbb5
cmpb $0x0, 0x19(%rbx)
jne 0xafb10
testl %ebp, %ebp
jne 0xafb10
xorl %ebp, %ebp
cmpb $0x0, 0x1e(%rbx)
jne 0xafb10
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movq %r14, %rdx
callq 0x15f8f5
movl %eax, %ebp
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
movl $0x1, %edx
callq 0x15fbb5
movq %r13, %rdi
callq 0x1f827b
movq 0x58(%rsp), %r13
cmpb $0x1, 0x1e(%rbx)
jne 0xafbfd
movq 0x4143a7(%rip), %rax    # 0x4c3ee8
movq (%rax), %rdx
leaq 0x2c164f(%rip), %rsi    # 0x37119a
movq %r13, %r12
movq %r13, %rdi
callq 0x1229c8
movl $0x2faf080, %r13d       # imm = 0x2FAF080
xorl %r14d, %r14d
leaq 0x60(%rsp), %r15
movq 0x48(%rsp), %rdi
callq 0x10ae3c
cmpb $0x1, 0x1d(%rbx)
jne 0xafb93
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x105a11
testq %rax, %rax
je 0xafbb9
movq 0x10(%rax), %rcx
shlq $0x3, %rcx
addq (%rax), %rcx
jmp 0xafbb5
movq %r15, %rdi
movq (%rsp), %rsi
movq %rax, %rdx
callq 0x15f76e
movq 0x70(%rsp), %rax
testq %rax, %rax
je 0xafbb9
movq 0x60(%rsp), %rcx
leaq (%rcx,%rax,8), %rcx
addq -0x8(%rcx), %r14
decq %r13
jne 0xafb64
leaq 0x2a8e6c(%rip), %rdi    # 0x358a31
movq %r14, %rsi
xorl %eax, %eax
callq 0x17070
movq 0x414312(%rip), %rax    # 0x4c3ee8
movq (%rax), %rdx
leaq 0x323cb0(%rip), %rsi    # 0x3d3890
movq %r12, %rdi
callq 0x1229c8
movq %r12, %rdi
callq 0x122778
movq %r12, %rdi
callq 0x122d79
movq 0x28(%rsp), %r12
cmpb $0x1, 0x1d(%rbx)
jne 0xafc0d
movq 0x20(%rsp), %rdi
callq 0x105f7d
movq 0x50(%rsp), %rdi
callq 0xee90c
cmpb $0x0, 0x1d(%rbx)
jne 0xafc26
movq (%rsp), %rdi
callq 0x15eade
cmpb $0x0, 0x19(%rbx)
jne 0xafc3c
cmpb $0x0, 0x1e(%rbx)
jne 0xafc3c
movq 0x8(%rsp), %rdi
callq 0x15eade
movq 0x10(%rsp), %rdi
callq 0x10916a
movq %r12, %rdi
callq 0x101e80
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1c194
callq 0x1c1cd
callq 0x1c206
callq 0x1c15b