#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 *);
char *malloc(int);
double scoreTextQgram(char *, int);
int /*<<< orphan*/ simplesubEncipher(char *, char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
float findaKey(char *text, int len, char *maxKey) {
  int i, j;
  int maxi, maxj;
  char temp, *deciphered = malloc(sizeof(char) * len);
  char testKey[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  double maxmaxscore = -99e99, maxscore, score;
  while (1) {
    simplesubEncipher(maxKey, text, deciphered, len);
    maxscore = scoreTextQgram(deciphered, len);
    maxi = 0;
    maxj = 0;
    for (i = 0; i < 26; i++) {
      for (j = i; j < 26; j++) {
        strcpy(testKey, maxKey);
        temp = testKey[i];
        testKey[i] = testKey[j];
        testKey[j] = temp;
        simplesubEncipher(testKey, text, deciphered, len);
        score = scoreTextQgram(deciphered, len);
        if (score > maxscore) {
          maxscore = score;
          maxi = i;
          maxj = j;
        }
      }
    }
    if (maxi == maxj)
      break;
    if (maxscore > maxmaxscore) {
      maxmaxscore = maxscore;
      temp = maxKey[maxi];
      maxKey[maxi] = maxKey[maxj];
      maxKey[maxj] = temp;
    }
  }
  free(deciphered);
  return maxmaxscore;
}


x86 assembly (clang, -O0):
<findaKey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    0xe90(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0xe8d(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    0xe8a(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xe87(%rip),%ax
mov    %ax,-0x48(%rbp)
mov    0xe7f(%rip),%al
mov    %al,-0x46(%rbp)
movsd  0xe4a(%rip),%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0xc(%rbp),%ecx
call   1050 <simplesubEncipher@plt>
mov    -0x38(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1070 <scoreTextQgram@plt>
movsd  %xmm0,-0x70(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x1a,-0x1c(%rbp)
jge    12b3 <findaKey+0x163>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x1a,-0x20(%rbp)
jge    12a0 <findaKey+0x150>
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
movslq -0x1c(%rbp),%rax
mov    -0x60(%rbp,%rax,1),%al
mov    %al,-0x29(%rbp)
movslq -0x20(%rbp),%rax
mov    -0x60(%rbp,%rax,1),%cl
movslq -0x1c(%rbp),%rax
mov    %cl,-0x60(%rbp,%rax,1)
mov    -0x29(%rbp),%cl
movslq -0x20(%rbp),%rax
mov    %cl,-0x60(%rbp,%rax,1)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0xc(%rbp),%ecx
call   1050 <simplesubEncipher@plt>
mov    -0x38(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1070 <scoreTextQgram@plt>
movsd  %xmm0,-0x78(%rbp)
movsd  -0x78(%rbp),%xmm0
ucomisd -0x70(%rbp),%xmm0
jbe    128d <findaKey+0x13d>
movsd  -0x78(%rbp),%xmm0
movsd  %xmm0,-0x70(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    1292 <findaKey+0x142>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1205 <findaKey+0xb5>
jmp    12a5 <findaKey+0x155>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f5 <findaKey+0xa5>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jne    12c4 <findaKey+0x174>
jmp    1315 <findaKey+0x1c5>
movsd  -0x70(%rbp),%xmm0
ucomisd -0x68(%rbp),%xmm0
jbe    1310 <findaKey+0x1c0>
movsd  -0x70(%rbp),%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x29(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x29(%rbp),%dl
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11bb <findaKey+0x6b>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movsd  -0x68(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findaKey>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rdx,%r15
mov    %esi,%r13d
mov    %rdi,%r12
mov    %esi,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
movups 0xea3(%rip),%xmm0
movups %xmm0,0x3b(%rsp)
movaps 0xe8c(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movsd  0xe6f(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %r15,0x28(%rsp)
mov    %r13d,0xc(%rsp)
mov    %r12,0x20(%rsp)
mov    %rax,0x18(%rsp)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %r13d,%ecx
call   1050 <simplesubEncipher@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
call   1070 <scoreTextQgram@plt>
xor    %r12d,%r12d
xor    %ebp,%ebp
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  %xmm0,0x50(%rsp)
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    0x28(%rsp),%rsi
call   1040 <strcpy@plt>
movzbl 0x30(%rsp,%r12,1),%eax
movzbl 0x30(%rsp,%r15,1),%ecx
mov    %cl,0x30(%rsp,%r12,1)
mov    %al,0x30(%rsp,%r15,1)
mov    %rbx,%rdi
mov    0x20(%rsp),%rsi
mov    0x18(%rsp),%rbx
mov    %rbx,%rdx
mov    0xc(%rsp),%r13d
mov    %r13d,%ecx
call   1050 <simplesubEncipher@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
call   1070 <scoreTextQgram@plt>
movsd  0x50(%rsp),%xmm1
ucomisd %xmm1,%xmm0
cmova  %r12d,%r14d
cmova  %r15d,%ebp
maxsd  %xmm1,%xmm0
add    $0x1,%r15
cmp    $0x1a,%r15
jne    11f0 <findaKey+0xa0>
add    $0x1,%r12
cmp    $0x1a,%r12
jne    11e0 <findaKey+0x90>
cmp    %ebp,%r14d
je     12ba <findaKey+0x16a>
ucomisd 0x10(%rsp),%xmm0
mov    0x28(%rsp),%r15
mov    0xc(%rsp),%r13d
mov    0x20(%rsp),%r12
mov    0x18(%rsp),%rbx
jbe    11b0 <findaKey+0x60>
movslq %r14d,%rax
mov    (%r15,%rax,1),%sil
movslq %ebp,%rdx
mov    (%r15,%rdx,1),%cl
mov    %cl,(%r15,%rax,1)
mov    %sil,(%r15,%rdx,1)
movsd  %xmm0,0x10(%rsp)
jmp    11b0 <findaKey+0x60>
mov    0x18(%rsp),%rdi
call   1030 <free@plt>
movsd  0x10(%rsp),%xmm0
cvtsd2ss %xmm0,%xmm0
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.findaKey.testKey = [27 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZ\00"
define float @findaKey(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
%12 = alloca i8*
%13 = alloca [27 x i8]
%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 = sext i32 %17 to i64
%19 = mul i64 1, %18
%20 = trunc i64 %19 to i32
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %12
%22 = bitcast [27 x i8]* %13 to i8*
store double -9.900000e+100, double* %14
br label %23
23: ; preds = %3, %112
%24 = load i8*, i8** %6
%25 = load i8*, i8** %4
%26 = load i8*, i8** %12
%27 = load i32, i32* %5
%28 = call i32 @simplesubEncipher(i8* %24, i8* %25, i8* %26, i32 %27)
%29 = load i8*, i8** %12
%30 = load i32, i32* %5
%31 = call double @scoreTextQgram(i8* %29, i32 %30)
store double %31, double* %15
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %7
br label %32
32: ; preds = %79, %23
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 26
br i1 %34, label %35, label %82
35: ; preds = %32
%36 = load i32, i32* %7
store i32 %36, i32* %8
br label %37
37: ; preds = %75, %35
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 26
br i1 %39, label %40, label %78
40: ; preds = %37
%41 = getelementptr inbounds [27 x i8], [27 x i8]* %13, i64 0, i64 0
%42 = load i8*, i8** %6
%43 = call i32 @strcpy(i8* %41, i8* %42)
%44 = load i32, i32* %7
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [27 x i8], [27 x i8]* %13, i64 0, i64 %45
%47 = load i8, i8* %46
store i8 %47, i8* %11
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [27 x i8], [27 x i8]* %13, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = load i32, i32* %7
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [27 x i8], [27 x i8]* %13, i64 0, i64 %53
store i8 %51, i8* %54
%55 = load i8, i8* %11
%56 = load i32, i32* %8
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [27 x i8], [27 x i8]* %13, i64 0, i64 %57
store i8 %55, i8* %58
%59 = getelementptr inbounds [27 x i8], [27 x i8]* %13, i64 0, i64 0
%60 = load i8*, i8** %4
%61 = load i8*, i8** %12
%62 = load i32, i32* %5
%63 = call i32 @simplesubEncipher(i8* %59, i8* %60, i8* %61, i32 %62)
%64 = load i8*, i8** %12
%65 = load i32, i32* %5
%66 = call double @scoreTextQgram(i8* %64, i32 %65)
store double %66, double* %16
%67 = load double, double* %16
%68 = load double, double* %15
%69 = fcmp ogt double %67, %68
br i1 %69, label %70, label %74
70: ; preds = %40
%71 = load double, double* %16
store double %71, double* %15
%72 = load i32, i32* %7
store i32 %72, i32* %9
%73 = load i32, i32* %8
store i32 %73, i32* %10
br label %74
74: ; preds = %70, %40
br label %75
75: ; preds = %74
%76 = load i32, i32* %8
%77 = add nsw i32 %76, 1
store i32 %77, i32* %8
br label %37
78: ; preds = %37
br label %79
79: ; preds = %78
%80 = load i32, i32* %7
%81 = add nsw i32 %80, 1
store i32 %81, i32* %7
br label %32
82: ; preds = %32
%83 = load i32, i32* %9
%84 = load i32, i32* %10
%85 = icmp eq i32 %83, %84
br i1 %85, label %86, label %87
86: ; preds = %82
br label %113
87: ; preds = %82
%88 = load double, double* %15
%89 = load double, double* %14
%90 = fcmp ogt double %88, %89
br i1 %90, label %91, label %112
91: ; preds = %87
%92 = load double, double* %15
store double %92, double* %14
%93 = load i8*, i8** %6
%94 = load i32, i32* %9
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i8, i8* %93, i64 %95
%97 = load i8, i8* %96
store i8 %97, i8* %11
%98 = load i8*, i8** %6
%99 = load i32, i32* %10
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i8, i8* %98, i64 %100
%102 = load i8, i8* %101
%103 = load i8*, i8** %6
%104 = load i32, i32* %9
%105 = sext i32 %104 to i64
%106 = getelementptr inbounds i8, i8* %103, i64 %105
store i8 %102, i8* %106
%107 = load i8, i8* %11
%108 = load i8*, i8** %6
%109 = load i32, i32* %10
%110 = sext i32 %109 to i64
%111 = getelementptr inbounds i8, i8* %108, i64 %110
store i8 %107, i8* %111
br label %112
112: ; preds = %91, %87
br label %23
113: ; preds = %86
%114 = load i8*, i8** %12
%115 = call i32 @free(i8* %114)
%116 = load double, double* %14
%117 = fptrunc double %116 to float
ret float %117
}

LLVM IR (-O2):
@__const.findaKey.testKey = [27 x i8] c"ABCDEFGHIJKLMNOPQRSTUVWXYZ\00"
define float @findaKey(i8* %0, i32 %1, i8* %2) {
%4 = alloca [27 x i8]
%5 = tail call i8* @malloc(i32 %1)
%6 = getelementptr inbounds [27 x i8], [27 x i8]* %4, i64 0, i64 0
br label %7
7: ; preds = %44, %3
%8 = phi double [ %34, %44 ], [ -9.900000e+100, %3 ]
br label %9
9: ; preds = %7, %42
%10 = call i32 @simplesubEncipher(i8* %2, i8* %0, i8* %5, i32 %1)
%11 = call double @scoreTextQgram(i8* %5, i32 %1)
br label %12
12: ; preds = %37, %9
%13 = phi i64 [ 0, %9 ], [ %38, %37 ]
%14 = phi double [ %11, %9 ], [ %34, %37 ]
%15 = phi i32 [ 0, %9 ], [ %33, %37 ]
%16 = phi i32 [ 0, %9 ], [ %31, %37 ]
%17 = getelementptr inbounds [27 x i8], [27 x i8]* %4, i64 0, i64 %13
%18 = trunc i64 %13 to i32
br label %19
19: ; preds = %12, %19
%20 = phi i64 [ %13, %12 ], [ %35, %19 ]
%21 = phi double [ %14, %12 ], [ %34, %19 ]
%22 = phi i32 [ %15, %12 ], [ %33, %19 ]
%23 = phi i32 [ %16, %12 ], [ %31, %19 ]
%24 = call i32 @strcpy(i8* %6, i8* %2)
%25 = load i8, i8* %17
%26 = getelementptr inbounds [27 x i8], [27 x i8]* %4, i64 0, i64 %20
%27 = load i8, i8* %26
store i8 %27, i8* %17
store i8 %25, i8* %26
%28 = call i32 @simplesubEncipher(i8* %6, i8* %0, i8* %5, i32 %1)
%29 = call double @scoreTextQgram(i8* %5, i32 %1)
%30 = fcmp ogt double %29, %21
%31 = select i1 %30, i32 %18, i32 %23
%32 = trunc i64 %20 to i32
%33 = select i1 %30, i32 %32, i32 %22
%34 = select i1 %30, double %29, double %21
%35 = add nuw nsw i64 %20, 1
%36 = icmp eq i64 %35, 26
br i1 %36, label %37, label %19
37: ; preds = %19
%38 = add nuw nsw i64 %13, 1
%39 = icmp eq i64 %38, 26
br i1 %39, label %40, label %12
40: ; preds = %37
%41 = icmp eq i32 %31, %33
br i1 %41, label %51, label %42
42: ; preds = %40
%43 = fcmp ogt double %34, %8
br i1 %43, label %44, label %9
44: ; preds = %42
%45 = sext i32 %31 to i64
%46 = getelementptr inbounds i8, i8* %2, i64 %45
%47 = load i8, i8* %46
%48 = sext i32 %33 to i64
%49 = getelementptr inbounds i8, i8* %2, i64 %48
%50 = load i8, i8* %49
store i8 %50, i8* %46
store i8 %47, i8* %49
br label %7
51: ; preds = %40
%52 = call i32 @free(i8* %5)
%53 = fptrunc double %8 to float
ret float %53
}