// File: /workspace/github_cpp_makefile_star2_O1/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 $0xc8, %rsp
movq %r8, %rbx
movq %rcx, %r13
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
leaq 0x48(%rsp), %rdi
callq 0x729d4
leaq 0x10(%r12), %rsi
movq %r13, %rdi
callq 0x72a08
leaq 0x28(%rsp), %rdi
callq 0x729d4
addq $0x20, %r15
leaq 0x38(%r14), %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x737de
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x7efad
leaq 0x68(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x72a86
leaq 0x48(%rsp), %rdi
movq %rbp, %rsi
callq 0x72a08
movsd 0x30(%r12), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x737de
mulsd 0x8(%rsp), %xmm0
jmp 0x7efe2
leaq 0x48(%rsp), %rdi
movq %r13, %rsi
callq 0x72a08
movsd 0x507e(%rip), %xmm0     # 0x84040
divsd 0x30(%r12), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x737de
xorpd 0xc12e(%rip), %xmm0     # 0x8b110
movapd %xmm0, 0x10(%rsp)
movq %r15, %rdi
callq 0x72e36
movsd %xmm0, 0x20(%rsp)
movq (%r12), %rax
leaq 0x48(%rsp), %rdx
leaq 0x28(%rsp), %rcx
movq %r12, %rdi
movq %r15, %rsi
movsd 0x8(%rsp), %xmm0
callq *0x38(%rax)
testb %al, %al
je 0x7f02f
movapd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
movq (%r12), %rax
movq %r12, %rdi
callq *0x40(%rax)
jmp 0x7f037
movsd 0x5009(%rip), %xmm0     # 0x84040
movsd %xmm0, 0x10(%rsp)
callq 0x78a64
addq $0x18, %r14
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x7f07f
movq (%r12), %rax
leaq 0xa8(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq *0x30(%rax)
leaq 0x68(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
leaq 0xa8(%rsp), %rdx
jmp 0x7f08f
leaq 0x68(%rsp), %rbp
leaq 0x28(%rsp), %rdx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x62640
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x72a08
leaq 0x20(%rbx), %rdi
leaq 0x88(%rsp), %rsi
callq 0x72a08
movq (%r12), %rax
leaq 0x48(%rsp), %rdx
leaq 0x28(%rsp), %rcx
movq %r12, %rdi
movq %r15, %rsi
movsd 0x8(%rsp), %xmm0
callq *0x38(%rax)
testb %al, %al
je 0x7f0e3
leaq 0x68(%rsp), %r15
leaq 0x28(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rsi
jmp 0x7f10c
movq (%r12), %rax
leaq 0xa8(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq *0x30(%rax)
leaq 0x68(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x62640
movq %rbx, %rdi
movq %r15, %rsi
callq 0x72a08
addq $0x20, %rbx
leaq 0x88(%rsp), %rsi
movq %rbx, %rdi
callq 0x72a08
movb $0x1, %al
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq