#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 */
typedef int uint8_t;

/* Variables and functions */
int *malloc(int);
double score(char *, int);
int /*<<< orphan*/ xorSingleValueInPlace(int *, int, int, int *);
double scoreSingleXOR(uint8_t *input, int inputlen) {
  double bestKeyScore = 0;
  uint8_t *possibleMessage = malloc(inputlen);
  for (uint8_t possibleKey = 0; possibleKey < 0xff; possibleKey++) {
    xorSingleValueInPlace(input, inputlen, possibleKey, possibleMessage);
    double possibleKeyScore = score((char *)possibleMessage, inputlen);
    if (possibleKeyScore > bestKeyScore) {
      bestKeyScore = possibleKeyScore;
    }
  }
  xorSingleValueInPlace(input, inputlen, 0xff, possibleMessage);
  double possibleKeyScore = score((char *)possibleMessage, inputlen);
  if (possibleKeyScore > bestKeyScore) {
    bestKeyScore = possibleKeyScore;
  }
  return bestKeyScore;
}


x86 assembly (clang, -O0):
<scoreSingleXOR>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0xff,-0x24(%rbp)
jge    11b8 <scoreSingleXOR+0x88>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1050 <xorSingleValueInPlace@plt>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <score@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    11a5 <scoreSingleXOR+0x75>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    11aa <scoreSingleXOR+0x7a>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    115a <scoreSingleXOR+0x2a>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0xff,%edx
call   1050 <xorSingleValueInPlace@plt>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <score@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    11f8 <scoreSingleXOR+0xc8>
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scoreSingleXOR>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
mov    %esi,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
xor    %ebp,%ebp
xorpd  %xmm0,%xmm0
nopl   (%rax)
movsd  %xmm0,(%rsp)
mov    %r14,%rdi
mov    %r15d,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1050 <xorSingleValueInPlace@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <score@plt>
maxsd  (%rsp),%xmm0
add    $0x1,%ebp
cmp    $0xff,%ebp
jne    1150 <scoreSingleXOR+0x20>
mov    %r14,%rdi
mov    %r15d,%esi
mov    $0xff,%edx
mov    %rbx,%rcx
movsd  %xmm0,(%rsp)
call   1050 <xorSingleValueInPlace@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <score@plt>
maxsd  (%rsp),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @scoreSingleXOR(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca double
%6 = alloca i32*
%7 = alloca i32
%8 = alloca double
%9 = alloca double
store i32* %0, i32** %3
store i32 %1, i32* %4
store double 0.000000e+00, double* %5
%10 = load i32, i32* %4
%11 = call i32* @malloc(i32 %10)
store i32* %11, i32** %6
store i32 0, i32* %7
br label %12
12: ; preds = %31, %2
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 255
br i1 %14, label %15, label %34
15: ; preds = %12
%16 = load i32*, i32** %3
%17 = load i32, i32* %4
%18 = load i32, i32* %7
%19 = load i32*, i32** %6
%20 = call i32 @xorSingleValueInPlace(i32* %16, i32 %17, i32 %18, i32* %19)
%21 = load i32*, i32** %6
%22 = bitcast i32* %21 to i8*
%23 = load i32, i32* %4
%24 = call double @score(i8* %22, i32 %23)
store double %24, double* %8
%25 = load double, double* %8
%26 = load double, double* %5
%27 = fcmp ogt double %25, %26
br i1 %27, label %28, label %30
28: ; preds = %15
%29 = load double, double* %8
store double %29, double* %5
br label %30
30: ; preds = %28, %15
br label %31
31: ; preds = %30
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %12
34: ; preds = %12
%35 = load i32*, i32** %3
%36 = load i32, i32* %4
%37 = load i32*, i32** %6
%38 = call i32 @xorSingleValueInPlace(i32* %35, i32 %36, i32 255, i32* %37)
%39 = load i32*, i32** %6
%40 = bitcast i32* %39 to i8*
%41 = load i32, i32* %4
%42 = call double @score(i8* %40, i32 %41)
store double %42, double* %9
%43 = load double, double* %9
%44 = load double, double* %5
%45 = fcmp ogt double %43, %44
br i1 %45, label %46, label %48
46: ; preds = %34
%47 = load double, double* %9
store double %47, double* %5
br label %48
48: ; preds = %46, %34
%49 = load double, double* %5
ret double %49
}

LLVM IR (-O2):
define double @scoreSingleXOR(i32* %0, i32 %1) {
%3 = tail call i32* @malloc(i32 %1)
%4 = bitcast i32* %3 to i8*
br label %10
5: ; preds = %10
%6 = tail call i32 @xorSingleValueInPlace(i32* %0, i32 %1, i32 255, i32* %3)
%7 = tail call double @score(i8* %4, i32 %1)
%8 = fcmp ogt double %7, %16
%9 = select i1 %8, double %7, double %16
ret double %9
10: ; preds = %2, %10
%11 = phi double [ 0.000000e+00, %2 ], [ %16, %10 ]
%12 = phi i32 [ 0, %2 ], [ %17, %10 ]
%13 = tail call i32 @xorSingleValueInPlace(i32* %0, i32 %1, i32 %12, i32* %3)
%14 = tail call double @score(i8* %4, i32 %1)
%15 = fcmp ogt double %14, %11
%16 = select i1 %15, double %14, double %11
%17 = add nuw nsw i32 %12, 1
%18 = icmp eq i32 %17, 255
br i1 %18, label %5, label %10
}