// File: Crawler995[P]Cpp-Ray-Tracing/libs/render_engine.cpp
// Function: RenderEngine::ray_tracing_recursive(Ray3, GeometryUnion&, int)
Color RenderEngine::ray_tracing_recursive(
    Ray3 ray,
    GeometryUnion &geo_union,
    int reflect_times
) {
    IntersectResult res = geo_union.intersect(ray);
    if(res.geometry) {
        double specular_factor = res.geometry -> get_material() -> get_specular_factor();
        Color color = res.geometry -> get_material() -> sample(ray, res.position, res.normal);
        color = color.multiply(1.0 - specular_factor);

        if(specular_factor > 0 && reflect_times < max_reflect_times) {
            Vector3 dir = res.normal.scale(-2.0 * res.normal.dot(ray.get_dir())).add(ray.get_dir()).normalize();
            Ray3 reflect_ray(res.position, dir);

            Color reflect_color = ray_tracing_recursive(reflect_ray, geo_union, reflect_times + 1).multiply(specular_factor);
            
            color = color.add(reflect_color);
        }

        color.ensure_not_overflow();
        return color;
    }
    else {
        return Color(0, 0, 0);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x370, %rsp            # imm = 0x370
movq %rsi, %rax
leaq 0x10(%rbp), %rsi
movq %rsi, -0x2f0(%rbp)
movq %rdi, -0x18(%rbp)
movq %rax, -0x20(%rbp)
movl %edx, -0x24(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x2e8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x2e0(%rbp)
leaq -0x98(%rbp), %rdi
movl $0x30, %edx
callq 0x20e0
movq -0x2e0(%rbp), %rsi
movups -0x98(%rbp), %xmm0
movups -0x88(%rbp), %xmm1
movups -0x78(%rbp), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
leaq -0x68(%rbp), %rdi
callq 0x4210
cmpq $0x0, -0x68(%rbp)
je 0x8146
movq -0x68(%rbp), %rdi
callq 0x3ba0
movq %rax, %rdi
callq 0x5040
movsd %xmm0, -0xa0(%rbp)
movq -0x68(%rbp), %rdi
callq 0x3ba0
movq %rax, %rdi
movq -0x2f0(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movaps %xmm2, -0xb0(%rbp)
movaps %xmm1, -0xc0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0xe0(%rbp)
movups -0x58(%rbp), %xmm0
movaps %xmm0, -0xf0(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x100(%rbp)
movups -0x40(%rbp), %xmm0
movaps %xmm0, -0x110(%rbp)
movq (%rdi), %rax
movq (%rax), %rax
movq -0x100(%rbp), %rdx
movq %rsp, %rcx
movq %rdx, 0x58(%rcx)
movaps -0x110(%rbp), %xmm0
movups %xmm0, 0x48(%rcx)
movq -0xe0(%rbp), %rdx
movq %rdx, 0x40(%rcx)
movaps -0xf0(%rbp), %xmm0
movups %xmm0, 0x30(%rcx)
movaps -0xd0(%rbp), %xmm0
movaps -0xc0(%rbp), %xmm1
movaps -0xb0(%rbp), %xmm2
movups %xmm2, 0x20(%rcx)
movups %xmm1, 0x10(%rcx)
movups %xmm0, (%rcx)
callq *%rax
movl %edx, -0x118(%rbp)
movq %rax, -0x120(%rbp)
movl -0x118(%rbp), %eax
movl %eax, -0x8(%rbp)
movq -0x120(%rbp), %rax
movq %rax, -0x10(%rbp)
movsd -0xa0(%rbp), %xmm1
movsd 0x1242(%rip), %xmm0     # 0x9098
subsd %xmm1, %xmm0
leaq -0x10(%rbp), %rdi
callq 0x4d50
movl %edx, -0x138(%rbp)
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rax
movq %rax, -0x12c(%rbp)
movl -0x138(%rbp), %eax
movl %eax, -0x124(%rbp)
movq -0x12c(%rbp), %rax
movq %rax, -0x10(%rbp)
movl -0x124(%rbp), %eax
movl %eax, -0x8(%rbp)
movsd -0xa0(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x813b
movq -0x2e8(%rbp), %rcx
movl -0x24(%rbp), %eax
cmpl 0xc(%rcx), %eax
jge 0x813b
movq -0x2f0(%rbp), %rsi
leaq -0x40(%rbp), %rax
movq %rax, -0x308(%rbp)
leaq -0x1a0(%rbp), %rdi
callq 0x3470
movq -0x308(%rbp), %rdi
movq -0x190(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x10(%rax)
movups -0x1a0(%rbp), %xmm0
movups %xmm0, (%rax)
callq 0x30d0
movq -0x308(%rbp), %rsi
movsd 0x1281(%rip), %xmm1     # 0x9198
mulsd %xmm1, %xmm0
leaq -0x188(%rbp), %rdi
movq %rdi, -0x300(%rbp)
callq 0x3030
movq -0x2f0(%rbp), %rsi
leaq -0x1b8(%rbp), %rdi
callq 0x3470
movq -0x300(%rbp), %rsi
movq -0x1a8(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x10(%rax)
movups -0x1b8(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x170(%rbp), %rdi
movq %rdi, -0x2f8(%rbp)
callq 0x2e70
movq -0x2f8(%rbp), %rsi
leaq -0x158(%rbp), %rdi
callq 0x2d90
movq -0x48(%rbp), %rax
movq %rax, -0x1f0(%rbp)
movups -0x58(%rbp), %xmm0
movaps %xmm0, -0x200(%rbp)
movq -0x148(%rbp), %rax
movq %rax, -0x210(%rbp)
movups -0x158(%rbp), %xmm0
movaps %xmm0, -0x220(%rbp)
movq -0x210(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x28(%rax)
movaps -0x220(%rbp), %xmm0
movups %xmm0, 0x18(%rax)
movq -0x1f0(%rbp), %rcx
movq %rcx, 0x10(%rax)
movaps -0x200(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x1e8(%rbp), %rdi
callq 0x3350
movq -0x2e8(%rbp), %rdi
movups -0x1e8(%rbp), %xmm0
movups -0x1d8(%rbp), %xmm1
movups -0x1c8(%rbp), %xmm2
movaps %xmm2, -0x250(%rbp)
movaps %xmm1, -0x260(%rbp)
movaps %xmm0, -0x270(%rbp)
movq -0x20(%rbp), %rsi
movl -0x24(%rbp), %edx
incl %edx
movaps -0x270(%rbp), %xmm0
movaps -0x260(%rbp), %xmm1
movaps -0x250(%rbp), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x7cd0
movl %edx, -0x278(%rbp)
movq %rax, -0x280(%rbp)
movl -0x278(%rbp), %eax
movl %eax, -0x238(%rbp)
movq -0x280(%rbp), %rax
movq %rax, -0x240(%rbp)
movsd -0xa0(%rbp), %xmm0
leaq -0x240(%rbp), %rdi
callq 0x4d50
movl %edx, -0x288(%rbp)
movq %rax, -0x290(%rbp)
movl -0x288(%rbp), %eax
movl %eax, -0x228(%rbp)
movq -0x290(%rbp), %rax
movq %rax, -0x230(%rbp)
movl -0x228(%rbp), %eax
movl %eax, -0x2a0(%rbp)
movq -0x230(%rbp), %rax
movq %rax, -0x2a8(%rbp)
movl -0x2a0(%rbp), %eax
movl %eax, -0x2b0(%rbp)
movq -0x2a8(%rbp), %rax
movq %rax, -0x2b8(%rbp)
movq -0x2b8(%rbp), %rsi
movl -0x2b0(%rbp), %edx
leaq -0x10(%rbp), %rdi
callq 0x4cf0
movl %edx, -0x2c0(%rbp)
movq %rax, -0x2c8(%rbp)
movq -0x2c8(%rbp), %rax
movq %rax, -0x29c(%rbp)
movl -0x2c0(%rbp), %eax
movl %eax, -0x294(%rbp)
movq -0x29c(%rbp), %rax
movq %rax, -0x10(%rbp)
movl -0x294(%rbp), %eax
movl %eax, -0x8(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x4e30
jmp 0x8155
leaq -0x10(%rbp), %rdi
xorl %ecx, %ecx
movl %ecx, %esi
movl %ecx, %edx
callq 0x4c50
movl -0x8(%rbp), %eax
movl %eax, -0x2d0(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x2d8(%rbp)
movq -0x2d8(%rbp), %rax
movl -0x2d0(%rbp), %edx
addq $0x370, %rsp            # imm = 0x370
popq %rbp
retq
nop