// File: logicx24[P]RayTracer/raytracer.cpp
// Function: Scene::phongShade(Ray&, Polygon*, vec3&, vec3&)
vec3 phongShade(Ray &ray, Polygon *polygon, vec3 &normal, vec3 &point) {
        vec3 total_intensity = vec3(0, 0, 0);
        total_intensity += multiplyComponents(ambientLight.color, polygon->mat.ka);
        for (int i = 0; i < lights.size(); i++) {
        	Light *light = lights[i];
        	vec3 intensity = light->intensityAt(point);
        	Ray shadowRay = light->lightRay(point);
            if (traceShadowRay(shadowRay, polygon)) {
                continue;
            }
        	vec3 l = light->lightVec(point);
            vec3 v = -ray.vec;
            vec3 r = -l + ((2*(l * normal)) * normal);
            l.normalize();
        	v.normalize();
        	r.normalize();
            vec3 coeffs = polygon->mat.kd * MAX(l * normal, 0.0f)
                        + polygon->mat.ks * pow(MAX(r * v, 0.0f), polygon->mat.sp);
            total_intensity += multiplyComponents(intensity, coeffs);
        }
        return total_intensity;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x220, %rsp            # imm = 0x220
movq %rdi, -0x1e8(%rbp)
movq %rdi, %rax
movq %rax, -0x1f8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1f0(%rbp)
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x6860
movq -0x1f0(%rbp), %rsi
addq $0xd0, %rsi
leaq -0x60(%rbp), %rdi
callq 0x7e40
movq -0x20(%rbp), %rsi
addq $0x8, %rsi
leaq -0x78(%rbp), %rdi
callq 0x7e40
leaq -0x48(%rbp), %rdi
leaq -0x60(%rbp), %rsi
leaq -0x78(%rbp), %rdx
callq 0x3890
movq -0x1e8(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0xe9d0
movl $0x0, -0x7c(%rbp)
movq -0x1f0(%rbp), %rdi
movslq -0x7c(%rbp), %rax
movq %rax, -0x200(%rbp)
addq $0xb8, %rdi
callq 0x9400
movq %rax, %rcx
movq -0x200(%rbp), %rax
cmpq %rcx, %rax
jae 0xe865
movq -0x1f0(%rbp), %rdi
addq $0xb8, %rdi
movslq -0x7c(%rbp), %rsi
callq 0x9450
movq (%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0xa0(%rbp), %rdi
callq 0xea20
movq -0x88(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0xd8(%rbp), %rdi
callq 0xeaa0
movq -0x1f0(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq -0xd8(%rbp), %rsi
callq 0xeb00
testb $0x1, %al
jne 0xe64a
jmp 0xe64f
jmp 0xe857
movq -0x88(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0xf0(%rbp), %rdi
callq 0xebb0
movq -0x18(%rbp), %rsi
addq $0x18, %rsi
leaq -0x108(%rbp), %rdi
callq 0xc780
leaq -0x138(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0xc780
movq -0x28(%rbp), %rsi
leaq -0xf0(%rbp), %rdi
callq 0xaee0
movaps %xmm0, %xmm1
movsd 0x16980(%rip), %xmm0    # 0x25028
mulsd %xmm1, %xmm0
movq -0x28(%rbp), %rsi
leaq -0x150(%rbp), %rdi
callq 0xb0d0
leaq -0x120(%rbp), %rdi
leaq -0x138(%rbp), %rsi
leaq -0x150(%rbp), %rdx
callq 0xb070
leaq -0xf0(%rbp), %rdi
callq 0xaeb0
leaq -0x108(%rbp), %rdi
callq 0xaeb0
leaq -0x120(%rbp), %rdi
callq 0xaeb0
movq -0x20(%rbp), %rax
addq $0x8, %rax
addq $0x18, %rax
movq %rax, -0x208(%rbp)
movq -0x28(%rbp), %rsi
leaq -0xf0(%rbp), %rdi
callq 0xaee0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xe740
movq -0x28(%rbp), %rsi
leaq -0xf0(%rbp), %rdi
callq 0xaee0
movsd %xmm0, -0x210(%rbp)
jmp 0xe74d
xorps %xmm0, %xmm0
movsd %xmm0, -0x210(%rbp)
jmp 0xe74d
movq -0x208(%rbp), %rsi
movsd -0x210(%rbp), %xmm0
leaq -0x180(%rbp), %rdi
callq 0xb110
movq -0x20(%rbp), %rax
addq $0x8, %rax
addq $0x30, %rax
movq %rax, -0x218(%rbp)
leaq -0x120(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0xaee0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xe7b4
leaq -0x120(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0xaee0
movsd %xmm0, -0x220(%rbp)
jmp 0xe7c1
xorps %xmm0, %xmm0
movsd %xmm0, -0x220(%rbp)
jmp 0xe7c1
movsd -0x220(%rbp), %xmm0
movq -0x20(%rbp), %rax
movss 0x68(%rax), %xmm1
callq 0xec10
movq -0x218(%rbp), %rsi
leaq -0x198(%rbp), %rdi
callq 0xb110
leaq -0x168(%rbp), %rdi
leaq -0x180(%rbp), %rsi
leaq -0x198(%rbp), %rdx
callq 0xb070
leaq -0x1c8(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x7e40
leaq -0x1e0(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x7e40
leaq -0x1b0(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
leaq -0x1e0(%rbp), %rdx
callq 0x3890
movq -0x1e8(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0xe9d0
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0xe5ad
movq -0x1f8(%rbp), %rax
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop