// File: /workspace/github_c_makefile_star2_O3/thegenemyers[P]DAMASKER/tandem.c
// Function: Entwine
static int Entwine(Path *jpath, Path *kpath, Trace_Buffer *tbuf, int *where)
{ int   ac, b2, y2, ae;
  int   i, j, k;
  int   num, den, min;
#ifdef SEE_ENTWINE
  int   strt, iflare, oflare;
#endif

  uint16 *ktrace = tbuf->trace + (uint64) (kpath->trace);
  uint16 *jtrace = tbuf->trace + (uint64) (jpath->trace);

  min   = 10000;
  num   = 0;
  den   = 0;

#ifdef SEE_ENTWINE
  strt = 1;
  printf("\n");
#endif

  y2 = jpath->bbpos;
  j  = jpath->abpos/MR_tspace;

  b2 = kpath->bbpos;
  k  = kpath->abpos/MR_tspace;

  if (jpath->abpos == kpath->abpos)
    { min = abs(y2-b2);
      if (min == 0)
        *where = kpath->abpos;
    }

  if (j < k)
    { ac = k*MR_tspace;

      j = 1 + 2*(k-j);
      k = 1;

      for (i = 1; i < j; i += 2)
        y2 += jtrace[i];
    }
  else
    { ac = j*MR_tspace;

      k = 1 + 2*(j-k);
      j = 1;

      for (i = 1; i < k; i += 2)
        b2 += ktrace[i];
    }

  ae = jpath->aepos;
  if (ae > kpath->aepos)
    ae = kpath->aepos;

  while (1)
    { ac += MR_tspace;
      if (ac >= ae)
        break;
      y2 += jtrace[j];
      b2 += ktrace[k];
      j += 2;
      k += 2;

#ifdef SEE_ENTWINE
      printf("   @ %5d : %5d %5d = %4d\n",ac,y2,b2,abs(b2-y2));
#endif

      i = abs(y2-b2);
      if (i <= min)
        { min = i;
          if (i == 0)
            *where = ac;
        }
      num += i;
      den += 1;
#ifdef SEE_ENTWINE
      if (strt)
        { strt   = 0;
          iflare = i;
        }
      oflare = i;
#endif
    }

  if (jpath->aepos == kpath->aepos)
    { i = abs(jpath->bepos-kpath->bepos);
      if (i <= min)
        { min = i;
          if (i == 0)
            *where = kpath->aepos;
        }
    }

#ifdef SEE_ENTWINE
  if (den == 0)
    printf("Nothing\n");
  else
    printf("MINIM = %d AVERAGE = %d  IFLARE = %d  OFLARE = %d\n",min,num/den,iflare,oflare);
#endif

  if (den == 0)
    return (-1);
  else
    return (min);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, %r8
movq (%rsi), %r15
movq (%rdi), %r13
movl 0x14(%rdi), %ebx
movl 0x10(%rdi), %r10d
movl 0x1de51(%rip), %r14d    # 0x24200
movl %r10d, %eax
cltd
idivl %r14d
movl %eax, %r9d
movl 0x14(%rsi), %ebp
movl 0x10(%rsi), %r11d
movl %r11d, %eax
cltd
idivl %r14d
movl $0x2710, %edx           # imm = 0x2710
cmpl %r11d, %r10d
jne 0x63e9
movl %ebx, %r11d
subl %ebp, %r11d
movl %r11d, %edx
negl %edx
cmovsl %r11d, %edx
cmpl %ebp, %ebx
jne 0x63e9
movl %r10d, (%rcx)
xorl %edx, %edx
movl %r9d, %r14d
movl 0x1de0d(%rip), %r10d    # 0x24200
subl %eax, %r14d
movq %r13, -0x8(%rsp)
jge 0x6441
movl %eax, %r14d
subl %r9d, %r14d
leal 0x1(,%r14,2), %r11d
movl $0x1, %r12d
testl %r14d, %r14d
jle 0x6481
movq %r15, -0x10(%rsp)
leaq (%r8,%r13,2), %r9
addl %r14d, %r14d
movl %r14d, %r14d
movl $0x1, %r13d
movzwl (%r9,%r13,2), %r15d
addl %r15d, %ebx
addq $0x2, %r13
cmpq %r14, %r13
jbe 0x642b
movl %eax, %r9d
jmp 0x647a
leal 0x1(,%r14,2), %r12d
movl $0x1, %r11d
testl %r14d, %r14d
jle 0x6484
movq %r15, -0x10(%rsp)
leaq (%r8,%r15,2), %rax
addl %r14d, %r14d
movl %r14d, %r14d
movl $0x1, %r13d
movzwl (%rax,%r13,2), %r15d
addl %r15d, %ebp
addq $0x2, %r13
cmpq %r14, %r13
jbe 0x6469
movq -0x10(%rsp), %r15
jmp 0x6484
movl %eax, %r9d
movl 0x18(%rdi), %r14d
movl 0x18(%rsi), %r13d
cmpl %r13d, %r14d
movl %r13d, %eax
cmovll %r14d, %eax
incl %r9d
imull %r10d, %r9d
cmpl %eax, %r9d
jge 0x650e
movslq %r12d, %r14
movslq %r11d, %r11
addq %r11, %r11
movq -0x8(%rsp), %r12
leaq (%r11,%r12,2), %r11
addq %r14, %r14
leaq (%r14,%r15,2), %r14
movl %r9d, %r15d
movl %ebp, %r12d
movl %ebx, %ebp
movzwl (%r8,%r11), %ebx
addl %ebp, %ebx
movzwl (%r8,%r14), %ebp
addl %r12d, %ebp
movl %ebx, %r13d
subl %ebp, %r13d
movl %r13d, %r12d
negl %r12d
cmovsl %r13d, %r12d
cmpl %edx, %r12d
jg 0x64fa
movl %r12d, %edx
cmpl %ebp, %ebx
jne 0x64fa
movl %r15d, (%rcx)
xorl %edx, %edx
movl 0x1dd06(%rip), %r10d    # 0x24200
addl %r10d, %r15d
addq $0x4, %r8
cmpl %eax, %r15d
jl 0x64be
movl 0x18(%rdi), %r14d
movl 0x18(%rsi), %r13d
cmpl %r13d, %r14d
jne 0x653a
movl 0x1c(%rdi), %edi
movl 0x1c(%rsi), %esi
movl %edi, %r10d
subl %esi, %r10d
movl %r10d, %r8d
negl %r8d
cmovsl %r10d, %r8d
cmpl %edx, %r8d
jg 0x653a
movl %r8d, %edx
cmpl %esi, %edi
jne 0x653a
movl %r13d, (%rcx)
xorl %edx, %edx
cmpl %eax, %r9d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovll %edx, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq