// File: logicx24[P]RayTracer/raytracer.cpp
// Function: Triangle::intersection(Ray&, float)
float intersection(Ray &ray, float eps) {
        vec3 normal = (edge2 ^ edge1).normalize();
        vec3 w0 = ray.pos - vertex1;
        float a = -(normal * w0);
        float b = normal * ray.vec;
        float r = a/b;

        if (fabs(b) < eps) {
            if (a == 0.0f) {
                return r;
            } else {
            	return MAXFLOAT;
            }
        }

        if (r < 0.0f) {
            return MAXFLOAT;
        }

        vec3 intersect = ray.pointAt(r);
        float uu, uv, vv, wu, wv, D;

        uu = edge1 * edge1;
        uv = edge1 * edge2;
        vv = edge2 * edge2;
        vec3 w = intersect - vertex1;
        wu = w * edge1;
        wv = w * edge2;
        D = uv * uv - uu * vv;

        float s, t;

        s = (uv * wv - vv * wu) / D;
        if (s < 0.0f || s > 1.0f) {
            return MAXFLOAT;
        }
        t = (uv * wu - uu * wv) / D;
        if (t < 0.0f || (s + t) > 1.0f) {
            return MAXFLOAT;
        }

        if (r < eps) {
            return MAXFLOAT;
        }
        return r;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movss %xmm0, -0x1c(%rbp)
movq -0x10(%rbp), %rdx
movq %rdx, -0xd0(%rbp)
movq %rdx, %rsi
addq $0x250, %rsi            # imm = 0x250
addq $0x238, %rdx            # imm = 0x238
leaq -0x50(%rbp), %rdi
callq 0xadc0
leaq -0x50(%rbp), %rdi
callq 0xaeb0
movq %rax, %rsi
leaq -0x38(%rbp), %rdi
callq 0x7e40
movq -0xd0(%rbp), %rdx
movq -0x18(%rbp), %rsi
addq $0x1f0, %rdx            # imm = 0x1F0
leaq -0x68(%rbp), %rdi
callq 0xa040
leaq -0x38(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0xaee0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x6c(%rbp)
movq -0x18(%rbp), %rsi
addq $0x18, %rsi
leaq -0x38(%rbp), %rdi
callq 0xaee0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x70(%rbp)
movss -0x6c(%rbp), %xmm0
divss -0x70(%rbp), %xmm0
movss %xmm0, -0x74(%rbp)
movss -0x70(%rbp), %xmm0
callq 0xaf40
movaps %xmm0, %xmm1
movss -0x1c(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0xa1fa
movss -0x6c(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0xa1e8
jp 0xa1e8
movss -0x74(%rbp), %xmm0
movss %xmm0, -0x4(%rbp)
jmp 0xa46c
movss 0x1ae40(%rip), %xmm0    # 0x25030
movss %xmm0, -0x4(%rbp)
jmp 0xa46c
xorps %xmm0, %xmm0
ucomiss -0x74(%rbp), %xmm0
jbe 0xa215
movss 0x1ae25(%rip), %xmm0    # 0x25030
movss %xmm0, -0x4(%rbp)
jmp 0xa46c
movq -0x18(%rbp), %rsi
movss -0x74(%rbp), %xmm0
leaq -0x90(%rbp), %rdi
callq 0xaf60
movq -0xd0(%rbp), %rsi
movq %rsi, %rdi
addq $0x238, %rdi            # imm = 0x238
addq $0x238, %rsi            # imm = 0x238
callq 0xaee0
movq -0xd0(%rbp), %rsi
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x94(%rbp)
movq %rsi, %rdi
addq $0x238, %rdi            # imm = 0x238
addq $0x250, %rsi            # imm = 0x250
callq 0xaee0
movq -0xd0(%rbp), %rsi
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x98(%rbp)
movq %rsi, %rdi
addq $0x250, %rdi            # imm = 0x250
addq $0x250, %rsi            # imm = 0x250
callq 0xaee0
movq -0xd0(%rbp), %rdx
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x9c(%rbp)
addq $0x1f0, %rdx            # imm = 0x1F0
leaq -0xc0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xa040
movq -0xd0(%rbp), %rsi
addq $0x238, %rsi            # imm = 0x238
leaq -0xc0(%rbp), %rdi
callq 0xaee0
movq -0xd0(%rbp), %rsi
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xa0(%rbp)
addq $0x250, %rsi            # imm = 0x250
leaq -0xc0(%rbp), %rdi
callq 0xaee0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xa4(%rbp)
movss -0x98(%rbp), %xmm0
movss -0x98(%rbp), %xmm2
movss -0x94(%rbp), %xmm1
mulss -0x9c(%rbp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0xa8(%rbp)
movss -0x98(%rbp), %xmm0
movss -0xa4(%rbp), %xmm2
movss -0x9c(%rbp), %xmm1
mulss -0xa0(%rbp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
divss -0xa8(%rbp), %xmm0
movss %xmm0, -0xc4(%rbp)
xorps %xmm0, %xmm0
ucomiss -0xc4(%rbp), %xmm0
ja 0xa3b5
movss -0xc4(%rbp), %xmm0
movss 0x1ac88(%rip), %xmm1    # 0x25038
ucomiss %xmm1, %xmm0
jbe 0xa3c7
movss 0x1ac73(%rip), %xmm0    # 0x25030
movss %xmm0, -0x4(%rbp)
jmp 0xa46c
movss -0x98(%rbp), %xmm0
movss -0xa0(%rbp), %xmm2
movss -0x94(%rbp), %xmm1
mulss -0xa4(%rbp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
divss -0xa8(%rbp), %xmm0
movss %xmm0, -0xc8(%rbp)
xorps %xmm0, %xmm0
ucomiss -0xc8(%rbp), %xmm0
ja 0xa435
movss -0xc4(%rbp), %xmm0
addss -0xc8(%rbp), %xmm0
movss 0x1ac08(%rip), %xmm1    # 0x25038
ucomiss %xmm1, %xmm0
jbe 0xa444
movss 0x1abf3(%rip), %xmm0    # 0x25030
movss %xmm0, -0x4(%rbp)
jmp 0xa46c
movss -0x74(%rbp), %xmm1
movss -0x1c(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0xa462
movss 0x1abd5(%rip), %xmm0    # 0x25030
movss %xmm0, -0x4(%rbp)
jmp 0xa46c
movss -0x74(%rbp), %xmm0
movss %xmm0, -0x4(%rbp)
movss -0x4(%rbp), %xmm0
addq $0xd0, %rsp
popq %rbp
retq
nopw (%rax,%rax)