// File: /workspace/github_cpp_makefile_star2_O1/srajangarg[P]image-morph/src/morph.cpp
// Function: distortImage(Image const&, std::vector<LineSegment, std::allocator<LineSegment>> const&, std::vector<LineSegment, std::allocator<LineSegment>> const&, double, double, double, double)
Image
distortImage(Image const & image,
             std::vector<LineSegment> const & seg_start,
             std::vector<LineSegment> const & seg_end,
             double t,
             double a, double b, double p)
{
  assert(seg_start.size() == seg_end.size());

  std::cout << "Distorting image..." << std::endl;

  int w = image.width();
  int h = image.height();
  int n = image.numChannels();
  
  Image result(w, h, n);
  Vec2 interpolated, dis, dissum, curr;
  LineSegment start_ln, end_ln;

  unsigned char sample[4];
  unsigned char* pix;
  double u, v, wt, wtsum;

  for (int row = 0; row < h; ++row)
  {
    for (int col = 0; col < w; ++col)
    {
      wtsum = 0;
      dissum = Vec2(0, 0);
      curr = Vec2(col, row);

      for (unsigned int i = 0; i < seg_start.size(); ++i)
      { 
        // src line
        start_ln = seg_start[i];
        // final line
        end_ln = start_ln.lerp(seg_end[i], t);

        u = end_ln.lineParameter(curr);
        v = end_ln.signedLineDistance(curr);

        // point interpolated wrt to the src line
        interpolated = start_ln.start() + u * (start_ln.direction())
                     + v * (start_ln.perp() / start_ln.length());

        // displacement vector from the line
        dis = (interpolated - curr);
        // weight of this displacement
        wt = pow(pow(start_ln.length(), p)/(a + start_ln.segmentDistance(curr, u, v)), b);
        dissum += dis * wt;
        wtsum += wt;
      }

      // weighted average
      interpolated = curr + (dissum/wtsum);
      sampleBilinear(image, interpolated, sample);

      // fill in the interpolated color
      pix = result.pixel(row, col);
      for (int channel = 0; channel < n; ++channel)
        pix[channel] = sample[channel];
    }
  }

  return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movsd %xmm3, 0xb0(%rsp)
movsd %xmm2, 0xa8(%rsp)
movsd %xmm1, 0xa0(%rsp)
movsd %xmm0, 0x78(%rsp)
movq %rcx, %rbx
movq 0x8(%rdx), %rax
subq (%rdx), %rax
movq 0x8(%rcx), %rcx
subq (%rbx), %rcx
cmpq %rcx, %rax
jne 0x3422
movq %rdx, %r14
movq %rsi, %rbp
movq %rdi, %r15
movq 0x191d5(%rip), %r13     # 0x1bfd0
leaq 0x13395(%rip), %rsi     # 0x16197
movl $0x13, %edx
movq %r13, %rdi
callq 0x2240
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2150
movl (%rbp), %esi
movl 0x4(%rbp), %r12d
movl 0x8(%rbp), %r13d
movq %r15, 0x70(%rsp)
movq %r15, %rdi
movl %esi, 0x4c(%rsp)
movl %r12d, %edx
movl %r13d, %ecx
callq 0x25b4
movl %r12d, 0x5c(%rsp)
testl %r12d, %r12d
jle 0x340b
movsd 0x13249(%rip), %xmm0    # 0x160b8
subsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movl $0x0, 0xc(%rsp)
leaq 0xf4(%rsp), %r15
xorpd %xmm1, %xmm1
movq %r13, 0x88(%rsp)
cmpl $0x0, 0x4c(%rsp)
jle 0x33ef
xorl %r12d, %r12d
xorpd %xmm0, %xmm0
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, 0x18(%rsp)
movq (%r14), %rcx
xorpd %xmm2, %xmm2
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
cmpq %rcx, 0x8(%r14)
je 0x3360
movq %rbp, %r13
xorl %eax, %eax
xorl %ebp, %ebp
movsd %xmm4, 0xc8(%rsp)
movsd %xmm3, 0xd0(%rsp)
movsd %xmm2, 0xd8(%rsp)
movl %ebp, %edx
shlq $0x5, %rdx
addq %rcx, %rdx
movl $0x8, %ecx
movsd -0x8(%rdx,%rcx), %xmm0
movsd %xmm0, 0xf0(%rsp,%rcx)
movsd (%rdx,%rcx), %xmm0
movsd %xmm0, 0xf8(%rsp,%rcx)
addq $0x10, %rcx
cmpq $0x28, %rcx
jne 0x2f01
movq (%rbx), %rcx
shlq $0x5, %rax
movsd 0xf8(%rsp), %xmm1
movsd 0x100(%rsp), %xmm2
movsd 0x98(%rsp), %xmm3
movapd %xmm3, %xmm0
movsd %xmm1, 0x28(%rsp)
mulsd %xmm1, %xmm0
movapd %xmm3, %xmm1
movsd %xmm2, 0x30(%rsp)
mulsd %xmm2, %xmm1
movsd (%rcx,%rax), %xmm2
movsd 0x78(%rsp), %xmm5
mulsd %xmm5, %xmm2
addsd %xmm0, %xmm2
movsd 0x8(%rcx,%rax), %xmm0
mulsd %xmm5, %xmm0
addsd %xmm1, %xmm0
movsd 0x108(%rsp), %xmm6
movapd %xmm3, %xmm1
mulsd %xmm6, %xmm1
movsd 0x110(%rsp), %xmm4
movapd %xmm4, 0x60(%rsp)
mulsd %xmm4, %xmm3
movsd 0x10(%rcx,%rax), %xmm4
mulsd %xmm5, %xmm4
addsd %xmm1, %xmm4
movsd 0x18(%rcx,%rax), %xmm1
mulsd %xmm5, %xmm1
addsd %xmm3, %xmm1
movsd %xmm2, 0x118(%rsp)
movsd %xmm0, 0x120(%rsp)
movsd %xmm4, 0x128(%rsp)
movsd %xmm1, 0x130(%rsp)
movl $0x8, %eax
movupd 0x110(%rsp,%rax), %xmm0
movupd %xmm0, 0x130(%rsp,%rax)
addq $0x10, %rax
cmpq $0x28, %rax
jne 0x2ff0
movsd 0x148(%rsp), %xmm1
movsd 0x138(%rsp), %xmm0
movsd 0x140(%rsp), %xmm2
movsd %xmm0, 0xb8(%rsp)
subsd %xmm0, %xmm1
movsd 0x150(%rsp), %xmm0
movsd %xmm2, 0xc0(%rsp)
subsd %xmm2, %xmm0
movsd %xmm0, 0x38(%rsp)
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
movsd %xmm1, (%rsp)
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
addsd %xmm2, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
jb 0x307f
sqrtsd %xmm0, %xmm0
jmp 0x3094
movsd %xmm6, 0x20(%rsp)
callq 0x2190
movsd 0x20(%rsp), %xmm6
xorpd %xmm1, %xmm1
movsd %xmm0, 0x80(%rsp)
subsd 0x28(%rsp), %xmm6
movapd 0x60(%rsp), %xmm0
subsd 0x30(%rsp), %xmm0
movapd %xmm0, 0x60(%rsp)
mulsd %xmm0, %xmm0
movsd %xmm6, 0x20(%rsp)
mulsd %xmm6, %xmm6
addsd %xmm0, %xmm6
xorps %xmm5, %xmm5
sqrtsd %xmm6, %xmm5
ucomisd %xmm1, %xmm6
movapd %xmm5, %xmm0
jae 0x3103
movapd %xmm6, %xmm0
movsd %xmm5, 0x50(%rsp)
movsd %xmm6, 0x90(%rsp)
callq 0x2190
movsd 0x90(%rsp), %xmm6
movsd 0x50(%rsp), %xmm5
xorpd %xmm1, %xmm1
movsd %xmm0, 0x50(%rsp)
ucomisd %xmm1, %xmm6
jae 0x311c
movapd %xmm6, %xmm0
callq 0x2190
movapd %xmm0, %xmm5
movsd 0x18(%rsp), %xmm0
subsd 0xb8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm2
subsd 0xc0(%rsp), %xmm2
movsd 0x38(%rsp), %xmm4
movapd %xmm4, %xmm1
mulsd %xmm2, %xmm1
movsd (%rsp), %xmm3
movapd %xmm3, %xmm6
mulsd %xmm0, %xmm6
addsd %xmm1, %xmm6
divsd 0x40(%rsp), %xmm6
movsd %xmm6, 0x40(%rsp)
mulsd %xmm3, %xmm2
mulsd %xmm4, %xmm0
subsd %xmm0, %xmm2
divsd 0x80(%rsp), %xmm2
movsd %xmm2, (%rsp)
movapd %xmm5, %xmm0
movsd 0xb0(%rsp), %xmm1
callq 0x23a0
movsd 0x40(%rsp), %xmm7
movapd %xmm0, %xmm6
xorpd %xmm2, %xmm2
ucomisd %xmm7, %xmm2
jbe 0x31d5
movsd 0x18(%rsp), %xmm0
subsd 0x28(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
subsd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
jb 0x3215
sqrtsd %xmm0, %xmm0
jmp 0x322c
ucomisd 0x12edb(%rip), %xmm7    # 0x160b8
movsd 0x20(%rsp), %xmm4
jbe 0x3239
movsd 0x18(%rsp), %xmm0
subsd 0x28(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
subsd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
jb 0x3215
sqrtsd %xmm0, %xmm0
jmp 0x3232
movsd %xmm6, 0x38(%rsp)
callq 0x2190
movsd 0x40(%rsp), %xmm7
movsd 0x38(%rsp), %xmm6
movsd 0x20(%rsp), %xmm4
movsd (%rsp), %xmm5
jmp 0x3250
movsd (%rsp), %xmm5
cvttsd2si %xmm5, %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movapd %xmm4, %xmm1
mulsd %xmm7, %xmm1
movapd 0x60(%rsp), %xmm8
mulsd %xmm8, %xmm7
movsd 0x28(%rsp), %xmm3
addsd %xmm1, %xmm3
movsd 0x30(%rsp), %xmm2
addsd %xmm7, %xmm2
xorpd 0x12e4f(%rip), %xmm8    # 0x160d0
movsd 0x12e2f(%rip), %xmm1    # 0x160b8
divsd 0x50(%rsp), %xmm1
mulsd %xmm1, %xmm8
mulsd %xmm4, %xmm1
mulsd %xmm5, %xmm8
addsd %xmm3, %xmm8
mulsd %xmm5, %xmm1
addsd %xmm2, %xmm1
subsd 0x18(%rsp), %xmm8
movapd %xmm8, 0x60(%rsp)
movsd %xmm1, 0x28(%rsp)
subsd 0x10(%rsp), %xmm1
movsd %xmm1, 0x30(%rsp)
addsd 0xa0(%rsp), %xmm0
divsd %xmm0, %xmm6
movapd %xmm6, %xmm0
movsd 0xa8(%rsp), %xmm1
callq 0x23a0
movapd 0x60(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd 0xd8(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd 0x30(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd 0xc8(%rsp), %xmm4
addsd %xmm1, %xmm4
movsd 0xd0(%rsp), %xmm3
addsd %xmm0, %xmm3
incl %ebp
movq (%r14), %rcx
movq 0x8(%r14), %rdx
subq %rcx, %rdx
sarq $0x5, %rdx
movq %rbp, %rax
cmpq %rbp, %rdx
ja 0x2ed8
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0xe8(%rsp)
movq %r13, %rbp
movq 0x88(%rsp), %r13
movsd 0x10(%rsp), %xmm1
movsd 0x12d50(%rip), %xmm0    # 0x160b8
divsd %xmm3, %xmm0
mulsd %xmm0, %xmm2
mulsd %xmm4, %xmm0
addsd 0x18(%rsp), %xmm2
addsd %xmm1, %xmm0
movsd %xmm2, 0xe0(%rsp)
movsd %xmm0, 0xe8(%rsp)
movq %rbp, %rdi
leaq 0xe0(%rsp), %rsi
movq %r15, %rdx
callq 0x2b84
testl %r13d, %r13d
jle 0x33cd
movq 0x70(%rsp), %rcx
movl (%rcx), %eax
imull 0xc(%rsp), %eax
addl %r12d, %eax
imull 0x8(%rcx), %eax
movslq %eax, %rdi
addq 0x10(%rcx), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x21a0
movsd 0x18(%rsp), %xmm0
addsd 0x12cdd(%rip), %xmm0    # 0x160b8
incl %r12d
cmpl 0x4c(%rsp), %r12d
movsd 0x10(%rsp), %xmm1
jne 0x2eb2
addsd 0x12cc1(%rip), %xmm1    # 0x160b8
movl 0xc(%rsp), %eax
incl %eax
movl %eax, 0xc(%rsp)
cmpl 0x5c(%rsp), %eax
jne 0x2e9a
movq 0x70(%rsp), %rax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x12cb7(%rip), %rdi     # 0x160e0
leaq 0x12cd3(%rip), %rsi     # 0x16103
leaq 0x12cda(%rip), %rcx     # 0x16111
movl $0x79, %edx
callq 0x2120