#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int numOfCharInString(char *, char);
int /*<<< orphan*/ printAs(void *, char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ setHexString(void *, char const *);
int strlen(char const *);
int /*<<< orphan*/ xorStringWithChar(char *, void *, int);
void decryptedSingleByteXOR(char *dest, const char *src) {
  int i = 0;
  int num = 0;
  int numMax = 0;
  int iWithBestMatch = 0;
  char val = '\0';
  char space = 0x20;
  int len = strlen(src);
  void *hexString = (char *)calloc(len + 1, sizeof(char));
  setHexString(hexString, src);
  printAs(hexString, "hex");
  for (i = 0; i < 255; i++) {
    val = i;
    xorStringWithChar(dest, hexString, val);
    num = numOfCharInString(dest, space);
    if (num > numMax) {
      numMax = num;
      iWithBestMatch = i;
    }
    memset(dest, 0, len + 1);
  }
  printf("iWithBestMatch=%d\n", iWithBestMatch);
  xorStringWithChar(dest, hexString, iWithBestMatch);
  free((void *)hexString);
}


x86 assembly (clang, -O0):
<decryptedSingleByteXOR>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
movb   $0x20,-0x22(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <setHexString@plt>
mov    -0x30(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   1090 <printAs@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xff,-0x14(%rbp)
jge    1273 <decryptedSingleByteXOR+0xe3>
mov    -0x14(%rbp),%eax
mov    %al,-0x21(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
movsbl -0x21(%rbp),%edx
call   10a0 <xorStringWithChar@plt>
mov    -0x8(%rbp),%rdi
movsbl -0x22(%rbp),%esi
call   10b0 <numOfCharInString@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    1254 <decryptedSingleByteXOR+0xc4>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1208 <decryptedSingleByteXOR+0x78>
mov    -0x20(%rbp),%esi
lea    0xd87(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   10a0 <xorStringWithChar@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decryptedSingleByteXOR>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %rdi,%r12
mov    %rsi,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,0x4(%rsp)
mov    %eax,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1080 <setHexString@plt>
lea    0xe2f(%rip),%rsi
mov    %rbx,%r15
mov    %rbx,%rdi
call   1090 <printAs@plt>
xor    %ebx,%ebx
xor    %ebp,%ebp
xor    %r14d,%r14d
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edx
sar    $0x18,%edx
mov    %r12,%rdi
mov    %r15,%rsi
call   10a0 <xorStringWithChar@plt>
mov    %r12,%rdi
mov    $0x20,%esi
call   10b0 <numOfCharInString@plt>
cmp    %r14d,%eax
cmovg  %ebp,%r13d
cmovg  %eax,%r14d
mov    %r12,%rdi
xor    %esi,%esi
mov    0x4(%rsp),%edx
call   1060 <memset@plt>
add    $0x1,%ebp
add    $0x1000000,%ebx
cmp    $0xff,%ebp
jne    11f0 <decryptedSingleByteXOR+0x60>
lea    0xdc6(%rip),%rdi
mov    %r13d,%esi
call   1050 <printf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13d,%edx
call   10a0 <xorStringWithChar@plt>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"hex\00"
@.str.1 = [19 x i8] c"iWithBestMatch=%d\0A\00"
define void @decryptedSingleByteXOR(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i8
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i8 0, i8* %9
store i8 32, i8* %10
%13 = load i8*, i8** %4
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %11
%15 = load i32, i32* %11
%16 = add nsw i32 %15, 1
%17 = call i64 @calloc(i32 %16, i32 1)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %12
%19 = load i8*, i8** %12
%20 = load i8*, i8** %4
%21 = call i32 @setHexString(i8* %19, i8* %20)
%22 = load i8*, i8** %12
%23 = call i32 @printAs(i8* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %5
br label %24
24: ; preds = %49, %2
%25 = load i32, i32* %5
%26 = icmp slt i32 %25, 255
br i1 %26, label %27, label %52
27: ; preds = %24
%28 = load i32, i32* %5
%29 = trunc i32 %28 to i8
store i8 %29, i8* %9
%30 = load i8*, i8** %3
%31 = load i8*, i8** %12
%32 = load i8, i8* %9
%33 = sext i8 %32 to i32
%34 = call i32 @xorStringWithChar(i8* %30, i8* %31, i32 %33)
%35 = load i8*, i8** %3
%36 = load i8, i8* %10
%37 = call i32 @numOfCharInString(i8* %35, i8 signext %36)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = load i32, i32* %7
%40 = icmp sgt i32 %38, %39
br i1 %40, label %41, label %44
41: ; preds = %27
%42 = load i32, i32* %6
store i32 %42, i32* %7
%43 = load i32, i32* %5
store i32 %43, i32* %8
br label %44
44: ; preds = %41, %27
%45 = load i8*, i8** %3
%46 = load i32, i32* %11
%47 = add nsw i32 %46, 1
%48 = call i32 @memset(i8* %45, i32 0, i32 %47)
br label %49
49: ; preds = %44
%50 = load i32, i32* %5
%51 = add nsw i32 %50, 1
store i32 %51, i32* %5
br label %24
52: ; preds = %24
%53 = load i32, i32* %8
%54 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %53)
%55 = load i8*, i8** %3
%56 = load i8*, i8** %12
%57 = load i32, i32* %8
%58 = call i32 @xorStringWithChar(i8* %55, i8* %56, i32 %57)
%59 = load i8*, i8** %12
%60 = call i32 @free(i8* %59)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"hex\00"
@.str.1 = [19 x i8] c"iWithBestMatch=%d\0A\00"
define void @decryptedSingleByteXOR(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = add nsw i32 %3, 1
%5 = tail call i64 @calloc(i32 %4, i32 1)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @setHexString(i8* %6, i8* %1)
%8 = tail call i32 @printAs(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %2, %9
%10 = phi i32 [ 0, %2 ], [ %21, %9 ]
%11 = phi i32 [ 0, %2 ], [ %19, %9 ]
%12 = phi i32 [ 0, %2 ], [ %18, %9 ]
%13 = shl nuw i32 %10, 24
%14 = ashr exact i32 %13, 24
%15 = tail call i32 @xorStringWithChar(i8* %0, i8* %6, i32 %14)
%16 = tail call i32 @numOfCharInString(i8* %0, i8 signext 32)
%17 = icmp sgt i32 %16, %11
%18 = select i1 %17, i32 %10, i32 %12
%19 = select i1 %17, i32 %16, i32 %11
%20 = tail call i32 @memset(i8* %0, i32 0, i32 %4)
%21 = add nuw nsw i32 %10, 1
%22 = icmp eq i32 %21, 255
br i1 %22, label %23, label %9
23: ; preds = %9
%24 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%25 = tail call i32 @xorStringWithChar(i8* %0, i8* %6, i32 %18)
%26 = tail call i32 @free(i8* %6)
ret void
}