// File: renzibei[P]TSProxy/aes.cpp
// Function: decrypt(char*, int, char const*, int, char const (&) [16], char const (&) [16], char const (&) [16])
int decrypt(char *dst, int dst_size, const char *src, int src_size, const char (&key)[16], const char (&random)[16], const char (&mac)[16]) {
	if (src_size > constants::AES_MAX_DATA_LENGTH) {
		LogHelper::log(Error, "src_size size should be no longer than: %lu, but size is: %d", constants::AES_MAX_DATA_LENGTH, src_size);
		return -5;
	}
	if (dst_size < src_size - 16) {
		LogHelper::log(Error, "dst size too small, dst size: %d, src size: %d", dst_size, src_size);
		return -6;
	}
	char *buf = new char[src_size];
	int deRet = decode(buf, src_size, src, src_size, key, random, mac);
	if (deRet != 0) {
		delete[] buf;
		LogHelper::log(Error, "Fail to decrypt");
		return deRet;
	}
	int real_size = src_size;
	while (buf[real_size - 1] == 0 && buf[real_size - 2] != 0xf) {
		real_size--;
	}
	if (buf[real_size - 2] != 0xf) {
		LogHelper::log(Error, "Padding error");
		delete[] buf;
		return -7;
	}
	real_size -= 2;
	if (dst_size < real_size) {
		LogHelper::log(Error, "dst_size less than real size, dist_size: %d, real_size: %d",dst_size, real_size);
		delete[] buf;
		return -8;
	}
	memcpy(dst, buf, real_size);
	delete[] buf;
	return real_size;
}

// Assembly:
subq $0x78, %rsp
movq 0x80(%rsp), %rax
movq %rdi, 0x68(%rsp)
movl %esi, 0x64(%rsp)
movq %rdx, 0x58(%rsp)
movl %ecx, 0x54(%rsp)
movq %r8, 0x48(%rsp)
movq %r9, 0x40(%rsp)
movslq 0x54(%rsp), %rax
cmpq $0xffcc, %rax           # imm = 0xFFCC
jbe 0xeb3b
movl 0x54(%rsp), %ecx
xorl %edi, %edi
leaq 0x2524(%rip), %rsi      # 0x11046
movl $0xffcc, %edx           # imm = 0xFFCC
movb $0x0, %al
callq 0x28f0
movl $0xfffffffb, 0x74(%rsp) # imm = 0xFFFFFFFB
jmp 0xed2c
movl 0x64(%rsp), %eax
movl 0x54(%rsp), %ecx
subl $0x10, %ecx
cmpl %ecx, %eax
jge 0xeb6f
movl 0x64(%rsp), %edx
movl 0x54(%rsp), %ecx
xorl %edi, %edi
leaq 0x2528(%rip), %rsi      # 0x11083
movb $0x0, %al
callq 0x28f0
movl $0xfffffffa, 0x74(%rsp) # imm = 0xFFFFFFFA
jmp 0xed2c
movslq 0x54(%rsp), %rdi
movq $-0x1, %rax
cmpq $0x0, %rdi
cmovlq %rax, %rdi
callq 0x2030
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
movl 0x54(%rsp), %esi
movq 0x58(%rsp), %rdx
movl 0x54(%rsp), %ecx
movq 0x48(%rsp), %r8
movq 0x40(%rsp), %r9
movq 0x80(%rsp), %rax
movq %rax, (%rsp)
callq 0xe610
movl %eax, 0x34(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0xebfc
movq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0xebdf
movq 0x28(%rsp), %rdi
callq 0x2280
xorl %edi, %edi
leaq 0x24ca(%rip), %rsi      # 0x110b2
movb $0x0, %al
callq 0x28f0
movl 0x34(%rsp), %eax
movl %eax, 0x74(%rsp)
jmp 0xed2c
movl 0x54(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0x38(%rsp), %rax
movl 0x30(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0x27(%rsp)
jne 0xec3f
movq 0x38(%rsp), %rax
movl 0x30(%rsp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xf, %eax
setne %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0xec49
jmp 0xec56
movl 0x30(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0xec04
movq 0x38(%rsp), %rax
movl 0x30(%rsp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xf, %eax
je 0xeca5
xorl %edi, %edi
leaq 0x244b(%rip), %rsi      # 0x110c2
movb $0x0, %al
callq 0x28f0
movq 0x38(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0xec98
movq 0x18(%rsp), %rdi
callq 0x2280
movl $0xfffffff9, 0x74(%rsp) # imm = 0xFFFFFFF9
jmp 0xed2c
movl 0x30(%rsp), %eax
subl $0x2, %eax
movl %eax, 0x30(%rsp)
movl 0x64(%rsp), %eax
cmpl 0x30(%rsp), %eax
jge 0xecf6
movl 0x64(%rsp), %edx
movl 0x30(%rsp), %ecx
xorl %edi, %edi
leaq 0x2405(%rip), %rsi      # 0x110d0
movb $0x0, %al
callq 0x28f0
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0xecec
movq 0x10(%rsp), %rdi
callq 0x2280
movl $0xfffffff8, 0x74(%rsp) # imm = 0xFFFFFFF8
jmp 0xed2c
movq 0x68(%rsp), %rdi
movq 0x38(%rsp), %rsi
movslq 0x30(%rsp), %rdx
callq 0x2180
movq 0x38(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xed24
movq 0x8(%rsp), %rdi
callq 0x2280
movl 0x30(%rsp), %eax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)