// File: /workspace/github_c_makefile_star2_O3/OfficialRepo[P]PokiLib/encrypt.c
// Function: encrypt
int encrypt(const unsigned char *input, size_t input_size,
	const unsigned char* iv, size_t iv_size,
	unsigned char* output, size_t * output_size) {
	unsigned char arr2[256];
	unsigned char arr3[256];
	size_t roundedsize, totalsize;

	if (iv_size != 32){
		return -1;
	}

	roundedsize = input_size + (256 - (input_size % 256));
	totalsize = roundedsize + 32;

	if (output == NULL){
		*output_size = totalsize;
		return 0;
	}
	if (*output_size < totalsize){
		*output_size = totalsize;
		return -1;
	}

	for (int j = 0; j < 8; j++){
		for (int i = 0; i < 32; i++){
			arr2[32 * j + i] = rotl8(iv[i], j); //rotate byte left
		}
	}

	memcpy(output, iv, 32);
	memcpy(output + 32, input, input_size);

	if (roundedsize > input_size)
	{
		memset(output + 32 + input_size, 0, roundedsize - input_size); //pad data with zeroes
	}
	output[totalsize - 1] = 256 - (input_size % 256);

	for (size_t offset = 32; offset < totalsize; offset += 256)
	{
		for (int i = 0; i < 256; i++){
			output[offset + i] ^= arr2[i];
		}
		sub_9E9D8(output + offset, arr3); // !! encryption here
		memcpy(arr2, arr3, 256);
		memcpy(output + offset, arr3, 256);
	}

	*output_size = totalsize;
	return 0;
}

// Assembly:
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x20, %rcx
jne 0x1709a
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %r9, %rbx
movq %r8, %r14
movq %rsi, %r15
movzbl %r15b, %eax
movl $0x100, %r12d           # imm = 0x100
subq %rax, %r12
leaq (%r12,%rsi), %rcx
addq $0x20, %rcx
testq %r8, %r8
je 0x17084
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rcx, (%rbx)
jb 0x17086
movq %rdi, %rsi
movq %rcx, (%rsp)
leaq (%r12,%r15), %rbp
leaq 0x10(%rsp), %rax
xorl %ecx, %ecx
movd 0x10a4(%rip), %xmm0     # 0x18010
movd %ecx, %xmm1
xorl %edi, %edi
movdqu (%rdx,%rdi), %xmm2
movdqa %xmm2, %xmm3
punpckhbw %xmm2, %xmm3    # xmm3 = xmm3[8],xmm2[8],xmm3[9],xmm2[9],xmm3[10],xmm2[10],xmm3[11],xmm2[11],xmm3[12],xmm2[12],xmm3[13],xmm2[13],xmm3[14],xmm2[14],xmm3[15],xmm2[15]
movdqa %xmm1, %xmm4
pand %xmm0, %xmm4
psllw %xmm4, %xmm3
psrlw $0x8, %xmm3
punpcklbw %xmm2, %xmm2    # xmm2 = xmm2[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
psllw %xmm4, %xmm2
psrlw $0x8, %xmm2
packuswb %xmm3, %xmm2
movdqa %xmm2, (%rax,%rdi)
addq $0x10, %rdi
cmpq $0x20, %rdi
jne 0x16f72
incq %rcx
addq $0x20, %rax
cmpq $0x8, %rcx
jne 0x16f6c
movups (%rdx), %xmm0
movups 0x10(%rdx), %xmm1
movups %xmm1, 0x10(%r14)
movups %xmm0, (%r14)
leaq 0x20(%r14), %r13
movq %r13, %rdi
movq %r15, %rdx
callq 0x10d0
cmpq %r15, %rbp
jbe 0x16ff1
addq %r13, %r15
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x1080
movb %r12b, 0x1f(%r14,%rbp)
movq (%rsp), %rcx
cmpq $0x21, %rcx
jb 0x17084
movq %rbx, 0x8(%rsp)
movl $0x20, %r12d
leaq 0x110(%rsp), %r15
movl $0x100, %ebx            # imm = 0x100
leaq (%r14,%r12), %rbp
xorl %eax, %eax
movups (%r13,%rax), %xmm0
xorps 0x10(%rsp,%rax), %xmm0
movups %xmm0, (%r13,%rax)
addq $0x10, %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x17022
movq %rbp, %rdi
movq %r15, %rsi
callq 0x10a0
movl $0x100, %edx            # imm = 0x100
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x10d0
movl $0x100, %edx            # imm = 0x100
movq %rbp, %rdi
movq %r15, %rsi
callq 0x10d0
addq %rbx, %r12
addq %rbx, %r13
movq (%rsp), %rcx
cmpq %rcx, %r12
jb 0x1701c
xorl %eax, %eax
movq 0x8(%rsp), %rbx
jmp 0x17086
xorl %eax, %eax
movq %rcx, (%rbx)
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq