// File: /workspace/github_c_makefile_star2_O3/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 0x112a1
movl 0x8(%rax), %r15d
leal (,%r15,4), %r12d
cmpl $0x1f, %r12d
ja 0xe1b8
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 0xdbca
movl %esi, 0x450(%rsp,%rdi,4)
incl %ebx
incq %rsi
cmpq %rsi, %rdx
jne 0xdbb4
cmpl $0x1000, %ebx           # imm = 0x1000
jne 0xe1ba
movl 0x4(%rax), %ecx
movl 0xc(%rax), %r13d
movq %rcx, %rax
movq %rcx, 0x48(%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 0xe1d6
movq 0x210(%rax), %rax
movq (%rax), %rdi
leaq 0x7b8e(%rip), %rsi      # 0x157b0
callq 0x6d79
testq %rax, %rax
je 0xe1eb
movq %rax, %r14
movq %r12, 0x50(%rsp)
leaq 0x7428(%rip), %rsi      # 0x15067
movq %rax, %rdi
callq 0x33a0
testq %rax, %rax
je 0xe20a
movq %rax, %rbx
movq 0x8(%rsp), %rax
movq 0x210(%rax), %rax
movq (%rax), %rax
movq %rax, (%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, 0x28(%rsp)
leaq (,%r14,8), %rdi
callq 0x3340
movq %rax, %rbx
movl $0x4, %esi
movq %r14, %rdi
callq 0x32a0
movq %r14, %r11
movq %rbx, %r14
movq %rax, 0x10(%rsp)
testq %r11, %r11
movq %r11, 0x20(%rsp)
jle 0xdd3a
xorl %ebx, %ebx
movl (%r12,%rbx,4), %edi
shlq $0x3, %rdi
callq 0x3340
movq 0x20(%rsp), %r11
movq %rax, (%r14,%rbx,8)
incq %rbx
cmpq %rbx, %r11
jne 0xdd1c
movq %r14, 0x58(%rsp)
movslq 0xb0(%rsp), %rax
movq %rax, 0x88(%rsp)
testq %rax, %rax
movq %r12, %r8
movq %r12, 0x30(%rsp)
movq (%rsp), %rdx
jle 0xdffd
movq 0x48(%rsp), %rax
subl %r15d, %eax
subl %r13d, %r15d
addl %eax, %eax
shll $0x2, %r15d
leaq 0x1(%r11), %rcx
movq %rcx, 0x80(%rsp)
movl %ebp, %ecx
movq %rcx, 0x90(%rsp)
movl %r15d, %r15d
movq $-0x1, %r10
movl %eax, %ecx
shlq %cl, %r10
movl %eax, %r12d
movq $-0x1, %rbx
notq %r10
movq 0x50(%rsp), %rsi
movl %esi, %ecx
shlq %cl, %r10
movq %r10, %r14
movl %eax, %ecx
shlq %cl, %r14
movl %esi, %ecx
shlq %cl, %rbx
notq %rbx
leaq 0x150(%rsp), %rdi
leaq 0x7297(%rip), %r13      # 0x15067
xorl %ebp, %ebp
movq %r10, 0x78(%rsp)
movq %rbx, 0x70(%rsp)
movq %r14, 0x68(%rsp)
leaq 0x72a9(%rip), %rsi      # 0x15091
movl %ebp, %ecx
movq %rdi, %rbx
xorl %eax, %eax
callq 0x3430
movq %rbx, %rdi
movq %r13, %rsi
callq 0x33a0
testq %rax, %rax
je 0xe170
movq %rax, %r14
movq %rbx, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x32e0
movq 0xf0(%rsp), %rbx
movq %rbx, %rdi
callq 0x3340
shrq $0x2, %rbx
movl $0x4, %esi
movq %rax, 0x98(%rsp)
movq %rax, %rdi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x30c0
movq %r14, %rdi
callq 0x3100
leaq 0x250(%rsp), %rbx
movq %rbx, %rdi
leaq 0x721a(%rip), %rsi      # 0x1507e
movq (%rsp), %rdx
movq %rbp, 0x18(%rsp)
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
movq %rbx, %rdi
movq %r13, %rsi
callq 0x33a0
testq %rax, %rax
je 0xe188
movq %rax, %rbx
movl $0x8, %esi
movq 0x28(%rsp), %rdi
movq 0x80(%rsp), %rdx
movq %rax, %rcx
callq 0x30c0
movq %rbx, %rdi
callq 0x3100
movq 0x20(%rsp), %r11
testl %r11d, %r11d
movq 0x30(%rsp), %r8
movq (%rsp), %rdx
movq 0x10(%rsp), %r9
movq 0x78(%rsp), %r10
movq 0x70(%rsp), %rbx
movq 0x68(%rsp), %r14
jle 0xdfdf
xorl %ebp, %ebp
cmpl $0x0, (%r8,%rbp,4)
je 0xdfd3
movq 0x28(%rsp), %rsi
movq (%rsi,%rbp,8), %rcx
movq 0x8(%rsi,%rbp,8), %rsi
subq %rcx, %rsi
jne 0xdf03
movl (%r9,%rbp,4), %eax
jmp 0xdfcd
movq 0x98(%rsp), %rax
leaq (%rax,%rcx,4), %rdi
movslq (%r9,%rbp,4), %r8
movq %r8, 0x38(%rsp)
shlq $0x3, %r8
movq 0x58(%rsp), %rcx
movq %rbp, 0x40(%rsp)
addq (%rcx,%rbp,8), %r8
xorl %r9d, %r9d
movq 0x90(%rsp), %rdx
movq %r14, %r13
movq %rbx, %r14
movq %r10, %rbp
movq 0x50(%rsp), %rbx
movq 0x18(%rsp), %rax
movl (%rdi,%r9,4), %r10d
movl %edx, %ecx
shlq %cl, %r10
addq %rax, %r10
movl %r10d, %r11d
andl $0xfff, %r11d           # imm = 0xFFF
movl %r15d, %ecx
shrq %cl, %r10
movl %ebx, %ecx
shlq %cl, %r10
movl 0x450(%rsp,%r11,4), %ecx
addq %rcx, %r10
movq %r10, %r11
andq %rbp, %r11
movl %ebx, %ecx
shrq %cl, %r11
movq %r10, %rcx
andq %r13, %rcx
addq %rcx, %r11
andq %r14, %r10
movl %r12d, %ecx
shlq %cl, %r10
addq %r11, %r10
movq %r10, (%r8,%r9,8)
incq %r9
cmpq %r9, %rsi
jne 0xdf48
movq 0x20(%rsp), %r11
movq 0x30(%rsp), %r8
movq (%rsp), %rdx
movq 0x10(%rsp), %r9
movq %rbp, %r10
movq %r14, %rbx
movq %r13, %r14
leaq 0x70a4(%rip), %r13      # 0x15067
movq 0x40(%rsp), %rbp
movq 0x38(%rsp), %rax
addl %eax, %esi
movl %esi, (%r9,%rbp,4)
incq %rbp
cmpq %r11, %rbp
jne 0xdedc
movq 0x18(%rsp), %rbp
incq %rbp
cmpq 0x88(%rsp), %rbp
leaq 0x150(%rsp), %rdi
jne 0xdde1
testl %r11d, %r11d
jle 0xe15b
movq 0x48(%rsp), %rax
addl %eax, %eax
movslq %eax, %rcx
orl $0x1, %eax
cltq
movq %rax, 0x18(%rsp)
addq $0x100, 0x8(%rsp)       # imm = 0x100
cmpl $0x2, %ecx
movl $0x1, %r15d
cmovgel %ecx, %r15d
movq %rcx, (%rsp)
leaq -0x1(%rcx), %rax
movq %rax, 0x10(%rsp)
negq %r15
leaq 0x9cb9(%rip), %rbx      # 0x17d00
xorl %r13d, %r13d
movl (%r8,%r13,4), %eax
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0xe14f
movq 0x18(%rsp), %rdi
callq 0x3340
movq %rax, %r12
movq (%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 0x733b(%rip), %rsi      # 0x153e3
movq 0x8(%rsp), %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x3430
movq %r14, %rdi
leaq 0x7384(%rip), %rsi      # 0x15445
callq 0x33a0
testq %rax, %rax
je 0xe1a0
movq %rax, %r14
movq 0x58(%rsp), %rax
movq %r13, 0x38(%rsp)
movq (%rax,%r13,8), %rax
movq %rax, 0x40(%rsp)
movq %r12, %rbp
addq 0x10(%rsp), %rbp
xorl %r13d, %r13d
cmpl $0x0, 0x48(%rsp)
jle 0xe11a
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 0xe102
movq %r14, %rdi
leaq 0x83c6(%rip), %rsi      # 0x164ea
movq %r12, %rdx
xorl %eax, %eax
callq 0x32c0
incq %r13
cmpq 0x28(%rsp), %r13
jne 0xe0f0
movq %r14, %rdi
callq 0x3100
movq 0x20(%rsp), %r11
movq 0x30(%rsp), %r8
movq 0x38(%rsp), %r13
incq %r13
cmpq %r11, %r13
jne 0xe04a
movl %r11d, %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 0x8d04(%rip), %rsi      # 0x16e82
leaq 0x150(%rsp), %rdx
jmp 0xe1b1
callq 0x3060
movl (%rax), %edi
leaq 0x8cec(%rip), %rsi      # 0x16e82
leaq 0x250(%rsp), %rdx
jmp 0xe1b1
callq 0x3060
movl (%rax), %edi
leaq 0x8218(%rip), %rsi      # 0x163c6
movq %rbp, %rdx
xorl %eax, %eax
callq 0x3330
xorl %ebx, %ebx
callq 0x3060
movl (%rax), %edi
leaq 0x8be1(%rip), %rsi      # 0x16da9
movl %ebx, %edx
movl $0x1000, %ecx           # imm = 0x1000
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x8c82(%rip), %rsi      # 0x16e66
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
movq 0x8(%rsp), %rax
movq 0x210(%rax), %rax
movq (%rax), %rdx
leaq 0x8be9(%rip), %rsi      # 0x16df1
jmp 0xe1b1
callq 0x3060
movl (%rax), %edi
leaq 0x784b(%rip), %rsi      # 0x15a63
movq %r14, %rdx
jmp 0xe1b1