// File: /workspace/github_cpp_makefile_star2_O3/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 0x71e94
leaq 0x10(%r12), %rsi
movq %r13, %rdi
callq 0x71ec8
leaq 0x28(%rsp), %rdi
callq 0x71e94
addq $0x20, %r15
leaq 0x38(%r14), %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x72ca9
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x7e747
leaq 0x68(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x71f46
leaq 0x48(%rsp), %rdi
movq %rbp, %rsi
callq 0x71ec8
movsd 0x30(%r12), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x72ca9
mulsd 0x8(%rsp), %xmm0
jmp 0x7e77c
leaq 0x48(%rsp), %rdi
movq %r13, %rsi
callq 0x71ec8
movsd 0x48e4(%rip), %xmm0     # 0x83040
divsd 0x30(%r12), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x72ca9
xorpd 0xb9b4(%rip), %xmm0     # 0x8a130
movapd %xmm0, 0x10(%rsp)
movq %r15, %rdi
callq 0x72322
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 0x7e7c9
movapd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
movq (%r12), %rax
movq %r12, %rdi
callq *0x40(%rax)
jmp 0x7e7d1
movsd 0x486f(%rip), %xmm0     # 0x83040
movsd %xmm0, 0x10(%rsp)
callq 0x78084
addq $0x18, %r14
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x7e819
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 0x7e829
leaq 0x68(%rsp), %rbp
leaq 0x28(%rsp), %rdx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x620f4
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x71ec8
leaq 0x20(%rbx), %rdi
leaq 0x88(%rsp), %rsi
callq 0x71ec8
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 0x7e87d
leaq 0x68(%rsp), %r15
leaq 0x28(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rsi
jmp 0x7e8a6
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 0x620f4
movq %rbx, %rdi
movq %r15, %rsi
callq 0x71ec8
addq $0x20, %rbx
leaq 0x88(%rsp), %rsi
movq %rbx, %rdi
callq 0x71ec8
movb $0x1, %al
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq