// File: /workspace/github_cpp_makefile_star2_O2/2TallTim[P]modern-raytracing/./material.hpp
// Function: dielectric::scatter(ray const&, hit_record const&, vec3&, ray&) const
virtual bool scatter(const ray& r_in,
                         const hit_record& rec,
                         vec3& attenuation,
                         ray& scattered) const {
        vec3 outward_normal;
        vec3 reflected = ray::reflect(unit_vector(r_in.direction()), rec.normal);
        double ni_over_nt;
        attenuation = vec3(1,1,1);
        vec3 refracted;
        double reflect_prob;
        double cosine;
        if( dot(r_in.direction(), rec.normal) > 0){
            outward_normal = -rec.normal;
            ni_over_nt = ref_idx;
            cosine = ref_idx * dot(r_in.direction(), rec.normal) / r_in.direction().length();
        }else{
            outward_normal = rec.normal;
            ni_over_nt = 1.0 / ref_idx;
            cosine = -dot(r_in.direction(), rec.normal) / r_in.direction().length();
        }

        if(ray::refract(r_in.direction(), outward_normal, ni_over_nt, refracted)){
            reflect_prob = ray::schlick(cosine, ref_idx);
        } else {
            reflect_prob = 1.0;
        }
        if(rng::gen01()< reflect_prob){
            scattered = ray(rec.p, reflected);
        }else{
            scattered = ray(rec.p, refracted);
        }
        
        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, %r12
movq %rdi, %r15
leaq 0x18(%rsi), %rax
movq %rax, 0x8(%rsp)
movsd 0x28(%rsi), %xmm0
movupd 0x18(%rsi), %xmm1
movapd %xmm1, %xmm2
mulpd %xmm1, %xmm2
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
movapd %xmm1, %xmm3
mulsd %xmm1, %xmm3
addsd %xmm2, %xmm3
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
sqrtsd %xmm2, %xmm2
divsd %xmm2, %xmm0
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
divpd %xmm2, %xmm1
leaq 0x10(%rsp), %rsi
movapd %xmm1, (%rsi)
movsd %xmm0, 0x10(%rsi)
leaq 0x20(%rdx), %rbp
leaq 0xb0(%rsp), %rdi
movq %rbp, %rdx
callq 0x60c4
movaps 0x3cf0(%rip), %xmm0     # 0xa1c0
movups %xmm0, (%r13)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x10(%r13)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
andq $0x0, 0x80(%rsp)
movsd 0x18(%r12), %xmm2
movsd 0x20(%r12), %xmm4
movsd 0x28(%r12), %xmm3
movupd 0x20(%r14), %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
movsd %xmm4, 0x58(%rsp)
mulsd %xmm4, %xmm1
movsd %xmm2, 0x68(%rsp)
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
movsd 0x30(%r14), %xmm1
movsd %xmm3, 0x60(%rsp)
mulsd %xmm1, %xmm3
addsd %xmm2, %xmm3
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm3
jbe 0x657b
movapd 0x3abb(%rip), %xmm2     # 0xa010
xorpd %xmm2, %xmm0
xorpd %xmm2, %xmm1
movapd %xmm0, 0x90(%rsp)
movlpd %xmm1, 0xa0(%rsp)
movsd 0x8(%r15), %xmm0
mulsd %xmm0, %xmm3
jmp 0x65a9
movq 0x10(%rbp), %rax
movq %rax, 0xa0(%rsp)
movups (%rbp), %xmm0
movaps %xmm0, 0x90(%rsp)
movsd 0x3ac5(%rip), %xmm0     # 0xa060
divsd 0x8(%r15), %xmm0
xorpd 0x3a67(%rip), %xmm3     # 0xa010
movapd %xmm3, 0x40(%rsp)
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rax
leaq 0x10(%rsp), %rdi
movq %rax, 0x10(%rdi)
movups (%rcx), %xmm1
movaps %xmm1, (%rdi)
leaq 0x90(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x672a
testb %al, %al
je 0x666a
movsd 0x58(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd 0x68(%rsp), %xmm0
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
movsd 0x60(%rsp), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
sqrtsd %xmm1, %xmm0
movapd 0x40(%rsp), %xmm2
divsd %xmm0, %xmm2
movsd 0x8(%r15), %xmm1
movsd 0x3a3d(%rip), %xmm0     # 0xa060
movapd %xmm0, %xmm3
subsd %xmm1, %xmm3
addsd %xmm0, %xmm1
divsd %xmm1, %xmm3
mulsd %xmm3, %xmm3
movsd %xmm3, 0x8(%rsp)
movapd %xmm0, %xmm1
subsd %xmm3, %xmm1
movsd %xmm1, 0x40(%rsp)
subsd %xmm2, %xmm0
movsd 0x3b09(%rip), %xmm1     # 0xa160
callq 0x32f0
mulsd 0x40(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
jmp 0x6672
movsd 0x39ee(%rip), %xmm0     # 0xa060
movsd %xmm0, 0x8(%rsp)
callq 0x3d54
addq $0x8, %r14
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x66b6
movq 0x10(%r14), %rax
movq %rax, 0x20(%rsp)
movups (%r14), %xmm0
movaps %xmm0, 0x10(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x38(%rsp)
movups 0xb0(%rsp), %xmm1
jmp 0x66da
movq 0x10(%r14), %rax
movq %rax, 0x20(%rsp)
movups (%r14), %xmm0
movaps %xmm0, 0x10(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x38(%rsp)
movaps 0x70(%rsp), %xmm1
movups %xmm1, 0x28(%rsp)
movups %xmm0, (%rbx)
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rbx)
movq 0x28(%rsp), %rax
movq %rax, 0x18(%rbx)
movq 0x30(%rsp), %rax
movq %rax, 0x20(%rbx)
movq 0x38(%rsp), %rax
movq %rax, 0x28(%rbx)
movb $0x1, %al
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq