// File: /workspace/github_c_makefile_star2_O1/yhg926[P]public_kssd/command_composite.c
// Function: abv_search
int abv_search (composite_opt_t *composite_opt){
 char* abv_fpath = malloc(PATHLEN);
 FILE *abvfh,*namefh,*abunMtx_idxfh,*abunMtx_fh,*y_l2n_fh;
 sprintf(abv_fpath,"%s/%s.%s",composite_opt->refdir,binVec_dirname,abunMtx_name_suffix);
 if(( namefh = fopen(abv_fpath,"r") ) == NULL) err(errno, "abv_search():%s",abv_fpath);
 int abv_fn = 0; int size = 100;
 char (*tmpfname)[PATHLEN] = malloc(size * PATHLEN);
 while( fgets(tmpfname[abv_fn],PATHLEN,namefh) != NULL) {
  tmpfname[abv_fn][strcspn(tmpfname[abv_fn], "\n")] = 0;
  if (abv_fn == size -1){
   size+=100;
    tmpfname = realloc( tmpfname, size * PATHLEN);
  }
  abv_fn++;
 }
 fclose(namefh);
 double *y_l2n = malloc(sizeof(double) * abv_fn);
 sprintf(abv_fpath,"%s/%s.%s",composite_opt->refdir,binVec_dirname,y_l2n_suffix);
 if((y_l2n_fh = fopen(abv_fpath,"rb") ) == NULL) err(errno, "abv_search():%s",abv_fpath);
 fread(y_l2n,sizeof(double),abv_fn,y_l2n_fh);
 fclose(y_l2n_fh);
 struct stat st;
 sprintf(abv_fpath,"%s/%s.%s",composite_opt->refdir,binVec_dirname,abunMtx_idx_suffix);
 stat(abv_fpath, &st);
 int num_otu = st.st_size / sizeof(int);
 int *abunMtx_idx= malloc(st.st_size);
 if((abunMtx_idxfh = fopen(abv_fpath,"rb") ) == NULL) err(errno, "abv_search():%s",abv_fpath);
 fread(abunMtx_idx,sizeof(int),num_otu,abunMtx_idxfh);
 fclose(abunMtx_idxfh);
  sprintf(abv_fpath,"%s/%s.%s",composite_opt->refdir,binVec_dirname,abunMtx_suffix);
  stat(abv_fpath, &st);
 binVec_t *abunMtx = malloc(st.st_size);
 if((abunMtx_fh = fopen(abv_fpath,"rb") ) == NULL) err(errno, "abv_search():%s",abv_fpath);
 fread(abunMtx,sizeof(binVec_t),st.st_size/sizeof(binVec_t),abunMtx_fh);
 fclose(abunMtx_fh);
 xny_pct_t *xny_pct ;
 int *abv_ids = malloc(sizeof(int) * abv_fn);
#define DFLT (-2)
 tmp_measure = malloc(sizeof(float) * abv_fn);
 if(composite_opt->s == 1) xny_pct = malloc(sizeof(xny_pct_t) *abv_fn);
 for(int i =0; i<composite_opt->num_remaining_args;i++){
  const char *ext = strrchr(composite_opt->remaining_args[i],'.');
  if(strcmp(ext + 1, binVec_suffix) != 0){
   printf("%dth argument %s is not a .abv file, skipped\n",i,composite_opt->remaining_args[i]);
   continue;
  }
  int n_abv_match = 0;
  if( strchr(composite_opt->remaining_args[i],'/') != NULL )
   abv_fpath = composite_opt->remaining_args[i];
  else sprintf(abv_fpath,"%s/%s/%s", composite_opt->refdir,binVec_dirname,composite_opt->remaining_args[i]);
  stat(abv_fpath, &st);
  int abv_len = st.st_size / sizeof(binVec_t);
  binVec_t *tmp_abv = malloc(sizeof(binVec_t)*(abv_len+1));
  if(( abvfh = fopen(abv_fpath,"rb") ) == NULL) err(errno, "abv_search():%s",abv_fpath);
  fread(tmp_abv,sizeof(binVec_t),abv_len,abvfh);
  for(int i = 0; i<abv_fn ; i++) tmp_measure[i] = DFLT;
  if(composite_opt->s == 1) memset(xny_pct,0, sizeof(xny_pct_t) *abv_fn );
  float xl2n = 0;
  for(int d = 0; d< abv_len; d++){
   int ref_idx = tmp_abv[d].ref_idx;
   xl2n += tmp_abv[d].pct*tmp_abv[d].pct;
   for(int j = ref_idx==0?0:abunMtx_idx[ref_idx-1]; j< abunMtx_idx[ref_idx]; j++){
    if(tmp_measure[abunMtx[j].ref_idx] == DFLT) {
     tmp_measure[abunMtx[j].ref_idx] = 0;
     abv_ids[n_abv_match] = abunMtx[j].ref_idx;
     n_abv_match++;
    }
    if(composite_opt->s == 1){
     tmp_measure[abunMtx[j].ref_idx] += (float)fabs(abunMtx[j].pct - tmp_abv[d].pct);
     xny_pct[abunMtx[j].ref_idx].x += tmp_abv[d].pct;
     xny_pct[abunMtx[j].ref_idx].y += abunMtx[j].pct;
    }
    else if (composite_opt->s == 2)
     tmp_measure[abunMtx[j].ref_idx] += (abunMtx[j].pct - tmp_abv[d].pct) * (abunMtx[j].pct - tmp_abv[d].pct);
    else
     tmp_measure[abunMtx[j].ref_idx] += abunMtx[j].pct * tmp_abv[d].pct;
   }
  }
  if(composite_opt->s == 0) {
   for(int n = 0; n < n_abv_match; n++)
    tmp_measure[abv_ids[n]] = tmp_measure[abv_ids[n]] / (sqrt(xl2n)*y_l2n[abv_ids[n]]) ;
  }
  printf("#Sample\t");
  if(composite_opt->s == 1) {
   for(int n = 0; n < n_abv_match; n++)
    tmp_measure[abv_ids[n]] += (2*100 - xny_pct[abv_ids[n]].x - xny_pct[abv_ids[n]].y);
   qsort(abv_ids,n_abv_match,sizeof(int),comparator_measure);
   printf("L1norm\n");
   for(int n = 0; n < n_abv_match; n++) printf("%s\t%lf\n",tmpfname[abv_ids[n]],tmp_measure[abv_ids[n]]);
  }
  else if(composite_opt->s == 2){
   qsort(abv_ids,n_abv_match,sizeof(int),comparator_measure);
   printf("L2norm\n");
   for(int n = 0; n < n_abv_match; n++) printf("%s\t%lf\n",tmpfname[abv_ids[n]],sqrt(tmp_measure[abv_ids[n]]));
  }
  else{
   qsort(abv_ids,n_abv_match,sizeof(int),comparator_measure);
   printf("CosineXY\n");
   for(int n = n_abv_match -1 ; n >= 0; n--) printf("%s\t%lf\n",tmpfname[abv_ids[n]],tmp_measure[abv_ids[n]]);
  }
  fclose(abvfh);
  free(tmp_abv);
 }
 free(tmp_measure);
 free(tmpfname);
 free(abunMtx);
 free(abunMtx_idx);
 free(y_l2n);
 if(composite_opt->s == 1) free(xny_pct);
 return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdi, %rbx
movl $0x100, %edi            # imm = 0x100
callq 0x3340
movq %rax, %r12
leaq 0x14(%rbx), %r15
leaq 0xf93a(%rip), %rsi      # 0x15377
leaq 0xf836(%rip), %rcx      # 0x1527a
leaq 0xf82a(%rip), %r8       # 0x15275
movq $0x0, 0x18(%rsp)
movq %rax, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x3430
leaq 0x11356(%rip), %rsi     # 0x16dbe
movq %r12, %rdi
callq 0x33a0
testq %rax, %rax
je 0x625f
movq %rax, %r14
movl $0x6400, %edi           # imm = 0x6400
callq 0x3340
movq %rax, %r13
movq %rax, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x3290
testq %rax, %rax
movq %rbx, 0x10(%rsp)
movq %r12, (%rsp)
movq %r15, 0x30(%rsp)
je 0x5b38
movq %r13, %rdi
movl $0x64, %r13d
movl $0x100, %ebp            # imm = 0x100
xorl %ebx, %ebx
movq %rdi, %r12
leaq 0x125e5(%rip), %rsi     # 0x180af
callq 0x3260
addq %r12, %rax
movb $0x0, -0x100(%rbp,%rax)
leal -0x1(%r13), %eax
cmpq %rax, %rbx
jne 0x5afb
addl $0x64, %r13d
movl %r13d, %eax
shll $0x8, %eax
movslq %eax, %rsi
movq %r12, %rdi
callq 0x3350
movq %rax, %r12
incq %rbx
leaq (%r12,%rbp), %r15
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x3290
movq %r15, %rdi
addq $0x100, %rbp            # imm = 0x100
testq %rax, %rax
jne 0x5ac3
movq %r12, 0x8(%rsp)
movl %ebx, %eax
movq %rax, 0x18(%rsp)
movq (%rsp), %r12
movq 0x30(%rsp), %r15
jmp 0x5b3f
movq %r13, 0x8(%rsp)
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x3100
leaq (,%rbx,8), %rdi
movq %rdi, 0x38(%rsp)
callq 0x3340
movq %rax, %rbp
leaq 0xf814(%rip), %rsi      # 0x15377
leaq 0xf710(%rip), %rcx      # 0x1527a
leaq 0xf717(%rip), %r8       # 0x15288
movq %r12, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x3430
leaq 0xf4e2(%rip), %rsi      # 0x15067
movq %r12, %rdi
callq 0x33a0
testq %rax, %rax
je 0x625f
movq %rax, %r14
movl $0x8, %esi
movq %rbp, 0x20(%rsp)
movq %rbp, %rdi
movq %rbx, %rdx
movq %rax, %rcx
callq 0x30c0
movq %r14, %rdi
callq 0x3100
leaq 0xf7b7(%rip), %rsi      # 0x15377
leaq 0xf6b3(%rip), %rcx      # 0x1527a
leaq 0xf6a2(%rip), %r8       # 0x15270
movq %r12, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x3430
leaq 0x78(%rsp), %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x32e0
movq 0x30(%r14), %r14
movq %r14, %rdi
callq 0x3340
movq %rax, %rbp
leaq 0xf466(%rip), %rsi      # 0x15067
movq %r12, %rdi
callq 0x33a0
testq %rax, %rax
je 0x625f
movq %r15, %r13
movq %rax, %r15
shrq $0x2, %r14
movslq %r14d, %rdx
movl $0x4, %esi
movq %rbp, 0x40(%rsp)
movq %rbp, %rdi
movq %rax, %rcx
callq 0x30c0
movq %r15, %rdi
callq 0x3100
leaq 0xf734(%rip), %rsi      # 0x15377
leaq 0xf630(%rip), %rcx      # 0x1527a
leaq 0xf61b(%rip), %r8       # 0x1526c
movq %r12, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x3430
leaq 0x78(%rsp), %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x32e0
movq 0x30(%r14), %r14
movq %r14, %rdi
callq 0x3340
movq %rax, %r15
leaq 0xf3e3(%rip), %rsi      # 0x15067
movq %r12, %rdi
callq 0x33a0
testq %rax, %rax
je 0x624b
movq %rax, %r12
shrq $0x3, %r14
movl $0x8, %esi
movq %r15, %rdi
movq %r14, %rdx
movq %rax, %rcx
callq 0x30c0
movq %r12, %rdi
callq 0x3100
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x3340
movq %rax, %r14
movq %rbx, %rdi
callq 0x3340
movq %rax, 0x187f3(%rip)     # 0x1e4c8
movq 0x10(%rsp), %r12
cmpl $0x1, 0x8(%r12)
jne 0x5cef
movq 0x38(%rsp), %rdi
callq 0x3340
movq %rax, %rbx
cmpl $0x0, 0x314(%r12)
jle 0x61f2
movl 0x18(%rsp), %eax
movq %rax, 0x50(%rsp)
xorl %r13d, %r13d
movq %r12, %rbp
movq 0x318(%r12), %rax
movq (%rax,%r13,8), %r12
movq %r12, %rdi
movl $0x2e, %esi
callq 0x31d0
leaq 0x1(%rax), %rdi
leaq 0xf537(%rip), %rsi      # 0x15268
callq 0x32b0
testl %eax, %eax
je 0x5d56
leaq 0xf5f3(%rip), %rdi      # 0x15334
movl %r13d, %esi
movq %r12, %rdx
xorl %eax, %eax
callq 0x3170
movq %rbp, %r12
jmp 0x61de
movq %r13, 0x60(%rsp)
movq %r12, %rdi
movl $0x2f, %esi
callq 0x3160
testq %rax, %rax
je 0x5d73
movq %r12, (%rsp)
jmp 0x5d94
movq (%rsp), %rdi
leaq 0xf612(%rip), %rsi      # 0x15390
movq 0x30(%rsp), %rdx
leaq 0xf4f0(%rip), %rcx      # 0x1527a
movq %r12, %r8
xorl %eax, %eax
callq 0x3430
movq (%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x32e0
movq 0xa8(%rsp), %rbp
shrq $0x3, %rbp
movq %rbp, %r13
shlq $0x20, %r13
movabsq $0x100000000, %rax      # imm = 0x100000000
leaq (%rax,%r13), %rdi
sarq $0x1d, %rdi
callq 0x3340
movq %rax, %r12
movq (%rsp), %rdi
leaq 0xf28d(%rip), %rsi      # 0x15067
callq 0x33a0
testq %rax, %rax
je 0x624b
sarq $0x20, %r13
movl $0x8, %esi
movq %r12, 0x48(%rsp)
movq %r12, %rdi
movq %r13, %rdx
movq %rax, 0x58(%rsp)
movq %rax, %rcx
callq 0x30c0
cmpl $0x0, 0x18(%rsp)
movq 0x50(%rsp), %rdx
je 0x5e2d
movq 0x186ac(%rip), %rax     # 0x1e4c8
xorl %ecx, %ecx
movl $0xc0000000, (%rax,%rcx,4) # imm = 0xC0000000
incq %rcx
cmpq %rcx, %rdx
jne 0x5e1e
movq 0x10(%rsp), %rax
movl 0x8(%rax), %r12d
cmpl $0x1, %r12d
jne 0x5e4b
movq %rbx, %rdi
xorl %esi, %esi
movq 0x38(%rsp), %rdx
callq 0x3210
testl %ebp, %ebp
movq 0x40(%rsp), %r8
movss 0xf3be(%rip), %xmm4     # 0x15218
movaps 0xf37f(%rip), %xmm5     # 0x151e0
jle 0x5f52
movq 0x1865a(%rip), %rax     # 0x1e4c8
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
xorps %xmm0, %xmm0
xorl %r13d, %r13d
movq 0x48(%rsp), %r9
movslq (%r9,%rcx,8), %rdx
movl $0x0, %esi
testq %rdx, %rdx
je 0x5e94
movl -0x4(%r8,%rdx,4), %esi
movss 0x4(%r9,%rcx,8), %xmm1
movslq (%r8,%rdx,4), %rdx
cmpl %edx, %esi
jge 0x5f35
movslq %esi, %rsi
movl (%r15,%rsi,8), %edi
movslq %edi, %rdi
movss (%rax,%rdi,4), %xmm2
ucomiss %xmm4, %xmm2
jne 0x5ece
jp 0x5ece
movl $0x0, (%rax,%rdi,4)
movslq %r13d, %r13
movl %edi, (%r14,%r13,4)
incl %r13d
movss 0x4(%r15,%rsi,8), %xmm2
cmpl $0x2, %r12d
je 0x5f11
cmpl $0x1, %r12d
jne 0x5f1b
movaps %xmm2, %xmm3
subss %xmm1, %xmm3
andps %xmm5, %xmm3
addss (%rax,%rdi,4), %xmm3
movss %xmm3, (%rax,%rdi,4)
movss (%rbx,%rdi,8), %xmm3
addss %xmm1, %xmm3
movss %xmm3, (%rbx,%rdi,8)
addss 0x4(%rbx,%rdi,8), %xmm2
movss %xmm2, 0x4(%rbx,%rdi,8)
jmp 0x5f29
subss %xmm1, %xmm2
mulss %xmm2, %xmm2
jmp 0x5f1f
mulss %xmm1, %xmm2
addss (%rax,%rdi,4), %xmm2
movss %xmm2, (%rax,%rdi,4)
incq %rsi
cmpq %rsi, %rdx
jne 0x5eaa
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
incq %rcx
cmpq %rbp, %rcx
jne 0x5e81
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
jmp 0x5f58
xorps %xmm1, %xmm1
xorl %r13d, %r13d
testl %r12d, %r12d
movq 0x20(%rsp), %rdx
jne 0x5ff6
testl %r13d, %r13d
jle 0x5ff6
movl %r13d, %r12d
xorps %xmm2, %xmm2
sqrtsd %xmm1, %xmm2
xorl %ebp, %ebp
movsd %xmm1, 0x70(%rsp)
movsd %xmm2, 0x68(%rsp)
movq 0x1853a(%rip), %rax     # 0x1e4c8
movslq (%r14,%rbp,4), %rcx
xorps %xmm3, %xmm3
cvtss2sd (%rax,%rcx,4), %xmm3
ucomisd 0xf266(%rip), %xmm1     # 0x15208
movapd %xmm2, %xmm0
jae 0x5fce
movapd %xmm1, %xmm0
movsd %xmm3, 0x28(%rsp)
callq 0x3460
movsd 0x28(%rsp), %xmm3
movsd 0x68(%rsp), %xmm2
movsd 0x70(%rsp), %xmm1
movq 0x20(%rsp), %rdx
movslq (%r14,%rbp,4), %rax
mulsd (%rdx,%rax,8), %xmm0
divsd %xmm0, %xmm3
xorps %xmm0, %xmm0
cvtsd2ss %xmm3, %xmm0
movq 0x184df(%rip), %rcx     # 0x1e4c8
movss %xmm0, (%rcx,%rax,4)
incq %rbp
cmpq %rbp, %r12
jne 0x5f87
leaq 0xf39c(%rip), %rdi      # 0x15399
xorl %eax, %eax
callq 0x3170
movq 0x10(%rsp), %rax
movl 0x8(%rax), %eax
cmpl $0x2, %eax
je 0x60d1
cmpl $0x1, %eax
jne 0x6152
testl %r13d, %r13d
movss 0xf1f3(%rip), %xmm1     # 0x1521c
jle 0x605b
movq 0x18496(%rip), %rax     # 0x1e4c8
movl %r13d, %ecx
xorl %edx, %edx
movslq (%r14,%rdx,4), %rsi
movaps %xmm1, %xmm0
subss (%rbx,%rsi,8), %xmm0
subss 0x4(%rbx,%rsi,8), %xmm0
addss (%rax,%rsi,4), %xmm0
movss %xmm0, (%rax,%rsi,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x6037
movslq %r13d, %rsi
movl $0x4, %edx
movq %r14, %rdi
leaq 0x37b(%rip), %rcx       # 0x63e8
callq 0x30b0
leaq 0xf670(%rip), %rdi      # 0x156e9
callq 0x30a0
testl %r13d, %r13d
movq 0x8(%rsp), %r12
leaq 0xf315(%rip), %rbp      # 0x153a2
jle 0x61c0
movl %r13d, %eax
movq %rax, 0x28(%rsp)
xorl %r13d, %r13d
movslq (%r14,%r13,4), %rsi
movq 0x1841f(%rip), %rax     # 0x1e4c8
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rsi,4), %xmm0
shlq $0x8, %rsi
addq %r12, %rsi
movq %rbp, %rdi
movb $0x1, %al
callq 0x3170
incq %r13
cmpq %r13, 0x28(%rsp)
jne 0x609e
jmp 0x61c0
movslq %r13d, %rsi
movl $0x4, %edx
movq %r14, %rdi
leaq 0x305(%rip), %rcx       # 0x63e8
callq 0x30b0
leaq 0xf5f3(%rip), %rdi      # 0x156e2
callq 0x30a0
testl %r13d, %r13d
jle 0x61c0
movl %r13d, %r12d
xorl %ebp, %ebp
movslq (%r14,%rbp,4), %r13
movq 0x183bb(%rip), %rax     # 0x1e4c8
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r13,4), %xmm0
ucomisd 0xf0ea(%rip), %xmm0     # 0x15208
jb 0x6126
sqrtsd %xmm0, %xmm0
jmp 0x612b
callq 0x3460
movq 0x8(%rsp), %rax
leaq 0xf26b(%rip), %rdi      # 0x153a2
shlq $0x8, %r13
addq %rax, %r13
movq %r13, %rsi
movb $0x1, %al
callq 0x3170
incq %rbp
cmpq %rbp, %r12
jne 0x6102
jmp 0x61c0
movslq %r13d, %rsi
movl $0x4, %edx
movq %r14, %rdi
leaq 0x284(%rip), %rcx       # 0x63e8
callq 0x30b0
leaq 0xf569(%rip), %rdi      # 0x156d9
callq 0x30a0
testl %r13d, %r13d
movq 0x8(%rsp), %rbp
leaq 0xf21e(%rip), %rcx      # 0x153a2
jle 0x61c0
movl %r13d, %r12d
incq %r12
movslq -0x8(%r14,%r12,4), %rsi
movq 0x18330(%rip), %rax     # 0x1e4c8
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rsi,4), %xmm0
shlq $0x8, %rsi
addq %rbp, %rsi
movq %rcx, %r13
movq %rcx, %rdi
movb $0x1, %al
callq 0x3170
movq %r13, %rcx
decq %r12
cmpq $0x1, %r12
jg 0x618c
movq 0x58(%rsp), %rdi
callq 0x3100
movq 0x48(%rsp), %rdi
callq 0x3040
movq 0x10(%rsp), %r12
movq 0x60(%rsp), %r13
incq %r13
movslq 0x314(%r12), %rax
cmpq %rax, %r13
jl 0x5d0a
movq 0x182cf(%rip), %rdi     # 0x1e4c8
callq 0x3040
movq 0x8(%rsp), %rdi
callq 0x3040
movq %r15, %rdi
callq 0x3040
movq 0x40(%rsp), %rdi
callq 0x3040
movq 0x20(%rsp), %rdi
callq 0x3040
cmpl $0x1, 0x8(%r12)
jne 0x6234
movq %rbx, %rdi
callq 0x3040
movl $0x1, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3060
movl (%rax), %edi
leaq 0xf127(%rip), %rsi      # 0x15380
movq (%rsp), %rdx
jmp 0x6270
callq 0x3060
movl (%rax), %edi
leaq 0xf113(%rip), %rsi      # 0x15380
movq %r12, %rdx
xorl %eax, %eax
callq 0x3330