#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 */
double RAND_MAX;
double exp(double);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ modifyKey(char *, char *);
int /*<<< orphan*/ playfairDecipher(char *, char *, char *, int);
double rand();
double scoreTextQgram(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
float playfairCrack(char *text, int len, char *bestKey) {
  int count;
  float T;
  char *deciphered = malloc(sizeof(char) * (len + 1));
  char testKey[26];
  char maxKey[26];
  double prob, dF, maxscore, score;
  double bestscore;
  strcpy(maxKey, bestKey);
  playfairDecipher(maxKey, text, deciphered, len);
  maxscore = scoreTextQgram(deciphered, len);
  bestscore = maxscore;
  for (T = 20; T >= 0; T -= 0.2) {
    for (count = 0; count < 10000; count++) {
      modifyKey(testKey, maxKey);
      playfairDecipher(testKey, text, deciphered, len);
      score = scoreTextQgram(deciphered, len);
      dF = score - maxscore;
      if (dF >= 0) {
        maxscore = score;
        strcpy(maxKey, testKey);
      } else if (T > 0) {
        prob = exp(dF / T);
        if (prob > 1.0 * rand() / RAND_MAX) {
          maxscore = score;
          strcpy(maxKey, testKey);
        }
      }
      if (maxscore > bestscore) {
        bestscore = maxscore;
        strcpy(bestKey, maxKey);
      }
    }
  }
  free(deciphered);
  return bestscore;
}


x86 assembly (clang, -O0):
<playfairCrack>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcpy@plt>
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0xc(%rbp),%ecx
call   1060 <playfairDecipher@plt>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1090 <scoreTextQgram@plt>
movsd  %xmm0,-0x88(%rbp)
ff
movsd  -0x88(%rbp),%xmm0
ff
movsd  %xmm0,-0x98(%rbp)
ff
movss  0xe06(%rip),%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0x20(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jb     13a4 <playfairCrack+0x224>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2710,-0x1c(%rbp)
jge    137c <playfairCrack+0x1fc>
lea    -0x50(%rbp),%rdi
lea    -0x70(%rbp),%rsi
call   1030 <modifyKey@plt>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0xc(%rbp),%ecx
call   1060 <playfairDecipher@plt>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1090 <scoreTextQgram@plt>
movsd  %xmm0,-0x90(%rbp)
ff
movsd  -0x90(%rbp),%xmm0
ff
subsd  -0x88(%rbp),%xmm0
ff
movsd  %xmm0,-0x80(%rbp)
movsd  -0x80(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     12a2 <playfairCrack+0x122>
movsd  -0x90(%rbp),%xmm0
ff
movsd  %xmm0,-0x88(%rbp)
ff
lea    -0x70(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1050 <strcpy@plt>
jmp    1336 <playfairCrack+0x1b6>
movss  -0x20(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jbe    1331 <playfairCrack+0x1b1>
movsd  -0x80(%rbp),%xmm0
movss  -0x20(%rbp),%xmm1
cvtss2sd %xmm1,%xmm1
divsd  %xmm1,%xmm0
call   1080 <exp@plt>
movsd  %xmm0,-0x78(%rbp)
movsd  -0x78(%rbp),%xmm0
movsd  %xmm0,-0xa0(%rbp)
ff
mov    $0x0,%al
call   10a0 <rand@plt>
movaps %xmm0,%xmm2
movsd  -0xa0(%rbp),%xmm0
ff
movsd  0xd1a(%rip),%xmm1
mulsd  %xmm2,%xmm1
mov    0x2ce7(%rip),%rax
divsd  (%rax),%xmm1
ucomisd %xmm1,%xmm0
jbe    132c <playfairCrack+0x1ac>
movsd  -0x90(%rbp),%xmm0
ff
movsd  %xmm0,-0x88(%rbp)
ff
lea    -0x70(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1050 <strcpy@plt>
jmp    1331 <playfairCrack+0x1b1>
jmp    1336 <playfairCrack+0x1b6>
movsd  -0x88(%rbp),%xmm0
ff
ucomisd -0x98(%rbp),%xmm0
ff
jbe    1369 <playfairCrack+0x1e9>
movsd  -0x88(%rbp),%xmm0
ff
movsd  %xmm0,-0x98(%rbp)
ff
mov    -0x18(%rbp),%rdi
lea    -0x70(%rbp),%rsi
call   1050 <strcpy@plt>
jmp    136e <playfairCrack+0x1ee>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1217 <playfairCrack+0x97>
jmp    1381 <playfairCrack+0x201>
movss  -0x20(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xc76(%rip),%xmm1
subsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x20(%rbp)
jmp    11ff <playfairCrack+0x7f>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movsd  -0x98(%rbp),%xmm0
ff
cvtsd2ss %xmm0,%xmm0
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<playfairCrack>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rdx,%rbp
mov    %esi,%r12d
mov    %rdi,%r15
lea    0x1(%r12),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0x40(%rsp),%r13
mov    %r13,%rdi
mov    %rbp,0x38(%rsp)
mov    %rbp,%rsi
call   1050 <strcpy@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r12d,%ecx
call   1060 <playfairDecipher@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1090 <scoreTextQgram@plt>
movss  0xe20(%rip),%xmm1
lea    0x60(%rsp),%rbp
movsd  %xmm0,0x10(%rsp)
movsd  %xmm0,0x8(%rsp)
jmp    1222 <playfairCrack+0xa2>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  0x28(%rsp),%xmm0
addsd  0xdfa(%rip),%xmm0
xorps  %xmm1,%xmm1
cvtsd2ss %xmm0,%xmm1
ucomiss 0xde8(%rip),%xmm1
jb     1327 <playfairCrack+0x1a7>
movss  %xmm1,0x24(%rsp)
xorps  %xmm0,%xmm0
cvtss2sd %xmm1,%xmm0
movsd  %xmm0,0x28(%rsp)
mov    $0x2710,%r14d
jmp    1246 <playfairCrack+0xc6>
nopl   (%rax)
add    $0xffffffff,%r14d
je     1200 <playfairCrack+0x80>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1030 <modifyKey@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r12d,%ecx
call   1060 <playfairDecipher@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1090 <scoreTextQgram@plt>
movaps %xmm0,%xmm3
movsd  0x8(%rsp),%xmm2
subsd  %xmm2,%xmm0
ucomisd 0xd8e(%rip),%xmm0
jae    12e0 <playfairCrack+0x160>
movss  0x24(%rsp),%xmm1
ucomiss 0xd73(%rip),%xmm1
jbe    12f7 <playfairCrack+0x177>
divsd  0x28(%rsp),%xmm0
movsd  %xmm3,0x18(%rsp)
call   1080 <exp@plt>
movsd  %xmm0,0x30(%rsp)
xor    %eax,%eax
call   10a0 <rand@plt>
movsd  0x18(%rsp),%xmm3
movsd  0x8(%rsp),%xmm2
mov    0x2d24(%rip),%rax
divsd  (%rax),%xmm0
movsd  0x30(%rsp),%xmm1
ucomisd %xmm0,%xmm1
jbe    12f7 <playfairCrack+0x177>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  %xmm3,0x18(%rsp)
mov    %r13,%rdi
mov    %rbp,%rsi
call   1050 <strcpy@plt>
movsd  0x18(%rsp),%xmm2
movsd  %xmm2,0x8(%rsp)
ucomisd 0x10(%rsp),%xmm2
jbe    1240 <playfairCrack+0xc0>
mov    0x38(%rsp),%rdi
mov    %r13,%rsi
call   1050 <strcpy@plt>
movsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x10(%rsp)
jmp    1240 <playfairCrack+0xc0>
mov    %rbx,%rdi
call   1040 <free@plt>
movsd  0x10(%rsp),%xmm0
cvtsd2ss %xmm0,%xmm0
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global double 0.000000e+00
define float @playfairCrack(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca float
%9 = alloca i8*
%10 = alloca [26 x i8]
%11 = alloca [26 x i8]
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
%16 = alloca double
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = mul i64 1, %19
%21 = trunc i64 %20 to i32
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %9
%23 = getelementptr inbounds [26 x i8], [26 x i8]* %11, i64 0, i64 0
%24 = load i8*, i8** %6
%25 = call i32 @strcpy(i8* %23, i8* %24)
%26 = getelementptr inbounds [26 x i8], [26 x i8]* %11, i64 0, i64 0
%27 = load i8*, i8** %4
%28 = load i8*, i8** %9
%29 = load i32, i32* %5
%30 = call i32 @playfairDecipher(i8* %26, i8* %27, i8* %28, i32 %29)
%31 = load i8*, i8** %9
%32 = load i32, i32* %5
%33 = call double @scoreTextQgram(i8* %31, i32 %32)
store double %33, double* %14
%34 = load double, double* %14
store double %34, double* %16
store float 2.000000e+01, float* %8
br label %35
35: ; preds = %100, %3
%36 = load float, float* %8
%37 = fcmp oge float %36, 0.000000e+00
br i1 %37, label %38, label %105
38: ; preds = %35
store i32 0, i32* %7
br label %39
39: ; preds = %96, %38
%40 = load i32, i32* %7
%41 = icmp slt i32 %40, 10000
br i1 %41, label %42, label %99
42: ; preds = %39
%43 = getelementptr inbounds [26 x i8], [26 x i8]* %10, i64 0, i64 0
%44 = getelementptr inbounds [26 x i8], [26 x i8]* %11, i64 0, i64 0
%45 = call i32 @modifyKey(i8* %43, i8* %44)
%46 = getelementptr inbounds [26 x i8], [26 x i8]* %10, i64 0, i64 0
%47 = load i8*, i8** %4
%48 = load i8*, i8** %9
%49 = load i32, i32* %5
%50 = call i32 @playfairDecipher(i8* %46, i8* %47, i8* %48, i32 %49)
%51 = load i8*, i8** %9
%52 = load i32, i32* %5
%53 = call double @scoreTextQgram(i8* %51, i32 %52)
store double %53, double* %15
%54 = load double, double* %15
%55 = load double, double* %14
%56 = fsub double %54, %55
store double %56, double* %13
%57 = load double, double* %13
%58 = fcmp oge double %57, 0.000000e+00
br i1 %58, label %59, label %64
59: ; preds = %42
%60 = load double, double* %15
store double %60, double* %14
%61 = getelementptr inbounds [26 x i8], [26 x i8]* %11, i64 0, i64 0
%62 = getelementptr inbounds [26 x i8], [26 x i8]* %10, i64 0, i64 0
%63 = call i32 @strcpy(i8* %61, i8* %62)
br label %86
64: ; preds = %42
%65 = load float, float* %8
%66 = fcmp ogt float %65, 0.000000e+00
br i1 %66, label %67, label %85
67: ; preds = %64
%68 = load double, double* %13
%69 = load float, float* %8
%70 = fpext float %69 to double
%71 = fdiv double %68, %70
%72 = call double @exp(double %71)
store double %72, double* %12
%73 = load double, double* %12
%74 = call double (...) @rand()
%75 = fmul double 1.000000e+00, %74
%76 = load double, double* @RAND_MAX
%77 = fdiv double %75, %76
%78 = fcmp ogt double %73, %77
br i1 %78, label %79, label %84
79: ; preds = %67
%80 = load double, double* %15
store double %80, double* %14
%81 = getelementptr inbounds [26 x i8], [26 x i8]* %11, i64 0, i64 0
%82 = getelementptr inbounds [26 x i8], [26 x i8]* %10, i64 0, i64 0
%83 = call i32 @strcpy(i8* %81, i8* %82)
br label %84
84: ; preds = %79, %67
br label %85
85: ; preds = %84, %64
br label %86
86: ; preds = %85, %59
%87 = load double, double* %14
%88 = load double, double* %16
%89 = fcmp ogt double %87, %88
br i1 %89, label %90, label %95
90: ; preds = %86
%91 = load double, double* %14
store double %91, double* %16
%92 = load i8*, i8** %6
%93 = getelementptr inbounds [26 x i8], [26 x i8]* %11, i64 0, i64 0
%94 = call i32 @strcpy(i8* %92, i8* %93)
br label %95
95: ; preds = %90, %86
br label %96
96: ; preds = %95
%97 = load i32, i32* %7
%98 = add nsw i32 %97, 1
store i32 %98, i32* %7
br label %39
99: ; preds = %39
br label %100
100: ; preds = %99
%101 = load float, float* %8
%102 = fpext float %101 to double
%103 = fsub double %102, 2.000000e-01
%104 = fptrunc double %103 to float
store float %104, float* %8
br label %35
105: ; preds = %35
%106 = load i8*, i8** %9
%107 = call i32 @free(i8* %106)
%108 = load double, double* %16
%109 = fptrunc double %108 to float
ret float %109
}

LLVM IR (-O2):
@RAND_MAX = global double 0.000000e+00
define float @playfairCrack(i8* %0, i32 %1, i8* %2) {
%4 = alloca [26 x i8]
%5 = alloca [26 x i8]
%6 = add nsw i32 %1, 1
%7 = tail call i8* @malloc(i32 %6)
%8 = getelementptr inbounds [26 x i8], [26 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [26 x i8], [26 x i8]* %5, i64 0, i64 0
%10 = call i32 @strcpy(i8* %9, i8* %2)
%11 = call i32 @playfairDecipher(i8* %9, i8* %0, i8* %7, i32 %1)
%12 = call double @scoreTextQgram(i8* %7, i32 %1)
br label %13
13: ; preds = %3, %47
%14 = phi double [ %12, %3 ], [ %44, %47 ]
%15 = phi double [ %12, %3 ], [ %39, %47 ]
%16 = phi float [ 2.000000e+01, %3 ], [ %49, %47 ]
%17 = fcmp ogt float %16, 0.000000e+00
%18 = fpext float %16 to double
br label %19
19: ; preds = %13, %43
%20 = phi double [ %14, %13 ], [ %44, %43 ]
%21 = phi i32 [ 0, %13 ], [ %45, %43 ]
%22 = phi double [ %15, %13 ], [ %39, %43 ]
%23 = call i32 @modifyKey(i8* %8, i8* %9)
%24 = call i32 @playfairDecipher(i8* %8, i8* %0, i8* %7, i32 %1)
%25 = call double @scoreTextQgram(i8* %7, i32 %1)
%26 = fsub double %25, %22
%27 = fcmp ult double %26, 0.000000e+00
br i1 %27, label %28, label %36
28: ; preds = %19
br i1 %17, label %29, label %38
29: ; preds = %28
%30 = fdiv double %26, %18
%31 = call double @exp(double %30)
%32 = call double (...) @rand()
%33 = load double, double* @RAND_MAX
%34 = fdiv double %32, %33
%35 = fcmp ogt double %31, %34
br i1 %35, label %36, label %38
36: ; preds = %29, %19
%37 = call i32 @strcpy(i8* %9, i8* %8)
br label %38
38: ; preds = %36, %28, %29
%39 = phi double [ %22, %29 ], [ %22, %28 ], [ %25, %36 ]
%40 = fcmp ogt double %39, %20
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @strcpy(i8* %2, i8* %9)
br label %43
43: ; preds = %38, %41
%44 = phi double [ %39, %41 ], [ %20, %38 ]
%45 = add nuw nsw i32 %21, 1
%46 = icmp eq i32 %45, 10000
br i1 %46, label %47, label %19
47: ; preds = %43
%48 = fadd double %18, -2.000000e-01
%49 = fptrunc double %48 to float
%50 = fcmp ult float %49, 0.000000e+00
br i1 %50, label %51, label %13
51: ; preds = %47
%52 = call i32 @free(i8* %7)
%53 = fptrunc double %44 to float
ret float %53
}