// File: /workspace/github_cpp_makefile_star2_O1/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, 0x58(%rsp)
movq %rdi, %rbx
cmpl $0x0, 0x20(%rsi)
je 0x41a69
movq %rsi, %r13
movq %r9, 0x68(%rsp)
movq %rbx, 0x20(%rsp)
movq %rdx, 0x50(%rsp)
movsd 0x98(%rdx), %xmm0
movsd %xmm0, 0x48(%rsp)
leaq 0x50(%rsi), %rax
movq %rax, 0x40(%rsp)
addq $0x78, %rsi
leaq 0x160(%rsp), %rbx
movq %rbx, %rdi
callq 0x76d62
movq %rbx, %rdi
callq 0x76eaa
cltq
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq 0xfd908(%rip), %rdx     # 0x13efa0
leaq 0x100(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x776a6
movq %r14, %rdi
callq 0x77074
leaq 0xe8(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x776a6
movq %r12, %rdi
callq 0x77074
leaq 0x106f08(%rip), %rsi    # 0x1485e0
leaq 0xb8(%rsp), %rdi
callq 0x88512
movq %r13, 0x28(%rsp)
leaq 0x58(%r13), %rax
movq %rax, 0x60(%rsp)
xorl %ebx, %ebx
leaq 0xd0(%rsp), %rbp
leaq 0xfe9d4(%rip), %r13     # 0x1400d8
movl $0x0, 0x8(%rsp)
xorl %r12d, %r12d
callq 0x8a700
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
movsd 0x9adac(%rip), %xmm0    # 0xdc4d0
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
callq 0x8a700
addsd %xmm0, %xmm0
addsd 0x9ad91(%rip), %xmm0    # 0xdc4d0
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
movsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
movl $0x4, %eax
ucomisd 0x9a8b6(%rip), %xmm1    # 0xdc018
ja 0x419cb
movl %ebx, 0xc(%rsp)
leaq 0x88(%rsp), %r14
movq %r14, %rdi
movq 0x60(%rsp), %rsi
movsd %xmm0, 0x30(%rsp)
callq 0x762ca
movq 0x28(%rsp), %r15
movsd 0x10(%rsp), %xmm0
mulsd 0x70(%r15), %xmm0
movq %rbp, %rdi
leaq 0x100(%rsp), %rsi
callq 0x77722
movq %r14, %rdi
movq %rbp, %rsi
callq 0x766a2
movsd 0x30(%rsp), %xmm0
mulsd 0x70(%r15), %xmm0
movq %rbp, %rdi
leaq 0xe8(%rsp), %rsi
callq 0x77722
movq %r14, %rdi
movq %rbp, %rsi
callq 0x766a2
movsd 0x28(%r15), %xmm0
movapd %xmm0, 0x30(%rsp)
movq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4ec74
movsd 0xa0(%r15), %xmm1
mulsd %xmm0, %xmm1
addsd 0x98(%r15), %xmm1
movsd %xmm1, 0x78(%rsp)
mulsd %xmm0, %xmm0
mulsd 0xa8(%r15), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd (%r13), %xmm0
movapd %xmm0, 0x10(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x7651c
movq %rbp, %rdi
callq 0x77074
movq 0x68(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x76fea
movsd (%r13), %xmm1
ucomisd %xmm0, %xmm1
jae 0x419ad
addsd %xmm0, %xmm0
leaq 0x130(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x77722
leaq 0x148(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x77a2b
movq %r14, %rdi
movq 0x58(%rsp), %rsi
movq %rbx, %rdx
callq 0x7651c
movq %r14, %rdi
callq 0x77074
movq %r14, %rdi
movq %r15, %rsi
callq 0x76fea
movsd (%r13), %xmm1
movl $0x4, %eax
ucomisd %xmm0, %xmm1
jae 0x4199c
movsd 0x80(%rsp), %xmm2
addsd 0x78(%rsp), %xmm2
movapd 0x30(%rsp), %xmm3
movapd %xmm3, %xmm1
divsd %xmm2, %xmm1
movapd 0x10(%rsp), %xmm4
cmpnltsd %xmm2, %xmm4
movapd %xmm4, %xmm2
andnpd %xmm1, %xmm2
andpd %xmm3, %xmm4
orpd %xmm2, %xmm4
movapd %xmm4, 0x10(%rsp)
movsd 0x48(%rsp), %xmm1
callq 0x220a0
movapd 0x10(%rsp), %xmm2
mulsd %xmm0, %xmm2
movq 0x50(%rsp), %rax
movsd 0x50(%rax), %xmm0
mulsd %xmm2, %xmm0
movsd 0x58(%rax), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x60(%rax), %xmm2
leaq 0xa0(%rsp), %rdi
callq 0x88530
movsd 0xa0(%rsp), %xmm0
movsd 0xa8(%rsp), %xmm1
movq 0x28(%rsp), %rax
mulsd 0x30(%rax), %xmm0
mulsd 0x38(%rax), %xmm1
movsd 0xb0(%rsp), %xmm2
mulsd 0x40(%rax), %xmm2
leaq 0x118(%rsp), %rbx
movq %rbx, %rdi
callq 0x88530
leaq 0xb8(%rsp), %rdi
movq %rbx, %rsi
callq 0x885e2
xorl %eax, %eax
leaq 0xfe735(%rip), %r13     # 0x1400d8
leaq 0xd0(%rsp), %rbp
jmp 0x419c3
movq 0x20(%rsp), %rdi
leaq 0x106c27(%rip), %rsi    # 0x1485e0
callq 0x88512
movl $0x1, %eax
movl 0xc(%rsp), %ebx
incl 0x8(%rsp)
testb $0x3, %al
jne 0x419e7
cmpl $0x7, %r12d
leal 0x1(%r12), %eax
setae %bl
movl %eax, %r12d
cmpl $0x8, %eax
jne 0x4170f
testb $0x1, %bl
movq 0x20(%rsp), %rbx
je 0x41a78
movq 0x40(%rsp), %rdi
callq 0x6847a
movsd %xmm0, 0x10(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x88512
movl 0x8(%rsp), %eax
testl %eax, %eax
jle 0x41a36
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
leaq 0x88(%rsp), %rdi
callq 0x8868e
movsd 0x88(%rsp), %xmm0
movsd 0x10(%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x90(%rsp), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x98(%rsp), %xmm2
movq %rbx, %rdi
callq 0x88530
jmp 0x41a78
leaq 0x106b70(%rip), %rsi    # 0x1485e0
movq %rbx, %rdi
callq 0x88512
movq %rbx, %rax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop