// File: jkingan[P]ladyheather/heathmsc.cpp
// Function: alloc_mtie_data(int, int)
int alloc_mtie_data(int id, int mtie_size)
{
int tau;
int i;

   // alloc mtie tau array, initialize it, and return number of bins
   if(id < 0) return (-10);
   if(id > MAX_MTIE_CHANS) return (-11);

   free_mtie_data(id); // free old info

   tau = 1;
   for(i=0; i<32; i++) {    // calculate powers of 2 table
      pow2[i] = tau;
      tau *= 2;
      mtie_intervals[id][i] = 0;   // init MTIE results table
      mtie[id][i] = (-BIG_NUM);  // init MTIE results table
      mtie0[id] = (-BIG_NUM);
      mtie_max[id] = (-BIG_NUM);
      mtie_min[id] = (BIG_NUM);
   }

   tau = 1;
   mtie_kmax = 0;
   while(1) { // calculate mtie bin count
      if(tau > mtie_size) break;
if(0 && debug_file) fprintf(debug_file, "MTIE_TAU %d: %d\n", mtie_kmax, tau);
      tau *= 2;
      ++mtie_kmax;
   }
   max_mtie_samples = tau/2;
if(0 && debug_file) fprintf(debug_file, "MAX_MTIE_SAMPLES: %d\n", max_mtie_samples);

   mtie_buf[id] = (double *) calloc(max_mtie_samples, sizeof(double));
   if(mtie_buf[id] == 0) {
      sprintf(out, "Could not allocate MTIE data buffer");
      error_exit(50, out);
      return 0;
   }

   if(mtie_kmax) {  // allocate mtie bins
      mtie_taus[id] = (struct MTIE_TAU *) calloc(mtie_kmax, sizeof(struct MTIE_TAU));
   }
   if(mtie_taus[id] == 0) {
      sprintf(out, "Could not allocate MTIE data arrays");
      error_exit(50, out);
      return 0;
   }

   tau = 1;
   i = 0;
   while(i < mtie_kmax) { // allocate and initialize mtie bins
//    mtie_taus[id][i].tau = tau;
if(0 && debug_file) fprintf(debug_file, "MTIE_TAU[%d] %d\n", i, tau);
//    mtie_taus[id][i].val = 0.0;
      mtie_taus[id][i].vals = (struct MTIE_VALS *) calloc(mtie_kmax, sizeof(struct MTIE_VALS));
      if(mtie_taus[id][i].vals == 0) {
         sprintf(out, "Could not allocate MTIE value arrays");
         error_exit(50, out);
         return -1;
      }

      mtie_taus[id][i].vals->min_vals = (double *) calloc(max_mtie_samples, sizeof(double));
      if(mtie_taus[id][i].vals->min_vals == 0) {
         sprintf(out, "Could not allocate MTIE min value arrays");
         error_exit(50, out);
         return (-2);
      }

      mtie_taus[id][i].vals->max_vals = (double *) calloc(max_mtie_samples, sizeof(double));
      if(mtie_taus[id][i].vals->max_vals == 0) {
         sprintf(out, "Could not allocate MTIE max value arrays");
         error_exit(50, out);
         return (-2);
      }

      tau *= 2;
      ++i;
   }

   return mtie_kmax;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
cmpl $0x0, -0x8(%rbp)
jge 0x15dc50
movl $0xfffffff6, -0x4(%rbp) # imm = 0xFFFFFFF6
jmp 0x15dfff
cmpl $0x4, -0x8(%rbp)
jle 0x15dc62
movl $0xfffffff5, -0x4(%rbp) # imm = 0xFFFFFFF5
jmp 0x15dfff
movl -0x8(%rbp), %edi
callq 0x15da50
movl $0x1, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
cmpl $0x20, -0x14(%rbp)
jge 0x15dd37
movl -0x10(%rbp), %edx
movslq -0x14(%rbp), %rcx
leaq 0x355810(%rip), %rax    # 0x4b34a0
movl %edx, (%rax,%rcx,4)
movl -0x10(%rbp), %eax
shll %eax
movl %eax, -0x10(%rbp)
movslq -0x8(%rbp), %rcx
leaq 0x35666a(%rip), %rax    # 0x4b4310
imulq $0x194, %rcx, %rcx      # imm = 0x194
addq %rcx, %rax
movslq -0x14(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movslq -0x8(%rbp), %rcx
leaq 0x35597a(%rip), %rax    # 0x4b3640
imulq $0x328, %rcx, %rcx      # imm = 0x328
addq %rcx, %rax
movslq -0x14(%rbp), %rcx
movsd 0x11384(%rip), %xmm0    # 0x16f060
movsd %xmm0, (%rax,%rcx,8)
movslq -0x8(%rbp), %rcx
leaq 0x356cb4(%rip), %rax    # 0x4b49a0
movsd 0x1136c(%rip), %xmm0    # 0x16f060
movsd %xmm0, (%rax,%rcx,8)
movslq -0x8(%rbp), %rcx
leaq 0x356c5c(%rip), %rax    # 0x4b4960
movsd 0x11354(%rip), %xmm0    # 0x16f060
movsd %xmm0, (%rax,%rcx,8)
movslq -0x8(%rbp), %rcx
leaq 0x356c64(%rip), %rax    # 0x4b4980
movsd 0x11334(%rip), %xmm0    # 0x16f058
movsd %xmm0, (%rax,%rcx,8)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x15dc78
movl $0x1, -0x10(%rbp)
movl $0x0, 0x356c9c(%rip)    # 0x4b49e4
movl -0x10(%rbp), %eax
cmpl -0xc(%rbp), %eax
jle 0x15dd52
jmp 0x15dd6b
movl -0x10(%rbp), %eax
shll %eax
movl %eax, -0x10(%rbp)
movl 0x356c84(%rip), %eax    # 0x4b49e4
addl $0x1, %eax
movl %eax, 0x356c7b(%rip)    # 0x4b49e4
jmp 0x15dd48
movl -0x10(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0x356c64(%rip)    # 0x4b49e0
movslq 0x356c5d(%rip), %rdi    # 0x4b49e0
movl $0x8, %esi
callq 0x9230
movq %rax, %rdx
movslq -0x8(%rbp), %rcx
leaq 0x356545(%rip), %rax    # 0x4b42e0
movq %rdx, (%rax,%rcx,8)
movslq -0x8(%rbp), %rcx
leaq 0x356536(%rip), %rax    # 0x4b42e0
cmpq $0x0, (%rax,%rcx,8)
jne 0x15dde3
leaq 0x23d648(%rip), %rdi    # 0x39b400
leaq 0x3a743(%rip), %rsi     # 0x198502
movb $0x0, %al
callq 0x9060
movl $0x32, %edi
leaq 0x23d62e(%rip), %rsi    # 0x39b400
callq 0xc050
movl $0x0, -0x4(%rbp)
jmp 0x15dfff
cmpl $0x0, 0x356bfa(%rip)    # 0x4b49e4
je 0x15de0f
movslq 0x356bf1(%rip), %rdi    # 0x4b49e4
movl $0x8, %esi
callq 0x9230
movq %rax, %rdx
movslq -0x8(%rbp), %rcx
leaq 0x356bb5(%rip), %rax    # 0x4b49c0
movq %rdx, (%rax,%rcx,8)
movslq -0x8(%rbp), %rcx
leaq 0x356ba6(%rip), %rax    # 0x4b49c0
cmpq $0x0, (%rax,%rcx,8)
jne 0x15de53
leaq 0x23d5d8(%rip), %rdi    # 0x39b400
leaq 0x3a6f7(%rip), %rsi     # 0x198526
movb $0x0, %al
callq 0x9060
movl $0x32, %edi
leaq 0x23d5be(%rip), %rsi    # 0x39b400
callq 0xc050
movl $0x0, -0x4(%rbp)
jmp 0x15dfff
movl $0x1, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl 0x356b7a(%rip), %eax    # 0x4b49e4
jge 0x15dff6
movslq 0x356b6d(%rip), %rdi    # 0x4b49e4
movl $0x10, %esi
callq 0x9230
movq %rax, %rdx
movslq -0x8(%rbp), %rcx
leaq 0x356b31(%rip), %rax    # 0x4b49c0
movq (%rax,%rcx,8), %rax
movslq -0x14(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movslq -0x8(%rbp), %rcx
leaq 0x356b1a(%rip), %rax    # 0x4b49c0
movq (%rax,%rcx,8), %rax
movslq -0x14(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x15dee7
leaq 0x23d544(%rip), %rdi    # 0x39b400
leaq 0x3a687(%rip), %rsi     # 0x19854a
movb $0x0, %al
callq 0x9060
movl $0x32, %edi
leaq 0x23d52a(%rip), %rsi    # 0x39b400
callq 0xc050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x15dfff
movslq 0x356af2(%rip), %rdi    # 0x4b49e0
movl $0x8, %esi
callq 0x9230
movq %rax, %rcx
movslq -0x8(%rbp), %rdx
leaq 0x356aba(%rip), %rax    # 0x4b49c0
movq (%rax,%rdx,8), %rax
movslq -0x14(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, (%rax)
movslq -0x8(%rbp), %rcx
leaq 0x356aa0(%rip), %rax    # 0x4b49c0
movq (%rax,%rcx,8), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq $0x0, (%rax)
jne 0x15df64
leaq 0x23d4c7(%rip), %rdi    # 0x39b400
leaq 0x3a62f(%rip), %rsi     # 0x19856f
movb $0x0, %al
callq 0x9060
movl $0x32, %edi
leaq 0x23d4ad(%rip), %rsi    # 0x39b400
callq 0xc050
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x15dfff
movslq 0x356a75(%rip), %rdi    # 0x4b49e0
movl $0x8, %esi
callq 0x9230
movq %rax, %rcx
movslq -0x8(%rbp), %rdx
leaq 0x356a3d(%rip), %rax    # 0x4b49c0
movq (%rax,%rdx,8), %rax
movslq -0x14(%rbp), %rdx
movq (%rax,%rdx,8), %rax
movq %rcx, 0x8(%rax)
movslq -0x8(%rbp), %rcx
leaq 0x356a22(%rip), %rax    # 0x4b49c0
movq (%rax,%rcx,8), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq $0x0, 0x8(%rax)
jne 0x15dfe0
leaq 0x23d448(%rip), %rdi    # 0x39b400
leaq 0x3a5d9(%rip), %rsi     # 0x198598
movb $0x0, %al
callq 0x9060
movl $0x32, %edi
leaq 0x23d42e(%rip), %rsi    # 0x39b400
callq 0xc050
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x15dfff
movl -0x10(%rbp), %eax
shll %eax
movl %eax, -0x10(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x15de61
movl 0x3569e8(%rip), %eax    # 0x4b49e4
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax,%rax)