#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(unsigned char *);
unsigned char *malloc(size_t);
float xor_english_str_score(unsigned char *, size_t);
int /*<<< orphan*/ xor_perform(unsigned char const *, size_t, size_t,
                               unsigned char *, int, unsigned char *, size_t);
int xor_key_guess_sb(const unsigned char *data, size_t len, size_t stride,
                     unsigned char *result) {
  float max_score = 0.0f;
  unsigned char *buf = malloc(len);
  if (buf == NULL) {
    return 1;
  }
  for (int key = 0; key < 256; ++key) {
    float score = 0.0f;
    xor_perform(data, len, stride, (unsigned char *)&key, 1, buf, len);
    score = xor_english_str_score(buf, len);
    if (score > max_score) {
      max_score = score;
      *result = key;
    }
  }
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<xor_key_guess_sb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1184 <xor_key_guess_sb+0x44>
movl   $0x1,-0x4(%rbp)
jmp    1220 <xor_key_guess_sb+0xe0>
movl   $0x0,-0x3c(%rbp)
cmpl   $0x100,-0x3c(%rbp)
jge    1210 <xor_key_guess_sb+0xd0>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x3c(%rbp),%rcx
mov    -0x38(%rbp),%r9
mov    -0x18(%rbp),%rax
mov    $0x1,%r8d
mov    %rax,(%rsp)
call   1060 <xor_perform@plt>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <xor_english_str_score@plt>
movss  %xmm0,-0x40(%rbp)
movss  -0x40(%rbp),%xmm0
ucomiss -0x2c(%rbp),%xmm0
jbe    11fd <xor_key_guess_sb+0xbd>
movss  -0x40(%rbp),%xmm0
movss  %xmm0,-0x2c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
jmp    1202 <xor_key_guess_sb+0xc2>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    118b <xor_key_guess_sb+0x4b>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xor_key_guess_sb>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rsi,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11e9 <xor_key_guess_sb+0xa9>
mov    %rax,%r13
movl   $0x0,0x10(%rsp)
xorps  %xmm0,%xmm0
movss  %xmm0,0x14(%rsp)
lea    0x10(%rsp),%rbp
jmp    119e <xor_key_guess_sb+0x5e>
cs nopw 0x0(%rax,%rax,1)
lea    0x1(%rax),%ecx
mov    %ecx,0x10(%rsp)
cmp    $0xff,%eax
jge    11dd <xor_key_guess_sb+0x9d>
mov    %rbx,(%rsp)
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %rbp,%rcx
mov    $0x1,%r8d
mov    %r13,%r9
call   1060 <xor_perform@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1040 <xor_english_str_score@plt>
ucomiss 0x14(%rsp),%xmm0
mov    0x10(%rsp),%eax
jbe    1190 <xor_key_guess_sb+0x50>
mov    %al,(%r14)
movss  %xmm0,0x14(%rsp)
jmp    1190 <xor_key_guess_sb+0x50>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    11ee <xor_key_guess_sb+0xae>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @xor_key_guess_sb(i8* %0, i64 %1, i64 %2, i8* nocapture writeonly %3) {
%5 = alloca i32
%6 = tail call i8* @malloc(i64 %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %24, label %8
8: ; preds = %4
%9 = bitcast i32* %5 to i8*
store i32 0, i32* %5
br label %12
10: ; preds = %20
%11 = call i32 @free(i8* %6)
br label %24
12: ; preds = %8, %20
%13 = phi float [ 0.000000e+00, %8 ], [ %21, %20 ]
%14 = call i32 @xor_perform(i8* %0, i64 %1, i64 %2, i8* %9, i32 1, i8* %6, i64 %1)
%15 = call float @xor_english_str_score(i8* %6, i64 %1)
%16 = fcmp ogt float %15, %13
%17 = load i32, i32* %5
br i1 %16, label %18, label %20
18: ; preds = %12
%19 = trunc i32 %17 to i8
store i8 %19, i8* %3
br label %20
20: ; preds = %12, %18
%21 = phi float [ %15, %18 ], [ %13, %12 ]
%22 = add nsw i32 %17, 1
store i32 %22, i32* %5
%23 = icmp slt i32 %17, 255
br i1 %23, label %12, label %10
24: ; preds = %4, %10
%25 = phi i32 [ 0, %10 ], [ 1, %4 ]
ret i32 %25
}