// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Graphics/R3AreaLight.cpp
// Function: gaps::R3AreaLight::SpecularReflection(gaps::R3Brdf const&, gaps::R3Point const&, gaps::R3Point const&, gaps::R3Vector const&) const
RNRgb R3AreaLight::
SpecularReflection(const R3Brdf& brdf, const R3Point& eye, 
    const R3Point& point, const R3Vector& normal) const
{
    // Parameter ????
    const int max_samples = 8;

    // Check if light is active
    if (!IsActive()) return RNblack_rgb;

    // Get material properties
    const RNRgb& Sc = brdf.Specular();
    RNScalar s = brdf.Shininess();

    // Get light properties
    const RNRgb& Ic = Color();

    // Get circle axes
    R3Vector direction = circle.Normal();
    RNDimension dim = direction.MinDimension();
    R3Vector axis1 = direction % R3xyz_triad[dim];
    axis1.Normalize();
    R3Vector axis2 = direction % axis1;
    axis2.Normalize();

    // Sample points on light source
    int sample_count = 0;
    RNRgb sample_sum = RNblack_rgb;
    for (int i = 0; i < max_samples; i++) {
        // Sample point in circle
        RNScalar r1 = 2.0 * RNRandomScalar() - 1.0;
        RNScalar r2 = 2.0 * RNRandomScalar() - 1.0;
        if (r1*r1 + r2*r2 > 1) continue;
        R3Point sample_point = Position();
        sample_point += r1 * Radius() * axis1;
        sample_point += r2 * Radius() * axis2;
        sample_count++;

        // Compute intensity at point
        RNScalar I = Intensity();
        RNLength d = R3Distance(point, sample_point);
        RNScalar denom = constant_attenuation;
        denom += d * linear_attenuation;
        denom += d * d * quadratic_attenuation;
        if (RNIsPositive(denom)) I /= denom;

        // Compute direction at point
        R3Vector L = sample_point - point;
        L.Normalize();

        // Compute specular reflection from sample_point
        RNScalar NL = normal.Dot(L);
        if (RNIsNegativeOrZero(NL)) return RNblack_rgb;
        R3Vector R = (2.0 * NL) * normal - L;
        R3Vector V = eye - point;
        V.Normalize();
        RNScalar VR = V.Dot(R);
        if (RNIsNegativeOrZero(VR)) continue;

        // Return specular component of reflection
        sample_sum += (I * pow(VR,s)) * Sc * Ic;
    }

    // Return specular reflection from area
    RNArea area = circle.Area();
    RNRgb sample_mean = sample_sum;
    if (sample_count > 0) sample_mean /= sample_count;
    return area * sample_mean;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %r8, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
movq %rdi, %rbx
cmpl $0x0, 0x20(%rsi)
je 0x3d173
movq %rsi, %r15
movq %r9, 0x60(%rsp)
movq %rbx, 0x18(%rsp)
movsd 0x98(%rdx), %xmm0
movsd %xmm0, 0x40(%rsp)
addq $0x50, %rdx
movq %rdx, 0x48(%rsp)
leaq 0x30(%rsi), %rax
movq %rax, 0x38(%rsp)
leaq 0x50(%rsi), %rax
movq %rax, 0x30(%rsp)
addq $0x78, %rsi
leaq 0x160(%rsp), %rbx
movq %rbx, %rdi
callq 0x6c044
movq %rbx, %rdi
callq 0x6c17e
cltq
imulq $0x18, %rax, %rdx
addq 0xf1157(%rip), %rdx     # 0x12dfa8
leaq 0xe8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6c6e6
movq %r14, %rdi
callq 0x6c336
leaq 0xd0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6c6e6
movq %r12, %rdi
callq 0x6c336
leaq 0xfa76f(%rip), %rsi     # 0x137600
leaq 0xa0(%rsp), %rdi
callq 0x7c020
movq %r15, 0x50(%rsp)
leaq 0x58(%r15), %rax
movq %rax, 0x58(%rsp)
xorl %r13d, %r13d
pushq $0x8
popq %r14
leaq 0xb8(%rsp), %rbp
leaq 0xf2236(%rip), %r12     # 0x12f0f8
subl $0x1, %r14d
jb 0x3d11c
callq 0x7dcd2
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
movsd 0x885ef(%rip), %xmm0    # 0xc54d0
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
callq 0x7dcd2
addsd %xmm0, %xmm0
addsd 0x885d5(%rip), %xmm0    # 0xc54d0
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
movsd (%rsp), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
ucomisd 0x88100(%rip), %xmm1    # 0xc5018
ja 0x3cec2
movl %r13d, 0x14(%rsp)
leaq 0x88(%rsp), %r13
movq %r13, %rdi
movq 0x58(%rsp), %rsi
movsd %xmm0, 0x20(%rsp)
callq 0x6b884
movq 0x50(%rsp), %rbx
movsd (%rsp), %xmm0
mulsd 0x70(%rbx), %xmm0
movq %rbp, %rdi
leaq 0xe8(%rsp), %rsi
callq 0x6c72a
movq %r13, %rdi
movq %rbp, %rsi
callq 0x6bbc4
movsd 0x20(%rsp), %xmm0
mulsd 0x70(%rbx), %xmm0
movq %rbp, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x6c72a
movq %r13, %rdi
movq %rbp, %rsi
callq 0x6bbc4
movsd 0x28(%rbx), %xmm0
movapd %xmm0, 0x20(%rsp)
movq 0x70(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x48146
movsd 0xa0(%rbx), %xmm1
mulsd %xmm0, %xmm1
addsd 0x98(%rbx), %xmm1
movsd %xmm1, 0x78(%rsp)
mulsd %xmm0, %xmm0
mulsd 0xa8(%rbx), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd (%r12), %xmm0
movapd %xmm0, (%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x6ba51
movq %rbp, %rdi
callq 0x6c336
movq %r15, %rbx
movq 0x60(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x6c2ae
movsd (%r12), %xmm1
ucomisd %xmm0, %xmm1
jae 0x3d17c
addsd %xmm0, %xmm0
leaq 0x130(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x6c72a
leaq 0x148(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x6c9dd
movq %r13, %rdi
movq 0x68(%rsp), %rsi
movq %rbx, %rdx
callq 0x6ba51
movq %r13, %rdi
callq 0x6c336
movq %r13, %rdi
movq %r15, %rsi
callq 0x6c2ae
movq %r12, %r15
movsd (%r12), %xmm1
ucomisd %xmm0, %xmm1
jae 0x3d104
movsd 0x80(%rsp), %xmm2
addsd 0x78(%rsp), %xmm2
movapd 0x20(%rsp), %xmm3
movapd %xmm3, %xmm1
divsd %xmm2, %xmm1
movapd (%rsp), %xmm4
cmpnltsd %xmm2, %xmm4
movapd %xmm4, %xmm2
andnpd %xmm1, %xmm2
andpd %xmm3, %xmm4
orpd %xmm2, %xmm4
movapd %xmm4, (%rsp)
movsd 0x40(%rsp), %xmm1
callq 0x220b0
mulsd (%rsp), %xmm0
leaq 0x100(%rsp), %rbx
movq %rbx, %rdi
movq 0x48(%rsp), %rsi
callq 0x3d3cb
leaq 0x118(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq 0x38(%rsp), %rdx
callq 0x3d360
leaq 0xa0(%rsp), %rdi
movq %r12, %rsi
callq 0x7c0ee
movl 0x14(%rsp), %r13d
incl %r13d
movq %r15, %r12
leaq 0xb8(%rsp), %rbp
jmp 0x3cec2
movq 0x30(%rsp), %rdi
callq 0x5ee84
movsd %xmm0, (%rsp)
leaq 0x88(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x7c020
testl %r13d, %r13d
jle 0x3d157
cvtsi2sd %r13d, %xmm0
leaq 0x88(%rsp), %rdi
callq 0x7c19a
leaq 0x88(%rsp), %rsi
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movsd (%rsp), %xmm0
callq 0x3d3cb
jmp 0x3d190
leaq 0xfa486(%rip), %rsi     # 0x137600
jmp 0x3d188
leaq 0xfa47d(%rip), %rsi     # 0x137600
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x7c020
movq %rbx, %rax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop