// File: /workspace/github_cpp_makefile_star2_O3/neubig[P]egret/Egret/src/bob-hash.cpp
// Function: hashlittle(void const*, unsigned long, unsigned int)
uint32_t hashlittle( const void *key, size_t length, uint32_t initval)
{
  uint32_t a,b,c;                                          /* internal state */
  union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */

  /* Set up the internal state */
  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;

  u.ptr = key;
  if (HASH_LITTLE_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]&0xffffff" actually reads beyond the end of the string, but
     * then masks off the part it's not allowed to read.  Because the
     * string is aligned, the masked-off tail 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]&0xffffff; b+=k[1]; a+=k[0]; break;
    case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
    case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
    case 8 : b+=k[1]; a+=k[0]; break;
    case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
    case 6 : b+=k[1]&0xffff; a+=k[0]; break;
    case 5 : b+=k[1]&0xff; a+=k[0]; break;
    case 4 : a+=k[0]; break;
    case 3 : a+=k[0]&0xffffff; break;
    case 2 : a+=k[0]&0xffff; break;
    case 1 : a+=k[0]&0xff; break;
    case 0 : return c;              /* zero length strings require no mixing */
    }

#else /* make valgrind happy */

    k8 = (const uint8_t *)k;
    switch(length)
    {
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
    case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
    case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
    case 9 : c+=k8[8];                   /* fall through */
    case 8 : b+=k[1]; a+=k[0]; break;
    case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
    case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
    case 5 : b+=k8[4];                   /* fall through */
    case 4 : a+=k[0]; break;
    case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
    case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
    case 1 : a+=k8[0]; break;
    case 0 : return c;
    }

#endif /* !valgrind */

  } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
    const uint16_t *k = (const uint16_t *)key;         /* read 16-bit chunks */
    const uint8_t  *k8;

    /*--------------- all but last block: aligned reads and different mixing */
    while (length > 12)
    {
      a += k[0] + (((uint32_t)k[1])<<16);
      b += k[2] + (((uint32_t)k[3])<<16);
      c += k[4] + (((uint32_t)k[5])<<16);
      mix(a,b,c);
      length -= 12;
      k += 6;
    }

    /*----------------------------- handle the last (probably partial) block */
    k8 = (const uint8_t *)k;
    switch(length)
    {
    case 12: c+=k[4]+(((uint32_t)k[5])<<16);
             b+=k[2]+(((uint32_t)k[3])<<16);
             a+=k[0]+(((uint32_t)k[1])<<16);
             break;
    case 11: c+=((uint32_t)k8[10])<<16;     /* fall through */
    case 10: c+=k[4];
             b+=k[2]+(((uint32_t)k[3])<<16);
             a+=k[0]+(((uint32_t)k[1])<<16);
             break;
    case 9 : c+=k8[8];                      /* fall through */
    case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
             a+=k[0]+(((uint32_t)k[1])<<16);
             break;
    case 7 : b+=((uint32_t)k8[6])<<16;      /* fall through */
    case 6 : b+=k[2];
             a+=k[0]+(((uint32_t)k[1])<<16);
             break;
    case 5 : b+=k8[4];                      /* fall through */
    case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
             break;
    case 3 : a+=((uint32_t)k8[2])<<16;      /* fall through */
    case 2 : a+=k[0];
             break;
    case 1 : a+=k8[0];
             break;
    case 0 : return c;                     /* zero length requires no mixing */
    }

  } 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 += k[0];
      a += ((uint32_t)k[1])<<8;
      a += ((uint32_t)k[2])<<16;
      a += ((uint32_t)k[3])<<24;
      b += k[4];
      b += ((uint32_t)k[5])<<8;
      b += ((uint32_t)k[6])<<16;
      b += ((uint32_t)k[7])<<24;
      c += k[8];
      c += ((uint32_t)k[9])<<8;
      c += ((uint32_t)k[10])<<16;
      c += ((uint32_t)k[11])<<24;
      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+=((uint32_t)k[11])<<24;
    case 11: c+=((uint32_t)k[10])<<16;
    case 10: c+=((uint32_t)k[9])<<8;
    case 9 : c+=k[8];
    case 8 : b+=((uint32_t)k[7])<<24;
    case 7 : b+=((uint32_t)k[6])<<16;
    case 6 : b+=((uint32_t)k[5])<<8;
    case 5 : b+=k[4];
    case 4 : a+=((uint32_t)k[3])<<24;
    case 3 : a+=((uint32_t)k[2])<<16;
    case 2 : a+=((uint32_t)k[1])<<8;
    case 1 : a+=k[0];
             break;
    case 0 : return c;
    }
  }

  final(a,b,c);
  return c;
}

