// File: 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
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1e0(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x4d20
movq -0x10(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x3cf0
leaq -0x70(%rbp), %rdi
leaq -0x88(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x8fd0
movq -0x18(%rbp), %rdx
addq $0x20, %rdx
leaq -0x58(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0xbc90
leaq -0xa8(%rbp), %rdi
movsd 0x9db0(%rip), %xmm2     # 0x17060
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x3e10
movq -0x20(%rbp), %rax
movq -0xa8(%rbp), %rcx
movq %rcx, (%rax)
movq -0xa0(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x98(%rbp), %rcx
movq %rcx, 0x10(%rax)
leaq -0xc0(%rbp), %rdi
callq 0x4d20
movq -0x10(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x3cf0
movq -0x18(%rbp), %rsi
addq $0x20, %rsi
leaq -0xe8(%rbp), %rdi
callq 0x3c50
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xd3e5
movq -0x18(%rbp), %rsi
addq $0x20, %rsi
leaq -0x100(%rbp), %rdi
callq 0xd5b0
movq -0x1e0(%rbp), %rax
movq -0x100(%rbp), %rcx
movq %rcx, -0x40(%rbp)
movq -0xf8(%rbp), %rcx
movq %rcx, -0x38(%rbp)
movq -0xf0(%rbp), %rcx
movq %rcx, -0x30(%rbp)
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x1f0(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x118(%rbp), %rdi
callq 0x3cf0
movq -0x18(%rbp), %rsi
addq $0x20, %rsi
leaq -0x118(%rbp), %rdi
callq 0x3c50
movaps %xmm0, %xmm1
movsd -0x1f0(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x1e8(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x130(%rbp), %rdi
callq 0x3cf0
leaq -0x130(%rbp), %rdi
callq 0x95f0
movaps %xmm0, %xmm1
movsd -0x1e8(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0xd0(%rbp)
jmp 0xd493
movq -0x1e0(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x20(%rcx), %rdx
movq %rdx, -0x40(%rbp)
movq 0x28(%rcx), %rdx
movq %rdx, -0x38(%rbp)
movq 0x30(%rcx), %rcx
movq %rcx, -0x30(%rbp)
movsd 0x9c50(%rip), %xmm0     # 0x17060
divsd 0x8(%rax), %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x148(%rbp), %rdi
callq 0x3cf0
movq -0x18(%rbp), %rsi
addq $0x20, %rsi
leaq -0x148(%rbp), %rdi
callq 0x3c50
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x1f8(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x160(%rbp), %rdi
callq 0x3cf0
leaq -0x160(%rbp), %rdi
callq 0x95f0
movaps %xmm0, %xmm1
movsd -0x1f8(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0xd0(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x178(%rbp), %rdi
callq 0x3cf0
movsd -0x90(%rbp), %xmm0
leaq -0x178(%rbp), %rdi
leaq -0x40(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
callq 0xd630
testb $0x1, %al
jne 0xd4c8
jmp 0xd4eb
movq -0x1e0(%rbp), %rax
movsd -0xd0(%rbp), %xmm0
movsd 0x8(%rax), %xmm1
callq 0xd7e0
movsd %xmm0, -0xc8(%rbp)
jmp 0xd4fb
movsd 0x9b6d(%rip), %xmm0     # 0x17060
movsd %xmm0, -0xc8(%rbp)
callq 0x5970
movaps %xmm0, %xmm1
movsd -0xc8(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xd540
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
leaq -0x1a8(%rbp), %rdi
leaq -0x58(%rbp), %rdx
callq 0xaf10
movq -0x28(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
movl $0x30, %edx
callq 0x31a0
jmp 0xd570
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
leaq -0x1d8(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
callq 0xaf10
movq -0x28(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
movl $0x30, %edx
callq 0x31a0
movb $0x1, %al
andb $0x1, %al
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
nopl (%rax)