#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 counthex(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ printhex(unsigned char *, int);
int scorearray(char *);
int /*<<< orphan*/ str2hex(char *, unsigned char *);
int /*<<< orphan*/ xorarray(unsigned char *, int, int, char *);
void challenge3() {
  printf("\nchallenge 3:\n");
  char in[] = "1b37373331363f78151b7f2b783431333d"
              "78397828372d363c78373e783a393b3736";
  int size = counthex(in);
  unsigned char hex[size];
  str2hex(in, hex);
  printhex(hex, size);
  int topscore = 0;
  char top[size];
  int c;
  for (c = 0; c <= 255; c++) {
    char current[size + 1];
    xorarray(hex, size, c, current);
    int score = scorearray(current);
    if (score > topscore) {
      topscore = score;
      int i;
      for (i = 0; i < size; i++)
        top[i] = current[i];
      top[i] = '\0';
    }
  }
  printf("topscore = '%d'\n", topscore);
  printf("result = '%s'\n", top);
}


x86 assembly (clang, -O0):
<challenge3>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
lea    0xe8e(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movabs $0x36333733623339,%rax
mov    %rax,-0x13(%rbp)
movaps 0xed2(%rip),%xmm0
movaps %xmm0,-0x20(%rbp)
movaps 0xeb7(%rip),%xmm0
movaps %xmm0,-0x30(%rbp)
movaps 0xe9c(%rip),%xmm0
movaps %xmm0,-0x40(%rbp)
movaps 0xe81(%rip),%xmm0
movaps %xmm0,-0x50(%rbp)
lea    -0x50(%rbp),%rdi
mov    %rdi,-0xb0(%rbp)
call   1050 <counthex@plt>
mov    -0xb0(%rbp),%rdi
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0xa8(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x68(%rbp)
call   1060 <str2hex@plt>
mov    -0xa8(%rbp),%rdi
mov    -0x54(%rbp),%esi
call   1070 <printhex@plt>
movl   $0x0,-0x6c(%rbp)
mov    -0x54(%rbp),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xa0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x78(%rbp)
movl   $0x0,-0x7c(%rbp)
cmpl   $0xff,-0x7c(%rbp)
jg     132e <challenge3+0x1ce>
mov    -0xa8(%rbp),%rdi
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x88(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xb8(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x90(%rbp)
mov    -0x54(%rbp),%esi
mov    -0x7c(%rbp),%edx
call   1080 <xorarray@plt>
mov    -0xb8(%rbp),%rdi
call   1040 <scorearray@plt>
mov    %eax,-0x94(%rbp)
mov    -0x94(%rbp),%eax
cmp    -0x6c(%rbp),%eax
jle    1316 <challenge3+0x1b6>
mov    -0x94(%rbp),%eax
mov    %eax,-0x6c(%rbp)
movl   $0x0,-0x98(%rbp)
mov    -0x98(%rbp),%eax
cmp    -0x54(%rbp),%eax
jge    1304 <challenge3+0x1a4>
mov    -0xa0(%rbp),%rax
mov    -0xb8(%rbp),%rcx
movslq -0x98(%rbp),%rdx
mov    (%rcx,%rdx,1),%dl
movslq -0x98(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x98(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x98(%rbp)
jmp    12bf <challenge3+0x15f>
mov    -0xa0(%rbp),%rax
movslq -0x98(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x88(%rbp),%rax
mov    %rax,%rsp
mov    -0x7c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7c(%rbp)
jmp    123a <challenge3+0xda>
mov    -0x6c(%rbp),%esi
lea    0xcd7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xa0(%rbp),%rsi
lea    0xcd3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x60(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<challenge3>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
lea    0xecd(%rip),%rdi
call   1030 <puts@plt>
movaps 0xe8c(%rip),%xmm0
movaps %xmm0,-0x70(%rbp)
movaps 0xe71(%rip),%xmm0
movaps %xmm0,-0x80(%rbp)
movaps 0xe56(%rip),%xmm0
movaps %xmm0,-0x90(%rbp)
movaps 0xe38(%rip),%xmm0
movaps %xmm0,-0xa0(%rbp)
movabs $0x36333733623339,%rax
mov    %rax,-0x63(%rbp)
lea    -0xa0(%rbp),%r14
mov    %r14,%rdi
call   1060 <counthex@plt>
mov    %eax,%r12d
mov    %rsp,-0x40(%rbp)
mov    %eax,%eax
mov    %rsp,%rbx
mov    %rax,-0x38(%rbp)
lea    0xf(%rax),%r15
and    $0xfffffffffffffff0,%r15
sub    %r15,%rbx
mov    %rbx,%rsp
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <str2hex@plt>
mov    %rbx,-0x58(%rbp)
mov    %rbx,%rdi
mov    %r12d,%esi
call   1090 <printhex@plt>
mov    %rsp,%rax
sub    %r15,%rax
mov    %rax,-0x30(%rbp)
mov    %rax,%rsp
lea    0x1(%r12),%eax
add    $0xffffffff,%r12d
add    $0x1,%r12
mov    %r12,-0x48(%rbp)
xor    %r14d,%r14d
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rax,-0x50(%rbp)
xor    %ebx,%ebx
jmp    128b <challenge3+0x10b>
cs nopw 0x0(%rax,%rax,1)
nop
mov    -0x30(%rbp),%r14
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <memcpy@plt>
mov    -0x48(%rbp),%rax
mov    %eax,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r12d,%r14d
mov    %r13,%rsp
add    $0x1,%ebx
cmp    $0x100,%ebx
je     12ca <challenge3+0x14a>
mov    %rsp,%r13
mov    %rsp,%r15
sub    -0x50(%rbp),%r15
mov    %r15,%rsp
mov    -0x58(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %ebx,%edx
mov    %r15,%rcx
call   10a0 <xorarray@plt>
mov    %r15,%rdi
call   1050 <scorearray@plt>
cmp    %r14d,%eax
jle    127d <challenge3+0xfd>
mov    %eax,%r12d
mov    -0x38(%rbp),%rdx
test   %edx,%edx
jg     1260 <challenge3+0xe0>
xor    %eax,%eax
mov    -0x30(%rbp),%r14
jmp    1273 <challenge3+0xf3>
lea    0xd74(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xd74(%rip),%rdi
mov    -0x30(%rbp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x40(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"\0Achallenge 3:\0A\00"
@__const.challenge3.in = [69 x i8] c"1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736\00"
@.str.1 = [17 x i8] c"topscore = '%d'\0A\00"
@.str.2 = [15 x i8] c"result = '%s'\0A\00"
define void @challenge3() {
%1 = alloca [69 x i8]
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%13 = bitcast [69 x i8]* %1 to i8*
%14 = getelementptr inbounds [69 x i8], [69 x i8]* %1, i64 0, i64 0
%15 = call i32 @counthex(i8* %14)
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %3
%19 = alloca i8, i64 %17
store i64 %17, i64* %4
%20 = getelementptr inbounds [69 x i8], [69 x i8]* %1, i64 0, i64 0
%21 = call i32 @str2hex(i8* %20, i8* %19)
%22 = load i32, i32* %2
%23 = call i32 @printhex(i8* %19, i32 %22)
store i32 0, i32* %5
%24 = load i32, i32* %2
%25 = zext i32 %24 to i64
%26 = alloca i8, i64 %25
store i64 %25, i64* %6
store i32 0, i32* %7
br label %27
27: ; preds = %66, %0
%28 = load i32, i32* %7
%29 = icmp sle i32 %28, 255
br i1 %29, label %30, label %69
30: ; preds = %27
%31 = load i32, i32* %2
%32 = add nsw i32 %31, 1
%33 = zext i32 %32 to i64
%34 = call i8* @llvm.stacksave()
store i8* %34, i8** %8
%35 = alloca i8, i64 %33
store i64 %33, i64* %9
%36 = load i32, i32* %2
%37 = load i32, i32* %7
%38 = call i32 @xorarray(i8* %19, i32 %36, i32 %37, i8* %35)
%39 = call i32 @scorearray(i8* %35)
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = load i32, i32* %5
%42 = icmp sgt i32 %40, %41
br i1 %42, label %43, label %64
43: ; preds = %30
%44 = load i32, i32* %10
store i32 %44, i32* %5
store i32 0, i32* %11
br label %45
45: ; preds = %57, %43
%46 = load i32, i32* %11
%47 = load i32, i32* %2
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %60
49: ; preds = %45
%50 = load i32, i32* %11
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %35, i64 %51
%53 = load i8, i8* %52
%54 = load i32, i32* %11
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %26, i64 %55
store i8 %53, i8* %56
br label %57
57: ; preds = %49
%58 = load i32, i32* %11
%59 = add nsw i32 %58, 1
store i32 %59, i32* %11
br label %45
60: ; preds = %45
%61 = load i32, i32* %11
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %26, i64 %62
store i8 0, i8* %63
br label %64
64: ; preds = %60, %30
%65 = load i8*, i8** %8
br label %66
66: ; preds = %64
%67 = load i32, i32* %7
%68 = add nsw i32 %67, 1
store i32 %68, i32* %7
br label %27
69: ; preds = %27
%70 = load i32, i32* %5
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %70)
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%73 = load i8*, i8** %3
ret void
}

LLVM IR (-O2):
@__const.challenge3.in = [69 x i8] c"1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736\00"
@.str.1 = [17 x i8] c"topscore = '%d'\0A\00"
@.str.2 = [15 x i8] c"result = '%s'\0A\00"
@str = [14 x i8] c"\0Achallenge 3:\00"
define void @challenge3() {
%1 = alloca [69 x i8]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%3 = getelementptr inbounds [69 x i8], [69 x i8]* %1, i64 0, i64 0
%4 = call i32 @counthex(i8* %3)
%5 = zext i32 %4 to i64
%6 = call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = call i32 @str2hex(i8* %3, i8* %7)
%9 = call i32 @printhex(i8* %7, i32 %4)
%10 = alloca i8, i64 %5
%11 = add nsw i32 %4, 1
%12 = zext i32 %11 to i64
%13 = icmp sgt i32 %4, 0
%14 = add i32 %4, -1
%15 = zext i32 %14 to i64
%16 = add nuw nsw i64 %15, 1
br label %17
17: ; preds = %0, %31
%18 = phi i32 [ 0, %0 ], [ %32, %31 ]
%19 = phi i32 [ 0, %0 ], [ %33, %31 ]
%20 = call i8* @llvm.stacksave()
%21 = alloca i8, i64 %12
%22 = call i32 @xorarray(i8* %7, i32 %4, i32 %19, i8* %21)
%23 = call i32 @scorearray(i8* %21)
%24 = icmp sgt i32 %23, %18
br i1 %24, label %25, label %31
25: ; preds = %17
br i1 %13, label %26, label %27
26: ; preds = %25
br label %27
27: ; preds = %26, %25
%28 = phi i64 [ 0, %25 ], [ %16, %26 ]
%29 = and i64 %28, 4294967295
%30 = getelementptr inbounds i8, i8* %10, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %27, %17
%32 = phi i32 [ %23, %27 ], [ %18, %17 ]
%33 = add nuw nsw i32 %19, 1
%34 = icmp eq i32 %33, 256
br i1 %34, label %35, label %17
35: ; preds = %31
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %10)
ret void
}