// File: alan0201tw[P]RayTracer/src/util.cpp
// Function: refract(vec3 const&, vec3 const&, float, vec3&)
bool refract(const vec3& _ray_direction, const vec3& _normal, float ni_over_nt, vec3& refracted)
{
    vec3 unit_direction = unit_vector(_ray_direction);
    float dt = dot(unit_direction, _normal);
    float discriminant = 1.0f - ni_over_nt * ni_over_nt * (1 - dt * dt);
    if(discriminant > 0.0f)
    {
        refracted = ni_over_nt * (unit_direction - _normal * dt) - _normal * sqrt(discriminant);
        return true;
    }
    return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movss %xmm0, -0x1c(%rbp)
movq %rdx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
movl %ecx, -0x38(%rbp)
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movsd -0x50(%rbp), %xmm0
movss -0x48(%rbp), %xmm1
callq 0x18b10
movss %xmm1, -0x58(%rbp)
movlpd %xmm0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x34(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x34(%rbp), %rdi
callq 0x11900
movss %xmm0, -0x64(%rbp)
movss -0x1c(%rbp), %xmm0
mulss -0x1c(%rbp), %xmm0
movss -0x64(%rbp), %xmm1
movss -0x64(%rbp), %xmm2
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm1
movss 0x5988b(%rip), %xmm2    # 0x7e1fc
addss %xmm2, %xmm1
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
mulss %xmm1, %xmm0
movss 0x5986e(%rip), %xmm1    # 0x7e1fc
addss %xmm1, %xmm0
movss %xmm0, -0x68(%rbp)
movss -0x68(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x24b09
movss -0x1c(%rbp), %xmm0
movss %xmm0, -0x124(%rbp)
movq -0x18(%rbp), %rdi
movss -0x64(%rbp), %xmm0
callq 0x10120
movss %xmm1, -0xa8(%rbp)
movlpd %xmm0, -0xb0(%rbp)
movl -0xa8(%rbp), %eax
movl %eax, -0x98(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xa0(%rbp)
leaq -0x34(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x11890
movaps %xmm0, %xmm2
movss -0x124(%rbp), %xmm0
movaps %xmm2, -0x120(%rbp)
movaps %xmm1, %xmm2
movaps -0x120(%rbp), %xmm1
movss %xmm2, -0xb8(%rbp)
movlpd %xmm1, -0xc0(%rbp)
movl -0xb8(%rbp), %eax
movl %eax, -0x88(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xfaa0
movss %xmm1, -0xc8(%rbp)
movlpd %xmm0, -0xd0(%rbp)
movl -0xc8(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x108(%rbp)
movss -0x68(%rbp), %xmm0
callq 0x11960
movq -0x108(%rbp), %rdi
callq 0x10120
movss %xmm1, -0xe8(%rbp)
movlpd %xmm0, -0xf0(%rbp)
movl -0xe8(%rbp), %eax
movl %eax, -0xd8(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x11890
movss %xmm1, -0xf8(%rbp)
movlpd %xmm0, -0x100(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0x74(%rbp)
movl -0xf8(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x28(%rbp), %rax
movq -0x74(%rbp), %rcx
movq %rcx, (%rax)
movl -0x6c(%rbp), %ecx
movl %ecx, 0x8(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x24b0d
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopl (%rax,%rax)