// File: /workspace/github_c_makefile_star2_O3/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 $0x88, %rsp
movq %r8, %r15
movq %rcx, %rbx
movl %edx, %ebp
movq %rsi, %r14
testq %r8, %r8
je 0xad1f1
movq %r15, %rdi
callq 0xfcc32
testb %al, %al
jne 0xad91c
testq %rbx, %rbx
je 0xad917
cmpb $0x1, 0x1d(%rbx)
jne 0xad217
leaq 0x71a(%rip), %rdx       # 0xad921
xorl %edi, %edi
xorl %esi, %esi
callq 0x1008fc
movq %rax, 0x28(%rsp)
jmp 0xad220
movq $0x0, 0x28(%rsp)
cmpb $0x1, 0x1e(%rbx)
jne 0xad237
leaq 0x2a2753(%rip), %rdi    # 0x34f980
callq 0x11c986
movq %rax, %r13
jmp 0xad23a
xorl %r13d, %r13d
movzbl 0x18(%rbx), %edi
movq 0x40bd3b(%rip), %rax    # 0x4b8f80
movq (%rax), %rdx
leaq 0x299685(%rip), %rsi    # 0x3468d4
callq 0x103e40
movq %rax, 0x20(%rsp)
cmpb $0x1, 0x18(%rbx)
movq %r15, 0x18(%rsp)
jne 0xad2ab
movq 0x20(%rbx), %rdi
callq 0x11b0ca
testq %rax, %rax
je 0xad2ab
movq 0x20(%rbx), %rdi
callq 0x11b0a1
leaq 0x2e6333(%rip), %rsi    # 0x3935b5
leaq 0x2a22e7(%rip), %rdx    # 0x34f570
movq %rax, %rdi
movl $0xf5, %ecx
movq %r15, %r8
callq 0xfcdbb
movq %rax, %r12
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x103eda
jmp 0xad2ae
xorl %r12d, %r12d
cmpb $0x1, 0x1e(%rbx)
jne 0xad2bc
movq %r13, %rdi
callq 0x11c9e3
callq 0xef0cd
movq %rax, %r15
movslq %ebp, %rax
movq (%r14,%rax,8), %rsi
movq %r15, %rdi
movq 0x18(%rsp), %rdx
callq 0xef129
movq %rax, 0x60(%rsp)
cmpb $0x1, 0x1e(%rbx)
jne 0xad2fc
movq 0x40bbee(%rip), %rax    # 0x4b8ed8
movq (%rax), %rdx
leaq 0x2a26a5(%rip), %rsi    # 0x34f999
movq %r13, %rdi
callq 0x11cc90
movq %r15, %rdi
callq 0xefbe3
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq $-0x1, 0x58(%rsp)
movq 0x60(%rsp), %r15
testq %r15, %r15
je 0xad34c
movq %r15, %rdi
callq 0xe7b9c
movl (%rbx), %ecx
cmpq %rcx, %rax
jae 0xad363
leaq 0x2a268b(%rip), %rsi    # 0x34f9bd
movq $0x0, 0x8(%rsp)
movq 0x18(%rsp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xfcb67
jmp 0xad355
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
jmp 0xad7d1
movq %rax, 0x68(%rsp)
movq %r15, %rdi
callq 0xebcd9
movq %rax, %r14
movq $-0x1, 0x58(%rsp)
cmpb $0x1, 0x1e(%rbx)
jne 0xad39b
movq %r14, %rdi
callq 0xd11be
movl (%rbx), %esi
movl %eax, %edi
callq 0x105ed6
decq %rax
movq %rax, 0x58(%rsp)
movb 0x1d(%rbx), %al
cmpb $0x0, 0x19(%rbx)
jne 0xad3ae
testb $0x1, %al
jne 0xad3ae
cmpb $0x0, 0x1e(%rbx)
je 0xad40f
movq $0x0, 0x8(%rsp)
testb $0x1, %al
movq %r13, 0x48(%rsp)
movq %r12, 0x30(%rsp)
je 0xad3d0
movq $0x0, 0x10(%rsp)
jmp 0xad44a
movq %r14, %rdi
callq 0xd11be
movl (%rbx), %esi
movq 0x8(%rbx), %rdx
movl %eax, %edi
movq %r15, %rcx
callq 0x156d64
cmpb $0x1, 0x1a(%rbx)
movq %rax, 0x10(%rsp)
jne 0xad44a
movq %rax, %r14
movq 0x10(%rbx), %rdx
cmpb $0x1, 0x1c(%rbx)
jne 0xad431
movl $0x2, %esi
movq %r14, %rdi
callq 0x157c1e
jmp 0xad43c
movq %r14, %rdi
callq 0xd11be
movl (%rbx), %esi
movq 0x8(%rbx), %rdx
movl %eax, %edi
movq %r15, %rcx
callq 0x156d64
movq %rax, 0x8(%rsp)
movb 0x1d(%rbx), %al
jmp 0xad3b7
movq %r14, %rdi
movq %rdx, %rsi
callq 0x157bef
cmpb $0x0, 0x1b(%rbx)
jne 0xad44a
movq %r14, %rdi
callq 0x157c4d
movl (%rbx), %edx
movq $0x0, 0x38(%rsp)
movq %r15, %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x1ee606
movq %rax, %r15
xorl %r12d, %r12d
movq 0x68(%rsp), %r14
movq %rax, 0x40(%rsp)
movq %r12, %rbp
movl (%rbx), %r12d
decl %r12d
movq %r14, %r13
subq %r12, %r13
cmpq %r13, %rbp
movq %rbp, 0x50(%rsp)
jae 0xad6e4
addq %rbp, %r12
movq 0x8(%rbx), %rdi
cmpb $0x1, 0x19(%rbx)
jne 0xad4b4
leaq -0x2(,%rdi,2), %rdi
callq 0x105d85
addq %rax, %r12
cmpq %r14, %r12
cmovaeq %r14, %r12
jmp 0xad4bf
decq %rdi
callq 0x105d85
addq %rax, %r12
movq %r15, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x1ee34c
movq %r15, %rdi
callq 0x1ee7e3
testq %rax, %rax
je 0xad679
movq %rax, %r14
movq 0x50(%rsp), %rbp
movl (%rbx), %eax
decl %eax
movq %r12, %rcx
subq %rax, %rcx
cmpq %rcx, %rbp
ja 0xad679
movb 0x1d(%rbx), %al
cmpb $0x0, 0x19(%rbx)
jne 0xad52a
testb $0x1, %al
jne 0xad52a
cmpb $0x0, (%r14)
jne 0xad52a
cmpb $0x0, 0x1e(%rbx)
jne 0xad52a
movq 0x8(%r14), %rsi
movq 0x8(%rsp), %rdi
movq %rbp, %rdx
movq 0x38(%rsp), %rcx
callq 0x157a08
movb 0x1d(%rbx), %al
testb $0x1, %al
je 0xad662
cmpb $0x0, (%r14)
jne 0xad662
movq 0x8(%r14), %r15
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x100add
movq %rax, %r14
testq %rax, %rax
jne 0xad58e
movl $0x18, %edi
leaq 0x2a200f(%rip), %rsi    # 0x34f570
movl $0xcb, %edx
callq 0x104226
movq %rax, %r14
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq 0x28(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x100b18
movq 0x8(%r14), %rax
testq %rax, %rax
je 0xad5aa
cmpq %rax, 0x10(%r14)
movq 0x40(%rsp), %r15
jae 0xad5dd
movq (%r14), %rax
jmp 0xad647
movq $0x14, 0x8(%r14)
movq (%r14), %rdi
movl $0xa0, %esi
leaq 0x2a1faf(%rip), %rdx    # 0x34f570
movl $0xd2, %ecx
callq 0x104461
movq %rax, (%r14)
testq %rax, %rax
movq 0x40(%rsp), %r15
jne 0xad650
jmp 0xad912
movq %rax, %xmm0
punpckldq 0x29a2f6(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x29a2fe(%rip), %xmm0   # 0x3478f0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x2a0bca(%rip), %xmm1   # 0x34e1d0
cvttsd2si %xmm1, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
subsd 0x29a706(%rip), %xmm1   # 0x347d20
cvttsd2si %xmm1, %rsi
andq %rdx, %rsi
orq %rcx, %rsi
addq %rax, %rsi
movq %rsi, 0x8(%r14)
movq (%r14), %rdi
shlq $0x3, %rsi
leaq 0x2a1f36(%rip), %rdx    # 0x34f570
movl $0xd6, %ecx
callq 0x104461
movq %rax, (%r14)
testq %rax, %rax
je 0xad90d
addq $0x10, %r14
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movq %rbp, (%rax,%rcx,8)
incq %rbp
movq %r15, %rdi
callq 0x1ee7e3
movq %rax, %r14
testq %rax, %rax
jne 0xad4e5
xorl %ebp, %ebp
cmpb $0x0, 0x1d(%rbx)
je 0xad688
movq 0x38(%rsp), %r14
jmp 0xad6cd
movq 0x38(%rsp), %rcx
leaq 0x1(%rcx), %r14
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq 0x50(%rsp), %rsi
movq %r12, %rdx
callq 0x15776e
movq %r15, %rdi
movq 0x20(%rsp), %rsi
callq 0x158129
cmpb $0x0, 0x1e(%rbx)
jne 0xad6c8
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x157dfc
movl %eax, %ebp
movq 0x40(%rsp), %r15
incq %r12
movq %r14, 0x38(%rsp)
testl %ebp, %ebp
movq 0x68(%rsp), %r14
je 0xad471
jmp 0xad6e6
xorl %ebp, %ebp
cmpb $0x0, 0x1d(%rbx)
je 0xad6fb
movq 0x48(%rsp), %r13
movq 0x30(%rsp), %r12
jmp 0xad7c9
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x157066
movq %r14, %rdi
movq 0x20(%rsp), %rsi
callq 0x158129
cmpq %r13, 0x50(%rsp)
movq 0x30(%rsp), %r12
jae 0xad72f
movb 0x19(%rbx), %al
testb %al, %al
je 0xad76d
movq 0x48(%rsp), %r13
jmp 0xad78f
xorl %ebp, %ebp
cmpb $0x0, 0x1e(%rbx)
jne 0xad748
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x157dfc
movl %eax, %ebp
cmpb $0x0, 0x19(%rbx)
jne 0xad721
testl %ebp, %ebp
jne 0xad721
xorl %ebp, %ebp
cmpb $0x0, 0x1e(%rbx)
jne 0xad721
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x157dfc
movl %eax, %ebp
jmp 0xad721
cmpb $0x0, 0x1e(%rbx)
movq 0x48(%rsp), %r13
jne 0xad78f
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl $0x1, %edx
callq 0x157f64
movb 0x19(%rbx), %al
testb $0x1, %al
jne 0xad7b5
testl %ebp, %ebp
jne 0xad7b5
xorl %ebp, %ebp
cmpb $0x0, 0x1e(%rbx)
jne 0xad7b5
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x157cf7
movl %eax, %ebp
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl $0x1, %edx
callq 0x157f64
movq %r15, %rdi
callq 0x1eeaa9
cmpb $0x1, 0x1e(%rbx)
jne 0xad8a7
movq %r12, 0x30(%rsp)
movq 0x40b6f1(%rip), %rax    # 0x4b8ed8
movq (%rax), %rdx
leaq 0x2ba772(%rip), %rsi    # 0x367f63
movq %r13, %r12
movq %r13, %rdi
callq 0x11cc90
movl $0x2faf080, %r13d       # imm = 0x2FAF080
xorl %r14d, %r14d
leaq 0x70(%rsp), %r15
movq 0x58(%rsp), %rdi
callq 0x105d85
cmpb $0x1, 0x1d(%rbx)
jne 0xad839
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x100add
testq %rax, %rax
je 0xad863
movq 0x10(%rax), %rcx
shlq $0x3, %rcx
addq (%rax), %rcx
jmp 0xad85f
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq %rax, %rdx
callq 0x157b70
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0xad863
movq 0x70(%rsp), %rcx
leaq (%rcx,%rax,8), %rcx
addq -0x8(%rcx), %r14
decq %r13
jne 0xad80a
leaq 0x2a2192(%rip), %rdi    # 0x34fa01
movq %r14, %rsi
xorl %eax, %eax
callq 0x17070
movq 0x40b658(%rip), %rax    # 0x4b8ed8
movq (%rax), %rdx
leaq 0x31c8e6(%rip), %rsi    # 0x3ca170
movq %r12, %rdi
callq 0x11cc90
movq %r12, %rdi
callq 0x11ca28
movq %r12, %rdi
callq 0x11d054
movq 0x30(%rsp), %r12
cmpb $0x1, 0x1d(%rbx)
jne 0xad8b7
movq 0x28(%rsp), %rdi
callq 0x10109b
movq 0x60(%rsp), %rdi
callq 0xea375
cmpb $0x0, 0x1d(%rbx)
jne 0xad8d1
movq 0x10(%rsp), %rdi
callq 0x156f2a
cmpb $0x0, 0x19(%rbx)
jne 0xad8e7
cmpb $0x0, 0x1e(%rbx)
jne 0xad8e7
movq 0x8(%rsp), %rdi
callq 0x156f2a
movq 0x20(%rsp), %rdi
callq 0x1040e0
movq %r12, %rdi
callq 0xfd018
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1c098
callq 0x1c0d1
callq 0x1c10a
callq 0x1c05f