// File: /workspace/github_c_makefile_star2_O3/yhg926[P]public_kssd/co2mco.c
// Function: cdb_kmerf2kmerdb
void cdb_kmerf2kmerdb(const char *mcodirname, const char *codirname, int cofnum, int comp_num, int p_fit_mem)
{
 kmerdb_index_t mco_map;
 size_t comp_sz = (1LLU << 4*COMPONENT_SZ);
 int binnum = ceil((double)cofnum/BIN_SZ);
 mco_map.row_bin_gnum = malloc((size_t)comp_sz*binnum*sizeof(unsigned int));
 mco_map.row_gnum = malloc(comp_sz * sizeof(unsigned int));
 mco_map.row_offset = malloc(comp_sz*sizeof(size_t) );
 mco_map.row_offset[0] = 0;
 size_t *cbdcoindex = malloc( sizeof(size_t)*(cofnum + 1) );
 char cbdcofname[PATHLEN]; char cbdcoindexf[PATHLEN];
 char mcofname[PATHLEN]; char mcoindexf[PATHLEN];
 mmp_uint_t mmpcbd_cofile;
 gid_arr_llist_t** mco = malloc(comp_sz* sizeof(gid_arr_llist_t*));
 for(unsigned int i = 0; i< comp_num; i++){
  memset(mco_map.row_bin_gnum,0,(size_t)comp_sz*binnum*sizeof(unsigned int) );
  memset(mco_map.row_gnum,0,comp_sz * sizeof(unsigned int));
  FILE *cbdfp, *cbdindexfp;
  sprintf(cbdcoindexf,"%s/combco.index.%d",codirname,i);
  sprintf(cbdcofname,"%s/combco.%d",codirname,i);
    if( (cbdfp = fopen(cbdcofname,"rb")) == NULL) err(errno,"%s",cbdcofname);
    if( (cbdindexfp = fopen(cbdcoindexf,"rb")) == NULL) err(errno,"%s",cbdcoindexf);
  fread(cbdcoindex,sizeof(size_t),cofnum + 1,cbdindexfp);
  mmpcbd_cofile = mmp_uint_arr(cbdcofname);
  for(int j = 0;j< cofnum; j++ ){
#pragma omp parallel for num_threads(p_fit_mem) schedule(guided)
   for(size_t k = cbdcoindex[j]; k< cbdcoindex[j+1]; k++){
    unsigned int ind = mmpcbd_cofile.mmpco[k];
    unsigned int mod = mco_map.row_gnum[ind] % GID_ARR_SZ ;
    gid_arr_llist_t* tmp;
    if(mod == 0){
     tmp = mco[ind];
     mco[ind] = malloc(sizeof(gid_arr_llist_t));
     if (mco[ind] == NULL) err(errno,"cdb_kmerf2kmerdb()::mco[ind]") ;
     mco[ind]->next = tmp;
    }
    mco[ind]->gidobj[mod] = j % BIN_SZ ;
    mco_map.row_gnum[ind]++;
    mco_map.row_bin_gnum[(size_t)ind*binnum + j/BIN_SZ]++;
   }
  }
  munmap(mmpcbd_cofile.mmpco, mmpcbd_cofile.fsize);
  for(size_t n=1; n<comp_sz; n++)
   mco_map.row_offset[n] = mco_map.row_offset[n-1] + mco_map.row_gnum[n-1];
  sprintf(mcoindexf,"%s/mco.index.%d",mcodirname,i);
  FILE *arrmco_index_fp = fopen(mcoindexf,"wb");
  if( arrmco_index_fp == NULL) err(errno,"%s",mcoindexf);
  fwrite(mco_map.row_offset,sizeof(size_t),comp_sz,arrmco_index_fp);
  fwrite(mco_map.row_bin_gnum,sizeof(unsigned int),(size_t)comp_sz*binnum, arrmco_index_fp);
  fclose(arrmco_index_fp);
  sprintf(mcofname,"%s/mco.%d",mcodirname,i);
  int arrmco_fp = open(mcofname,O_RDWR|O_CREAT,0600) ;
  if (arrmco_fp == -1) err(errno,"cdb_kmerf2kmerdb()::%s",mcofname);
  size_t mco_comp_fsize = sizeof(gidobj_t)*(mco_map.row_offset[comp_sz-1] + mco_map.row_gnum[comp_sz-1]) ;
  if(ftruncate(arrmco_fp,mco_comp_fsize) == -1) err(errno,"cdb_kmerf2kmerdb()::ftruncate");
  gidobj_t* mco_mmpf = mmap(NULL,mco_comp_fsize,PROT_WRITE,MAP_SHARED,arrmco_fp,0);
  close(arrmco_fp);
#pragma omp parallel for num_threads(p_fit_mem) schedule(guided)
  for(size_t s = 0; s< comp_sz ; s++ ){
   if( mco_map.row_gnum[s] == 0 ) continue;
    gid_arr_llist_t *tmpblk;
    gidobj_t* current_blkpos_mapin_arrmco = mco_mmpf + mco_map.row_offset[s] + mco_map.row_gnum[s] ;
    int blk_num = mco_map.row_gnum[s]/GID_ARR_SZ;
    int remainder = mco_map.row_gnum[s] % GID_ARR_SZ;
    if( remainder > 0 ) blk_num+=1;
    int blk_len;
    for(int blk = 0; blk < blk_num; blk++){
     if((blk==0) && (remainder > 0) ) blk_len = remainder;
     else blk_len = GID_ARR_SZ;
     current_blkpos_mapin_arrmco -= blk_len;
     memcpy(current_blkpos_mapin_arrmco, mco[s]->gidobj, blk_len * sizeof(gidobj_t));
     tmpblk = mco[s];
     mco[s] = mco[s]->next;
     free(tmpblk);
    }
  }
  if ( msync( mco_mmpf, mco_comp_fsize, MS_ASYNC ) < 0 )
          err(errno,"cdb_kmerf2kmerdb()::msync failed");
   munmap(mco_mmpf,mco_comp_fsize);
  fclose(cbdfp);
  fclose(cbdindexfp);
 }
  free(mco_map.row_bin_gnum);
  free(mco_map.row_gnum);
  free(mco_map.row_offset);
  free(cbdcoindex);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x548, %rsp            # imm = 0x548
movl %ecx, %ebp
movl %edx, %ebx
movq %rsi, 0x88(%rsp)
movq %rdi, %r14
cvtsi2sd %edx, %xmm0
mulsd 0x11555(%rip), %xmm0    # 0x15008
callq 0x3490
cvttsd2si %xmm0, %eax
cltq
movq %rax, 0x40(%rsp)
movq %rax, %rdi
shlq $0x1e, %rdi
movq %rdi, 0x80(%rsp)
callq 0x3340
movq %rax, 0x18(%rsp)
movl $0x40000000, %edi       # imm = 0x40000000
callq 0x3340
movq %rax, (%rsp)
movl $0x80000000, %edi       # imm = 0x80000000
callq 0x3340
movq %rax, %r13
movq $0x0, (%rax)
movl %ebx, 0x5c(%rsp)
movslq %ebx, %rbx
leaq 0x8(,%rbx,8), %rdi
callq 0x3340
movq %rax, 0x20(%rsp)
movl $0x80000000, %edi       # imm = 0x80000000
callq 0x3340
movq %rax, 0x30(%rsp)
movl %ebp, 0x58(%rsp)
testl %ebp, %ebp
je 0x4015
movq 0x40(%rsp), %rax
shlq $0x1c, %rax
movq %rax, 0x68(%rsp)
leaq 0x1(%rbx), %rax
movq %rax, 0x78(%rsp)
movl %ebx, %eax
movq %rax, 0xa8(%rsp)
leaq 0x140(%rsp), %r15
xorl %ebp, %ebp
movq %r14, 0x70(%rsp)
movq %r13, 0x60(%rsp)
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movq 0x80(%rsp), %rdx
callq 0x3210
movl $0x40000000, %edx       # imm = 0x40000000
movq (%rsp), %rdi
xorl %esi, %esi
callq 0x3210
movq %r15, %rdi
leaq 0x114e9(%rip), %rsi     # 0x1507e
movq 0x88(%rsp), %rbx
movq %rbx, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
leaq 0x240(%rsp), %r12
movq %r12, %rdi
leaq 0x114d6(%rip), %rsi     # 0x15091
movq %rbx, %rdx
movl %ebp, 0x14(%rsp)
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
movq %r12, %rdi
leaq 0x11492(%rip), %rbx     # 0x15067
movq %rbx, %rsi
callq 0x33a0
movq %rax, 0x98(%rsp)
testq %rax, %rax
je 0x4061
movq %r15, %rdi
movq %rbx, %rsi
callq 0x33a0
testq %rax, %rax
je 0x4079
movl $0x8, %esi
movq 0x20(%rsp), %rdi
movq 0x78(%rsp), %rdx
movq %rax, 0x90(%rsp)
movq %rax, %rcx
callq 0x30c0
leaq 0x240(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x3380
movl %eax, %ebp
movl %eax, %r12d
shrl $0x1f, %r12d
callq 0x3060
movq %rax, %r14
movl (%rax), %edi
callq 0x3470
movl %r12d, %edi
leaq 0x11558(%rip), %rsi     # 0x151af
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x46f2
movl %ebp, %edi
leaq 0xb0(%rsp), %rsi
callq 0x34a0
movq 0xe0(%rsp), %rsi
xorl %edi, %edi
movq %rsi, 0xa0(%rsp)
movl $0x1, %edx
movl $0x2, %ecx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x3150
movq %rax, %r15
xorl %r12d, %r12d
cmpq $-0x1, %rax
sete %r12b
movq %r14, 0x8(%rsp)
movl (%r14), %edi
callq 0x3470
movl %r12d, %edi
leaq 0x11503(%rip), %rsi     # 0x151c2
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x46f2
movl %ebp, %edi
callq 0x3220
movq %r15, %rbx
cmpl $0x0, 0x5c(%rsp)
movq 0x40(%rsp), %rcx
movq (%rsp), %rsi
movq 0x30(%rsp), %r8
jle 0x3db8
movq 0x20(%rsp), %rax
movq (%rax), %r9
xorl %edx, %edx
movq %r9, %r12
movq 0x20(%rsp), %rax
movq %rdx, 0x28(%rsp)
movq 0x8(%rax,%rdx,8), %r9
cmpq %r9, %r12
jae 0x3da2
movq 0x28(%rsp), %rax
movzwl %ax, %r10d
shrl $0x10, %eax
movq 0x18(%rsp), %rdx
leaq (%rdx,%rax,4), %rax
movq %r9, 0x38(%rsp)
movl %r10d, 0x50(%rsp)
movq %rax, 0x48(%rsp)
movl (%rbx,%r12,4), %ebp
movl (%rsi,%rbp,4), %r13d
movq %r13, %r15
movq (%r8,%rbp,8), %r14
andq $0xf, %r15
jne 0x3d88
movl $0x48, %edi
callq 0x3340
movq 0x30(%rsp), %r8
movq %rax, (%r8,%rbp,8)
testq %rax, %rax
je 0x404c
movq %r14, 0x40(%rax)
movq %rax, %r14
movq 0x40(%rsp), %rcx
movq (%rsp), %rsi
movq 0x38(%rsp), %r9
movl 0x50(%rsp), %r10d
movq 0x48(%rsp), %rax
movl %r10d, (%r14,%r15,4)
incl %r13d
movl %r13d, (%rsi,%rbp,4)
imulq %rcx, %rbp
incl (%rax,%rbp,4)
incq %r12
cmpq %r12, %r9
jne 0x3d38
movq 0x28(%rsp), %rdx
incq %rdx
cmpq 0xa8(%rsp), %rdx
jne 0x3cf9
movq %rbx, %rdi
movq 0xa0(%rsp), %rsi
callq 0x3360
movq (%rsp), %rsi
movq 0x60(%rsp), %r13
movq (%r13), %rax
movl $0x1, %ecx
movl -0x4(%rsi,%rcx,4), %edx
addq %rdx, %rax
movq %rax, (%r13,%rcx,8)
incq %rcx
cmpq $0x10000000, %rcx       # imm = 0x10000000
jne 0x3dda
leaq 0x340(%rsp), %r14
movq %r14, %rdi
leaq 0x112b7(%rip), %rsi     # 0x150bb
movq 0x70(%rsp), %rbx
movq %rbx, %rdx
movl 0x14(%rsp), %ebp
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
movq %r14, %rdi
leaq 0x11247(%rip), %rsi     # 0x1506a
callq 0x33a0
testq %rax, %rax
je 0x4091
movq %rax, %r12
movl $0x8, %esi
movl $0x10000000, %edx       # imm = 0x10000000
movq %r13, %rdi
movq %rax, %rcx
callq 0x3450
movl $0x4, %esi
movq 0x18(%rsp), %rdi
movq 0x68(%rsp), %rdx
movq %r12, %rcx
callq 0x3450
movq %r12, %rdi
callq 0x3100
leaq 0x440(%rsp), %r14
movq %r14, %rdi
leaq 0x11251(%rip), %rsi     # 0x150cb
movq %rbx, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x3430
movq %r14, %rdi
movl $0x42, %esi
movl $0x180, %edx            # imm = 0x180
xorl %eax, %eax
callq 0x3380
cmpl $-0x1, %eax
je 0x40a9
movl %eax, %r12d
movq (%rsp), %rax
movl 0x3ffffffc(%rax), %ebx
addq 0x7ffffff8(%r13), %rbx
shlq $0x2, %rbx
movl %r12d, %edi
movq %rbx, %rsi
callq 0x31e0
cmpl $-0x1, %eax
je 0x40c6
xorl %edi, %edi
movq %rbx, 0x28(%rsp)
movq %rbx, %rsi
movl $0x2, %edx
movl $0x1, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x3150
movq %rax, %r14
movl %r12d, %edi
callq 0x3220
xorl %edi, %edi
movq (%rsp), %rdx
movq 0x30(%rsp), %rsi
movq %r14, 0x48(%rsp)
movl (%rdx,%rdi,4), %eax
testq %rax, %rax
je 0x3fab
movl %eax, %ecx
shrl $0x4, %ecx
xorl %r15d, %r15d
movl %eax, %r8d
andl $0xf, %r8d
movl %r8d, 0x38(%rsp)
setne %r15b
addl %ecx, %r15d
je 0x3fab
movq (%r13,%rdi,8), %rcx
leaq (%r14,%rcx,4), %rcx
leaq (%rcx,%rax,4), %r12
movl 0x38(%rsp), %ebx
movq %rdi, 0x50(%rsp)
movq (%rsi,%rdi,8), %r14
xorl %r13d, %r13d
movq %r14, %rbp
testl %r13d, %r13d
movl $0x10, %edx
cmovel %ebx, %edx
cmpl $0x0, 0x38(%rsp)
movl $0x10, %eax
cmovel %eax, %edx
shll $0x2, %edx
subq %rdx, %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x32f0
movq 0x40(%r14), %r14
movq %rbp, %rdi
callq 0x3040
incl %r13d
cmpl %r13d, %r15d
jne 0x3f4f
movq 0x30(%rsp), %rsi
movq 0x50(%rsp), %rdi
movq %r14, (%rsi,%rdi,8)
movq (%rsp), %rdx
movq 0x60(%rsp), %r13
movq 0x48(%rsp), %r14
incq %rdi
cmpq $0x10000000, %rdi       # imm = 0x10000000
jne 0x3f09
movq %r14, %rdi
movq 0x28(%rsp), %rbx
movq %rbx, %rsi
movl $0x1, %edx
callq 0x3120
testl %eax, %eax
js 0x40d6
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3360
movq 0x98(%rsp), %rdi
callq 0x3100
movq 0x90(%rsp), %rdi
callq 0x3100
movl 0x14(%rsp), %ebp
incl %ebp
cmpl 0x58(%rsp), %ebp
leaq 0x140(%rsp), %r15
jne 0x3b67
movq 0x18(%rsp), %rdi
callq 0x3040
movq (%rsp), %rdi
callq 0x3040
movq %r13, %rdi
callq 0x3040
movq 0x20(%rsp), %rdi
callq 0x3040
addq $0x548, %rsp            # imm = 0x548
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
movl (%rax), %edi
leaq 0x11044(%rip), %rsi     # 0x1509e
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0x12357(%rip), %rsi     # 0x163c6
leaq 0x240(%rsp), %rdx
jmp 0x40bf
callq 0x3060
movl (%rax), %edi
leaq 0x1233f(%rip), %rsi     # 0x163c6
leaq 0x140(%rsp), %rdx
jmp 0x40bf
movq 0x8(%rsp), %rax
movl (%rax), %edi
leaq 0x12327(%rip), %rsi     # 0x163c6
leaq 0x340(%rsp), %rdx
jmp 0x40bf
movq 0x8(%rsp), %rax
movl (%rax), %edi
leaq 0x1101e(%rip), %rsi     # 0x150d5
leaq 0x440(%rsp), %rdx
xorl %eax, %eax
callq 0x3330
movq 0x8(%rsp), %rax
movl (%rax), %edi
leaq 0x11018(%rip), %rsi     # 0x150ec
jmp 0x405a
movq 0x8(%rsp), %rax
movl (%rax), %edi
leaq 0x11026(%rip), %rsi     # 0x1510a
jmp 0x405a