#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 fmod(double, int);
int sqrt(int);
int hash(const char *key, int m) {
  double A = (sqrt(5) - 1) / 2;
  int k, i;
  k = 0;
  for (i = 0; key[i] != '\0'; i++)
    k = k + key[i];
  return m * fmod(A * k, 1);
}


x86 assembly (clang, -O0):
<hash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x5,%edi
call   1040 <sqrt@plt>
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1192 <hash+0x72>
mov    -0x1c(%rbp),%eax
mov    -0x8(%rbp),%rcx
movslq -0x20(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    115b <hash+0x3b>
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
movsd  -0x18(%rbp),%xmm0
cvtsi2sdl -0x1c(%rbp),%xmm1
mulsd  %xmm1,%xmm0
mov    $0x1,%edi
call   1030 <fmod@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
imul   %ecx,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hash>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    $0x5,%edi
call   1040 <sqrt@plt>
mov    (%rbx),%cl
test   %cl,%cl
je     1156 <hash+0x36>
add    $0x1,%rbx
xor    %edx,%edx
xchg   %ax,%ax
movsbl %cl,%ecx
add    %ecx,%edx
movzbl (%rbx),%ecx
add    $0x1,%rbx
test   %cl,%cl
jne    1140 <hash+0x20>
cvtsi2sd %edx,%xmm1
jmp    115a <hash+0x3a>
xorpd  %xmm1,%xmm1
mov    %eax,%ecx
add    $0xffffffff,%ecx
shr    $0x1f,%ecx
add    %ecx,%eax
add    $0xffffffff,%eax
sar    %eax
cvtsi2sd %eax,%xmm0
mulsd  %xmm1,%xmm0
mov    $0x1,%edi
call   1030 <fmod@plt>
imul   %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @hash(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca double
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = call i32 @sqrt(i32 5)
%9 = sub nsw i32 %8, 1
%10 = sdiv i32 %9, 2
%11 = sitofp i32 %10 to double
store double %11, double* %5
store i32 0, i32* %6
store i32 0, i32* %7
br label %12
12: ; preds = %29, %2
%13 = load i8*, i8** %3
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %32
20: ; preds = %12
%21 = load i32, i32* %6
%22 = load i8*, i8** %3
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = add nsw i32 %21, %27
store i32 %28, i32* %6
br label %29
29: ; preds = %20
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
store i32 %31, i32* %7
br label %12
32: ; preds = %12
%33 = load i32, i32* %4
%34 = load double, double* %5
%35 = load i32, i32* %6
%36 = sitofp i32 %35 to double
%37 = fmul double %34, %36
%38 = call i32 @fmod(double %37, i32 1)
%39 = mul nsw i32 %33, %38
ret i32 %39
}

LLVM IR (-O2):
define i32 @hash(i8* nocapture %0, i32 %1) {
%3 = tail call i32 @sqrt(i32 5)
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %18, label %6
6: ; preds = %2, %6
%7 = phi i64 [ %12, %6 ], [ 0, %2 ]
%8 = phi i8 [ %14, %6 ], [ %4, %2 ]
%9 = phi i32 [ %11, %6 ], [ 0, %2 ]
%10 = sext i8 %8 to i32
%11 = add nsw i32 %9, %10
%12 = add nuw nsw i64 %7, 1
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %16, label %6
16: ; preds = %6
%17 = sitofp i32 %11 to double
br label %18
18: ; preds = %16, %2
%19 = phi double [ 0.000000e+00, %2 ], [ %17, %16 ]
%20 = add nsw i32 %3, -1
%21 = sdiv i32 %20, 2
%22 = sitofp i32 %21 to double
%23 = fmul double %19, %22
%24 = tail call i32 @fmod(double %23, i32 1)
%25 = mul nsw i32 %24, %1
ret i32 %25
}