// File: eliebe-5[P]Catmull-clark-subdivision/halfedge.cc
// Function: Mesh::newVertex(HEVert*)
v3 Mesh::newVertex(HEVert *vert)
{
    
    HEEdge* temp = vert->edge->pair->next;

    int n = 0;

    v3 avgFace(0, 0, 0);
    v3 avgAvg(0, 0, 0);
    
    while(true)
    {
        n++;
        avgFace = avgFace + edgePoint(temp)->pos;
        avgAvg = avgAvg + avgPoint(temp);

        if(temp == vert->edge)
            break;
        temp = temp->pair->next;
    }

    float ndiv = 1.f/(float)n;
    
    avgFace = avgFace * ndiv;
    avgAvg = avgAvg * ndiv;

    v3 res;
    
    res = (avgFace * ndiv) + ((avgAvg*2) * ndiv) + (vert->pos*(n - 3) * ndiv);
    
    return res;
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0x8(%rsp)
movq %rdi, %rax
movq %rax, 0x10(%rsp)
movq %rdi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
movq %rdx, 0xe0(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xe0(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, 0xd8(%rsp)
movl $0x0, 0xd4(%rsp)
leaq 0xc8(%rsp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x5fd0
leaq 0xbc(%rsp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x5fd0
movq 0x18(%rsp), %rdi
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
movq 0xd8(%rsp), %rsi
callq 0x3fa0
movq %rax, %rdx
leaq 0xb0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x65d0
movq 0x18(%rsp), %rsi
movq 0xb0(%rsp), %rax
movq %rax, 0xc8(%rsp)
movl 0xb8(%rsp), %eax
movl %eax, 0xd0(%rsp)
movq 0xd8(%rsp), %rdx
leaq 0x98(%rsp), %rdi
callq 0x40f0
leaq 0xa4(%rsp), %rdi
leaq 0xbc(%rsp), %rsi
leaq 0x98(%rsp), %rdx
callq 0x65d0
movq 0xa4(%rsp), %rax
movq %rax, 0xbc(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0xc4(%rsp)
movq 0xd8(%rsp), %rax
movq 0xe0(%rsp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x45c2
jmp 0x45df
movq 0xd8(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, 0xd8(%rsp)
jmp 0x44fc
cvtsi2ssl 0xd4(%rsp), %xmm1
movss 0x7a8c(%rip), %xmm0     # 0xc07c
divss %xmm1, %xmm0
movss %xmm0, 0x94(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
leaq 0x94(%rsp), %rdx
callq 0x6640
movq 0x88(%rsp), %rax
movq %rax, 0xc8(%rsp)
movl 0x90(%rsp), %eax
movl %eax, 0xd0(%rsp)
leaq 0x7c(%rsp), %rdi
leaq 0xbc(%rsp), %rsi
leaq 0x94(%rsp), %rdx
callq 0x6640
movq 0x8(%rsp), %rdi
movq 0x7c(%rsp), %rax
movq %rax, 0xbc(%rsp)
movl 0x84(%rsp), %eax
movl %eax, 0xc4(%rsp)
callq 0x66a0
leaq 0x58(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
leaq 0x94(%rsp), %rdx
callq 0x6640
movss 0x79df(%rip), %xmm0     # 0xc078
movss %xmm0, 0x3c(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0xbc(%rsp), %rsi
leaq 0x3c(%rsp), %rdx
callq 0x6640
leaq 0x4c(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x94(%rsp), %rdx
callq 0x6640
leaq 0x64(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x4c(%rsp), %rdx
callq 0x65d0
movq 0xe0(%rsp), %rsi
movl 0xd4(%rsp), %eax
subl $0x3, %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x20(%rsp)
leaq 0x24(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x6640
leaq 0x30(%rsp), %rdi
leaq 0x24(%rsp), %rsi
leaq 0x94(%rsp), %rdx
callq 0x6640
leaq 0x70(%rsp), %rdi
leaq 0x64(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x65d0
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rax
movq 0x70(%rsp), %rdx
movq %rdx, (%rcx)
movl 0x78(%rsp), %edx
movl %edx, 0x8(%rcx)
addq $0xf8, %rsp
retq
nopl (%rax,%rax)