// Assembly:
leal (%rsi,%rdx), %eax
addl $0xdeadbeef, %eax       # imm = 0xDEADBEEF
testb $0x3, %dil
je 0x18615
testb $0x1, %dil
jne 0x18698
cmpq $0xd, %rsi
jb 0x187a9
movl %eax, %ecx
movl %eax, %edx
addl (%rdi), %edx
addl 0x4(%rdi), %ecx
addl 0x8(%rdi), %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 0x185a2
jmp 0x187ad
cmpq $0xd, %rsi
jb 0x1878c
movl %eax, %ecx
movl %eax, %r8d
addl (%rdi), %r8d
addl 0x4(%rdi), %ecx
addl 0x8(%rdi), %eax
movl %eax, %edx
roll $0x4, %edx
subl %eax, %r8d
xorl %r8d, %edx
addl %ecx, %eax
subl %edx, %ecx
movl %edx, %r9d
roll $0x6, %r9d
xorl %ecx, %r9d
addl %eax, %edx
movl %r9d, %r10d
roll $0x8, %r10d
subl %r9d, %eax
xorl %eax, %r10d
addl %edx, %r9d
subl %r10d, %edx
movl %r10d, %r8d
roll $0x10, %r8d
xorl %edx, %r8d
addl %r9d, %r10d
movl %r8d, %ecx
roll $0x13, %ecx
subl %r8d, %r9d
xorl %r9d, %ecx
addl %r10d, %r8d
subl %ecx, %r10d
movl %ecx, %eax
roll $0x4, %eax
xorl %r10d, %eax
addl %r8d, %ecx
addq $-0xc, %rsi
addq $0xc, %rdi
cmpq $0xc, %rsi
ja 0x18624
jmp 0x18791
cmpq $0xd, %rsi
jb 0x187c5
movl %eax, %ecx
movl %eax, %r8d
movzbl (%rdi), %edx
addl %r8d, %edx
movzbl 0x1(%rdi), %r8d
shll $0x8, %r8d
addl %edx, %r8d
movzbl 0x2(%rdi), %r9d
shll $0x10, %r9d
movzbl 0x3(%rdi), %edx
shll $0x18, %edx
addl %r9d, %edx
addl %r8d, %edx
movzbl 0x4(%rdi), %r8d
addl %ecx, %r8d
movzbl 0x5(%rdi), %r9d
shll $0x8, %r9d
addl %r8d, %r9d
movzbl 0x6(%rdi), %r8d
shll $0x10, %r8d
movzbl 0x7(%rdi), %ecx
shll $0x18, %ecx
addl %r8d, %ecx
addl %r9d, %ecx
movzbl 0x8(%rdi), %r8d
addl %eax, %r8d
movzbl 0x9(%rdi), %eax
shll $0x8, %eax
addl %r8d, %eax
movzbl 0xa(%rdi), %r8d
shll $0x10, %r8d
movzbl 0xb(%rdi), %r9d
shll $0x18, %r9d
addl %r8d, %r9d
addl %eax, %r9d
movl %r9d, %eax
roll $0x4, %eax
subl %r9d, %edx
xorl %edx, %eax
addl %ecx, %r9d
subl %eax, %ecx
movl %eax, %edx
roll $0x6, %edx
xorl %ecx, %edx
addl %r9d, %eax
movl %edx, %r10d
roll $0x8, %r10d
subl %edx, %r9d
xorl %r9d, %r10d
addl %eax, %edx
subl %r10d, %eax
movl %r10d, %r8d
roll $0x10, %r8d
xorl %eax, %r8d
addl %edx, %r10d
movl %r8d, %ecx
roll $0x13, %ecx
subl %r8d, %edx
xorl %edx, %ecx
addl %r10d, %r8d
subl %ecx, %r10d
movl %ecx, %eax
roll $0x4, %eax
xorl %r10d, %eax
addl %r8d, %ecx
addq $-0xc, %rsi
addq $0xc, %rdi
cmpq $0xc, %rsi
ja 0x186a7
jmp 0x187ca
movl %eax, %r8d
movl %eax, %ecx
leaq 0x27aa0(%rip), %rdx     # 0x40238
movslq (%rdx,%rsi,4), %rsi
addq %rdx, %rsi
jmpq *%rsi
movzwl (%rdi), %edx
jmp 0x18880
movl %eax, %edx
movl %eax, %ecx
leaq 0x27a50(%rip), %r8      # 0x40204
movslq (%r8,%rsi,4), %rsi
addq %r8, %rsi
jmpq *%rsi
movzbl (%rdi), %esi
jmp 0x188b0
movl %eax, %r8d
movl %eax, %ecx
leaq 0x279ff(%rip), %rdx     # 0x401d0
movslq (%rdx,%rsi,4), %rsi
addq %rdx, %rsi
jmpq *%rsi
movzbl 0xb(%rdi), %edx
shll $0x18, %edx
addl %edx, %eax
movzbl 0xa(%rdi), %edx
shll $0x10, %edx
addl %edx, %eax
movzbl 0x9(%rdi), %edx
shll $0x8, %edx
addl %edx, %eax
movzbl 0x8(%rdi), %edx
addl %edx, %eax
movzbl 0x7(%rdi), %edx
shll $0x18, %edx
addl %edx, %ecx
movzbl 0x6(%rdi), %edx
shll $0x10, %edx
addl %edx, %ecx
movzbl 0x5(%rdi), %edx
shll $0x8, %edx
addl %edx, %ecx
movzbl 0x4(%rdi), %edx
addl %edx, %ecx
movzbl 0x3(%rdi), %edx
shll $0x18, %edx
addl %edx, %r8d
movzbl 0x2(%rdi), %edx
shll $0x10, %edx
addl %edx, %r8d
movzbl 0x1(%rdi), %edx
shll $0x8, %edx
addl %edx, %r8d
movzbl (%rdi), %edx
jmp 0x18880
movl $0xffffff, %edx         # imm = 0xFFFFFF
andl 0x8(%rdi), %edx
jmp 0x18879
addl 0x8(%rdi), %eax
jmp 0x1887b
movzbl 0x8(%rdi), %edx
jmp 0x18879
movl $0xffffff, %edx         # imm = 0xFFFFFF
andl (%rdi), %edx
jmp 0x18880
movl $0xffffff, %edx         # imm = 0xFFFFFF
andl 0x4(%rdi), %edx
jmp 0x18871
movzbl 0x4(%rdi), %edx
jmp 0x18871
movzwl 0x4(%rdi), %edx
addl %edx, %ecx
jmp 0x1887e
movzwl 0x8(%rdi), %edx
addl %edx, %eax
addl 0x4(%rdi), %ecx
movl (%rdi), %edx
addl %r8d, %edx
jmp 0x188cb
movzbl 0xa(%rdi), %esi
shll $0x10, %esi
addl %esi, %eax
movzwl 0x8(%rdi), %esi
jmp 0x1889d
addl 0x8(%rdi), %eax
jmp 0x1889f
movzbl 0x8(%rdi), %esi
addl %esi, %eax
addl 0x4(%rdi), %ecx
jmp 0x188c9
movzbl 0x2(%rdi), %esi
shll $0x10, %esi
addl %esi, %edx
movzwl (%rdi), %esi
addl %esi, %edx
jmp 0x188cb
movzbl 0x6(%rdi), %esi
shll $0x10, %esi
addl %esi, %ecx
movzwl 0x4(%rdi), %esi
jmp 0x188c7
movzbl 0x4(%rdi), %esi
addl %esi, %ecx
addl (%rdi), %edx
xorl %ecx, %eax
movl %ecx, %esi
roll $0xe, %esi
subl %esi, %eax
xorl %eax, %edx
movl %eax, %esi
roll $0xb, %esi
subl %esi, %edx
movl %edx, %esi
roll $0x19, %esi
xorl %edx, %ecx
subl %esi, %ecx
xorl %ecx, %eax
movl %ecx, %esi
roll $0x10, %esi
subl %esi, %eax
xorl %eax, %edx
movl %eax, %esi
roll $0x4, %esi
subl %esi, %edx
xorl %edx, %ecx
roll $0xe, %edx
subl %edx, %ecx
xorl %ecx, %eax
roll $0x18, %ecx
subl %ecx, %eax
retq