// File: /workspace/github_c_makefile_star2_O2/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 0x10(%rdx), %r14
movq (%rsi), %r12
movq (%rdi), %r15
movl 0x14(%rdi), %r11d
movl 0x10(%rdi), %r9d
movl 0x1d543(%rip), %ebp     # 0x22200
movl %r9d, %eax
cltd
idivl %ebp
movl %eax, %r8d
movl 0x14(%rsi), %ebx
movl 0x10(%rsi), %r10d
movl %r10d, %eax
cltd
idivl %ebp
movl $0x2710, %edx           # imm = 0x2710
cmpl %r10d, %r9d
jne 0x4cf6
movl %r11d, %r10d
subl %ebx, %r10d
movl %r10d, %edx
negl %edx
cmovsl %r10d, %edx
cmpl %ebx, %r11d
jne 0x4cf6
movl %r9d, (%rcx)
xorl %edx, %edx
movl %r8d, %r10d
movl 0x1d500(%rip), %r9d     # 0x22200
subl %eax, %r10d
movq %r14, -0x8(%rsp)
jge 0x4d38
leaq (%r14,%r15,2), %r13
movl %eax, %r10d
subl %r8d, %r10d
addl %r10d, %r10d
movslq %r10d, %r8
movabsq $0x1, %rbp
cmpq %r8, %rbp
jg 0x4d5f
movzwl (%r13,%rbp,2), %r14d
addl %r14d, %r11d
addq $0x2, %rbp
jmp 0x4d24
leaq (%r14,%r12,2), %rax
addl %r10d, %r10d
movslq %r10d, %r13
movabsq $0x1, %rbp
cmpq %r13, %rbp
jg 0x4d72
movzwl (%rax,%rbp,2), %r14d
addl %r14d, %ebx
addq $0x2, %rbp
jmp 0x4d4c
orl $0x1, %r10d
movslq %r10d, %r13
movabsq $0x1, %rbp
jmp 0x4d86
orl $0x1, %r10d
movslq %r10d, %rbp
movabsq $0x1, %r13
movl %r8d, %eax
imull %r9d, %eax
movl 0x18(%rdi), %r8d
movl 0x18(%rsi), %r10d
cmpl %r10d, %r8d
cmovll %r8d, %r10d
addq %r12, %r12
leaq (%r12,%rbp,2), %r12
movq -0x8(%rsp), %r8
addq %r8, %r12
addq %r15, %r15
leaq (%r15,%r13,2), %r15
addq %r8, %r15
xorl %r8d, %r8d
addl %r9d, %eax
cmpl %r10d, %eax
jge 0x4dfa
movzwl (%r15,%r8,4), %ebp
addl %ebp, %r11d
movzwl (%r12,%r8,4), %ebp
addl %ebp, %ebx
movl %r11d, %r14d
subl %ebx, %r14d
movl %r14d, %ebp
negl %ebp
cmovsl %r14d, %ebp
cmpl %edx, %ebp
jle 0x4de3
movl %edx, %ebp
jmp 0x4df3
cmpl %ebx, %r11d
jne 0x4df3
movl %eax, (%rcx)
xorl %ebp, %ebp
movl 0x1d40d(%rip), %r9d     # 0x22200
incq %r8
movl %ebp, %edx
jmp 0x4db5
movl 0x18(%rdi), %eax
cmpl 0x18(%rsi), %eax
jne 0x4e28
movl 0x1c(%rdi), %edi
movl 0x1c(%rsi), %esi
movl %edi, %r10d
subl %esi, %r10d
movl %r10d, %r9d
negl %r9d
cmovsl %r10d, %r9d
cmpl %edx, %r9d
jg 0x4e28
movl %r9d, %edx
cmpl %esi, %edi
jne 0x4e28
movl %eax, (%rcx)
xorl %edx, %edx
xorl %eax, %eax
cmpl $0x1, %r8d
sbbl %eax, %eax
orl %edx, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq