#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*/ free(char *);
scalar_t__ hamdist_str(char *, char *, unsigned int);
char *malloc(unsigned int);
int /*<<< orphan*/ memcpy(char *, char const *, unsigned int);
float hamdist_keyscore(const char *txtdat, unsigned int txtsize,
                       unsigned int keysize) {
  unsigned int keyblk_idx, score = 0;
  char *curblk, *prevblk;
  curblk = malloc(keysize + 1);
  if (NULL == curblk)
    return -1;
  curblk[keysize] = 0;
  prevblk = malloc(keysize + 1);
  if (NULL == prevblk)
    return -1;
  prevblk[keysize] = 0;
  memcpy(curblk, txtdat, keysize);
  for (keyblk_idx = keysize; keyblk_idx < txtsize; keyblk_idx += keysize) {
    memcpy(prevblk, curblk, keysize);
    memcpy(curblk, txtdat + keysize, keysize);
    score += hamdist_str(prevblk, curblk, keysize);
  }
  free(curblk);
  free(prevblk);
  return score / ((float)keysize);
}


x86 assembly (clang, -O0):
<hamdist_keyscore>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    1186 <hamdist_keyscore+0x46>
movss  0xe84(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    1266 <hamdist_keyscore+0x126>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    11be <hamdist_keyscore+0x7e>
movss  0xe4c(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    1266 <hamdist_keyscore+0x126>
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    123b <hamdist_keyscore+0xfb>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%eax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <hamdist_str@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11df <hamdist_keyscore+0x9f>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
cvtsi2ss %rax,%xmm0
mov    -0x18(%rbp),%eax
cvtsi2ss %rax,%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hamdist_keyscore>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %esi,%r13d
mov    %rdi,%r12
lea    0x1(%rbx),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
movss  0xe98(%rip),%xmm0
test   %rax,%rax
je     1248 <hamdist_keyscore+0x108>
mov    %rax,%r14
mov    %ebx,%r15d
movb   $0x0,(%rax,%r15,1)
mov    %ebp,%edi
call   1060 <malloc@plt>
movss  0xe75(%rip),%xmm0
test   %rax,%rax
je     1248 <hamdist_keyscore+0x108>
mov    %rax,%rbp
movb   $0x0,(%rax,%r15,1)
mov    %r14,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
xorps  %xmm0,%xmm0
cmp    %r13d,%ebx
jae    1214 <hamdist_keyscore+0xd4>
mov    %r15,0x10(%rsp)
add    %r15,%r12
mov    %r12,0x8(%rsp)
mov    %r13d,%r12d
xor    %r13d,%r13d
mov    %ebx,%r15d
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
mov    %r14,%rdi
mov    0x8(%rsp),%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <hamdist_str@plt>
add    %eax,%r13d
add    %ebx,%r15d
cmp    %r12d,%r15d
jb     11d0 <hamdist_keyscore+0x90>
mov    %r13d,%eax
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
mov    0x10(%rsp),%r15
movss  %xmm0,0x8(%rsp)
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %r15,%xmm0
movss  0x8(%rsp),%xmm1
divss  %xmm0,%xmm1
movss  %xmm1,0x8(%rsp)
movss  0x8(%rsp),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define float @hamdist_keyscore(i8* %0, i32 %1, i32 %2) {
%4 = alloca float
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %9
%12 = load i32, i32* %7
%13 = add i32 %12, 1
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %10
%15 = load i8*, i8** %10
%16 = icmp eq i8* null, %15
br i1 %16, label %17, label %18
17: ; preds = %3
store float -1.000000e+00, float* %4
br label %77
18: ; preds = %3
%19 = load i8*, i8** %10
%20 = load i32, i32* %7
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8 0, i8* %22
%23 = load i32, i32* %7
%24 = add i32 %23, 1
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %11
%26 = load i8*, i8** %11
%27 = icmp eq i8* null, %26
br i1 %27, label %28, label %29
28: ; preds = %18
store float -1.000000e+00, float* %4
br label %77
29: ; preds = %18
%30 = load i8*, i8** %11
%31 = load i32, i32* %7
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
%34 = load i8*, i8** %10
%35 = load i8*, i8** %5
%36 = load i32, i32* %7
%37 = call i32 @memcpy(i8* %34, i8* %35, i32 %36)
%38 = load i32, i32* %7
store i32 %38, i32* %8
br label %39
39: ; preds = %63, %29
%40 = load i32, i32* %8
%41 = load i32, i32* %6
%42 = icmp ult i32 %40, %41
br i1 %42, label %43, label %67
43: ; preds = %39
%44 = load i8*, i8** %11
%45 = load i8*, i8** %10
%46 = load i32, i32* %7
%47 = call i32 @memcpy(i8* %44, i8* %45, i32 %46)
%48 = load i8*, i8** %10
%49 = load i8*, i8** %5
%50 = load i32, i32* %7
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = load i32, i32* %7
%54 = call i32 @memcpy(i8* %48, i8* %52, i32 %53)
%55 = load i8*, i8** %11
%56 = load i8*, i8** %10
%57 = load i32, i32* %7
%58 = call i64 @hamdist_str(i8* %55, i8* %56, i32 %57)
%59 = load i32, i32* %9
%60 = zext i32 %59 to i64
%61 = add nsw i64 %60, %58
%62 = trunc i64 %61 to i32
store i32 %62, i32* %9
br label %63
63: ; preds = %43
%64 = load i32, i32* %7
%65 = load i32, i32* %8
%66 = add i32 %65, %64
store i32 %66, i32* %8
br label %39
67: ; preds = %39
%68 = load i8*, i8** %10
%69 = call i32 @free(i8* %68)
%70 = load i8*, i8** %11
%71 = call i32 @free(i8* %70)
%72 = load i32, i32* %9
%73 = uitofp i32 %72 to float
%74 = load i32, i32* %7
%75 = uitofp i32 %74 to float
%76 = fdiv float %73, %75
store float %76, float* %4
br label %77
77: ; preds = %67, %28, %17
%78 = load float, float* %4
ret float %78
}

LLVM IR (-O2):
define float @hamdist_keyscore(i8* %0, i32 %1, i32 %2) {
%4 = add i32 %2, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %36, label %7
7: ; preds = %3
%8 = zext i32 %2 to i64
%9 = getelementptr inbounds i8, i8* %5, i64 %8
store i8 0, i8* %9
%10 = tail call i8* @malloc(i32 %4)
%11 = icmp eq i8* %10, null
br i1 %11, label %36, label %12
12: ; preds = %7
%13 = getelementptr inbounds i8, i8* %10, i64 %8
store i8 0, i8* %13
%14 = tail call i32 @memcpy(i8* %5, i8* %0, i32 %2)
%15 = icmp ult i32 %2, %1
br i1 %15, label %16, label %30
16: ; preds = %12
%17 = getelementptr inbounds i8, i8* %0, i64 %8
br label %18
18: ; preds = %16, %18
%19 = phi i32 [ %2, %16 ], [ %26, %18 ]
%20 = phi i32 [ 0, %16 ], [ %25, %18 ]
%21 = tail call i32 @memcpy(i8* %10, i8* %5, i32 %2)
%22 = tail call i32 @memcpy(i8* %5, i8* %17, i32 %2)
%23 = tail call i64 @hamdist_str(i8* %10, i8* %5, i32 %2)
%24 = trunc i64 %23 to i32
%25 = add i32 %20, %24
%26 = add i32 %19, %2
%27 = icmp ult i32 %26, %1
br i1 %27, label %18, label %28
28: ; preds = %18
%29 = uitofp i32 %25 to float
br label %30
30: ; preds = %28, %12
%31 = phi float [ 0.000000e+00, %12 ], [ %29, %28 ]
%32 = tail call i32 @free(i8* %5)
%33 = tail call i32 @free(i8* %10)
%34 = uitofp i32 %2 to float
%35 = fdiv float %31, %34
br label %36
36: ; preds = %7, %3, %30
%37 = phi float [ %35, %30 ], [ -1.000000e+00, %3 ], [ -1.000000e+00, %7 ]
ret float %37
}