// File: /workspace/github_cpp_makefile_star2_O2/logicx24[P]RayTracer/raytracer.cpp
// Function: Scene::traceRay(Ray&, int)
vec3 traceRay(Ray &testRay, int depth) {
		Polygon *hitObject = NULL;
    	float min_dist = MAXFLOAT;
		vec3 minIntersection;
    	Ray ray;
		for (int i = 0; i < objects.size(); i++) {
			ray = objects[i]->transformRay(testRay);
			float t = objects[i]->intersection(ray, eps);
			vec3 intersection = ray.pointAt(t);
			intersection = objects[i]->transformIntersection(intersection);
			float d = distance(ray.pos, intersection);
            if (t != MAXFLOAT && d < min_dist) {
                min_dist = d;
                hitObject = objects[i];
                minIntersection = intersection;
		    }
		}
		if (hitObject != NULL) {
			//minIntersection = hitObject->inverseTransform * minIntersection;
            vec3 normal = hitObject->normal(hitObject->inverseTransform * minIntersection);
            normal = hitObject->transformNormal(normal);
            normal.normalize();
            vec3 reflectionVec = ray.vec - (2*(ray.vec * normal))*normal;
            reflectionVec.normalize();
            Ray reflected = Ray(minIntersection, reflectionVec);
            if (depth == 0) {
            	return phongShade(ray, hitObject, normal, minIntersection);
            } else {
        	    return phongShade(ray, hitObject, normal, minIntersection)
        	           + multiplyComponents(traceRay(reflected, depth - 1), hitObject->mat.kr);
		    }
		} else {
		    return vec3(0, 0, 0);

        }
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movl %ecx, -0x3c(%rbp)
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, -0x38(%rbp)
xorps %xmm0, %xmm0
leaq -0xa0(%rbp), %r15
movaps %xmm0, 0x20(%r15)
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
movss 0xb26d(%rip), %xmm0     # 0x13088
xorl %r12d, %r12d
leaq -0x108(%rbp), %rbx
movss %xmm0, -0x2c(%rbp)
movq $0x0, -0x48(%rbp)
movq 0xa0(%r14), %rax
movq 0xa8(%r14), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r12, %rcx
jbe 0x7fc8
movq (%rax,%r12,8), %rsi
movq %rbx, %rdi
movq %r13, %rdx
callq 0x8324
movups -0x108(%rbp), %xmm0
movups -0xf8(%rbp), %xmm1
movups -0xe8(%rbp), %xmm2
movaps %xmm0, -0xa0(%rbp)
movaps %xmm1, -0x90(%rbp)
movaps %xmm2, -0x80(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq 0xa0(%r14), %rax
movq (%rax,%r12,8), %rdi
movss 0x114(%r14), %xmm0
movq (%rdi), %rax
movq %r15, %rsi
callq *0x8(%rax)
movss %xmm0, -0x40(%rbp)
cvtss2sd %xmm0, %xmm3
movsd -0x88(%rbp), %xmm1
mulsd %xmm3, %xmm1
movsd -0x80(%rbp), %xmm2
mulsd %xmm3, %xmm2
mulsd -0x78(%rbp), %xmm3
addsd -0xa0(%rbp), %xmm1
addsd -0x98(%rbp), %xmm2
addsd -0x90(%rbp), %xmm3
movsd %xmm1, -0x108(%rbp)
movsd %xmm2, -0x100(%rbp)
movsd %xmm3, -0xf8(%rbp)
movq 0xa0(%r14), %rax
movq (%rax,%r12,8), %rsi
leaq -0x60(%rbp), %rdi
movq %rbx, %rdx
callq 0x846a
movsd -0x60(%rbp), %xmm0
movsd -0x58(%rbp), %xmm1
movsd %xmm0, -0xb8(%rbp)
movsd %xmm0, -0x108(%rbp)
movsd %xmm1, -0xb0(%rbp)
movsd %xmm1, -0x100(%rbp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd %xmm0, -0xf8(%rbp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x363a
movss -0x40(%rbp), %xmm1
ucomiss 0xb11a(%rip), %xmm1     # 0x13088
jne 0x7f72
jnp 0x7fc0
movss -0x2c(%rbp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x7fc0
movq 0xa0(%r14), %rax
movq (%rax,%r12,8), %rax
movq %rax, -0x48(%rbp)
movsd -0xb8(%rbp), %xmm1
movsd %xmm1, -0xd0(%rbp)
movsd -0xb0(%rbp), %xmm1
movsd %xmm1, -0xc8(%rbp)
movsd -0xa8(%rbp), %xmm1
movsd %xmm1, -0xc0(%rbp)
movss %xmm0, -0x2c(%rbp)
incq %r12
jmp 0x7e32
movq -0x48(%rbp), %r13
testq %r13, %r13
je 0x8192
leaq 0xf0(%r13), %rsi
leaq -0x168(%rbp), %r15
leaq -0xd0(%rbp), %rbx
movq %r15, %rdi
movq %rbx, %rdx
callq 0x84f6
movq (%r13), %rax
leaq -0x60(%rbp), %r12
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq *0x10(%rax)
leaq -0x108(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x857e
movsd (%r15), %xmm0
movsd %xmm0, (%r12)
movups 0x8(%r15), %xmm0
movups %xmm0, 0x8(%r12)
movq %r12, %rdi
callq 0x7286
movsd -0x88(%rbp), %xmm0
movsd -0x80(%rbp), %xmm1
movsd (%r12), %xmm2
movsd 0x8(%r12), %xmm3
movapd %xmm1, %xmm4
mulsd %xmm3, %xmm4
movapd %xmm0, %xmm5
mulsd %xmm2, %xmm5
addsd %xmm4, %xmm5
movsd -0x78(%rbp), %xmm4
movsd 0x10(%r12), %xmm6
movapd %xmm4, %xmm7
mulsd %xmm6, %xmm7
addsd %xmm5, %xmm7
addsd %xmm7, %xmm7
mulsd %xmm7, %xmm2
mulsd %xmm7, %xmm3
mulsd %xmm6, %xmm7
subsd %xmm2, %xmm0
subsd %xmm3, %xmm1
subsd %xmm7, %xmm4
leaq -0x150(%rbp), %r12
movsd %xmm0, (%r12)
movsd %xmm1, 0x8(%r12)
movsd %xmm4, 0x10(%r12)
movq %r12, %rdi
callq 0x7286
movsd (%rbx), %xmm0
movsd %xmm0, (%r15)
movups 0x8(%rbx), %xmm0
movups %xmm0, 0x8(%r15)
movsd (%r12), %xmm0
movsd %xmm0, 0x18(%r15)
movupd 0x8(%r12), %xmm0
movupd %xmm0, 0x20(%r15)
movabsq $0x7f7fffff3a83126f, %rax # imm = 0x7F7FFFFF3A83126F
movq %rax, 0x30(%r15)
movl -0x3c(%rbp), %r12d
testl %r12d, %r12d
je 0x81a5
leaq -0x138(%rbp), %rbx
leaq -0xa0(%rbp), %rdx
leaq -0x60(%rbp), %r8
leaq -0xd0(%rbp), %r9
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rcx
callq 0x85e6
decl %r12d
leaq -0x120(%rbp), %r15
leaq -0x108(%rbp), %rdx
movq %r15, %rdi
movq %r14, %rsi
movl %r12d, %ecx
callq 0x7dda
movsd 0x50(%r13), %xmm0
movsd 0x58(%r13), %xmm1
movsd 0x60(%r13), %xmm2
mulsd (%r15), %xmm0
mulsd 0x8(%r15), %xmm1
mulsd 0x10(%r15), %xmm2
addsd (%rbx), %xmm0
addsd 0x8(%rbx), %xmm1
addsd 0x10(%rbx), %xmm2
movq -0x38(%rbp), %rbx
movsd %xmm0, (%rbx)
movsd %xmm1, 0x8(%rbx)
movsd %xmm2, 0x10(%rbx)
jmp 0x81c9
xorpd %xmm0, %xmm0
movq -0x38(%rbp), %rbx
movupd %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
jmp 0x81c9
leaq -0xa0(%rbp), %rdx
leaq -0x60(%rbp), %r8
leaq -0xd0(%rbp), %r9
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rcx
callq 0x85e6
movq %rbx, %rax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq