#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 strlen(char *);
int ELFHash(char str[]) {
  int hash = 0;
  int x = 0;
  int i;
  for (i = 0; i < strlen(str); i++) {
    hash = (hash << 4) + str[i];
    if ((x = (int)(hash & 0xF0000000L)) != 0) {
      hash ^= (x >> 24);
      hash &= ~x;
    }
  }
  return (hash & 0x7FFFFFFF);
}


x86 assembly (clang, -O0):
<ELFHash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jge    11ac <ELFHash+0x9c>
mov    -0xc(%rbp),%eax
shl    $0x4,%eax
mov    -0x8(%rbp),%rcx
movslq -0x14(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
movabs $0xf0000000,%rcx
and    %rcx,%rax
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
je     1199 <ELFHash+0x89>
mov    -0x10(%rbp),%eax
sar    $0x18,%eax
xor    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
xor    $0xffffffff,%eax
and    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    119e <ELFHash+0x8e>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1131 <ELFHash+0x21>
mov    -0xc(%rbp),%eax
and    $0x7fffffff,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ELFHash>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %eax,%eax
jle    116f <ELFHash+0x5f>
xor    %ebx,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
shl    $0x4,%ebp
movsbl (%r14,%rbx,1),%eax
add    %ebp,%eax
mov    %eax,%ecx
and    $0xf0000000,%ecx
mov    %ecx,%edx
sar    $0x18,%edx
xor    %eax,%edx
mov    %ecx,%ebp
not    %ebp
and    %edx,%ebp
test   %ecx,%ecx
cmove  %eax,%ebp
add    $0x1,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     1130 <ELFHash+0x20>
and    $0x7fffffff,%ebp
jmp    1171 <ELFHash+0x61>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ELFHash(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
br label %6
6: ; preds = %36, %1
%7 = load i32, i32* %5
%8 = load i8*, i8** %2
%9 = call i32 @strlen(i8* %8)
%10 = icmp slt i32 %7, %9
br i1 %10, label %11, label %39
11: ; preds = %6
%12 = load i32, i32* %3
%13 = shl i32 %12, 4
%14 = load i8*, i8** %2
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = add nsw i32 %13, %19
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = and i64 %22, 4026531840
%24 = trunc i64 %23 to i32
store i32 %24, i32* %4
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %35
26: ; preds = %11
%27 = load i32, i32* %4
%28 = ashr i32 %27, 24
%29 = load i32, i32* %3
%30 = xor i32 %29, %28
store i32 %30, i32* %3
%31 = load i32, i32* %4
%32 = xor i32 %31, -1
%33 = load i32, i32* %3
%34 = and i32 %33, %32
store i32 %34, i32* %3
br label %35
35: ; preds = %26, %11
br label %36
36: ; preds = %35
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
store i32 %38, i32* %5
br label %6
39: ; preds = %6
%40 = load i32, i32* %3
%41 = and i32 %40, 2147483647
ret i32 %41
}

LLVM IR (-O2):
define i32 @ELFHash(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, 0
br i1 %3, label %4, label %25
4: ; preds = %1, %4
%5 = phi i64 [ %19, %4 ], [ 0, %1 ]
%6 = phi i32 [ %18, %4 ], [ 0, %1 ]
%7 = shl i32 %6, 4
%8 = getelementptr inbounds i8, i8* %0, i64 %5
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = add nsw i32 %7, %10
%12 = and i32 %11, -268435456
%13 = icmp eq i32 %12, 0
%14 = ashr exact i32 %12, 24
%15 = xor i32 %14, %11
%16 = xor i32 %12, -1
%17 = and i32 %15, %16
%18 = select i1 %13, i32 %11, i32 %17
%19 = add nuw nsw i64 %5, 1
%20 = tail call i32 @strlen(i8* %0)
%21 = sext i32 %20 to i64
%22 = icmp slt i64 %19, %21
br i1 %22, label %4, label %23
23: ; preds = %4
%24 = and i32 %18, 2147483647
br label %25
25: ; preds = %23, %1
%26 = phi i32 [ 0, %1 ], [ %24, %23 ]
ret i32 %26
}