// File: /workspace/github_cpp_makefile_star2_O1/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 %r9, %rbx
movq %rdx, %r14
movq %rsi, %rax
movq %rdi, %r15
movq 0x12b8(%rsp), %rcx
movzbl 0x2a0(%rsp), %ebp
movb $0x0, (%rcx)
vxorpd %xmm0, %xmm0, %xmm0
leaq 0x110(%rsp), %rsi
vmovupd %zmm0, 0x100(%rsi)
vmovupd %zmm0, 0xc0(%rsi)
vmovupd %zmm0, 0x80(%rsi)
vmovupd %zmm0, 0x40(%rsi)
vmovupd %zmm0, (%rsi)
movq %rax, %rdi
vzeroupper
callq 0x35c5
movq $0x0, (%rbx)
testl %ebp, %ebp
je 0x41d6
movq %rbx, %r8
leaq 0x290(%rsp), %rcx
movl $0x1, %edx
shlxl %ebp, %edx, %eax
cltq
movq %rax, 0x8(%rsp)
movzbl 0x1011(%rcx), %ebx
vcvtsi2sd %ebx, %xmm1, %xmm0
vmovsd %xmm0, 0x28(%rsp)
leaq 0x11(%rcx), %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, %esi
movq %r8, 0x18(%rsp)
movq %r14, 0x10(%rsp)
movl %ebp, 0x4(%rsp)
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)
vxorpd %xmm0, %xmm0, %xmm0
vmovapd %xmm0, 0xf0(%rsp)
xorl %eax, %eax
xorl %r12d, %r12d
shlxl %eax, %edx, %ecx
movslq %ecx, %rcx
testq %rcx, %rsi
je 0x3f19
movzbl %r12b, %ecx
incb %r12b
movb %al, 0xf0(%rsp,%rcx)
incl %eax
cmpl %eax, %ebp
jne 0x3efe
movq %rsi, 0x20(%rsp)
movzbl %r12b, %ebp
vmovsd 0x28(%rsp), %xmm0
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
leaq 0x290(%rsp), %r11
movq 0x18(%rsp), %r8
vcvttsd2usi %xmm0, %rax
movq %rax, 0x30(%rsp)
testq %rax, %rax
setne %al
movq 0x10(%rsp), %r9
movq 0x12b8(%rsp), %r10
je 0x4190
xorl %r14d, %r14d
movb %al, 0x3(%rsp)
movq %r12, 0x38(%rsp)
testb %r12b, %r12b
je 0x3ffa
xorl %ecx, %ecx
movq %r14, %rax
movzbl 0xf0(%rsp,%rcx), %esi
xorl %edx, %edx
divq %rbx
movzbl (%r11,%rsi), %edi
shll $0x8, %esi
addq %r13, %rsi
movb (%rdx,%rsi), %dl
movb %dl, (%r15,%rdi)
incq %rcx
cmpq %rcx, %rbp
jne 0x3fd3
vpxor (%r15), %xmm2, %xmm0
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)
xorl %eax, %eax
xorl %r12d, %r12d
movabsq $0x2800000400000000, %rsi # imm = 0x2800000400000000
movb (%r9,%rax), %cl
xorb 0x100(%rsp,%rax), %cl
cmpq $0xf, %rax
jne 0x4061
cmpb $0x1, %cl
je 0x4077
jmp 0x408c
leal -0x7f(%rcx), %edx
cmpb $-0x5f, %dl
jb 0x408c
cmpb $0x3d, %cl
ja 0x4077
movzbl %cl, %ecx
btq %rcx, %rsi
jb 0x408c
cmpq $0xf, %rax
leaq 0x1(%rax), %rcx
setae %r12b
movq %rcx, %rax
cmpq $0x10, %rcx
jne 0x4049
testb $0x1, %r12b
je 0x40a2
movb $0x1, %al
movb %al, (%r10)
movb %al, 0x3095(%rip)       # 0x7132
jmp 0x4174
movq (%r8), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%r8)
cmpb $0x1, 0x306d(%rip)      # 0x7120
jne 0x4174
movabsq $-0x6832fd919f07a5f5, %rcx # imm = 0x97CD026E60F85A0B
imulq %rcx, %rax
movabsq $0x2f9a04dcc1f0b416, %rcx # imm = 0x2F9A04DCC1F0B416
addq %rcx, %rax
rorxq $0x8, %rax, %rax
movabsq $0xe5109ec20, %rcx      # imm = 0xE5109EC20
cmpq %rcx, %rax
ja 0x4174
leaq 0x135f(%rip), %rdi      # 0x5453
movq %r15, %rsi
leaq 0x1330(%rip), %rdx      # 0x542e
callq 0x3826
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
leaq 0x290(%rsp), %r11
movq 0x12b8(%rsp), %r10
movq 0x10(%rsp), %r9
movq 0x18(%rsp), %r8
testb $0x1, %r12b
jne 0x41ae
incq %r14
cmpq 0x30(%rsp), %r14
setb %al
movq 0x38(%rsp), %r12
jne 0x3fc0
movq 0x20(%rsp), %rsi
incq %rsi
cmpq 0x8(%rsp), %rsi
movl 0x4(%rsp), %ebp
movl $0x1, %edx
jb 0x3eec
jmp 0x41d6
movq 0x20(%rsp), %rsi
incq %rsi
cmpq 0x8(%rsp), %rsi
setae %al
movb 0x3(%rsp), %cl
orb %al, %cl
testb $0x1, %cl
movl 0x4(%rsp), %ebp
movl $0x1, %edx
je 0x3eec
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq