#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 */
scalar_t__ hamming_distance(unsigned char *, unsigned char *, int);
size_t find_best_key_size(unsigned char *data, size_t data_len) {
  size_t best_size = 0;
  size_t best_score = 1000000;
  int key = 0;
  for (key = 2; key <= 40; ++key) {
    int i = 0;
    size_t score = 0;
    for (i = 0; i < 10; ++i) {
      score += hamming_distance(&data[i * key], &data[(i + 1) * key], key);
    }
    score = score * 10000 / key;
    if (score < best_score) {
      best_score = score;
      best_size = key;
    }
  }
  return best_size;
}


x86 assembly (clang, -O0):
<find_best_key_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0xf4240,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x2,-0x24(%rbp)
cmpl   $0x28,-0x24(%rbp)
jg     11ef <find_best_key_size+0xdf>
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0xa,-0x28(%rbp)
jge    11a9 <find_best_key_size+0x99>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%eax
imul   -0x24(%rbp),%eax
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%eax
add    $0x1,%eax
imul   -0x24(%rbp),%eax
cltq
add    %rax,%rsi
mov    -0x24(%rbp),%edx
call   1030 <hamming_distance@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    115e <find_best_key_size+0x4e>
imul   $0x2710,-0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11dc <find_best_key_size+0xcc>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
movslq -0x24(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11e1 <find_best_key_size+0xd1>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    113e <find_best_key_size+0x2e>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_best_key_size>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rdi,0x58(%rsp)
mov    $0xf4240,%ecx
mov    $0x2,%r14d
mov    $0x10,%r11d
mov    $0x8,%r10d
mov    $0x4,%ebp
mov    $0x6,%edi
mov    $0xa,%r8d
mov    $0xc,%r9d
mov    $0xe,%esi
mov    $0x12,%ebx
mov    $0x14,%r15d
xor    %edx,%edx
mov    0x58(%rsp),%r13
jmp    11e4 <find_best_key_size+0xd4>
nopl   0x0(%rax)
xor    %edx,%edx
div    %r14
mov    0x50(%rsp),%rcx
mov    0x38(%rsp),%r11
mov    0x30(%rsp),%r10
mov    0x48(%rsp),%rbp
mov    0x8(%rsp),%rdi
mov    0x28(%rsp),%r8
mov    0x20(%rsp),%r9
mov    0x18(%rsp),%rsi
mov    0x10(%rsp),%rbx
mov    0x40(%rsp),%rdx
cmp    %rcx,%rax
cmovb  %r14,%rdx
cmovb  %rax,%rcx
add    $0x1,%r14
add    $0x8,%r11
add    $0x4,%r10
add    $0x2,%rbp
add    $0x3,%rdi
add    $0x5,%r8
add    $0x6,%r9
add    $0x7,%rsi
add    $0x9,%rbx
add    $0xa,%r15
cmp    $0x29,%r14
je     1350 <find_best_key_size+0x240>
mov    %r10,0x30(%rsp)
mov    %r8,0x28(%rsp)
mov    %r9,0x20(%rsp)
mov    %rsi,0x18(%rsp)
mov    %r11,0x38(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r15,0x60(%rsp)
mov    %rdx,0x40(%rsp)
mov    %rcx,0x50(%rsp)
lea    0x0(%r13,%r14,1),%r12
mov    %rdi,%rbx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1030 <hamming_distance@plt>
mov    %rax,0x8(%rsp)
mov    %rbp,0x48(%rsp)
mov    %r13,%r15
add    %rbp,%r15
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <hamming_distance@plt>
mov    %rax,%rbp
add    0x8(%rsp),%rbp
mov    %rbx,0x8(%rsp)
lea    (%rbx,%r13,1),%r12
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1030 <hamming_distance@plt>
mov    %rax,%rbx
add    %rbp,%rbx
mov    0x30(%rsp),%rax
lea    (%rax,%r13,1),%r15
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <hamming_distance@plt>
mov    %rax,%rbp
add    %rbx,%rbp
mov    0x28(%rsp),%rax
lea    (%rax,%r13,1),%r12
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1030 <hamming_distance@plt>
mov    %rax,%rbx
add    %rbp,%rbx
mov    0x20(%rsp),%rax
lea    (%rax,%r13,1),%r15
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <hamming_distance@plt>
mov    %rax,%rbp
add    %rbx,%rbp
mov    0x18(%rsp),%rax
lea    (%rax,%r13,1),%r12
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1030 <hamming_distance@plt>
mov    %rax,%rbx
add    %rbp,%rbx
mov    0x38(%rsp),%rax
lea    (%rax,%r13,1),%r15
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <hamming_distance@plt>
mov    %rax,%rbp
add    %rbx,%rbp
mov    0x10(%rsp),%rax
lea    (%rax,%r13,1),%r12
mov    %r15,%rdi
mov    0x60(%rsp),%r15
mov    %r12,%rsi
mov    %r14d,%edx
call   1030 <hamming_distance@plt>
mov    %rax,%rbx
add    %rbp,%rbx
lea    (%r15,%r13,1),%rsi
mov    %r12,%rdi
mov    %r14d,%edx
call   1030 <hamming_distance@plt>
add    %rbx,%rax
imul   $0x2710,%rax,%rax
mov    %rax,%rcx
or     %r14,%rcx
shr    $0x20,%rcx
jne    1170 <find_best_key_size+0x60>
xor    %edx,%edx
div    %r14d
jmp    1175 <find_best_key_size+0x65>
mov    %rdx,%rax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @find_best_key_size(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
store i8* %0, i8** %3
store i64 %1, i64* %4
store i64 0, i64* %5
store i64 1000000, i64* %6
store i32 0, i32* %7
store i32 2, i32* %7
br label %10
10: ; preds = %52, %2
%11 = load i32, i32* %7
%12 = icmp sle i32 %11, 40
br i1 %12, label %13, label %55
13: ; preds = %10
store i32 0, i32* %8
store i64 0, i64* %9
store i32 0, i32* %8
br label %14
14: ; preds = %35, %13
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 10
br i1 %16, label %17, label %38
17: ; preds = %14
%18 = load i8*, i8** %3
%19 = load i32, i32* %8
%20 = load i32, i32* %7
%21 = mul nsw i32 %19, %20
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %18, i64 %22
%24 = load i8*, i8** %3
%25 = load i32, i32* %8
%26 = add nsw i32 %25, 1
%27 = load i32, i32* %7
%28 = mul nsw i32 %26, %27
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %24, i64 %29
%31 = load i32, i32* %7
%32 = call i64 @hamming_distance(i8* %23, i8* %30, i32 %31)
%33 = load i64, i64* %9
%34 = add i64 %33, %32
store i64 %34, i64* %9
br label %35
35: ; preds = %17
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 1
store i32 %37, i32* %8
br label %14
38: ; preds = %14
%39 = load i64, i64* %9
%40 = mul i64 %39, 10000
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = udiv i64 %40, %42
store i64 %43, i64* %9
%44 = load i64, i64* %9
%45 = load i64, i64* %6
%46 = icmp ult i64 %44, %45
br i1 %46, label %47, label %51
47: ; preds = %38
%48 = load i64, i64* %9
store i64 %48, i64* %6
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
store i64 %50, i64* %5
br label %51
51: ; preds = %47, %38
br label %52
52: ; preds = %51
%53 = load i32, i32* %7
%54 = add nsw i32 %53, 1
store i32 %54, i32* %7
br label %10
55: ; preds = %10
%56 = load i64, i64* %5
ret i64 %56
}

LLVM IR (-O2):
define i64 @find_best_key_size(i8* %0, i64 %1) {
br label %3
3: ; preds = %2, %3
%4 = phi i64 [ 2, %2 ], [ %51, %3 ]
%5 = phi i64 [ 1000000, %2 ], [ %50, %3 ]
%6 = phi i64 [ 0, %2 ], [ %49, %3 ]
%7 = getelementptr inbounds i8, i8* %0, i64 %4
%8 = trunc i64 %4 to i32
%9 = tail call i64 @hamming_distance(i8* %0, i8* %7, i32 %8)
%10 = shl nuw nsw i64 %4, 1
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = tail call i64 @hamming_distance(i8* %7, i8* %11, i32 %8)
%13 = add i64 %12, %9
%14 = mul nuw nsw i64 %4, 3
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = tail call i64 @hamming_distance(i8* %11, i8* %15, i32 %8)
%17 = add i64 %16, %13
%18 = shl nuw nsw i64 %4, 2
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = tail call i64 @hamming_distance(i8* %15, i8* %19, i32 %8)
%21 = add i64 %20, %17
%22 = mul nuw nsw i64 %4, 5
%23 = getelementptr inbounds i8, i8* %0, i64 %22
%24 = tail call i64 @hamming_distance(i8* %19, i8* %23, i32 %8)
%25 = add i64 %24, %21
%26 = mul nuw nsw i64 %4, 6
%27 = getelementptr inbounds i8, i8* %0, i64 %26
%28 = tail call i64 @hamming_distance(i8* %23, i8* %27, i32 %8)
%29 = add i64 %28, %25
%30 = mul nuw nsw i64 %4, 7
%31 = getelementptr inbounds i8, i8* %0, i64 %30
%32 = tail call i64 @hamming_distance(i8* %27, i8* %31, i32 %8)
%33 = add i64 %32, %29
%34 = shl nuw nsw i64 %4, 3
%35 = getelementptr inbounds i8, i8* %0, i64 %34
%36 = tail call i64 @hamming_distance(i8* %31, i8* %35, i32 %8)
%37 = add i64 %36, %33
%38 = mul nuw nsw i64 %4, 9
%39 = getelementptr inbounds i8, i8* %0, i64 %38
%40 = tail call i64 @hamming_distance(i8* %35, i8* %39, i32 %8)
%41 = add i64 %40, %37
%42 = mul nuw nsw i64 %4, 10
%43 = getelementptr inbounds i8, i8* %0, i64 %42
%44 = tail call i64 @hamming_distance(i8* %39, i8* %43, i32 %8)
%45 = add i64 %44, %41
%46 = mul i64 %45, 10000
%47 = udiv i64 %46, %4
%48 = icmp ult i64 %47, %5
%49 = select i1 %48, i64 %4, i64 %6
%50 = select i1 %48, i64 %47, i64 %5
%51 = add nuw nsw i64 %4, 1
%52 = icmp eq i64 %51, 41
br i1 %52, label %53, label %3
53: ; preds = %3
ret i64 %49
}