// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0x270, %rsp            # imm = 0x270
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
cmpq $0x20, -0x28(%rbp)
je 0x31dc6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3203d
movq -0x18(%rbp), %rax
movq -0x18(%rbp), %rdx
andq $0xff, %rdx
movl $0x100, %ecx            # imm = 0x100
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x248(%rbp)
movq -0x248(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x250(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x31e1a
movq -0x250(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x3203d
movq -0x38(%rbp), %rax
movq (%rax), %rax
cmpq -0x250(%rbp), %rax
jae 0x31e44
movq -0x250(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3203d
movl $0x0, -0x254(%rbp)
cmpl $0x8, -0x254(%rbp)
jge 0x31ec4
movl $0x0, -0x258(%rbp)
cmpl $0x20, -0x258(%rbp)
jge 0x31eb1
movq -0x20(%rbp), %rax
movslq -0x258(%rbp), %rcx
movb (%rax,%rcx), %al
movl -0x254(%rbp), %esi
movzbl %al, %edi
callq 0x32050
movb %al, %cl
movl -0x254(%rbp), %eax
shll $0x5, %eax
addl -0x258(%rbp), %eax
cltq
movb %cl, -0x140(%rbp,%rax)
movl -0x258(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x258(%rbp)
jmp 0x31e61
jmp 0x31eb3
movl -0x254(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x254(%rbp)
jmp 0x31e4e
movq -0x30(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rax)
movq 0x18(%rcx), %rcx
movq %rcx, 0x18(%rax)
movq -0x30(%rbp), %rdi
addq $0x20, %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x10d0
movq -0x248(%rbp), %rax
cmpq -0x18(%rbp), %rax
jbe 0x31f2a
movq -0x30(%rbp), %rdi
addq $0x20, %rdi
addq -0x18(%rbp), %rdi
movq -0x248(%rbp), %rdx
subq -0x18(%rbp), %rdx
xorl %esi, %esi
callq 0x1080
movq -0x18(%rbp), %rcx
andq $0xff, %rcx
movl $0x100, %eax            # imm = 0x100
subq %rcx, %rax
movb %al, %dl
movq -0x30(%rbp), %rax
movq -0x250(%rbp), %rcx
subq $0x1, %rcx
movb %dl, (%rax,%rcx)
movq $0x20, -0x260(%rbp)
movq -0x260(%rbp), %rax
cmpq -0x250(%rbp), %rax
jae 0x32028
movl $0x0, -0x264(%rbp)
cmpl $0x100, -0x264(%rbp)    # imm = 0x100
jge 0x31fc4
movslq -0x264(%rbp), %rax
movzbl -0x140(%rbp,%rax), %esi
movq -0x30(%rbp), %rax
movq -0x260(%rbp), %rcx
movslq -0x264(%rbp), %rdx
addq %rdx, %rcx
movzbl (%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, (%rax,%rcx)
movl -0x264(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x264(%rbp)
jmp 0x31f7a
movq -0x30(%rbp), %rdi
addq -0x260(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0x10a0
leaq -0x140(%rbp), %rdi
leaq -0x240(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x10d0
movq -0x30(%rbp), %rdi
addq -0x260(%rbp), %rdi
leaq -0x240(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x10d0
movq -0x260(%rbp), %rax
addq $0x100, %rax            # imm = 0x100
movq %rax, -0x260(%rbp)
jmp 0x31f5c
movq -0x250(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x270, %rsp            # imm = 0x270
popq %rbp
retq
nopl (%rax)