// File: /workspace/github_cpp_makefile_star2_O3/cgvwzq[P]aes_bf/src/aes-brute-force.cpp
// Function: aes_brute_force::search(voffset, unsigned char*, unsigned char*, unsigned char*, unsigned char*, unsigned char*, unsigned long&, bool&)
static
	void search(voffset_t v, uint8_t cipher[16], uint8_t key[16], uint8_t iv[16], uint8_t plain_mask[16], uint8_t plain[16], uint64_t &aes_cnt, bool &found)
	{
		uint8_t r[16];
		uint64_t n_loops = 1 << v.n_offsets; // iterate over all offsets combinations 
		found = false;
		__m128i key_schedule[20] = {0};
		aes128_load_key (key, key_schedule);
		aes_cnt = 0;

		for (uint64_t cnt=1; cnt < n_loops; cnt++)
		{
			// create index of offsets. more elegant with hamming weight and binary operations...
			uint8_t index[16] = {0}, ones = 0;
			for (uint8_t i=0; i < v.n_offsets; i++)
			{
				if (cnt & (1 << i))
				{
					index[ones++] = i;
				}
			}
			uint64_t n_chars = std::pow(v.n_valid_chars, ones);
			for (uint64_t c=0; c < n_chars; c++)
			{
				uint64_t t = c;
				for (uint8_t o=0; o < ones; o++)
				{
					cipher[v.offsets[index[o]]] = v.valid_chars[index[o]][t % v.n_valid_chars];
					t = t / v.n_valid_chars;
					
				}
				aes128_dec (key_schedule, cipher, r);
				#ifndef PADDING
					if (is_valid_plaintext (r, plain_mask, plain, iv))
				#else
					if (is_valid_plaintext_padding (r, plain_mask, plain, iv))
				#endif
				{
					found = true;
					done = true;
					return;
				}
				aes_cnt++;
				if (debug && !((aes_cnt+1) % 300000000))
				{
					println_128 ("\t(debug): ", cipher);
				}
			}
		}
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq 0x12b8(%rsp), %rax
movq %rdi, %r15
vxorps %xmm0, %xmm0, %xmm0
leaq 0x110(%rsp), %rdi
movzbl 0x2a0(%rsp), %ebp
movq %r9, %rbx
movq %rdx, %r14
movb $0x0, (%rax)
vmovups %zmm0, 0x100(%rdi)
vmovups %zmm0, 0xc0(%rdi)
vmovups %zmm0, 0x80(%rdi)
vmovups %zmm0, 0x40(%rdi)
vmovups %zmm0, (%rdi)
vmovupd (%rsi), %xmm0
vzeroupper
callq 0x354f
movq $0x0, (%rbx)
testl %ebp, %ebp
je 0x40e0
leaq 0x290(%rsp), %r13
vmovdqa 0x110(%rsp), %xmm1
vmovdqa 0x1b0(%rsp), %xmm2
vmovdqa 0x1c0(%rsp), %xmm3
vmovdqa 0x1d0(%rsp), %xmm4
vmovdqa 0x1e0(%rsp), %xmm5
vmovdqa 0x1f0(%rsp), %xmm6
vmovdqa 0x200(%rsp), %xmm7
vmovdqa 0x210(%rsp), %xmm8
vmovdqa 0x220(%rsp), %xmm9
vmovdqa 0x230(%rsp), %xmm10
vmovdqa 0x240(%rsp), %xmm11
movl $0x1, %edx
movl $0x2, %ecx
movq %rbx, %r8
movl $0x1, %esi
movq %r8, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movl %ebp, 0x4(%rsp)
movzbl 0x1011(%r13), %r12d
shlxl %ebp, %edx, %eax
leaq 0x11(%r13), %rbx
cmpl $0x3, %eax
cmovael %eax, %ecx
movslq %ecx, %rax
movq %rax, 0x18(%rsp)
vcvtsi2sd %r12d, %xmm12, %xmm0
vmovdqa %xmm1, 0xe0(%rsp)
vmovdqa %xmm2, 0xd0(%rsp)
vmovdqa %xmm3, 0xc0(%rsp)
vmovdqa %xmm4, 0xb0(%rsp)
vmovdqa %xmm5, 0xa0(%rsp)
vmovdqa %xmm6, 0x90(%rsp)
vmovdqa %xmm7, 0x80(%rsp)
vmovdqa %xmm8, 0x70(%rsp)
vmovdqa %xmm9, 0x60(%rsp)
vmovdqa %xmm10, 0x50(%rsp)
vmovdqa %xmm11, 0x40(%rsp)
vmovsd %xmm0, 0x20(%rsp)
vxorpd %xmm0, %xmm0, %xmm0
xorl %eax, %eax
xorl %edi, %edi
vmovapd %xmm0, 0xf0(%rsp)
shlxl %eax, %edx, %ecx
movslq %ecx, %rcx
testq %rcx, %rsi
je 0x3e76
movzbl %dil, %ecx
incb %dil
movb %al, 0xf0(%rsp,%rcx)
incl %eax
cmpl %eax, %ebp
jne 0x3e5b
vmovsd 0x20(%rsp), %xmm0
movzbl %dil, %ebp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x28(%rsp)
movl %ebp, %edi
callq 0x21e0
vmovdqa 0x40(%rsp), %xmm11
vmovdqa 0x50(%rsp), %xmm10
vmovdqa 0x60(%rsp), %xmm9
vmovdqa 0x70(%rsp), %xmm8
vmovdqa 0x80(%rsp), %xmm7
vmovdqa 0x90(%rsp), %xmm6
vmovdqa 0xa0(%rsp), %xmm5
vmovdqa 0xb0(%rsp), %xmm4
vmovdqa 0xc0(%rsp), %xmm3
vmovdqa 0xd0(%rsp), %xmm2
vmovdqa 0xe0(%rsp), %xmm1
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %r9
vcvttsd2usi %xmm0, %rax
movabsq $0x2f9a04dcc1f0b416, %r11 # imm = 0x2F9A04DCC1F0B416
movabsq $-0x6832fd919f07a5f5, %r10 # imm = 0x97CD026E60F85A0B
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x40b0
xorl %r14d, %r14d
cmpb $0x0, 0x38(%rsp)
je 0x3f57
xorl %ecx, %ecx
movq %r14, %rax
movzbl 0xf0(%rsp,%rcx), %esi
xorl %edx, %edx
divq %r12
incq %rcx
movzbl (%r13,%rsi), %edi
shll $0x8, %esi
addq %rbx, %rsi
movb (%rdx,%rsi), %dl
movb %dl, (%r15,%rdi)
cmpq %rcx, %rbp
jne 0x3f2f
vpxor (%r15), %xmm2, %xmm0
movl $0x1, %eax
movabsq $0x2800000400000000, %rsi # imm = 0x2800000400000000
vaesdec %xmm3, %xmm0, %xmm0
vaesdec %xmm4, %xmm0, %xmm0
vaesdec %xmm5, %xmm0, %xmm0
vaesdec %xmm6, %xmm0, %xmm0
vaesdec %xmm7, %xmm0, %xmm0
vaesdec %xmm8, %xmm0, %xmm0
vaesdec %xmm9, %xmm0, %xmm0
vaesdec %xmm10, %xmm0, %xmm0
vaesdec %xmm11, %xmm0, %xmm0
vaesdeclast %xmm1, %xmm0, %xmm0
vmovdqa %xmm0, 0x100(%rsp)
vmovd %xmm0, %ecx
xorb (%r9), %cl
leal -0x7f(%rcx), %edx
cmpb $-0x5f, %dl
jb 0x3fe0
cmpb $0x3d, %cl
ja 0x3fc3
movzbl %cl, %ecx
btq %rcx, %rsi
jb 0x3fe0
movb (%r9,%rax), %cl
xorb 0x100(%rsp,%rax), %cl
incq %rax
cmpq $0x10, %rax
jne 0x3fad
cmpb $0x1, %cl
je 0x40ce
movq (%r8), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%r8)
cmpb $0x1, 0x312f(%rip)      # 0x7120
jne 0x40a2
imulq %r10, %rax
movabsq $0xe5109ec20, %rcx      # imm = 0xE5109EC20
addq %r11, %rax
rorxq $0x8, %rax, %rax
cmpq %rcx, %rax
ja 0x40a2
leaq 0x1435(%rip), %rdi      # 0x5453
leaq 0x1409(%rip), %rdx      # 0x542e
movq %r15, %rsi
callq 0x37ac
vmovdqa 0x40(%rsp), %xmm11
vmovdqa 0x50(%rsp), %xmm10
vmovdqa 0x60(%rsp), %xmm9
vmovdqa 0x70(%rsp), %xmm8
vmovdqa 0x80(%rsp), %xmm7
vmovdqa 0x90(%rsp), %xmm6
vmovdqa 0xa0(%rsp), %xmm5
vmovdqa 0xb0(%rsp), %xmm4
vmovdqa 0xc0(%rsp), %xmm3
vmovdqa 0xd0(%rsp), %xmm2
vmovdqa 0xe0(%rsp), %xmm1
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r8
movabsq $0x2f9a04dcc1f0b416, %r11 # imm = 0x2F9A04DCC1F0B416
movabsq $-0x6832fd919f07a5f5, %r10 # imm = 0x97CD026E60F85A0B
incq %r14
cmpq 0x30(%rsp), %r14
jne 0x3f23
movq 0x28(%rsp), %rsi
movl 0x4(%rsp), %ebp
movl $0x1, %edx
incq %rsi
cmpq 0x18(%rsp), %rsi
jne 0x3e4a
jmp 0x40e0
movq 0x12b8(%rsp), %rcx
movb $0x1, %al
movb %al, (%rcx)
movb %al, 0x3052(%rip)       # 0x7132
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq