// File: /workspace/github_cpp_makefile_star2_O3/neubig[P]egret/Egret/src/bob-hash.cpp
// Function: hashbig(void const*, unsigned long, unsigned int)
uint32_t hashbig( const void *key, size_t length, uint32_t initval)
{
  uint32_t a,b,c;
  union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */

  /* Set up the internal state */
  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;

  u.ptr = key;
  if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
    const uint32_t *k = (const uint32_t *)key;         /* read 32-bit chunks */
//    const uint8_t  *k8;

    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
    while (length > 12)
    {
      a += k[0];
      b += k[1];
      c += k[2];
      mix(a,b,c);
      length -= 12;
      k += 3;
    }

    /*----------------------------- handle the last (probably partial) block */
    /* 
     * "k[2]<<8" actually reads beyond the end of the string, but
     * then shifts out the part it's not allowed to read.  Because the
     * string is aligned, the illegal read is in the same word as the
     * rest of the string.  Every machine with memory protection I've seen
     * does it on word boundaries, so is OK with this.  But VALGRIND will
     * still catch it and complain.  The masking trick does make the hash
     * noticably faster for short strings (like English words).
     */
#ifndef VALGRIND

    switch(length)
    {
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
    case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
    case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
    case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
    case 8 : b+=k[1]; a+=k[0]; break;
    case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
    case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
    case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
    case 4 : a+=k[0]; break;
    case 3 : a+=k[0]&0xffffff00; break;
    case 2 : a+=k[0]&0xffff0000; break;
    case 1 : a+=k[0]&0xff000000; break;
    case 0 : return c;              /* zero length strings require no mixing */
    }

#else  /* make valgrind happy */

    k8 = (const uint8_t *)k;
    switch(length)                   /* all the case statements fall through */
    {
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
    case 11: c+=((uint32_t)k8[10])<<8;  /* fall through */
    case 10: c+=((uint32_t)k8[9])<<16;  /* fall through */
    case 9 : c+=((uint32_t)k8[8])<<24;  /* fall through */
    case 8 : b+=k[1]; a+=k[0]; break;
    case 7 : b+=((uint32_t)k8[6])<<8;   /* fall through */
    case 6 : b+=((uint32_t)k8[5])<<16;  /* fall through */
    case 5 : b+=((uint32_t)k8[4])<<24;  /* fall through */
    case 4 : a+=k[0]; break;
    case 3 : a+=((uint32_t)k8[2])<<8;   /* fall through */
    case 2 : a+=((uint32_t)k8[1])<<16;  /* fall through */
    case 1 : a+=((uint32_t)k8[0])<<24; break;
    case 0 : return c;
    }

#endif /* !VALGRIND */

  } else {                        /* need to read the key one byte at a time */
    const uint8_t *k = (const uint8_t *)key;

    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
    while (length > 12)
    {
      a += ((uint32_t)k[0])<<24;
      a += ((uint32_t)k[1])<<16;
      a += ((uint32_t)k[2])<<8;
      a += ((uint32_t)k[3]);
      b += ((uint32_t)k[4])<<24;
      b += ((uint32_t)k[5])<<16;
      b += ((uint32_t)k[6])<<8;
      b += ((uint32_t)k[7]);
      c += ((uint32_t)k[8])<<24;
      c += ((uint32_t)k[9])<<16;
      c += ((uint32_t)k[10])<<8;
      c += ((uint32_t)k[11]);
      mix(a,b,c);
      length -= 12;
      k += 12;
    }

    /*-------------------------------- last block: affect all 32 bits of (c) */
    switch(length)                   /* all the case statements fall through */
    {
    case 12: c+=k[11];
    case 11: c+=((uint32_t)k[10])<<8;
    case 10: c+=((uint32_t)k[9])<<16;
    case 9 : c+=((uint32_t)k[8])<<24;
    case 8 : b+=k[7];
    case 7 : b+=((uint32_t)k[6])<<8;
    case 6 : b+=((uint32_t)k[5])<<16;
    case 5 : b+=((uint32_t)k[4])<<24;
    case 4 : a+=k[3];
    case 3 : a+=((uint32_t)k[2])<<8;
    case 2 : a+=((uint32_t)k[1])<<16;
    case 1 : a+=((uint32_t)k[0])<<24;
             break;
    case 0 : return c;
    }
  }

  final(a,b,c);
  return c;
}

// Assembly:
leal (%rsi,%rdx), %eax
addl $0xdeadbeef, %eax       # imm = 0xDEADBEEF
cmpq $0xd, %rsi
jb 0x18da8
movl %eax, %ecx
movl %eax, %edx
movzbl (%rdi), %r8d
shll $0x18, %r8d
addl %edx, %r8d
movzbl 0x1(%rdi), %edx
shll $0x10, %edx
movzbl 0x2(%rdi), %r9d
shll $0x8, %r9d
addl %edx, %r9d
addl %r8d, %r9d
movzbl 0x3(%rdi), %edx
addl %r9d, %edx
movzbl 0x4(%rdi), %r8d
shll $0x18, %r8d
addl %ecx, %r8d
movzbl 0x5(%rdi), %ecx
shll $0x10, %ecx
movzbl 0x6(%rdi), %r9d
shll $0x8, %r9d
addl %ecx, %r9d
addl %r8d, %r9d
movzbl 0x7(%rdi), %ecx
addl %r9d, %ecx
movzbl 0x8(%rdi), %r8d
shll $0x18, %r8d
addl %eax, %r8d
movzbl 0x9(%rdi), %eax
shll $0x10, %eax
movzbl 0xa(%rdi), %r9d
shll $0x8, %r9d
addl %eax, %r9d
addl %r8d, %r9d
movzbl 0xb(%rdi), %eax
addl %r9d, %eax
movl %eax, %r8d
roll $0x4, %r8d
subl %eax, %edx
xorl %edx, %r8d
addl %ecx, %eax
subl %r8d, %ecx
movl %r8d, %r9d
roll $0x6, %r9d
xorl %ecx, %r9d
addl %eax, %r8d
movl %r9d, %r10d
roll $0x8, %r10d
subl %r9d, %eax
xorl %eax, %r10d
addl %r8d, %r9d
subl %r10d, %r8d
movl %r10d, %edx
roll $0x10, %edx
xorl %r8d, %edx
addl %r9d, %r10d
movl %edx, %ecx
roll $0x13, %ecx
subl %edx, %r9d
xorl %r9d, %ecx
addl %r10d, %edx
subl %ecx, %r10d
movl %ecx, %eax
roll $0x4, %eax
xorl %r10d, %eax
addl %edx, %ecx
addq $-0xc, %rsi
addq $0xc, %rdi
cmpq $0xc, %rsi
ja 0x18cc2
jmp 0x18dac
movl %eax, %edx
movl %eax, %ecx
leaq 0x27555(%rip), %r8      # 0x40308
movslq (%r8,%rsi,4), %rsi
addq %r8, %rsi
jmpq *%rsi
movzbl 0xb(%rdi), %esi
addl %esi, %eax
movzbl 0xa(%rdi), %esi
shll $0x8, %esi
addl %esi, %eax
movzbl 0x9(%rdi), %esi
shll $0x10, %esi
addl %esi, %eax
movzbl 0x8(%rdi), %esi
shll $0x18, %esi
addl %esi, %eax
movzbl 0x7(%rdi), %esi
addl %esi, %ecx
movzbl 0x6(%rdi), %esi
shll $0x8, %esi
addl %esi, %ecx
movzbl 0x5(%rdi), %esi
shll $0x10, %esi
addl %esi, %ecx
movzbl 0x4(%rdi), %esi
shll $0x18, %esi
addl %esi, %ecx
movzbl 0x3(%rdi), %esi
addl %esi, %edx
movzbl 0x2(%rdi), %esi
shll $0x8, %esi
addl %esi, %edx
movzbl 0x1(%rdi), %esi
shll $0x10, %esi
addl %esi, %edx
movzbl (%rdi), %esi
shll $0x18, %esi
addl %edx, %esi
xorl %ecx, %eax
movl %ecx, %edx
roll $0xe, %edx
subl %edx, %eax
xorl %eax, %esi
movl %eax, %edx
roll $0xb, %edx
subl %edx, %esi
movl %esi, %edx
roll $0x19, %edx
xorl %esi, %ecx
subl %edx, %ecx
xorl %ecx, %eax
movl %ecx, %edx
roll $0x10, %edx
subl %edx, %eax
xorl %eax, %esi
movl %eax, %edx
roll $0x4, %edx
subl %edx, %esi
xorl %esi, %ecx
roll $0xe, %esi
subl %esi, %ecx
xorl %ecx, %eax
roll $0x18, %ecx
subl %ecx, %eax
retq