#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 tolower(char const);
unsigned int hach_string(const char *str) {
  int hash = 5381;
  const char *s;
  for (s = str; *s != '\0'; s++) {
    hash = ((hash << 5) + hash) + tolower(*s);
  }
  return (hash & 0x7FFFFFFF) % (5381 - 1);
}


x86 assembly (clang, -O0):
<hach_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1505,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     116e <hach_string+0x5e>
mov    -0xc(%rbp),%eax
shl    $0x5,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <tolower@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    112b <hach_string+0x1b>
mov    -0xc(%rbp),%eax
and    $0x7fffffff,%eax
mov    $0x1504,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hach_string>:
push   %rbp
push   %rbx
push   %rax
mov    (%rdi),%cl
test   %cl,%cl
je     1168 <hach_string+0x58>
mov    %rdi,%rbx
add    $0x1,%rbx
mov    $0x1505,%eax
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,%ebp
shl    $0x5,%ebp
add    %eax,%ebp
movsbl %cl,%edi
call   1030 <tolower@plt>
add    %ebp,%eax
movzbl (%rbx),%ecx
add    $0x1,%rbx
test   %cl,%cl
jne    1130 <hach_string+0x20>
and    $0x7fffffff,%eax
mov    $0xc2e710f1,%ecx
imul   %rax,%rcx
shr    $0x2c,%rcx
imul   $0x1504,%ecx,%ecx
sub    %ecx,%eax
jmp    116d <hach_string+0x5d>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @hach_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i8* %0, i8** %2
store i32 5381, i32* %3
%5 = load i8*, i8** %2
store i8* %5, i8** %4
br label %6
6: ; preds = %20, %1
%7 = load i8*, i8** %4
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %23
11: ; preds = %6
%12 = load i32, i32* %3
%13 = shl i32 %12, 5
%14 = load i32, i32* %3
%15 = add nsw i32 %13, %14
%16 = load i8*, i8** %4
%17 = load i8, i8* %16
%18 = call i32 @tolower(i8 signext %17)
%19 = add nsw i32 %15, %18
store i32 %19, i32* %3
br label %20
20: ; preds = %11
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %4
br label %6
23: ; preds = %6
%24 = load i32, i32* %3
%25 = and i32 %24, 2147483647
%26 = srem i32 %25, 5380
ret i32 %26
}

LLVM IR (-O2):
define i32 @hach_string(i8* nocapture %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %17, label %4
4: ; preds = %1, %4
%5 = phi i8 [ %12, %4 ], [ %2, %1 ]
%6 = phi i8* [ %11, %4 ], [ %0, %1 ]
%7 = phi i32 [ %10, %4 ], [ 5381, %1 ]
%8 = mul i32 %7, 33
%9 = tail call i32 @tolower(i8 signext %5)
%10 = add nsw i32 %9, %8
%11 = getelementptr inbounds i8, i8* %6, i64 1
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 0
br i1 %13, label %14, label %4
14: ; preds = %4
%15 = and i32 %10, 2147483647
%16 = urem i32 %15, 5380
br label %17
17: ; preds = %14, %1
%18 = phi i32 [ 1, %1 ], [ %16, %14 ]
ret i32 %18
}