#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 foursquareCrack(char *, int, char *, char *);
int /*<<< orphan*/ foursquareDecipher(char *, char *, 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[] = "HMMKEQESDTMDHLAWFWMNKSOSFOMRFNLWLKHNSQGGEKXEOLLVDXNRSQQGARTF"
                  "KSAVNUDLFNDHESPZGQTWESAGPGSQSQSTPKUSBBQLQHESAGPGSQSQGXLNAVHT"
                  "PMHMKKNYGSUGDMTPDGFNKYAVHXLWGEKRILESLZZOFNAVIHRHRKAGHSMYUGEG"
                  "NSRGAVMVOQPRLNKRXLMYLQPXILESQYBNRHRKAGKYQXDIHMPGPYOERZOLBEZL"
                  "URFWLWUOLDDPNSQYAGMUQPQWESBEZVEQESDTMDBQLWDIUSHB";
  int len = strlen(cipher);
  char *out = malloc(sizeof(char) * (len + 1));
  srand((unsigned)time(NULL));
  printf("Running foursquarecrack, this could take a few minutes...\n");
  char key1[] = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
  char key2[] = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
  int i = 0;
  double score, maxscore = -99e99;
  while (1) {
    i++;
    score = foursquareCrack(cipher, len, key1, key2);
    if (score > maxscore) {
      maxscore = score;
      printf("best score so far: %f, on iteration %d\n", score, i);
      printf("    Key: '%s','%s'\n", key1, key2);
      foursquareDecipher(key1, key2, cipher, out, len);
      printf("    plaintext: '%s'\n", out);
    }
  }
  free(out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x140(%rbp),%rdi
lea    0xe69(%rip),%rsi
mov    $0x121,%edx
call   1080 <memcpy@plt>
lea    -0x140(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x144(%rbp)
mov    -0x144(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x150(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
lea    0xf63(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0xf3b(%rip),%rax
mov    %rax,-0x170(%rbp)
mov    0xf35(%rip),%rax
mov    %rax,-0x168(%rbp)
mov    0xf2f(%rip),%rax
mov    %rax,-0x160(%rbp)
mov    0xf29(%rip),%ax
mov    %ax,-0x158(%rbp)
mov    0xf03(%rip),%rax
mov    %rax,-0x190(%rbp)
mov    0xefd(%rip),%rax
mov    %rax,-0x188(%rbp)
mov    0xef7(%rip),%rax
mov    %rax,-0x180(%rbp)
mov    0xef1(%rip),%ax
mov    %ax,-0x178(%rbp)
movl   $0x0,-0x194(%rbp)
movsd  0xd80(%rip),%xmm0
movsd  %xmm0,-0x1a8(%rbp)
ff
mov    -0x194(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x194(%rbp)
lea    -0x140(%rbp),%rdi
mov    -0x144(%rbp),%esi
lea    -0x170(%rbp),%rdx
lea    -0x190(%rbp),%rcx
call   1030 <foursquareCrack@plt>
movsd  %xmm0,-0x1a0(%rbp)
ff
movsd  -0x1a0(%rbp),%xmm0
ff
ucomisd -0x1a8(%rbp),%xmm0
ff
jbe    135a <main+0x1da>
movsd  -0x1a0(%rbp),%xmm0
ff
movsd  %xmm0,-0x1a8(%rbp)
ff
movsd  -0x1a0(%rbp),%xmm0
ff
mov    -0x194(%rbp),%esi
lea    0xe9b(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
lea    -0x170(%rbp),%rsi
lea    -0x190(%rbp),%rdx
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x170(%rbp),%rdi
lea    -0x190(%rbp),%rsi
lea    -0x140(%rbp),%rdx
mov    -0x150(%rbp),%rcx
mov    -0x144(%rbp),%r8d
call   1040 <foursquareDecipher@plt>
mov    -0x150(%rbp),%rsi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1288 <main+0x108>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x178,%rsp
lea    0xe68(%rip),%rsi
lea    0x50(%rsp),%r14
mov    $0x121,%edx
mov    %r14,%rdi
call   1090 <memcpy@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%edi
call   10b0 <malloc@plt>
mov    %rax,%r15
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1080 <srand@plt>
lea    0xfc7(%rip),%rdi
call   1050 <puts@plt>
movupd 0xf59(%rip),%xmm0
movupd %xmm0,0x3a(%rsp)
movaps 0xf42(%rip),%xmm1
movaps %xmm1,0x30(%rsp)
movupd %xmm0,0x1a(%rsp)
movaps %xmm1,0x10(%rsp)
movsd  0xdea(%rip),%xmm1
lea    0x30(%rsp),%r12
lea    0x10(%rsp),%r13
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
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    %r13,%rcx
call   1030 <foursquareCrack@plt>
movsd  0x8(%rsp),%xmm1
ucomisd %xmm1,%xmm0
jbe    1240 <main+0xb0>
lea    0xef4(%rip),%rdi
movsd  %xmm0,(%rsp)
mov    %ebp,%esi
mov    $0x1,%al
call   1070 <printf@plt>
lea    0xf07(%rip),%rdi
mov    %r12,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %ebx,%r8d
call   1040 <foursquareDecipher@plt>
lea    0xef3(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
movsd  (%rsp),%xmm1
jmp    1230 <main+0xa0>

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

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