#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 *);
char *breakSingleXOR(uint8_t *input, int inputlen) {
  uint8_t bestKey = 0;
  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) {
      bestKey = possibleKey;
      bestKeyScore = possibleKeyScore;
    }
  }
  xorSingleValueInPlace(input, inputlen, 0xff, possibleMessage);
  double possibleKeyScore = score((char *)possibleMessage, inputlen);
  if (possibleKeyScore > bestKeyScore) {
    bestKey = 0xff;
    bestKeyScore = possibleKeyScore;
  }
  xorSingleValueInPlace(input, inputlen, bestKey, possibleMessage);
  return (char *)possibleMessage;
}


x86 assembly (clang, -O0):
<breakSingleXOR>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%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    11c5 <breakSingleXOR+0x95>
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    11b2 <breakSingleXOR+0x82>
mov    -0x24(%rbp),%eax
mov    %eax,-0x10(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    11b7 <breakSingleXOR+0x87>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1161 <breakSingleXOR+0x31>
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    120c <breakSingleXOR+0xdc>
movl   $0xff,-0x10(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1050 <xorSingleValueInPlace@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<breakSingleXOR>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
mov    %esi,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
xorpd  %xmm0,%xmm0
xor    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  %xmm0,0x8(%rsp)
mov    %r14,%rdi
mov    %r15d,%esi
mov    %ebx,%edx
mov    %r12,%rcx
call   1050 <xorSingleValueInPlace@plt>
mov    %r12,%rdi
mov    %r15d,%esi
call   1030 <score@plt>
movsd  0x8(%rsp),%xmm1
ucomisd %xmm1,%xmm0
cmova  %ebx,%ebp
maxsd  %xmm1,%xmm0
add    $0x1,%ebx
cmp    $0xff,%ebx
jne    1160 <breakSingleXOR+0x30>
mov    $0xff,%ebx
mov    %r14,%rdi
mov    %r15d,%esi
mov    $0xff,%edx
mov    %r12,%rcx
movsd  %xmm0,0x8(%rsp)
call   1050 <xorSingleValueInPlace@plt>
mov    %r12,%rdi
mov    %r15d,%esi
call   1030 <score@plt>
ucomisd 0x8(%rsp),%xmm0
cmova  %ebx,%ebp
mov    %r14,%rdi
mov    %r15d,%esi
mov    %ebp,%edx
mov    %r12,%rcx
call   1050 <xorSingleValueInPlace@plt>
mov    %r12,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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