// File: BartMassey[P]ancient-raytracer/poly.cc
// Function: poly::intersect(ray, intersection&)
int poly::intersect( ray r, intersection &s ) {
  float b, t;
  
  r.transform( toi );
  
  // now we have the ray in object coords...
  point rd( r.d() );
  point ro( r.o() );
  
  b = Z( rd );
  if( fabs( b ) < TINY ) {
    // ray is parallel to plane, so no hit
    return 0;
  }
  t = -Z( ro ) / b;
  if( t < TINY ) {
    // ray is behind plane, so no hit
    return 0;
  }
  point i( ro + (rd * t) );
#if DEBUGLEVEL > 0
  assert( fabs( Z(i) ) < TINY );
#endif  
  if( !contains( i ) ) {
    // ray is outside poly
    return 0;
  }
#if DEBUGLEVEL > 0
  assert( cnormal );
#endif
  s.normal = new point( *cnormal );
  s.at = new point( X(i), Y(i) );
  s.t = t;

  return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rsi, -0xd0(%rbp)
movq %rdi, %rax
movq -0xd0(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
movq %rax, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rsi
movq %rsi, -0xc8(%rbp)
addq $0xc0, %rsi
callq 0x6c40
movq -0xc0(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x5140
movq -0xc0(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x5170
jmp 0x4748
leaq -0x48(%rbp), %rdi
callq 0x51a0
movss %xmm0, -0xd4(%rbp)
jmp 0x475b
movss -0xd4(%rbp), %xmm0
movss %xmm0, -0x24(%rbp)
movss -0x24(%rbp), %xmm0
callq 0x5200
movss %xmm0, -0xd8(%rbp)
jmp 0x477c
movss -0xd8(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x68a8(%rip), %xmm0     # 0xb038
ucomisd %xmm1, %xmm0
jbe 0x47cb
movl $0x0, -0x4(%rbp)
movl $0x1, -0x78(%rbp)
jmp 0x49ef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x4a16
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x4a0d
leaq -0x68(%rbp), %rdi
callq 0x51a0
movss %xmm0, -0xdc(%rbp)
jmp 0x47de
movss -0xdc(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
divss -0x24(%rbp), %xmm0
movss %xmm0, -0x28(%rbp)
movss -0x28(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x682a(%rip), %xmm0     # 0xb038
ucomisd %xmm1, %xmm0
jbe 0x4827
movl $0x0, -0x4(%rbp)
movl $0x1, -0x78(%rbp)
jmp 0x49ef
movss -0x28(%rbp), %xmm0
leaq -0xb8(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3760
jmp 0x483e
leaq -0x98(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0xb8(%rbp), %rdx
callq 0x3530
jmp 0x4857
leaq -0xb8(%rbp), %rdi
callq 0x2fd0
movq -0xc8(%rbp), %rdi
movq (%rdi), %rax
movq 0x58(%rax), %rax
leaq -0x98(%rbp), %rsi
callq *%rax
movl %eax, -0xe0(%rbp)
jmp 0x4882
movl -0xe0(%rbp), %eax
cmpl $0x0, %eax
jne 0x48ce
movl $0x0, -0x4(%rbp)
movl $0x1, -0x78(%rbp)
jmp 0x49d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x2fd0
jmp 0x4a0d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x49e1
movl $0x20, %edi
callq 0x2110
movq %rax, -0xe8(%rbp)
jmp 0x48e1
movq -0xe8(%rbp), %rdi
movq -0xc8(%rbp), %rax
movq 0x158(%rax), %rsi
callq 0x2ef0
jmp 0x48fd
movq -0xe8(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movl $0x20, %edi
callq 0x2110
movq %rax, -0xf0(%rbp)
jmp 0x491f
leaq -0x98(%rbp), %rdi
callq 0x5080
movss %xmm0, -0xf4(%rbp)
jmp 0x4935
leaq -0x98(%rbp), %rdi
callq 0x50e0
movss %xmm0, -0xf8(%rbp)
jmp 0x494b
movss -0xf8(%rbp), %xmm1
movss -0xf4(%rbp), %xmm0
movq -0xf0(%rbp), %rdi
callq 0x5220
jmp 0x4969
movq -0xf0(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movss -0x28(%rbp), %xmm0
movq -0x20(%rbp), %rax
movss %xmm0, (%rax)
movl $0x1, -0x4(%rbp)
movl $0x1, -0x78(%rbp)
jmp 0x49d3
movq -0xe8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
movl $0x20, %esi
callq 0x2120
jmp 0x49e1
movq -0xf0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
movl $0x20, %esi
callq 0x2120
jmp 0x49e1
leaq -0x98(%rbp), %rdi
callq 0x2fd0
jmp 0x49ef
leaq -0x98(%rbp), %rdi
callq 0x2fd0
jmp 0x4a0d
leaq -0x68(%rbp), %rdi
callq 0x2fd0
leaq -0x48(%rbp), %rdi
callq 0x2fd0
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
leaq -0x68(%rbp), %rdi
callq 0x2fd0
leaq -0x48(%rbp), %rdi
callq 0x2fd0
movq -0x70(%rbp), %rdi
callq 0x21c0
nopl (%rax,%rax)