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


x86 assembly (clang, -O0):
<hash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x2a,-0x14(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xec2(%rip),%rdi
call   1040 <scanf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x10(%rbp)
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     119b <hash+0x7b>
mov    -0x14(%rbp),%eax
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
movsbl (%rcx,%rdx,1),%ecx
imul   -0xc(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1156 <hash+0x36>
movslq -0x14(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hash>:
push   %rbx
mov    %rdi,%rbx
lea    0xed5(%rip),%rdi
mov    %rbx,%rsi
call   1040 <scanf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    (%rbx),%dl
test   %dl,%dl
je     1168 <hash+0x48>
mov    $0x2a,%eax
mov    $0x1,%ecx
nopl   0x0(%rax,%rax,1)
movsbl %dl,%edx
imul   %ecx,%edx
add    %edx,%eax
movzbl (%rbx,%rcx,1),%edx
add    $0x1,%rcx
test   %dl,%dl
jne    1150 <hash+0x30>
cltq
pop    %rbx
ret
mov    $0x2a,%eax
pop    %rbx
ret

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

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 = tail call i32 @strlen(i8* %0)
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %20, label %6
6: ; preds = %1, %6
%7 = phi i64 [ %14, %6 ], [ 1, %1 ]
%8 = phi i8 [ %16, %6 ], [ %4, %1 ]
%9 = phi i32 [ %13, %6 ], [ 42, %1 ]
%10 = sext i8 %8 to i32
%11 = trunc i64 %7 to i32
%12 = mul nsw i32 %11, %10
%13 = add nsw i32 %12, %9
%14 = add nuw i64 %7, 1
%15 = getelementptr inbounds i8, i8* %0, i64 %7
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %18, label %6
18: ; preds = %6
%19 = sext i32 %13 to i64
br label %20
20: ; preds = %18, %1
%21 = phi i64 [ 42, %1 ], [ %19, %18 ]
ret i64 %21
}