#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 */

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ hash_bits_valid(unsigned int);
unsigned long hash_str(const char *str, unsigned int bits) {
  unsigned long hash, mask;
  char c;
  assert(hash_bits_valid(bits));
  for (hash = 0;; str++) {
    c = *str;
    if (c == '\0') {
      break;
    }
    hash = ((hash << 5) - hash) + c;
  }
  mask = (~0UL >> (64 - bits));
  return hash & mask;
}


x86 assembly (clang, -O0):
<hash_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <hash_bits_valid@plt>
mov    %eax,%edi
call   1040 <assert@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%eax
cmp    $0x0,%eax
jne    1161 <hash_str+0x41>
jmp    118a <hash_str+0x6a>
mov    -0x18(%rbp),%rax
shl    $0x5,%rax
sub    -0x18(%rbp),%rax
movsbq -0x21(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1146 <hash_str+0x26>
mov    $0x40,%eax
sub    -0xc(%rbp),%eax
mov    %eax,%eax
mov    %eax,%ecx
mov    $0xffffffffffffffff,%rax
shr    %cl,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
and    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hash_str>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %esi,%edi
call   1030 <hash_bits_valid@plt>
mov    %eax,%edi
call   1040 <assert@plt>
mov    (%rbx),%cl
test   %cl,%cl
je     116e <hash_str+0x4e>
add    $0x1,%rbx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rax,%rdx
shl    $0x5,%rdx
sub    %rax,%rdx
movsbq %cl,%rax
add    %rdx,%rax
movzbl (%rbx),%ecx
add    $0x1,%rbx
test   %cl,%cl
jne    1150 <hash_str+0x30>
jmp    1170 <hash_str+0x50>
xor    %eax,%eax
neg    %bpl
mov    %ebp,%ecx
shl    %cl,%rax
shr    %cl,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i64 @hash_str(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = call i32 @hash_bits_valid(i32 %8)
%10 = call i32 @assert(i32 %9)
store i64 0, i64* %5
br label %11
11: ; preds = %26, %2
%12 = load i8*, i8** %3
%13 = load i8, i8* %12
store i8 %13, i8* %7
%14 = load i8, i8* %7
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %11
br label %29
18: ; preds = %11
%19 = load i64, i64* %5
%20 = shl i64 %19, 5
%21 = load i64, i64* %5
%22 = sub i64 %20, %21
%23 = load i8, i8* %7
%24 = sext i8 %23 to i64
%25 = add i64 %22, %24
store i64 %25, i64* %5
br label %26
26: ; preds = %18
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %3
br label %11
29: ; preds = %17
%30 = load i32, i32* %4
%31 = sub i32 64, %30
%32 = zext i32 %31 to i64
%33 = lshr i64 -1, %32
store i64 %33, i64* %6
%34 = load i64, i64* %5
%35 = load i64, i64* %6
%36 = and i64 %34, %35
ret i64 %36
}

LLVM IR (-O2):
define i64 @hash_str(i8* nocapture %0, i32 %1) {
%3 = tail call i32 @hash_bits_valid(i32 %1)
%4 = tail call i32 @assert(i32 %3)
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %17, label %7
7: ; preds = %2, %7
%8 = phi i8 [ %15, %7 ], [ %5, %2 ]
%9 = phi i8* [ %14, %7 ], [ %0, %2 ]
%10 = phi i64 [ %13, %7 ], [ 0, %2 ]
%11 = mul i64 %10, 31
%12 = sext i8 %8 to i64
%13 = add i64 %11, %12
%14 = getelementptr inbounds i8, i8* %9, i64 1
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
br i1 %16, label %17, label %7
17: ; preds = %7, %2
%18 = phi i64 [ 0, %2 ], [ %13, %7 ]
%19 = sub i32 64, %1
%20 = zext i32 %19 to i64
%21 = lshr i64 -1, %20
%22 = and i64 %18, %21
ret i64 %22
}