// File: /workspace/github_cpp_makefile_star2_O3/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 $0x168, %rsp            # imm = 0x168
movq %r8, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq %rdi, %rbx
cmpl $0x0, 0x20(%rsi)
je 0x41545
movq %rsi, %rbp
movq %r9, 0x50(%rsp)
movq %rbx, 0x18(%rsp)
movq %rdx, 0x40(%rsp)
movsd 0x98(%rdx), %xmm0
movsd %xmm0, 0x38(%rsp)
leaq 0x50(%rsi), %rax
movq %rax, 0x30(%rsp)
addq $0x78, %rsi
leaq 0x150(%rsp), %rbx
movq %rbx, %rdi
callq 0x75e6e
movq %rbx, %rdi
callq 0x75faa
cltq
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq 0xfbdcc(%rip), %rdx     # 0x13cfa0
leaq 0xf0(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x76792
movq %r14, %rdi
callq 0x76174
leaq 0xd8(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x76792
movq %r12, %rdi
callq 0x76174
leaq 0x1053cc(%rip), %rsi    # 0x1465e0
leaq 0xa8(%rsp), %rdi
callq 0x873e2
leaq 0x58(%rbp), %rax
movq %rax, 0x48(%rsp)
xorl %r14d, %r14d
movl $0x8, %r12d
leaq 0xc0(%rsp), %r15
leaq 0xfce96(%rip), %rbx     # 0x13e0d8
callq 0x89368
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
movsd 0x99279(%rip), %xmm0    # 0xda4d0
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
callq 0x89368
addsd %xmm0, %xmm0
addsd 0x9925f(%rip), %xmm0    # 0xda4d0
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
movsd (%rsp), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
ucomisd 0x98d8a(%rip), %xmm1    # 0xda018
ja 0x414d0
movl %r12d, 0x10(%rsp)
movl %r14d, 0x14(%rsp)
leaq 0x78(%rsp), %r13
movq %r13, %rdi
movq 0x48(%rsp), %rsi
movsd %xmm0, 0x20(%rsp)
callq 0x753da
movsd (%rsp), %xmm0
mulsd 0x70(%rbp), %xmm0
movq %r15, %rdi
leaq 0xf0(%rsp), %rsi
callq 0x767fa
movq %r13, %rdi
movq %r15, %rsi
callq 0x757ac
movsd 0x20(%rsp), %xmm0
mulsd 0x70(%rbp), %xmm0
movq %r15, %rdi
leaq 0xd8(%rsp), %rsi
callq 0x767fa
movq %r13, %rdi
movq %r15, %rsi
callq 0x757ac
movsd 0x28(%rbp), %xmm0
movapd %xmm0, 0x20(%rsp)
movq %rbp, %r14
movq %r15, %rbp
movq 0x60(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x4e0d4
movsd 0xa0(%r14), %xmm1
mulsd %xmm0, %xmm1
addsd 0x98(%r14), %xmm1
movsd %xmm1, 0x68(%rsp)
mulsd %xmm0, %xmm0
movq %r14, %r12
mulsd 0xa8(%r14), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd (%rbx), %xmm0
movapd %xmm0, (%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x75626
movq %rbp, %rdi
callq 0x76174
movq %r15, %r13
movq 0x50(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x760ea
movsd (%rbx), %xmm1
ucomisd %xmm0, %xmm1
jae 0x4154e
addsd %xmm0, %xmm0
leaq 0x120(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x767fa
leaq 0x138(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x76adb
movq %r14, %rdi
movq 0x58(%rsp), %rsi
movq %r13, %rdx
callq 0x75626
movq %r14, %rdi
callq 0x76174
movq %r14, %rdi
movq %r15, %rsi
callq 0x760ea
movq %rbx, %r15
movsd (%rbx), %xmm1
ucomisd %xmm0, %xmm1
movq %r12, %rbp
jae 0x414b8
movsd 0x70(%rsp), %xmm2
addsd 0x68(%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 0x38(%rsp), %xmm1
callq 0x22090
movapd (%rsp), %xmm2
mulsd %xmm0, %xmm2
movq 0x40(%rsp), %rax
movsd 0x50(%rax), %xmm0
mulsd %xmm2, %xmm0
movsd 0x58(%rax), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x60(%rax), %xmm2
leaq 0x90(%rsp), %rdi
callq 0x87400
movsd 0x90(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
mulsd 0x30(%rbp), %xmm0
mulsd 0x38(%rbp), %xmm1
movsd 0xa0(%rsp), %xmm2
mulsd 0x40(%rbp), %xmm2
leaq 0x108(%rsp), %rbx
movq %rbx, %rdi
callq 0x87400
leaq 0xa8(%rsp), %rdi
movq %rbx, %rsi
callq 0x874b2
movl 0x14(%rsp), %r14d
incl %r14d
movq %r15, %rbx
leaq 0xc0(%rsp), %r15
movl 0x10(%rsp), %r12d
decl %r12d
jne 0x41242
movq 0x30(%rsp), %rdi
callq 0x676ea
movsd %xmm0, (%rsp)
leaq 0x78(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x873e2
testl %r14d, %r14d
jle 0x41511
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
leaq 0x78(%rsp), %rdi
callq 0x8755e
movsd 0x78(%rsp), %xmm0
movsd (%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x80(%rsp), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x88(%rsp), %xmm2
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x87400
jmp 0x41562
leaq 0x105094(%rip), %rsi    # 0x1465e0
jmp 0x4155a
leaq 0x10508b(%rip), %rsi    # 0x1465e0
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x873e2
movq %rbx, %rax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop