// File: /workspace/github_cpp_makefile_star2_O2/ddunbar[P]PDSample/ScallopedSector.cpp
// Function: ScallopedSector::subtractDisk(Vec2&, float, std::vector<ScallopedSector, std::allocator<ScallopedSector>>*)
void ScallopedSector::subtractDisk(Vec2 &C, float r, std::vector<ScallopedSector> *regions)
{
	std::vector<float> angles;

	Vec2 v(C.x - P.x, C.y-P.y);
	float d = sqrt(v.x*v.x + v.y*v.y);

	if (r<d) {
		float theta = atan2(v.y, v.x);
		float x = sqrt(d*d-r*r);
		float angle, alpha = asin(r/d);
		
		angle = canonizeAngle(theta+alpha);
		if (a1<angle && angle<a2) {
			if (distToCurve(angle,0)<x && x<distToCurve(angle,1))
				angles.push_back(angle);
		}

		angle = canonizeAngle(theta-alpha);
		if (a1<angle && angle<a2) {
			if (distToCurve(angle,0)<x && x<distToCurve(angle,1)) 
				angles.push_back(angle);
		}
	}

	for (int arcIndex=0; arcIndex<2; arcIndex++) {
		Vec2 &C2 = arcs[arcIndex].P;
		float R = arcs[arcIndex].r;
		Vec2 v(C.x - C2.x, C.y - C2.y);
		float d = sqrt(v.x*v.x + v.y*v.y);

		if (d>FLT_EPSILON) {
			float invD = 1.0f/d;
			float x = (d*d - r*r + R*R)*invD*.5f;
			float k = R*R - x*x;

			if (k>0) {
				float y = sqrt(k);
				float vx = v.x*invD;
				float vy = v.y*invD;
				float vx_x = vx*x, vy_x = vy*x;
				float vx_y = vx*y, vy_y = vy*y;
				float angle;

				angle = canonizeAngle(atan2(C2.y + vy_x + vx_y - P.y,
											C2.x + vx_x - vy_y - P.x));
				if (a1<angle && angle<a2) angles.push_back(angle);

				angle = canonizeAngle(atan2(C2.y + vy_x - vx_y - P.y,
											C2.x + vx_x + vy_y - P.x));
				if (a1<angle && angle<a2) angles.push_back(angle);
			}
		}
	}
	
	sort(angles.begin(), angles.end());
	angles.insert(angles.begin(), a1);
	angles.push_back(a2);

	for (unsigned int i=1; i<angles.size(); i++) {
		float a1 = angles[i-1], a2 = angles[i];
		float midA = (a1+a2)*.5f;
		float inner = distToCurve(midA,0);
		float outer = distToCurve(midA,1);
		float d1, d2;

		distToCircle(midA, C, r, &d1, &d2); // d1<=d2

		if (d2<inner || d1>outer) {
			regions->push_back(ScallopedSector(P, a1, a2, arcs[0].P, arcs[0].r, arcs[0].sign, arcs[1].P, arcs[1].r, arcs[1].sign));
		} else {
			if (inner<d1) {
				regions->push_back(ScallopedSector(P, a1, a2, arcs[0].P, arcs[0].r, arcs[0].sign, C, r, -1));
			}
			if (d2<outer) {
				regions->push_back(ScallopedSector(P, a1, a2, C, r, 1, arcs[1].P, arcs[1].r, arcs[1].sign));
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdx, 0x60(%rsp)
movaps %xmm0, %xmm4
movq %rsi, %r14
movq %rdi, %r15
andq $0x0, 0x40(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movss (%rsi), %xmm1
movss 0x4(%rsi), %xmm0
subss (%rdi), %xmm1
subss 0x4(%rdi), %xmm0
movaps %xmm0, %xmm2
mulss %xmm0, %xmm2
movaps %xmm1, %xmm3
mulss %xmm1, %xmm3
addss %xmm2, %xmm3
sqrtss %xmm3, %xmm2
ucomiss %xmm4, %xmm2
movaps %xmm4, 0x70(%rsp)
jbe 0x73a1
movss %xmm2, (%rsp)
callq 0x2220
movss (%rsp), %xmm2
movss %xmm0, 0x20(%rsp)
movaps 0x4213(%rip), %xmm1     # 0xb580
movaps 0x70(%rsp), %xmm0
xorps %xmm0, %xmm1
mulss %xmm0, %xmm1
movaps %xmm2, %xmm0
mulss %xmm2, %xmm0
movaps %xmm1, 0x80(%rsp)
addss %xmm1, %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x77a1
sqrtss %xmm0, %xmm0
jmp 0x77ab
movaps 0x41d8(%rip), %xmm0     # 0xb580
xorps %xmm4, %xmm0
mulss %xmm4, %xmm0
movaps %xmm0, 0x80(%rsp)
leaq 0x14(%r15), %rax
movq %rax, 0x98(%rsp)
leaq 0x8(%r15), %rax
movq %rax, 0xa0(%rsp)
leaq 0xc(%r15), %r12
pushq $0x1c
popq %rbx
leaq 0x30(%rsp), %r13
leaq 0xac(%rsp), %rbp
cmpq $0x64, %rbx
je 0x7591
movsd (%r14), %xmm3
movsd -0x8(%r15,%rbx), %xmm0
subps %xmm0, %xmm3
movaps %xmm3, %xmm0
mulps %xmm3, %xmm0
shufps $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movaps %xmm3, %xmm1
mulss %xmm3, %xmm1
addss %xmm0, %xmm1
sqrtss %xmm1, %xmm2
ucomiss 0x4154(%rip), %xmm2     # 0xb570
jbe 0x7588
movss (%r15,%rbx), %xmm0
movss 0x2040(%rip), %xmm4     # 0x9470
divss %xmm2, %xmm4
mulss %xmm2, %xmm2
addss 0x80(%rsp), %xmm2
mulss %xmm0, %xmm0
addss %xmm0, %xmm2
mulss %xmm4, %xmm2
mulss 0x1c07(%rip), %xmm2     # 0x905c
movaps %xmm2, %xmm1
mulss %xmm2, %xmm1
subss %xmm1, %xmm0
ucomiss 0x410d(%rip), %xmm0     # 0xb574
jbe 0x7588
jb 0x7475
sqrtss %xmm0, %xmm0
jmp 0x7496
movaps %xmm2, 0x20(%rsp)
movaps %xmm3, 0x10(%rsp)
movaps %xmm4, (%rsp)
callq 0x2310
movaps (%rsp), %xmm4
movaps 0x10(%rsp), %xmm3
movaps 0x20(%rsp), %xmm2
shufps $0x0, %xmm4, %xmm4      # xmm4 = xmm4[0,0,0,0]
mulps %xmm4, %xmm3
movaps %xmm0, %xmm4
unpcklps %xmm2, %xmm4            # xmm4 = xmm4[0],xmm2[0],xmm4[1],xmm2[1]
mulps %xmm3, %xmm4
unpcklps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
movaps %xmm4, %xmm1
shufps $0x55, %xmm4, %xmm1     # xmm1 = xmm1[1,1],xmm4[1,1]
movss -0x4(%r15,%rbx), %xmm0
movaps %xmm1, 0x10(%rsp)
addss %xmm1, %xmm0
movaps %xmm4, (%rsp)
addss %xmm4, %xmm0
subss 0x4(%r15), %xmm0
mulps %xmm3, %xmm2
movss -0x8(%r15,%rbx), %xmm1
addss %xmm2, %xmm1
movaps %xmm2, 0x20(%rsp)
shufps $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
movaps %xmm2, 0x50(%rsp)
subss %xmm2, %xmm1
subss (%r15), %xmm1
callq 0x2220
movq %r15, %rdi
callq 0x71b0
movss %xmm0, 0xac(%rsp)
ucomiss 0x8(%r15), %xmm0
jbe 0x7526
movss 0xc(%r15), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x7526
movq %r13, %rdi
movq %rbp, %rsi
callq 0x7c24
movaps 0x10(%rsp), %xmm0
addss -0x4(%r15,%rbx), %xmm0
subss (%rsp), %xmm0
subss 0x4(%r15), %xmm0
movss -0x8(%r15,%rbx), %xmm1
addss 0x20(%rsp), %xmm1
addss 0x50(%rsp), %xmm1
subss (%r15), %xmm1
callq 0x2220
movq %r15, %rdi
callq 0x71b0
movss %xmm0, 0xac(%rsp)
ucomiss 0x8(%r15), %xmm0
jbe 0x7588
movss 0xc(%r15), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x7588
movq %r13, %rdi
movq %rbp, %rsi
callq 0x7c24
addq $0x24, %rbx
jmp 0x73e3
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x7e5b
leaq 0x30(%rsp), %rdi
movq (%rdi), %rsi
movq 0xa0(%rsp), %rdx
callq 0x7c46
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x7c24
leaq 0x38(%r15), %r13
xorl %eax, %eax
leaq 0x68(%rsp), %r12
leaq 0xac(%rsp), %rbp
leal 0x1(%rax), %ebx
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %rbx, %rdx
jbe 0x7785
movl %eax, %eax
movss (%rcx,%rax,4), %xmm1
movss (%rcx,%rbx,4), %xmm0
movss %xmm1, 0x50(%rsp)
movss %xmm0, (%rsp)
addss %xmm0, %xmm1
mulss 0x1a47(%rip), %xmm1     # 0x905c
movss %xmm1, 0x20(%rsp)
movq %r15, %rdi
movaps %xmm1, %xmm0
xorl %esi, %esi
callq 0x6fe2
movss %xmm0, 0x10(%rsp)
movq %r15, %rdi
movss 0x20(%rsp), %xmm0
pushq $0x1
popq %rsi
callq 0x6fe2
movss %xmm0, 0x80(%rsp)
movq %r15, %rdi
movss 0x20(%rsp), %xmm0
movq %r14, %rsi
movaps 0x70(%rsp), %xmm1
leaq 0x6c(%rsp), %rdx
movq %r12, %rcx
callq 0x71f0
movss 0x10(%rsp), %xmm3
movss 0x80(%rsp), %xmm1
movss 0x68(%rsp), %xmm2
ucomiss %xmm2, %xmm3
ja 0x768b
movss 0x6c(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x76d8
movss 0x1c(%r15), %xmm2
movss 0x20(%r15), %xmm3
movss 0x40(%r15), %xmm4
movss 0x44(%r15), %xmm5
movq %rbp, %rdi
movq %r15, %rsi
movss 0x50(%rsp), %xmm0
movss (%rsp), %xmm1
movq 0x98(%rsp), %rdx
movq %r13, %rcx
callq 0x6c88
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0x82fe
movl %ebx, %eax
jmp 0x75d5
ucomiss %xmm3, %xmm0
jbe 0x7739
movss %xmm2, 0x20(%rsp)
movss 0x1c(%r15), %xmm2
movss 0x20(%r15), %xmm3
movq %rbp, %rdi
movq %r15, %rsi
movss 0x50(%rsp), %xmm0
movss (%rsp), %xmm1
movq 0x98(%rsp), %rdx
movq %r14, %rcx
movaps 0x70(%rsp), %xmm4
movss 0x1d54(%rip), %xmm5     # 0x946c
callq 0x6c88
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0x82fe
movss 0x80(%rsp), %xmm1
movss 0x20(%rsp), %xmm2
ucomiss %xmm2, %xmm1
jbe 0x76d1
movss 0x40(%r15), %xmm4
movss 0x44(%r15), %xmm5
movq %rbp, %rdi
movq %r15, %rsi
movss 0x50(%rsp), %xmm0
movss (%rsp), %xmm1
movq %r14, %rdx
movaps 0x70(%rsp), %xmm2
movss 0x1d05(%rip), %xmm3     # 0x9470
movq %r13, %rcx
callq 0x6c88
movq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0x82fe
jmp 0x76d1
leaq 0x30(%rsp), %rdi
callq 0x7ce2
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2310
movss (%rsp), %xmm2
movss %xmm0, 0x10(%rsp)
movaps 0x70(%rsp), %xmm0
divss %xmm2, %xmm0
callq 0x22a0
movaps %xmm0, %xmm1
movss 0x20(%rsp), %xmm0
movss %xmm1, (%rsp)
addss %xmm1, %xmm0
movq %r15, %rdi
callq 0x71b0
movss %xmm0, 0xac(%rsp)
ucomiss 0x8(%r15), %xmm0
jbe 0x7839
movss 0xc(%r15), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x7839
movq %r15, %rdi
xorl %esi, %esi
movss %xmm0, 0x50(%rsp)
callq 0x6fe2
movss 0x10(%rsp), %xmm1
ucomiss %xmm0, %xmm1
movss 0x50(%rsp), %xmm0
jbe 0x7839
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x6fe2
ucomiss 0x10(%rsp), %xmm0
jbe 0x7839
leaq 0x30(%rsp), %rdi
leaq 0xac(%rsp), %rsi
callq 0x7c24
movss 0x20(%rsp), %xmm0
subss (%rsp), %xmm0
movq %r15, %rdi
callq 0x71b0
movss %xmm0, 0xac(%rsp)
ucomiss 0x8(%r15), %xmm0
jbe 0x73b7
movss 0xc(%r15), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x73b7
movq %r15, %rdi
xorl %esi, %esi
callq 0x6fe2
movss 0x10(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x73b7
movss 0xac(%rsp), %xmm0
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x6fe2
ucomiss 0x10(%rsp), %xmm0
jbe 0x73b7
leaq 0x30(%rsp), %rdi
leaq 0xac(%rsp), %rsi
callq 0x7c24
jmp 0x73b7
jmp 0x78ca
jmp 0x78ca
jmp 0x78ca
jmp 0x78ca
jmp 0x78ca
jmp 0x78ca
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x7ce2
movq %rbx, %rdi
callq 0x2300
nop