// File: /workspace/github_c_makefile_star2_O1/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), %r11
movq (%rsi), %r15
movq (%rdi), %rax
movq %rax, -0x8(%rsp)
movl 0x14(%rdi), %ebx
movl 0x10(%rdi), %r10d
movl 0x1e1c4(%rip), %r14d    # 0x24200
movl %r10d, %eax
cltd
idivl %r14d
movl %eax, %r8d
movl 0x14(%rsi), %ebp
movl 0x10(%rsi), %r9d
movl %r9d, %eax
cltd
idivl %r14d
movl $0x2710, %edx           # imm = 0x2710
cmpl %r9d, %r10d
jne 0x6076
movl %ebx, %r10d
subl %ebp, %r10d
movl %r10d, %edx
negl %edx
cmovsl %r10d, %edx
cmpl %ebp, %ebx
jne 0x6076
movl %r9d, (%rcx)
xorl %edx, %edx
movl %r8d, %r13d
movl 0x1e180(%rip), %r9d     # 0x24200
subl %eax, %r13d
jge 0x60ce
movl %eax, %r14d
subl %r8d, %r14d
leal 0x1(,%r14,2), %r10d
movl $0x1, %r12d
testl %r14d, %r14d
jle 0x610e
movq %r15, -0x10(%rsp)
movq -0x8(%rsp), %r8
leaq (%r11,%r8,2), %r8
addl %r14d, %r14d
movl %r14d, %r13d
movl $0x1, %r14d
movzwl (%r8,%r14,2), %r15d
addl %r15d, %ebx
addq $0x2, %r14
cmpq %r13, %r14
jbe 0x60b8
movl %eax, %r8d
jmp 0x6107
leal 0x1(,%r13,2), %r12d
movl $0x1, %r10d
testl %r13d, %r13d
jle 0x6111
movq %r15, -0x10(%rsp)
leaq (%r11,%r15,2), %rax
addl %r13d, %r13d
movl %r13d, %r13d
movl $0x1, %r14d
movzwl (%rax,%r14,2), %r15d
addl %r15d, %ebp
addq $0x2, %r14
cmpq %r13, %r14
jbe 0x60f6
movq -0x10(%rsp), %r15
jmp 0x6111
movl %eax, %r8d
imull %r8d, %r9d
movl 0x18(%rdi), %r8d
movl 0x18(%rsi), %eax
cmpl %eax, %r8d
cmovll %r8d, %eax
addl 0x1e0d6(%rip), %r9d     # 0x24200
cmpl %eax, %r9d
jge 0x6190
movslq %r12d, %r14
movslq %r10d, %r8
addq %r8, %r8
movq -0x8(%rsp), %r10
leaq (%r8,%r10,2), %r8
addq %r14, %r14
leaq (%r14,%r15,2), %r10
movl %r9d, %r14d
movl %ebp, %r15d
movl %ebx, %ebp
movzwl (%r11,%r8), %ebx
addl %ebp, %ebx
movzwl (%r11,%r10), %ebp
addl %r15d, %ebp
movl %ebx, %r12d
subl %ebp, %r12d
movl %r12d, %r15d
negl %r15d
cmovsl %r12d, %r15d
cmpl %edx, %r15d
jg 0x6180
movl %r15d, %edx
cmpl %ebp, %ebx
jne 0x6180
movl %r14d, (%rcx)
xorl %edx, %edx
addl 0x1e079(%rip), %r14d    # 0x24200
addq $0x4, %r11
cmpl %eax, %r14d
jl 0x614b
movl 0x18(%rsi), %r8d
cmpl %r8d, 0x18(%rdi)
jne 0x61c1
movl 0x1c(%rdi), %edi
movl 0x1c(%rsi), %esi
movl %edi, %r11d
subl %esi, %r11d
movl %r11d, %r10d
negl %r10d
cmovsl %r11d, %r10d
cmpl %edx, %r10d
jg 0x61c1
movl %r10d, %edx
cmpl %esi, %edi
jne 0x61c1
movl %r8d, (%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