#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 bifidCrack(char *, int, char *, int);
int /*<<< orphan*/ bifidDecipher(char *, int, char *, char *, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ srand(unsigned int);
int strlen(char *);
scalar_t__ time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  char cipher[] =
      "KWTAZQLAWWZCPONIVBTTBVQUZUGRNHAYIYGIAAYURCUQLDFTYVHTNQEENUPAIFCUNQTNGITE"
      "FUSHFDWHRIFSVTBISYDHHASQSROMUEVPQHHCCRBYTQBHWYRRHTEPEKHOBFSZUQBTSYRSQUDC"
      "SAOVUUGXOAUYWHPGAYHDNKEZPFKKWRIEHDWPEIOTBKESYETPBPOGTHQSPUMDOVUEQAUPCPFC"
      "QHRPHSOPQRSSLPEVWNIQDIOTSQESDHURIEREN";
  int period = 7;
  int len = strlen(cipher);
  char *out = malloc(sizeof(char) * (len + 1));
  srand((unsigned)time(NULL));
  printf("Running bifidcrack, this could take a few minutes...\n");
  char key[] = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
  int i = 0;
  double score, maxscore = -99e99;
  while (1) {
    i++;
    score = bifidCrack(cipher, len, key, period);
    if (score > maxscore) {
      maxscore = score;
      printf("best score so far: %f, on iteration %d\n", score, i);
      printf("    Key: '%s'\n", key);
      bifidDecipher(key, period, cipher, out, len);
      printf("    plaintext: '%s'\n", out);
    }
  }
  free(out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x110(%rbp),%rdi
lea    0xe69(%rip),%rsi
mov    $0xfe,%edx
call   1060 <memcpy@plt>
movl   $0x7,-0x114(%rbp)
lea    -0x110(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x118(%rbp)
mov    -0x118(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x120(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
lea    0xf29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0xf01(%rip),%rax
mov    %rax,-0x140(%rbp)
mov    0xefb(%rip),%rax
mov    %rax,-0x138(%rbp)
mov    0xef5(%rip),%rax
mov    %rax,-0x130(%rbp)
mov    0xeef(%rip),%ax
mov    %ax,-0x128(%rbp)
movl   $0x0,-0x144(%rbp)
movsd  0xdae(%rip),%xmm0
movsd  %xmm0,-0x158(%rbp)
ff
mov    -0x144(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x144(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x118(%rbp),%esi
lea    -0x140(%rbp),%rdx
mov    -0x114(%rbp),%ecx
call   1070 <bifidCrack@plt>
movsd  %xmm0,-0x150(%rbp)
ff
movsd  -0x150(%rbp),%xmm0
ff
ucomisd -0x158(%rbp),%xmm0
ff
jbe    1323 <main+0x1a3>
movsd  -0x150(%rbp),%xmm0
ff
movsd  %xmm0,-0x158(%rbp)
ff
movsd  -0x150(%rbp),%xmm0
ff
mov    -0x144(%rbp),%esi
lea    0xe95(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
lea    -0x140(%rbp),%rsi
lea    0xea8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x140(%rbp),%rdi
mov    -0x114(%rbp),%esi
lea    -0x110(%rbp),%rdx
mov    -0x120(%rbp),%rcx
mov    -0x118(%rbp),%r8d
call   10a0 <bifidDecipher@plt>
mov    -0x120(%rbp),%rsi
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125a <main+0xda>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
lea    0xe68(%rip),%rsi
lea    0x30(%rsp),%r14
mov    $0xfe,%edx
mov    %r14,%rdi
call   1070 <memcpy@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
lea    0xf92(%rip),%rdi
call   1030 <puts@plt>
movups 0xf2a(%rip),%xmm0
movups %xmm0,0x1a(%rsp)
movapd 0xf13(%rip),%xmm0
movapd %xmm0,0x10(%rsp)
movsd  0xdf5(%rip),%xmm1
lea    0x10(%rsp),%r12
lea    0xf4a(%rip),%r13
xor    %ebp,%ebp
nopl   0x0(%rax)
movsd  %xmm1,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%ebp
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r12,%rdx
mov    $0x7,%ecx
call   1080 <bifidCrack@plt>
movsd  0x8(%rsp),%xmm1
ucomisd %xmm1,%xmm0
jbe    1230 <main+0xa0>
lea    0xed2(%rip),%rdi
movsd  %xmm0,(%rsp)
mov    %ebp,%esi
mov    $0x1,%al
call   1050 <printf@plt>
lea    0xee5(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
mov    $0x7,%esi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %ebx,%r8d
call   10b0 <bifidDecipher@plt>
mov    %r13,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
movsd  (%rsp),%xmm1
jmp    1220 <main+0x90>

LLVM IR (-O0):
@__const.main.cipher = [254 x i8] c"KWTAZQLAWWZCPONIVBTTBVQUZUGRNHAYIYGIAAYURCUQLDFTYVHTNQEENUPAIFCUNQTNGITEFUSHFDWHRIFSVTBISYDHHASQSROMUEVPQHHCCRBYTQBHWYRRHTEPEKHOBFSZUQBTSYRSQUDCSAOVUUGXOAUYWHPGAYHDNKEZPFKKWRIEHDWPEIOTBKESYETPBPOGTHQSPUMDOVUEQAUPCPFCQHRPHSOPQRSSLPEVWNIQDIOTSQESDHURIEREN\00"
@.str = [54 x i8] c"Running bifidcrack, this could take a few minutes...\0A\00"
@__const.main.key = [26 x i8] c"ABCDEFGHIKLMNOPQRSTUVWXYZ\00"
@.str.1 = [40 x i8] c"best score so far: %f, on iteration %d\0A\00"
@.str.2 = [15 x i8] c" Key: '%s'\0A\00"
@.str.3 = [21 x i8] c" plaintext: '%s'\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [254 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca [26 x i8]
%11 = alloca i32
%12 = alloca double
%13 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = bitcast [254 x i8]* %6 to i8*
store i32 7, i32* %7
%15 = getelementptr inbounds [254 x i8], [254 x i8]* %6, i64 0, i64 0
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%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 = call i64 @time(i32* null)
%24 = trunc i64 %23 to i32
%25 = call i32 @srand(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%27 = bitcast [26 x i8]* %10 to i8*
store i32 0, i32* %11
store double -9.900000e+100, double* %13
br label %28
28: ; preds = %2, %54
%29 = load i32, i32* %11
%30 = add nsw i32 %29, 1
store i32 %30, i32* %11
%31 = getelementptr inbounds [254 x i8], [254 x i8]* %6, i64 0, i64 0
%32 = load i32, i32* %8
%33 = getelementptr inbounds [26 x i8], [26 x i8]* %10, i64 0, i64 0
%34 = load i32, i32* %7
%35 = call double @bifidCrack(i8* %31, i32 %32, i8* %33, i32 %34)
store double %35, double* %12
%36 = load double, double* %12
%37 = load double, double* %13
%38 = fcmp ogt double %36, %37
br i1 %38, label %39, label %54
39: ; preds = %28
%40 = load double, double* %12
store double %40, double* %13
%41 = load double, double* %12
%42 = load i32, i32* %11
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), double %41, i32 %42)
%44 = getelementptr inbounds [26 x i8], [26 x i8]* %10, i64 0, i64 0
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %44)
%46 = getelementptr inbounds [26 x i8], [26 x i8]* %10, i64 0, i64 0
%47 = load i32, i32* %7
%48 = getelementptr inbounds [254 x i8], [254 x i8]* %6, i64 0, i64 0
%49 = load i8*, i8** %9
%50 = load i32, i32* %8
%51 = call i32 @bifidDecipher(i8* %46, i32 %47, i8* %48, i8* %49, i32 %50)
%52 = load i8*, i8** %9
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %39, %28
br label %28
}

LLVM IR (-O2):
@__const.main.cipher = [254 x i8] c"KWTAZQLAWWZCPONIVBTTBVQUZUGRNHAYIYGIAAYURCUQLDFTYVHTNQEENUPAIFCUNQTNGITEFUSHFDWHRIFSVTBISYDHHASQSROMUEVPQHHCCRBYTQBHWYRRHTEPEKHOBFSZUQBTSYRSQUDCSAOVUUGXOAUYWHPGAYHDNKEZPFKKWRIEHDWPEIOTBKESYETPBPOGTHQSPUMDOVUEQAUPCPFCQHRPHSOPQRSSLPEVWNIQDIOTSQESDHURIEREN\00"
@__const.main.key = [26 x i8] c"ABCDEFGHIKLMNOPQRSTUVWXYZ\00"
@.str.1 = [40 x i8] c"best score so far: %f, on iteration %d\0A\00"
@.str.2 = [15 x i8] c" Key: '%s'\0A\00"
@.str.3 = [21 x i8] c" plaintext: '%s'\0A\00"
@str = [53 x i8] c"Running bifidcrack, this could take a few minutes...\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [254 x i8]
%4 = alloca [26 x i8]
%5 = getelementptr inbounds [254 x i8], [254 x i8]* %3, i64 0, i64 0
%6 = call i32 @strlen(i8* %5)
%7 = add nsw i32 %6, 1
%8 = call i8* @malloc(i32 %7)
%9 = call i64 @time(i32* null)
%10 = trunc i64 %9 to i32
%11 = call i32 @srand(i32 %10)
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str, i64 0, i64 0))
%13 = getelementptr inbounds [26 x i8], [26 x i8]* %4, i64 0, i64 0
br label %14
14: ; preds = %22, %2
%15 = phi i32 [ %19, %22 ], [ 0, %2 ]
%16 = phi double [ %20, %22 ], [ -9.900000e+100, %2 ]
br label %17
17: ; preds = %14, %17
%18 = phi i32 [ %19, %17 ], [ %15, %14 ]
%19 = add nuw nsw i32 %18, 1
%20 = call double @bifidCrack(i8* %5, i32 %6, i8* %13, i32 7)
%21 = fcmp ogt double %20, %16
br i1 %21, label %22, label %17
22: ; preds = %17
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), double %20, i32 %19)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%25 = call i32 @bifidDecipher(i8* %13, i32 7, i8* %5, i8* %8, i32 %6)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %8)
br label %14
}