// File: /workspace/github_cpp_makefile_star2_O3/DavidNKraemer[P]convex-hull/cvhull/GrahamScan.cpp
// Function: ams562::GrahamScan::apply(std::vector<ams562::Point2D, std::allocator<ams562::Point2D>>&, std::vector<ams562::Point2D, std::allocator<ams562::Point2D>>&) const
void GrahamScan::apply(std::vector<Point2D> &pc, std::vector<Point2D> &ch) const {

    //
    // First find the lowest point by using std::max_element
    // if there are ties, pick the most left one
    auto sw_iterator = std::min_element(
        pc.begin(), 
        pc.end(),

        // comparison function returns true if p1 is lower than p2
        is_lower);

    //
    // Then, swap that point with the first element in pc
    std::iter_swap(pc.begin(), sw_iterator);

    //
    // Next step is to sort the 2nd point to the last point in pc
    // by polar angle with the first point in pc
    // Note that the first point in pc is the lowest point in the
    // point cloud. std::sort
    std::sort(pc.begin() + 1, 
        pc.end(), 
        [&pc](Point2D p1, Point2D p2) {
        return pc[0].orientation(p1, p2) < 0; 
        }
        );

    //
    // Push back the first and second points to the back
    // of ch, push_back of ch should be used
    ch.push_back(*pc.begin());
    ch.push_back(*(pc.begin() + 1));

    //
    // Bonus, I do the cur for you

    auto cur = pc.begin() + 2;
    while (cur != pc.end()) {
      //
      // Get the last 2 points in ch by using iterators
      // note that you can start with ch.end(), be aware
      // of the position of ch.end()

      auto ch_ult = ch.end() - 1; // ultimate
      auto ch_pen = ch.end() - 2; // penultimate

      //
      // Compute the orientation information of ch's last1
      // last2 and cur

      //
      // if cur is to left of last1_last2
      // then push cur to ch
      if (is_left(*cur, *ch_pen, *ch_ult)) {
        ch.push_back(*cur);
        cur ++;
      }

      //
      // else if cur is collinear and cur lies futher
      // replace the last point of ch by cur
      else if (is_collinear(*cur, *ch_pen, *ch_ult)) {
        *ch_ult = *cur;
        cur ++;
      }

      //
      // else, pop the last point back from ch
      // and keep ch unchanged
      else {
        ch.pop_back();
      }
    }
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq %rsi, %r14
movq (%rsi), %r12
movq 0x8(%rsi), %r15
cmpq %r15, %r12
sete %al
leaq 0x8(%r12), %r13
cmpq %r15, %r13
sete %cl
orb %al, %cl
movl (%r12), %eax
movl %eax, %ecx
movq %r12, %rsi
jne 0x33cd
movl %eax, %edx
movq %r13, %rcx
movq %r12, %rdi
movl (%rcx), %r8d
movl 0x4(%rcx), %esi
xorl %r9d, %r9d
cmpl %edx, %r8d
setl %r9b
xorl %r10d, %r10d
cmpl 0x4(%rdi), %esi
setl %r10b
cmovel %r9d, %r10d
movq %rcx, %rsi
testb %r10b, %r10b
jne 0x33bc
movl %edx, %r8d
movq %rdi, %rsi
addq $0x8, %rcx
movl %r8d, %edx
movq %rsi, %rdi
cmpq %r15, %rcx
jne 0x3390
movl (%rsi), %ecx
movl 0x4(%r12), %edx
movl %ecx, (%r12)
movl 0x4(%rsi), %ecx
movl %ecx, 0x4(%r12)
movl %eax, (%rsi)
movl %edx, 0x4(%rsi)
cmpq %r15, %r13
je 0x3452
movq %r15, %rbp
subq %r13, %rbp
movq %rbp, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rcx
callq 0x34f1
cmpq $0x81, %rbp
jl 0x3444
addq $0x88, %r12
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x3913
cmpq %r15, %r12
je 0x3452
movq %r12, %rdi
movq %r14, %rsi
callq 0x39bc
addq $0x8, %r12
jmp 0x342e
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x3913
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x2e26
movq (%r14), %rsi
addq $0x8, %rsi
movq %rbx, %rdi
callq 0x2e26
movq (%r14), %r15
addq $0x10, %r15
cmpq 0x8(%r14), %r15
je 0x34e2
movq 0x8(%rbx), %rax
movl (%r15), %ecx
movl 0x4(%r15), %edx
movl -0x10(%rax), %esi
movl -0xc(%rax), %edi
movl -0x8(%rax), %r8d
movl %r8d, %r10d
subl %esi, %r10d
movl %edx, %r9d
subl %edi, %r9d
imull %r9d, %r10d
movl %ecx, %r11d
subl %esi, %r11d
subl -0x4(%rax), %edi
imull %r11d, %edi
addl %edi, %r10d
js 0x34c7
subl %r8d, %esi
imull %r9d, %esi
cmpl %esi, %edi
jne 0x34d8
movl %ecx, -0x8(%rax)
movl %edx, -0x4(%rax)
addq $0x8, %r15
jmp 0x3473
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2e26
addq $0x8, %r15
jmp 0x3473
addq $-0x8, %rax
movq %rax, 0x8(%rbx)
jmp 0x3473
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq