// File: /workspace/github_cpp_makefile_star2_O3/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, 0x70(%rsp)
movsd %xmm2, 0x68(%rsp)
movsd %xmm1, 0x60(%rsp)
movapd %xmm0, 0x90(%rsp)
movq %rcx, %rbx
movq 0x8(%rdx), %rax
subq (%rdx), %rax
movq 0x8(%rcx), %rcx
subq (%rbx), %rcx
cmpq %rcx, %rax
jne 0x3243
movq %rdx, %r14
movq %rsi, %rbp
movq %rdi, %r15
movq 0x1920b(%rip), %r13     # 0x1bfd0
leaq 0x133cb(%rip), %rsi     # 0x16197
movl $0x13, %edx
movq %r13, %rdi
callq 0x2220
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2140
movl (%rbp), %esi
movl 0x4(%rbp), %r12d
movl 0x8(%rbp), %r13d
movq %r15, %rdi
movl %esi, 0xc(%rsp)
movl %r12d, %edx
movl %r13d, %ecx
callq 0x2594
movl %r12d, 0x18(%rsp)
testl %r12d, %r12d
movq %r15, %rax
jle 0x3231
movsd 0x13281(%rip), %xmm1    # 0x160b8
movapd 0x90(%rsp), %xmm0
subsd %xmm0, %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd %xmm0, 0xd0(%rsp)
movapd %xmm1, 0xe0(%rsp)
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
movapd %xmm1, 0xc0(%rsp)
movq $0x0, 0x10(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq %rax, 0x58(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x3207
unpcklpd 0x20(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
movapd %xmm0, 0xa0(%rsp)
xorl %r15d, %r15d
xorpd %xmm5, %xmm5
movl %r15d, 0x1c(%rsp)
movq %r13, %r15
movq %rbp, %r13
movq (%r14), %rcx
cmpq %rcx, 0x8(%r14)
movapd %xmm5, 0x30(%rsp)
je 0x315c
movapd %xmm5, %xmm0
unpcklpd 0x20(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
movapd %xmm0, 0xf0(%rsp)
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
movl $0x1, %ebp
xorl %eax, %eax
movsd %xmm2, 0x80(%rsp)
movapd %xmm1, 0x130(%rsp)
shlq $0x5, %rax
movq (%rbx), %rdx
movsd 0x10(%rdx,%rax), %xmm2
movapd 0x90(%rsp), %xmm0
mulsd %xmm0, %xmm2
movsd 0x18(%rdx,%rax), %xmm3
mulsd %xmm0, %xmm3
movupd (%rcx,%rax), %xmm7
movupd 0x10(%rcx,%rax), %xmm6
movapd 0xe0(%rsp), %xmm4
movapd %xmm4, %xmm0
mulsd %xmm6, %xmm0
movapd %xmm6, %xmm1
unpckhpd %xmm6, %xmm1            # xmm1 = xmm1[1],xmm6[1]
mulsd %xmm4, %xmm1
addsd %xmm3, %xmm1
addsd %xmm2, %xmm0
movupd (%rdx,%rax), %xmm2
mulpd 0xd0(%rsp), %xmm2
movapd 0xc0(%rsp), %xmm3
mulpd %xmm7, %xmm3
addpd %xmm2, %xmm3
movapd %xmm5, %xmm2
subsd %xmm3, %xmm2
shufpd $0x2, 0xa0(%rsp), %xmm0 # xmm0 = xmm0[0],mem[1]
subpd %xmm3, %xmm0
unpckhpd %xmm3, %xmm3            # xmm3 = xmm3[1,1]
subsd %xmm3, %xmm1
movapd %xmm0, %xmm4
unpckhpd %xmm0, %xmm4            # xmm4 = xmm4[1],xmm0[1]
movapd %xmm4, %xmm3
mulsd %xmm1, %xmm3
movapd %xmm0, %xmm8
mulsd %xmm2, %xmm8
addsd %xmm3, %xmm8
mulsd %xmm1, %xmm2
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm4
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
divsd %xmm0, %xmm8
movapd %xmm8, 0x100(%rsp)
subsd %xmm2, %xmm4
sqrtsd %xmm0, %xmm0
divsd %xmm0, %xmm4
movapd %xmm4, 0x40(%rsp)
movapd %xmm7, 0xb0(%rsp)
subpd %xmm7, %xmm6
movapd %xmm6, %xmm0
unpckhpd %xmm6, %xmm0            # xmm0 = xmm0[1],xmm6[1]
movapd %xmm0, 0x110(%rsp)
mulsd %xmm0, %xmm0
movapd %xmm6, 0x120(%rsp)
movapd %xmm6, %xmm1
mulsd %xmm6, %xmm1
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x70(%rsp), %xmm1
callq 0x2380
movapd 0x100(%rsp), %xmm5
xorpd %xmm1, %xmm1
ucomisd %xmm5, %xmm1
jbe 0x304a
movapd 0x30(%rsp), %xmm1
movapd 0xb0(%rsp), %xmm4
jmp 0x3063
movapd 0xb0(%rsp), %xmm4
ucomisd 0x1305d(%rip), %xmm5    # 0x160b8
jbe 0x308b
movapd 0x30(%rsp), %xmm1
subsd %xmm4, %xmm1
movapd %xmm4, %xmm2
unpckhpd %xmm4, %xmm2            # xmm2 = xmm2[1],xmm4[1]
movapd 0x20(%rsp), %xmm3
subsd %xmm2, %xmm3
mulsd %xmm3, %xmm3
mulsd %xmm1, %xmm1
addsd %xmm3, %xmm1
sqrtsd %xmm1, %xmm1
jmp 0x309f
cvttsd2si 0x40(%rsp), %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
unpcklpd %xmm5, %xmm5            # xmm5 = xmm5[0,0]
movapd 0x120(%rsp), %xmm3
mulpd %xmm3, %xmm5
addpd %xmm5, %xmm4
movapd 0x110(%rsp), %xmm5
xorpd 0x1300b(%rip), %xmm5    # 0x160d0
movsd 0x12feb(%rip), %xmm2    # 0x160b8
divsd 0x78(%rsp), %xmm2
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
unpcklpd %xmm3, %xmm5            # xmm5 = xmm5[0],xmm3[0]
mulpd %xmm2, %xmm5
movapd 0x40(%rsp), %xmm2
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
mulpd %xmm5, %xmm2
addpd %xmm4, %xmm2
subpd 0xf0(%rsp), %xmm2
movapd %xmm2, 0x40(%rsp)
addsd 0x60(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0x68(%rsp), %xmm1
callq 0x2380
movsd 0x80(%rsp), %xmm2
addsd %xmm0, %xmm2
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd 0x40(%rsp), %xmm0
movapd 0x130(%rsp), %xmm1
addpd %xmm0, %xmm1
movl %ebp, %eax
movq (%r14), %rcx
movq 0x8(%r14), %rdx
subq %rcx, %rdx
sarq $0x5, %rdx
incl %ebp
cmpq %rax, %rdx
movapd 0x30(%rsp), %xmm5
ja 0x2ee0
jmp 0x3164
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
movsd 0x12f4c(%rip), %xmm0    # 0x160b8
divsd %xmm2, %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm1, %xmm0
movapd 0xa0(%rsp), %xmm1
movsd %xmm5, %xmm1            # xmm1 = xmm5[0],xmm1[1]
addpd %xmm0, %xmm1
movapd %xmm1, 0x140(%rsp)
movq %r13, %r12
movq %r13, %rdi
leaq 0x140(%rsp), %rsi
leaq 0x8c(%rsp), %rbp
movq %rbp, %rdx
callq 0x2b54
movq %r15, %r13
testl %r13d, %r13d
movq 0x58(%rsp), %rax
movl 0x1c(%rsp), %r15d
jle 0x31e8
movl (%rax), %ecx
imull 0x10(%rsp), %ecx
addl %r15d, %ecx
imull 0x8(%rax), %ecx
movslq %ecx, %rdi
addq 0x10(%rax), %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %rax, %rbp
callq 0x2180
movq %rbp, %rax
movapd 0x30(%rsp), %xmm5
addsd 0x12ec2(%rip), %xmm5    # 0x160b8
incl %r15d
cmpl 0xc(%rsp), %r15d
movq %r12, %rbp
jne 0x2ea0
movapd 0x20(%rsp), %xmm0
addsd 0x12ea3(%rip), %xmm0    # 0x160b8
movapd %xmm0, 0x20(%rsp)
movq 0x10(%rsp), %rcx
incl %ecx
movq %rcx, 0x10(%rsp)
cmpl 0x18(%rsp), %ecx
jne 0x2e7f
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x12e96(%rip), %rdi     # 0x160e0
leaq 0x12eb2(%rip), %rsi     # 0x16103
leaq 0x12eb9(%rip), %rcx     # 0x16111
movl $0x79, %edx
callq 0x2110