// File: /workspace/github_c_makefile_star2_O2/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, %r14
movl $0x100, %edi            # imm = 0x100
callq 0x3340
movq %rax, %rbx
leaq 0x14(%r14), %rdx
leaq 0xeb74(%rip), %rsi      # 0x14377
leaq 0xea70(%rip), %rcx      # 0x1427a
leaq 0xea64(%rip), %r8       # 0x14275
movq %rax, %rdi
movq %rdx, 0x30(%rsp)
xorl %eax, %eax
callq 0x3430
leaq 0x10597(%rip), %rsi     # 0x15dbe
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x33a0
testq %rax, %rax
je 0x5f7f
movq %rax, %rbx
movq %r14, 0x20(%rsp)
movl $0x6400, %edi           # imm = 0x6400
callq 0x3340
movq %rax, 0x8(%rsp)
pushq $0x64
popq %r13
xorl %ebp, %ebp
movq $0x0, 0x18(%rsp)
xorl %r14d, %r14d
movq $0x0, 0x28(%rsp)
movq 0x8(%rsp), %r15
leaq (%r15,%rbp), %r12
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdx
callq 0x3290
testq %rax, %rax
je 0x58e1
movq %r12, %rdi
leaq 0x117f8(%rip), %rsi     # 0x1708f
callq 0x3260
addq %r15, %rax
movb $0x0, (%rbp,%rax)
leal -0x1(%r13), %eax
cmpq %rax, 0x28(%rsp)
jne 0x58c9
addl $0x64, %r13d
movl %r13d, %eax
shll $0x8, %eax
movslq %eax, %rsi
movq %r15, %rdi
callq 0x3350
movq %rax, 0x8(%rsp)
incq 0x28(%rsp)
addq $0x4, %r14
addq $0x8, 0x18(%rsp)
addq $0x100, %rbp            # imm = 0x100
jmp 0x586f
movq %rbx, %rdi
callq 0x3100
movq 0x18(%rsp), %rdi
callq 0x3340
movq %rax, %r12
leaq 0xea7a(%rip), %rsi      # 0x14377
leaq 0xe976(%rip), %rcx      # 0x1427a
leaq 0xe97d(%rip), %r8       # 0x14288
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq 0x30(%rsp), %r15
movq %r15, %rdx
xorl %eax, %eax
callq 0x3430
leaq 0xe73e(%rip), %rsi      # 0x14067
movq %r13, %rdi
callq 0x33a0
testq %rax, %rax
je 0x5fac
movq %rax, %rbx
pushq $0x8
popq %rsi
movq %r12, 0x40(%rsp)
movq %r12, %rdi
movq 0x28(%rsp), %rdx
movq %rax, %rcx
callq 0x30c0
movq %rbx, %rdi
callq 0x3100
leaq 0xea13(%rip), %rsi      # 0x14377
leaq 0xe90f(%rip), %rcx      # 0x1427a
leaq 0xe8fe(%rip), %r8       # 0x14270
movq %r13, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x3430
leaq 0x78(%rsp), %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x32e0
movq 0x30(%rbx), %r15
movq %r15, %rdi
callq 0x3340
movq %rax, %rbp
leaq 0xe6c2(%rip), %rsi      # 0x14067
movq %r13, %rdi
callq 0x33a0
testq %rax, %rax
movq %r13, %rbx
je 0x5f99
movq %rax, %r12
shrq $0x2, %r15
movslq %r15d, %rdx
pushq $0x4
popq %rsi
movq %rbp, 0x38(%rsp)
movq %rbp, %rdi
movq %rax, %rcx
callq 0x30c0
movq %r12, %rdi
callq 0x3100
leaq 0xe992(%rip), %rsi      # 0x14377
leaq 0xe88e(%rip), %rcx      # 0x1427a
leaq 0xe879(%rip), %r8       # 0x1426c
movq %rbx, %rdi
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x3430
leaq 0x78(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x32e0
movq 0x30(%r15), %r15
movq %r15, %rdi
callq 0x3340
movq %rax, %r13
leaq 0xe63f(%rip), %rsi      # 0x14067
movq %rbx, %rdi
callq 0x33a0
testq %rax, %rax
je 0x5f99
movq %rax, %r12
shrq $0x3, %r15
pushq $0x8
popq %rsi
movq %r13, %rdi
movq %r15, %rdx
movq %rax, %rcx
callq 0x30c0
movq %r12, %rdi
callq 0x3100
movq %r14, %rdi
callq 0x3340
movq %rax, %r15
movq %r14, %rdi
callq 0x3340
movq %rax, 0x17a55(%rip)     # 0x1d4c8
movq 0x20(%rsp), %r12
cmpl $0x1, 0x8(%r12)
movq 0x8(%rsp), %rbp
jne 0x5a92
movq 0x18(%rsp), %rdi
callq 0x3340
movq %rax, %r14
xorl %ebx, %ebx
movslq 0x314(%r12), %rax
cmpq %rax, %rbx
jge 0x5f2a
movq 0x318(%r12), %rax
movq (%rax,%rbx,8), %r12
movq %r12, %rdi
pushq $0x2e
popq %rsi
callq 0x31d0
leaq 0x1(%rax), %rdi
leaq 0xe7a1(%rip), %rsi      # 0x14268
callq 0x32b0
testl %eax, %eax
je 0x5ae8
leaq 0xe85d(%rip), %rdi      # 0x14334
movl %ebx, %esi
movq %r12, %rdx
xorl %eax, %eax
callq 0x3170
jmp 0x5f1d
movq %rbx, 0x58(%rsp)
movq %r12, %rdi
pushq $0x2f
popq %rsi
callq 0x3160
testq %rax, %rax
je 0x5b02
movq %r12, %rbx
jmp 0x5b27
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
leaq 0xe87f(%rip), %rsi      # 0x14390
movq 0x30(%rsp), %rdx
leaq 0xe75d(%rip), %rcx      # 0x1427a
movq %r12, %r8
xorl %eax, %eax
callq 0x3430
movq %rbx, %rdi
leaq 0x78(%rsp), %rsi
callq 0x32e0
movq 0xa8(%rsp), %r12
shrq $0x3, %r12
movq %r12, %rbp
shlq $0x20, %rbp
movabsq $0x100000000, %rax      # imm = 0x100000000
leaq (%rax,%rbp), %rdi
sarq $0x1d, %rdi
callq 0x3340
movq %rbx, %rdi
movq %rax, %rbx
movq %rdi, 0x10(%rsp)
leaq 0xe4f7(%rip), %rsi      # 0x14067
callq 0x33a0
testq %rax, %rax
je 0x5f7f
sarq $0x20, %rbp
movq %rbx, 0x48(%rsp)
movq %rbx, %rdi
pushq $0x8
popq %rsi
movq %rbp, %rdx
movq %rax, 0x50(%rsp)
movq %rax, %rcx
callq 0x30c0
movq 0x17924(%rip), %rax     # 0x1d4c8
xorl %ecx, %ecx
movq 0x28(%rsp), %rdx
movq 0x38(%rsp), %r9
movaps 0xe629(%rip), %xmm6     # 0x141e0
cmpq %rcx, %rdx
je 0x5bc8
movl $0xc0000000, (%rax,%rcx,4) # imm = 0xC0000000
incq %rcx
jmp 0x5bb7
movq 0x20(%rsp), %rax
movl 0x8(%rax), %ebp
cmpl $0x1, %ebp
jne 0x5bf0
movq %r14, %rdi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
callq 0x3210
movaps 0xe5f5(%rip), %xmm6     # 0x141e0
movq 0x38(%rsp), %r9
xorl %eax, %eax
testl %r12d, %r12d
cmovlel %eax, %r12d
xorps %xmm0, %xmm0
xorl %ebx, %ebx
movss 0xe612(%rip), %xmm5     # 0x14218
movq 0x48(%rsp), %r10
cmpq %r12, %rax
je 0x5ccd
movslq (%r10,%rax,8), %rdx
testq %rdx, %rdx
je 0x5c24
movslq -0x4(%r9,%rdx,4), %rcx
jmp 0x5c28
xorl %edx, %edx
xorl %ecx, %ecx
movss 0x4(%r10,%rax,8), %xmm1
movaps %xmm1, %xmm2
mulss %xmm1, %xmm2
addss %xmm2, %xmm0
movslq (%r9,%rdx,4), %rdx
movq 0x17883(%rip), %rsi     # 0x1d4c8
cmpq %rdx, %rcx
jge 0x5cc5
movl (%r13,%rcx,8), %r8d
movslq %r8d, %rdi
movss (%rsi,%rdi,4), %xmm2
ucomiss %xmm5, %xmm2
jne 0x5c6a
jp 0x5c6a
movslq %ebx, %rbx
movl %r8d, (%r15,%rbx,4)
incl %ebx
xorps %xmm2, %xmm2
movss 0x4(%r13,%rcx,8), %xmm3
movaps %xmm3, %xmm4
subss %xmm1, %xmm4
cmpl $0x2, %ebp
je 0x5ca4
cmpl $0x1, %ebp
jne 0x5cb3
andps %xmm6, %xmm4
addss %xmm4, %xmm2
movss %xmm2, (%rsi,%rdi,4)
movsd (%r14,%rdi,8), %xmm2
movaps %xmm1, %xmm4
unpcklps %xmm3, %xmm4            # xmm4 = xmm4[0],xmm3[0],xmm4[1],xmm3[1]
addps %xmm2, %xmm4
movlps %xmm4, (%r14,%rdi,8)
jmp 0x5cc0
mulss %xmm4, %xmm4
addss %xmm2, %xmm4
movss %xmm4, (%rsi,%rdi,4)
jmp 0x5cc0
mulss %xmm1, %xmm3
addss %xmm2, %xmm3
movss %xmm3, (%rsi,%rdi,4)
incq %rcx
jmp 0x5c45
incq %rax
jmp 0x5c0b
testl %ebp, %ebp
movq 0x40(%rsp), %rdx
je 0x5d4d
leaq 0xe6bc(%rip), %rdi      # 0x14399
xorl %eax, %eax
callq 0x3170
movq 0x20(%rsp), %rax
movl 0x8(%rax), %eax
movslq %ebx, %rsi
cmpl $0x2, %eax
je 0x5e32
cmpl $0x1, %eax
movq 0x8(%rsp), %rbp
jne 0x5eb5
movq 0x177bb(%rip), %rax     # 0x1d4c8
testl %ebx, %ebx
movl $0x0, %ecx
cmovlel %ecx, %ebx
xorl %ecx, %ecx
movss 0xe4fb(%rip), %xmm1     # 0x1421c
cmpq %rcx, %rbx
je 0x5dde
movslq (%r15,%rcx,4), %rdx
movaps %xmm1, %xmm0
subss (%r14,%rdx,8), %xmm0
subss 0x4(%r14,%rdx,8), %xmm0
addss (%rax,%rdx,4), %xmm0
movss %xmm0, (%rax,%rdx,4)
incq %rcx
jmp 0x5d21
cvtss2sd %xmm0, %xmm1
testl %ebx, %ebx
movl $0x0, %r12d
cmovgl %ebx, %r12d
sqrtsd %xmm1, %xmm2
xorl %ebp, %ebp
movsd %xmm1, 0x68(%rsp)
movsd %xmm2, 0x60(%rsp)
cmpq %rbp, %r12
je 0x5cd6
movq 0x17749(%rip), %rax     # 0x1d4c8
movslq (%r15,%rbp,4), %rcx
cvtss2sd (%rax,%rcx,4), %xmm3
ucomisd 0xe478(%rip), %xmm1     # 0x14208
movapd %xmm2, %xmm0
jae 0x5dbc
movapd %xmm1, %xmm0
movsd %xmm3, 0x70(%rsp)
callq 0x3460
movsd 0x70(%rsp), %xmm3
movsd 0x60(%rsp), %xmm2
movsd 0x68(%rsp), %xmm1
movq 0x40(%rsp), %rdx
movslq (%r15,%rbp,4), %rax
mulsd (%rdx,%rax,8), %xmm0
divsd %xmm0, %xmm3
cvtsd2ss %xmm3, %xmm0
movq 0x176f4(%rip), %rcx     # 0x1d4c8
movss %xmm0, (%rcx,%rax,4)
incq %rbp
jmp 0x5d6f
movq %r15, %rdi
pushq $0x4
popq %rdx
leaq 0x312(%rip), %rcx       # 0x60fd
callq 0x30b0
leaq 0xe8f2(%rip), %rdi      # 0x146e9
callq 0x30a0
xorl %r12d, %r12d
cmpq %r12, %rbx
je 0x5f04
movslq (%r15,%r12,4), %rsi
movq 0x176b5(%rip), %rax     # 0x1d4c8
cvtss2sd (%rax,%rsi,4), %xmm0
shlq $0x8, %rsi
addq %rbp, %rsi
leaq 0xe57c(%rip), %rdi      # 0x143a2
movb $0x1, %al
callq 0x3170
incq %r12
jmp 0x5dff
movq %r15, %rdi
pushq $0x4
popq %rdx
leaq 0x2be(%rip), %rcx       # 0x60fd
callq 0x30b0
leaq 0xe897(%rip), %rdi      # 0x146e2
callq 0x30a0
testl %ebx, %ebx
movl $0x0, %eax
cmovlel %eax, %ebx
xorl %r12d, %r12d
movq 0x8(%rsp), %rbp
cmpq %r12, %rbx
je 0x5f04
movslq (%r15,%r12,4), %rbp
movq 0x17652(%rip), %rax     # 0x1d4c8
cvtss2sd (%rax,%rbp,4), %xmm0
ucomisd 0xe385(%rip), %xmm0     # 0x14208
jb 0x5e8b
sqrtsd %xmm0, %xmm0
jmp 0x5e90
callq 0x3460
shlq $0x8, %rbp
movq 0x8(%rsp), %rax
addq %rax, %rbp
leaq 0xe4ff(%rip), %rdi      # 0x143a2
movq %rbp, %rsi
movq %rax, %rbp
movb $0x1, %al
callq 0x3170
incq %r12
jmp 0x5e62
movq %r15, %rdi
pushq $0x4
popq %rdx
leaq 0x23b(%rip), %rcx       # 0x60fd
callq 0x30b0
leaq 0xe80b(%rip), %rdi      # 0x146d9
callq 0x30a0
movl %ebx, %ebx
testl %ebx, %ebx
jle 0x5f04
movslq -0x4(%r15,%rbx,4), %rsi
movq 0x175e3(%rip), %rax     # 0x1d4c8
cvtss2sd (%rax,%rsi,4), %xmm0
decq %rbx
shlq $0x8, %rsi
addq %rbp, %rsi
leaq 0xe4a7(%rip), %rdi      # 0x143a2
movb $0x1, %al
callq 0x3170
jmp 0x5ed5
movq 0x50(%rsp), %rdi
callq 0x3100
movq 0x48(%rsp), %rdi
callq 0x3040
movq 0x58(%rsp), %rbx
incq %rbx
movq 0x20(%rsp), %r12
jmp 0x5a94
movq 0x17597(%rip), %rdi     # 0x1d4c8
callq 0x3040
movq %rbp, %rdi
callq 0x3040
movq %r13, %rdi
callq 0x3040
movq 0x38(%rsp), %rdi
callq 0x3040
movq 0x40(%rsp), %rdi
callq 0x3040
cmpl $0x1, 0x8(%r12)
jne 0x5f6a
movq %r14, %rdi
callq 0x3040
pushq $0x1
popq %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3060
movl (%rax), %edi
leaq 0xe3f3(%rip), %rsi      # 0x14380
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x3330
callq 0x3060
movl (%rax), %edi
leaq 0xe3d9(%rip), %rsi      # 0x14380
movq %rbx, %rdx
jmp 0x5f92
callq 0x3060
movl (%rax), %edi
leaq 0xe3c6(%rip), %rsi      # 0x14380
movq %r13, %rdx
jmp 0x5f92