#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ md5_signature(unsigned char *, int, unsigned char *);
uint32_t ketamaHash(const char *key, size_t key_length, uint32_t alignment) {
  unsigned char results[16];
  md5_signature((unsigned char *)key, (int)key_length, results);
  return ((uint32_t)(results[3 + alignment * 4] & 0xFF) << 24) |
         ((uint32_t)(results[2 + alignment * 4] & 0xFF) << 16) |
         ((uint32_t)(results[1 + alignment * 4] & 0xFF) << 8) |
         (results[0 + alignment * 4] & 0xFF);
}


x86 assembly (clang, -O0):
<ketamaHash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
call   1030 <md5_signature@plt>
mov    -0x14(%rbp),%eax
shl    $0x2,%eax
add    $0x3,%eax
cltq
movzbl -0x30(%rbp,%rax,1),%eax
and    $0xff,%eax
shl    $0x18,%eax
mov    -0x14(%rbp),%ecx
shl    $0x2,%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movzbl -0x30(%rbp,%rcx,1),%ecx
and    $0xff,%ecx
shl    $0x10,%ecx
or     %ecx,%eax
mov    -0x14(%rbp),%ecx
shl    $0x2,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movzbl -0x30(%rbp,%rcx,1),%ecx
and    $0xff,%ecx
shl    $0x8,%ecx
or     %ecx,%eax
mov    -0x14(%rbp),%ecx
shl    $0x2,%ecx
add    $0x0,%ecx
movslq %ecx,%rcx
movzbl -0x30(%rbp,%rcx,1),%ecx
and    $0xff,%ecx
or     %ecx,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ketamaHash>:
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %rsp,%rdx
call   1030 <md5_signature@plt>
movslq %ebx,%rax
lea    0x0(,%rax,4),%rcx
movslq %ecx,%rcx
movzbl 0x3(%rsp,%rcx,1),%edx
shl    $0x18,%edx
movzbl 0x2(%rsp,%rcx,1),%esi
shl    $0x10,%esi
or     %edx,%esi
movzbl 0x1(%rsp,%rcx,1),%ecx
shl    $0x8,%ecx
or     %esi,%ecx
movzbl (%rsp,%rax,4),%eax
or     %ecx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @ketamaHash(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca [16 x i8]
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = load i64, i64* %5
%10 = trunc i64 %9 to i32
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%12 = call i32 @md5_signature(i8* %8, i32 %10, i8* %11)
%13 = load i32, i32* %6
%14 = mul nsw i32 %13, 4
%15 = add nsw i32 3, %14
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = and i32 %19, 255
%21 = shl i32 %20, 24
%22 = load i32, i32* %6
%23 = mul nsw i32 %22, 4
%24 = add nsw i32 2, %23
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = and i32 %28, 255
%30 = shl i32 %29, 16
%31 = or i32 %21, %30
%32 = load i32, i32* %6
%33 = mul nsw i32 %32, 4
%34 = add nsw i32 1, %33
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = and i32 %38, 255
%40 = shl i32 %39, 8
%41 = or i32 %31, %40
%42 = load i32, i32* %6
%43 = mul nsw i32 %42, 4
%44 = add nsw i32 0, %43
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = and i32 %48, 255
%50 = or i32 %41, %49
ret i32 %50
}

LLVM IR (-O2):
define i32 @ketamaHash(i8* %0, i64 %1, i32 %2) {
%4 = alloca [16 x i8]
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%6 = trunc i64 %1 to i32
%7 = call i32 @md5_signature(i8* %0, i32 %6, i8* %5)
%8 = shl nsw i32 %2, 2
%9 = or i32 %8, 3
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %10
%12 = load i8, i8* %11
%13 = zext i8 %12 to i32
%14 = shl nuw i32 %13, 24
%15 = or i32 %8, 2
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = shl nuw nsw i32 %19, 16
%21 = or i32 %20, %14
%22 = or i32 %8, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = shl nuw nsw i32 %26, 8
%28 = or i32 %21, %27
%29 = sext i32 %8 to i64
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = or i32 %28, %32
ret i32 %33
}