// File: /workspace/github_c_makefile_star2_O2/yhg926[P]public_kssd/command_reverse.c
// Function: co_reverse2kmer
int co_reverse2kmer(reverse_opt_val_t *opt_val)
{
 dim_shuffle_t* shuf_arr = read_dim_shuffle_file(opt_val->shufile);
 int shuf_arr_len = 1LLU << (4 * shuf_arr->dim_shuffle_stat.subk) ;
 unsigned int rev_shuf_arr[MIN_SUBCTX_DIM_SMP_SZ];
 int count = 0;
 for(unsigned int i=0; i< shuf_arr_len; i++) {
  if( shuf_arr->shuffled_dim[i] < MIN_SUBCTX_DIM_SMP_SZ ){
   rev_shuf_arr[shuf_arr->shuffled_dim[i]] = i ;
   count++;
  }
 }
 if(count != MIN_SUBCTX_DIM_SMP_SZ) err(errno,"count %d not match MIN_SUBCTX_DIM_SMP_SZ %d",count,MIN_SUBCTX_DIM_SMP_SZ);
 int comp_code_bits = shuf_arr->dim_shuffle_stat.k - shuf_arr->dim_shuffle_stat.drlevel > COMPONENT_SZ
           ? 4*(shuf_arr->dim_shuffle_stat.k - shuf_arr->dim_shuffle_stat.drlevel - COMPONENT_SZ ) : 0 ;
 int inner_ctx_bits = shuf_arr->dim_shuffle_stat.subk * 4;
 int half_outer_ctx_bits = (shuf_arr->dim_shuffle_stat.k - shuf_arr->dim_shuffle_stat.subk) *2 ;
 int pf_bits = ( shuf_arr->dim_shuffle_stat.subk - shuf_arr->dim_shuffle_stat.drlevel ) * 4;
 int TL = shuf_arr->dim_shuffle_stat.k * 2;
  if (!(opt_val->num_remaining_args >0 ))
  err(errno,"need speficy the query path");
 const char *qryco_dstat_fpath = NULL;
  qryco_dstat_fpath = test_get_fullpath(opt_val->remaining_args[0],co_dstat);
 if( qryco_dstat_fpath == NULL )
  err(errno,"%s is not a valid query folder",opt_val->remaining_args[0]);
 FILE *qry_co_stat_fp;
 if (( qry_co_stat_fp = fopen(qryco_dstat_fpath,"rb")) == NULL) err(errno,"qry co stat file:%s",qryco_dstat_fpath);
 char *qryco_dname = opt_val->remaining_args[0];
 co_dstat_t co_qry_dstat;
 fread( &co_qry_dstat, sizeof(co_dstat_t), 1, qry_co_stat_fp);
 ctx_obj_ct_t * qry_ctx_ct_list = malloc(co_qry_dstat.infile_num * sizeof(ctx_obj_ct_t));
 fread(qry_ctx_ct_list,sizeof(ctx_obj_ct_t),co_qry_dstat.infile_num,qry_co_stat_fp);
 char (*cofname)[PATHLEN] = malloc(co_qry_dstat.infile_num * PATHLEN);
 fread(cofname,PATHLEN,co_qry_dstat.infile_num,qry_co_stat_fp);
 fclose(qry_co_stat_fp);
 FILE *cbd_fcode_comp_fp,*cbd_fcode_comp_index_fp;
  struct stat cbd_fcode_stat;
 size_t *fco_pos = malloc(sizeof(size_t) * (co_qry_dstat.infile_num + 1) );
 char co_cbd_fcode[PATHLEN];char co_cbd_index_fcode[PATHLEN];
 llong **kmer = malloc( co_qry_dstat.infile_num * sizeof(llong*) );
 int *filled_len = calloc( co_qry_dstat.infile_num, sizeof(int));
 for(int k = 0; k < co_qry_dstat.infile_num; k++){
  kmer[k] = malloc( sizeof(llong) * qry_ctx_ct_list[k] );
 }
 int p_fit_mem = opt_val->p ;
 for ( int j = 0; j < co_qry_dstat.comp_num; j++ ) {
  sprintf(co_cbd_fcode,"%s/combco.%d",qryco_dname,j);
  if( (cbd_fcode_comp_fp = fopen(co_cbd_fcode,"rb"))==NULL) err(errno,"co_reverse2kmer()::%s",co_cbd_fcode);
    stat(co_cbd_fcode, &cbd_fcode_stat);
  unsigned int *cbd_fcode_mem = malloc(cbd_fcode_stat.st_size);
  fread(cbd_fcode_mem,sizeof(unsigned int),cbd_fcode_stat.st_size/sizeof(unsigned int),cbd_fcode_comp_fp);
  fclose(cbd_fcode_comp_fp);
  sprintf(co_cbd_index_fcode,"%s/combco.index.%d",qryco_dname,j);
  if( (cbd_fcode_comp_index_fp = fopen(co_cbd_index_fcode,"rb"))==NULL)
        err(errno,"co_reverse2kmer()::%s",co_cbd_index_fcode);
  fread(fco_pos,sizeof(size_t),co_qry_dstat.infile_num + 1 ,cbd_fcode_comp_index_fp);
  fclose(cbd_fcode_comp_index_fp);
  #pragma omp parallel for num_threads(p_fit_mem) schedule(guided)
  for(int k = 0; k < co_qry_dstat.infile_num; k++){
   if(qry_ctx_ct_list[k]==0) continue;
   char *kstring = malloc(TL + 1);
   kstring[TL] = '\0';
   for(int n = 0; n < fco_pos[k+1] - fco_pos[k]; n++){
    unsigned int ind = cbd_fcode_mem[ fco_pos[k] + n ];
    kmer[k][filled_len[k] + n] = core_reverse2unituple(ind,j,comp_code_bits,pf_bits,inner_ctx_bits,half_outer_ctx_bits,rev_shuf_arr);
   }
   filled_len[k] += (fco_pos[k+1] - fco_pos[k]) ;
  }
 }
 #pragma omp parallel for num_threads(p_fit_mem) schedule(guided)
 for(int k = 0; k < co_qry_dstat.infile_num; k++) {
  if(qry_ctx_ct_list[k]==0) continue;
    char *kstring = malloc(TL + 1);
    kstring[TL] = '\0';
  char *filename;
  (filename = strrchr(cofname[k],'/') ) ? ++filename : (filename = cofname[k]);
  char fullname[PATHLEN];
  sprintf(fullname,"%s/%s",opt_val->outdir,filename);
  FILE *kmerf;
  if ( ( kmerf = fopen(fullname,"w")) == NULL ) err(errno,"%s",filename);
  for (int n =0 ; n< qry_ctx_ct_list[k]; n++){
   llong unituple = kmer[k][n];
   for(int i=0; i<TL;i++){
    kstring[TL-i-1] = Mapbase[unituple % 4] ;
    unituple >>= 2 ;
   }
   fprintf(kmerf,"%s\n",kstring);
  }
  fclose(kmerf);
 }
 return co_qry_dstat.infile_num;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4438, %rsp           # imm = 0x4438
movq %rdi, %r14
callq 0x103a0
movl 0x8(%rax), %ecx
movq %rcx, %r12
leal (,%rcx,4), %ecx
pushq $0x1
popq %rdx
movl %ecx, 0xc(%rsp)
shlq %cl, %rdx
movl %edx, %ecx
xorl %edx, %edx
xorl %ebx, %ebx
cmpq %rdx, %rcx
je 0xd057
movq 0x10(%rax), %rsi
movslq (%rsi,%rdx,4), %rsi
cmpq $0xfff, %rsi            # imm = 0xFFF
jg 0xd052
movl %edx, 0x430(%rsp,%rsi,4)
incl %ebx
incq %rdx
jmp 0xd033
cmpl $0x1000, %ebx           # imm = 0x1000
jne 0xd532
movl 0x4(%rax), %ecx
movl 0xc(%rax), %ebp
movl %ecx, %ebx
movl %ecx, %eax
subl %ebp, %eax
xorl %ecx, %ecx
cmpl $0x8, %eax
leal -0x1c(,%rax,4), %eax
cmovll %ecx, %eax
movl %eax, 0x38(%rsp)
cmpl $0x0, 0x208(%r14)
jle 0xd54e
movq %r14, 0x10(%rsp)
movq 0x210(%r14), %rax
movq (%rax), %rdi
leaq 0x76ba(%rip), %rsi      # 0x14760
callq 0x68a5
testq %rax, %rax
je 0xd563
movq %rax, %r15
leaq 0x6fa9(%rip), %rsi      # 0x14067
movq %rax, %rdi
callq 0x33a0
testq %rax, %rax
je 0xd582
movq %rax, %r14
movl %ebx, %eax
subl %r12d, %eax
addl %eax, %eax
movl %eax, 0x30(%rsp)
subl %ebp, %r12d
shll $0x2, %r12d
movq %r12, 0x40(%rsp)
addl %ebx, %ebx
movl %ebx, 0x58(%rsp)
movq 0x10(%rsp), %rax
movq 0x210(%rax), %rax
movq (%rax), %rax
movq %rax, 0x70(%rsp)
leaq 0x80(%rsp), %r12
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
movq %r12, %rdi
movq %r14, %rcx
callq 0x30c0
movl 0x14(%r12), %ebp
movslq %ebp, %r12
leaq (,%r12,4), %rdi
callq 0x3340
pushq $0x4
popq %rbx
movq %rax, 0x20(%rsp)
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x30c0
movq %r12, %rdi
shlq $0x8, %rdi
callq 0x3340
movl $0x100, %esi            # imm = 0x100
movq %rax, 0x60(%rsp)
movq %rax, %rdi
movq %r12, %rdx
movq %r14, %rcx
callq 0x30c0
movq %r14, %rdi
callq 0x3100
leaq 0x1(%r12), %rax
movq %rax, 0x68(%rsp)
leaq 0x8(,%r12,8), %rdi
callq 0x3340
movq %rax, 0x18(%rsp)
leaq (,%r12,8), %rdi
callq 0x3340
movq %rax, 0x48(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x32a0
movq %rax, %r14
xorl %ebx, %ebx
testl %r12d, %r12d
movl $0x0, %eax
movl %ebp, 0x54(%rsp)
cmovgl %ebp, %eax
movq %rax, 0x28(%rsp)
cmpq %rbx, 0x28(%rsp)
je 0xd1ee
movq 0x20(%rsp), %rax
movl (%rax,%rbx,4), %edi
shlq $0x3, %rdi
callq 0x3340
movq 0x48(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
jmp 0xd1c8
movl 0x90(%rsp), %ecx
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
movl %ecx, 0x5c(%rsp)
leaq 0x130(%rsp), %r15
leaq 0x6e58(%rip), %r13      # 0x14067
xorl %r12d, %r12d
cmpl 0x5c(%rsp), %r12d
movq 0x70(%rsp), %rbx
je 0xd389
movq %r15, %rdi
leaq 0x6e65(%rip), %rsi      # 0x14091
movq %rbx, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x3430
movq %r15, %rdi
movq %r13, %rsi
callq 0x33a0
testq %rax, %rax
je 0xd4ea
movq %rax, %rbp
movq %r15, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x32e0
movq 0xd0(%rsp), %r15
movq %r15, %rdi
callq 0x3340
shrq $0x2, %r15
movq %rax, 0x78(%rsp)
movq %rax, %rdi
pushq $0x4
popq %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x30c0
movq %rbp, %rdi
callq 0x3100
leaq 0x230(%rsp), %r15
movq %r15, %rdi
leaq 0x6dda(%rip), %rsi      # 0x1407e
movq %rbx, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x3430
movq %r15, %rdi
movq %r13, %rsi
callq 0x33a0
testq %rax, %rax
je 0xd502
movq %rax, %rbx
movq 0x18(%rsp), %rdi
pushq $0x8
popq %rsi
movq 0x68(%rsp), %rdx
movq %rax, %rcx
callq 0x30c0
movq %rbx, %rdi
callq 0x3100
xorl %ebp, %ebp
cmpq 0x28(%rsp), %rbp
je 0xd372
movq 0x20(%rsp), %rax
cmpl $0x0, (%rax,%rbp,4)
je 0xd36a
movq 0x18(%rsp), %rcx
movq (%rcx,%rbp,8), %rax
movq 0x8(%rcx,%rbp,8), %r13
subq %rax, %r13
movq 0x78(%rsp), %rcx
leaq (%rcx,%rax,4), %rbx
xorl %r15d, %r15d
cmpq %r15, %r13
je 0xd366
movl (%rbx,%r15,4), %edi
leaq 0x430(%rsp), %rax
movq %rax, (%rsp)
movl %r12d, %esi
movl 0x38(%rsp), %edx
movq 0x40(%rsp), %rcx
movl 0xc(%rsp), %r8d
movl 0x30(%rsp), %r9d
callq 0xd595
movq 0x48(%rsp), %rcx
movq (%rcx,%rbp,8), %rcx
movl (%r14,%rbp,4), %edx
addl %r15d, %edx
movslq %edx, %rdx
movq %rax, (%rcx,%rdx,8)
incq %r15
jmp 0xd31a
addl %r13d, (%r14,%rbp,4)
incq %rbp
jmp 0xd2e7
incl %r12d
leaq 0x130(%rsp), %r15
leaq 0x6ce3(%rip), %r13      # 0x14067
jmp 0xd212
movl 0x58(%rsp), %eax
movslq %eax, %rcx
orl $0x1, %eax
cltq
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %r14
addq $0x100, %r14            # imm = 0x100
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
movq %rax, 0x40(%rsp)
movq %rcx, 0x30(%rsp)
leal -0x1(%rcx), %eax
movl %eax, 0xc(%rsp)
leaq 0x98e8(%rip), %rbx      # 0x16cb0
xorl %ebp, %ebp
movq %r14, 0x10(%rsp)
cmpq 0x28(%rsp), %rbp
je 0xd4d4
movq 0x20(%rsp), %rax
movl (%rax,%rbp,4), %r12d
testq %r12, %r12
je 0xd4cc
movq 0x18(%rsp), %rdi
callq 0x3340
movq %rax, %r15
movq 0x30(%rsp), %rax
movb $0x0, (%r15,%rax)
movq %rbp, %r13
shlq $0x8, %r13
addq 0x60(%rsp), %r13
movq %r13, %rdi
pushq $0x2f
popq %rsi
callq 0x31d0
leaq 0x1(%rax), %rcx
testq %rax, %rax
cmoveq %r13, %rcx
leaq 0x330(%rsp), %r13
movq %r13, %rdi
leaq 0x6f5c(%rip), %rsi      # 0x14393
movq %r14, %rdx
movq %rcx, %r14
xorl %eax, %eax
callq 0x3430
movq %r13, %rdi
leaq 0x6fa7(%rip), %rsi      # 0x143f5
callq 0x33a0
testq %rax, %rax
je 0xd51a
movq %rax, %r13
movq 0x48(%rsp), %rax
movq %rbp, 0x38(%rsp)
movq (%rax,%rbp,8), %rbp
xorl %r14d, %r14d
cmpq %r12, %r14
je 0xd4ba
movq (%rbp,%r14,8), %rax
movq 0x40(%rsp), %rcx
movl 0xc(%rsp), %edx
subq $0x1, %rcx
jb 0xd4a1
movl %eax, %esi
andl $0x3, %esi
movb (%rbx,%rsi), %sil
movslq %edx, %rdx
movb %sil, (%r15,%rdx)
shrq $0x2, %rax
decl %edx
jmp 0xd483
movq %r13, %rdi
leaq 0x7fef(%rip), %rsi      # 0x1549a
movq %r15, %rdx
xorl %eax, %eax
callq 0x32c0
incq %r14
jmp 0xd470
movq %r13, %rdi
callq 0x3100
movq 0x10(%rsp), %r14
movq 0x38(%rsp), %rbp
incq %rbp
jmp 0xd3cf
movl 0x54(%rsp), %eax
addq $0x4438, %rsp           # imm = 0x4438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3060
movl (%rax), %edi
leaq 0x893a(%rip), %rsi      # 0x15e32
leaq 0x130(%rsp), %rdx
jmp 0xd52b
callq 0x3060
movl (%rax), %edi
leaq 0x8922(%rip), %rsi      # 0x15e32
leaq 0x230(%rsp), %rdx
jmp 0xd52b
callq 0x3060
movl (%rax), %edi
leaq 0x7e4e(%rip), %rsi      # 0x15376
movq %r14, %rdx
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x8819(%rip), %rsi      # 0x15d59
movl %ebx, %edx
movl $0x1000, %ecx           # imm = 0x1000
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x88ba(%rip), %rsi      # 0x15e16
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
movq 0x10(%rsp), %rax
movq 0x210(%rax), %rax
movq (%rax), %rdx
leaq 0x8821(%rip), %rsi      # 0x15da1
jmp 0xd52b
callq 0x3060
movl (%rax), %edi
leaq 0x7483(%rip), %rsi      # 0x14a13
movq %r15, %rdx
jmp 0xd52b