#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*/ scanf(char *, char *);
long hash(char *word) {
  long ascii_counter = 42;
  long counter = 0;
  scanf("%s", word);
  while (word[counter] != '\0') {
    ascii_counter = ascii_counter + (word[counter] * (counter + 1));
    counter++;
  }
  return ascii_counter;
}


x86 assembly (clang, -O0):
<hash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x2a,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xec9(%rip),%rdi
call   1030 <scanf@plt>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1186 <hash+0x76>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rdx
movsbq (%rcx,%rdx,1),%rcx
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
imul   %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    113c <hash+0x2c>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hash>:
push   %rbx
mov    %rdi,%rbx
lea    0xee5(%rip),%rdi
mov    %rbx,%rsi
call   1030 <scanf@plt>
mov    (%rbx),%dl
mov    $0x2a,%eax
test   %dl,%dl
je     1157 <hash+0x47>
mov    $0x1,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbq %dl,%rdx
imul   %rcx,%rdx
add    %rdx,%rax
movzbl (%rbx,%rcx,1),%edx
add    $0x1,%rcx
test   %dl,%dl
jne    1140 <hash+0x30>
pop    %rbx
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
define i64 @hash(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
store i8* %0, i8** %2
store i64 42, i64* %3
store i64 0, i64* %4
%5 = load i8*, i8** %2
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %7
7: ; preds = %14, %1
%8 = load i8*, i8** %2
%9 = load i64, i64* %4
%10 = getelementptr inbounds i8, i8* %8, i64 %9
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %7
%15 = load i64, i64* %3
%16 = load i8*, i8** %2
%17 = load i64, i64* %4
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i64
%21 = load i64, i64* %4
%22 = add nsw i64 %21, 1
%23 = mul nsw i64 %20, %22
%24 = add nsw i64 %15, %23
store i64 %24, i64* %3
%25 = load i64, i64* %4
%26 = add nsw i64 %25, 1
store i64 %26, i64* %4
br label %7
27: ; preds = %7
%28 = load i64, i64* %3
ret i64 %28
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
define i64 @hash(i8* %0) {
%2 = tail call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
br i1 %4, label %16, label %5
5: ; preds = %1, %5
%6 = phi i8 [ %14, %5 ], [ %3, %1 ]
%7 = phi i64 [ %10, %5 ], [ 0, %1 ]
%8 = phi i64 [ %12, %5 ], [ 42, %1 ]
%9 = sext i8 %6 to i64
%10 = add nuw nsw i64 %7, 1
%11 = mul nsw i64 %10, %9
%12 = add nsw i64 %11, %8
%13 = getelementptr inbounds i8, i8* %0, i64 %10
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %16, label %5
16: ; preds = %5, %1
%17 = phi i64 [ 42, %1 ], [ %12, %5 ]
ret i64 %17
}