// File: /workspace/github_c_makefile_star2_O1/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 $0x4458, %rsp           # imm = 0x4458
movq %rdi, 0x8(%rsp)
callq 0x10ef5
movl 0x8(%rax), %r15d
leal (,%r15,4), %r12d
xorl %ebx, %ebx
cmpl $0x1f, %r12d
ja 0xd90a
movl $0x1, %esi
movl %r12d, %ecx
shlq %cl, %rsi
movq 0x10(%rax), %rcx
cmpl $0x2, %esi
movl $0x1, %edx
cmovael %esi, %edx
xorl %esi, %esi
xorl %ebx, %ebx
movslq (%rcx,%rsi,4), %rdi
cmpq $0xfff, %rdi            # imm = 0xFFF
jg 0xd902
movl %esi, 0x450(%rsp,%rdi,4)
incl %ebx
incq %rsi
cmpq %rsi, %rdx
jne 0xd8ec
cmpl $0x1000, %ebx           # imm = 0x1000
jne 0xded5
movl 0x4(%rax), %ecx
movl 0xc(%rax), %r13d
movq %rcx, %rax
movq %rcx, 0x50(%rsp)
subl %r13d, %eax
xorl %ecx, %ecx
cmpl $0x8, %eax
leal -0x1c(,%rax,4), %ebp
cmovll %ecx, %ebp
movq 0x8(%rsp), %rax
cmpl $0x0, 0x208(%rax)
jle 0xdef1
movq 0x210(%rax), %rax
movq (%rax), %rdi
leaq 0x7e06(%rip), %rsi      # 0x15760
callq 0x6bcc
testq %rax, %rax
je 0xdf06
movq %rax, %r14
leaq 0x76f5(%rip), %rsi      # 0x15067
movq %rax, %rdi
callq 0x33a0
testq %rax, %rax
je 0xdf25
movq %rax, %rbx
movq %r12, 0x18(%rsp)
movq 0x8(%rsp), %rax
movq 0x210(%rax), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
leaq 0xa0(%rsp), %r14
movl $0x20, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x30c0
movslq 0x14(%r14), %r14
leaq (,%r14,4), %rdi
callq 0x3340
movq %rax, %r12
movl $0x4, %esi
movq %rax, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x30c0
movq %r14, %rdi
shlq $0x8, %rdi
callq 0x3340
movl $0x100, %esi            # imm = 0x100
movq %rax, 0x60(%rsp)
movq %rax, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x30c0
movq %rbx, %rdi
callq 0x3100
leaq 0x8(,%r14,8), %rdi
callq 0x3340
movq %rax, 0x20(%rsp)
leaq (,%r14,8), %rdi
callq 0x3340
movq %rax, %rbx
movl $0x4, %esi
movq %r14, %rdi
callq 0x32a0
movq %r14, %rsi
movq %rbx, %r14
movq %rax, 0x40(%rsp)
testq %rsi, %rsi
movq %rsi, 0x38(%rsp)
jle 0xda73
xorl %ebx, %ebx
movl (%r12,%rbx,4), %edi
shlq $0x3, %rdi
callq 0x3340
movq 0x38(%rsp), %rsi
movq %rax, (%r14,%rbx,8)
incq %rbx
cmpq %rbx, %rsi
jne 0xda55
movq %r14, 0x58(%rsp)
movq %r12, 0x28(%rsp)
movslq 0xb0(%rsp), %rax
movq %rax, 0x90(%rsp)
testq %rax, %rax
movq 0x10(%rsp), %rdx
jle 0xdd15
movq 0x50(%rsp), %rax
subl %r15d, %eax
subl %r13d, %r15d
addl %eax, %eax
shll $0x2, %r15d
leaq 0x1(%rsi), %rcx
movq %rcx, 0x88(%rsp)
movl %ebp, %r10d
movl %r15d, %r11d
movq $-0x1, %rbp
movl %eax, %ecx
shlq %cl, %rbp
movl %eax, %r13d
movq $-0x1, %r14
notq %rbp
movq 0x18(%rsp), %rsi
movl %esi, %ecx
shlq %cl, %rbp
movq %rbp, %r12
movl %eax, %ecx
shlq %cl, %r12
movl %esi, %ecx
shlq %cl, %r14
notq %r14
xorl %eax, %eax
movq %r11, 0x80(%rsp)
movq %rbp, 0x78(%rsp)
movq %r14, 0x70(%rsp)
movq %r10, 0x48(%rsp)
movq %r13, 0x68(%rsp)
leaq 0x250(%rsp), %r15
movq %r15, %rdi
leaq 0x7570(%rip), %rsi      # 0x15091
movq %rdx, %r13
movq %rax, %rbp
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
movq %r15, %rdi
leaq 0x752d(%rip), %r14      # 0x15067
movq %r14, %rsi
callq 0x33a0
testq %rax, %rax
je 0xde8d
movq %rax, %rbx
movq %r15, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x32e0
movq 0xf0(%rsp), %r15
movq %r15, %rdi
callq 0x3340
shrq $0x2, %r15
movl $0x4, %esi
movq %rax, 0x98(%rsp)
movq %rax, %rdi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x30c0
movq %rbx, %rdi
callq 0x3100
leaq 0x150(%rsp), %rbx
movq %rbx, %rdi
leaq 0x74d7(%rip), %rsi      # 0x1507e
movq %r13, %rdx
movq %rbp, 0x30(%rsp)
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
movq %rbx, %rdi
movq %r14, %rsi
callq 0x33a0
testq %rax, %rax
je 0xdea5
movq %rax, %rbx
movl $0x8, %esi
movq 0x20(%rsp), %rdi
movq 0x88(%rsp), %rdx
movq %rax, %rcx
callq 0x30c0
movq %rbx, %rdi
callq 0x3100
movq 0x38(%rsp), %rsi
testl %esi, %esi
movq 0x28(%rsp), %rcx
movq 0x10(%rsp), %rdx
movq 0x48(%rsp), %r10
movq 0x80(%rsp), %r11
movq 0x78(%rsp), %rbp
movq 0x70(%rsp), %r14
movq 0x68(%rsp), %r13
jle 0xdcff
xorl %eax, %eax
cmpl $0x0, (%rcx,%rax,4)
je 0xdcf3
movq 0x20(%rsp), %rdx
movq (%rdx,%rax,8), %rcx
movq 0x8(%rdx,%rax,8), %rdx
subq %rcx, %rdx
movq %r12, %rbx
movq 0x18(%rsp), %r12
movq 0x30(%rsp), %r15
je 0xdcd9
movq 0x98(%rsp), %rsi
leaq (%rsi,%rcx,4), %rsi
movq 0x40(%rsp), %rcx
movslq (%rcx,%rax,4), %rdi
shlq $0x3, %rdi
movq 0x58(%rsp), %rcx
addq (%rcx,%rax,8), %rdi
xorl %r8d, %r8d
movl (%rsi,%r8,4), %r9d
movl %r10d, %ecx
shlq %cl, %r9
addq %r15, %r9
movl %r9d, %r10d
andl $0xfff, %r10d           # imm = 0xFFF
movl %r11d, %ecx
shrq %cl, %r9
movl %r12d, %ecx
shlq %cl, %r9
movl 0x450(%rsp,%r10,4), %ecx
addq %rcx, %r9
movq %r9, %r10
andq %rbp, %r10
movl %r12d, %ecx
shrq %cl, %r10
movq %r9, %rcx
andq %rbx, %rcx
addq %rcx, %r10
andq %r14, %r9
movl %r13d, %ecx
shlq %cl, %r9
addq %r10, %r9
movq 0x48(%rsp), %r10
movq %r9, (%rdi,%r8,8)
incq %r8
cmpq %r8, %rdx
jne 0xdc79
movq 0x40(%rsp), %rcx
addl %edx, (%rcx,%rax,4)
movq 0x28(%rsp), %rcx
movq 0x10(%rsp), %rdx
movq %rbx, %r12
movq 0x38(%rsp), %rsi
incq %rax
cmpq %rsi, %rax
jne 0xdc26
movq 0x30(%rsp), %rax
incq %rax
cmpq 0x90(%rsp), %rax
jne 0xdb0f
testl %esi, %esi
movq 0x28(%rsp), %rcx
jle 0xde79
movq 0x50(%rsp), %rax
addl %eax, %eax
movslq %eax, %rdx
orl $0x1, %eax
cltq
movq %rax, 0x20(%rsp)
addq $0x100, 0x8(%rsp)       # imm = 0x100
cmpl $0x2, %edx
movl $0x1, %r15d
cmovgel %edx, %r15d
movq %rdx, 0x10(%rsp)
leaq -0x1(%rdx), %rax
movq %rax, 0x18(%rsp)
negq %r15
leaq 0x9f6c(%rip), %rbx      # 0x17cd0
xorl %r13d, %r13d
movl (%rcx,%r13,4), %eax
movq %rax, 0x48(%rsp)
testq %rax, %rax
je 0xde6d
movq 0x20(%rsp), %rdi
callq 0x3340
movq %rax, %r12
movq 0x10(%rsp), %rax
movb $0x0, (%r12,%rax)
movq %r13, %r14
shlq $0x8, %r14
addq 0x60(%rsp), %r14
movq %r14, %rdi
movl $0x2f, %esi
callq 0x31d0
leaq 0x1(%rax), %rbp
testq %rax, %rax
cmoveq %r14, %rbp
leaq 0x350(%rsp), %r14
movq %r14, %rdi
leaq 0x75cd(%rip), %rsi      # 0x15393
movq 0x8(%rsp), %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x3430
movq %r14, %rdi
leaq 0x7616(%rip), %rsi      # 0x153f5
callq 0x33a0
testq %rax, %rax
je 0xdebd
movq %rax, %r14
movq 0x58(%rsp), %rax
movq %r13, 0x30(%rsp)
movq (%rax,%r13,8), %rax
movq %rax, 0x40(%rsp)
movq %r12, %rbp
addq 0x18(%rsp), %rbp
xorl %r13d, %r13d
cmpl $0x0, 0x50(%rsp)
jle 0xde38
movq 0x40(%rsp), %rax
movq (%rax,%r13,8), %rax
xorl %ecx, %ecx
movl %eax, %edx
andl $0x3, %edx
movb (%rbx,%rdx), %dl
movb %dl, (%rbp,%rcx)
shrq $0x2, %rax
decq %rcx
cmpq %rcx, %r15
jne 0xde20
movq %r14, %rdi
leaq 0x8658(%rip), %rsi      # 0x1649a
movq %r12, %rdx
xorl %eax, %eax
callq 0x32c0
incq %r13
cmpq 0x48(%rsp), %r13
jne 0xde0e
movq %r14, %rdi
callq 0x3100
movq 0x38(%rsp), %rsi
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %r13
incq %r13
cmpq %rsi, %r13
jne 0xdd67
movl %esi, %eax
addq $0x4458, %rsp           # imm = 0x4458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3060
movl (%rax), %edi
leaq 0x8f97(%rip), %rsi      # 0x16e32
leaq 0x250(%rsp), %rdx
jmp 0xdece
callq 0x3060
movl (%rax), %edi
leaq 0x8f7f(%rip), %rsi      # 0x16e32
leaq 0x150(%rsp), %rdx
jmp 0xdece
callq 0x3060
movl (%rax), %edi
leaq 0x84ab(%rip), %rsi      # 0x16376
movq %rbp, %rdx
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x8e76(%rip), %rsi      # 0x16d59
movl %ebx, %edx
movl $0x1000, %ecx           # imm = 0x1000
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x8f17(%rip), %rsi      # 0x16e16
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
movq 0x8(%rsp), %rax
movq 0x210(%rax), %rax
movq (%rax), %rdx
leaq 0x8e7e(%rip), %rsi      # 0x16da1
jmp 0xdece
callq 0x3060
movl (%rax), %edi
leaq 0x7ae0(%rip), %rsi      # 0x15a13
movq %r14, %rdx
jmp 0xdece