// File: /workspace/github_cpp_makefile_star2_O2/Lut99[P]RayTracer/src/lib/materials/Dielectric.cpp
// Function: RayTracer::Dielectric::scatter(RayTracer::Ray const&, RayTracer::HitRecord const&, RayTracer::Vec3&, RayTracer::Ray&) const
bool Dielectric::scatter(const Ray& ray_in, const HitRecord& record, Vec3& attenuation, Ray& ray_out) const {
    Vec3 outward_normal;
    double ni_over_nt;
    attenuation = this->albedo;
    Vec3 refracted;

    double reflect_prob;
    double cosine;

    // I think this decides on what side of the sphere we hit; in to out or out to in.
    if (dot(ray_in.direction, record.normal) > 0) {
        outward_normal = -record.normal;
        ni_over_nt = this->ref_idx;
        cosine = this->ref_idx * dot(ray_in.direction, record.normal) / ray_in.direction.length();
    } else {
        outward_normal = record.normal;
        ni_over_nt = 1.0 / this->ref_idx;
        cosine = -dot(ray_in.direction, record.normal) / ray_in.direction.length();
    }

    if (this->refract(ray_in.direction, outward_normal, ni_over_nt, refracted)) {
        reflect_prob = this->schlick(cosine);
    } else {
        reflect_prob = 1.0;
    }

    if (random_double() < reflect_prob) {
        ray_out = Ray(record.hitpoint, this->reflect(ray_in.direction, record.normal));
    } else {
        ray_out = Ray(record.hitpoint, refracted);
    }

   if (this->refract(ray_in.direction, outward_normal, ni_over_nt, refracted)) {
       ray_out = Ray(record.hitpoint, refracted);
   } else {
       ray_out = Ray(record.hitpoint, this->reflect(ray_in.direction, record.normal));
   }

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r8, %rbx
movq %rcx, %r13
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
leaq 0x58(%rsp), %rdi
callq 0x7e44c
leaq 0x10(%r12), %rsi
movq %r13, %rdi
callq 0x7e47e
leaq 0x38(%rsp), %rdi
callq 0x7e44c
addq $0x20, %r15
leaq 0x38(%r14), %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x7ee56
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq %rbx, 0x28(%rsp)
jbe 0x86cbc
leaq 0x78(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x7e4fc
leaq 0x58(%rsp), %rdi
movq %rbp, %rsi
callq 0x7e47e
movsd 0x30(%r12), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x7ee56
mulsd 0x8(%rsp), %xmm0
jmp 0x86cf1
leaq 0x58(%rsp), %rdi
movq %r13, %rsi
callq 0x7e47e
movsd 0x436f(%rip), %xmm0     # 0x8b040
divsd 0x30(%r12), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x7ee56
xorpd 0xb4df(%rip), %xmm0     # 0x921d0
movapd %xmm0, 0x10(%rsp)
movq %r15, %rdi
callq 0x7e7b0
movsd %xmm0, 0x30(%rsp)
movq (%r12), %rax
leaq 0x58(%rsp), %rdx
leaq 0x38(%rsp), %rcx
movq %r12, %rdi
movq %r15, %rsi
movsd 0x8(%rsp), %xmm0
callq *0x38(%rax)
testb %al, %al
je 0x86d3e
movapd 0x10(%rsp), %xmm0
divsd 0x30(%rsp), %xmm0
movq (%r12), %rax
movq %r12, %rdi
callq *0x40(%rax)
jmp 0x86d46
movsd 0x42fa(%rip), %xmm0     # 0x8b040
movsd %xmm0, 0x10(%rsp)
callq 0x82648
addq $0x18, %r14
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x86d8c
movq (%r12), %rax
leaq 0xb8(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq *0x30(%rax)
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
jmp 0x86d9c
leaq 0x78(%rsp), %rbx
leaq 0x38(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x724c4
movq 0x28(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x8642a
movq (%r12), %rax
leaq 0x58(%rsp), %rdx
leaq 0x38(%rsp), %rcx
movq %r12, %rdi
movq %r15, %rsi
movsd 0x8(%rsp), %xmm0
callq *0x38(%rax)
testb %al, %al
je 0x86def
leaq 0x78(%rsp), %rbx
leaq 0x38(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x724c4
movq %rbp, %rdi
movq %rbx, %rsi
jmp 0x86e23
movq (%r12), %rax
leaq 0xb8(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq *0x30(%rax)
leaq 0x78(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x724c4
movq %rbp, %rdi
movq %r15, %rsi
callq 0x8642a
movb $0x1, %al
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq