// File: /workspace/github_cpp_makefile_star2_O2/akw0088[P]openssl/aes_download.cpp
// Function: aes_decrypt(unsigned char*, unsigned char*, char*, unsigned int&, char*)
int aes_decrypt(unsigned char *key, unsigned char *iv, char *filename, unsigned int &decryptedtext_len, char *remote_hash)
{
	unsigned int size = 0;
	unsigned char *ciphertext = (unsigned char *)get_file(filename, &size);
	if (ciphertext == NULL)
	{
		printf("Unable to open %s\r\n", filename);
		return -1;
	}

	/* Buffer for the decrypted text */
	unsigned char *decryptedtext = (unsigned char *)malloc(size);
	if (decryptedtext == NULL)
	{
		perror("malloc failed");
		return -1;
	}

	unsigned int ciphertext_len = size;

	decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext);
	decryptedtext[decryptedtext_len] = '\0';

	char new_filename[256] = {0};



	unsigned char local_hash[33] = {0};

	md5sum((char *)decryptedtext, decryptedtext_len, (char *)&local_hash[0]);


	delete [] ciphertext;
	if (strcmp(remote_hash, (char *)&local_hash[0]) == 0)
	{
		printf("MD5 Hash Pass: %s\r\n", remote_hash);
	}
	else
	{
		printf("MD5 Hash Fail: local %s != remote %s\r\n", local_hash, remote_hash);
		free((void *)decryptedtext);
		return -1;
	}


	sprintf(new_filename, "%s_decrypted", filename);
	write_file(new_filename, (char *)decryptedtext, decryptedtext_len);
	free((void *)decryptedtext);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %r8, -0x48(%rbp)
movq %rcx, %r14
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r12
leaq -0x2c(%rbp), %rsi
andl $0x0, (%rsi)
movq %rdx, %rdi
callq 0x24a3
testq %rax, %rax
je 0x29ef
movq %rax, %r15
movq %r14, -0x38(%rbp)
movq %rbx, -0x40(%rbp)
movl -0x2c(%rbp), %r14d
movq %r14, %rdi
callq 0x2260
testq %rax, %rax
je 0x2a61
movq %rax, %rbx
movq %r15, %rdi
movl %r14d, %esi
movq %r12, %rdx
movq %r13, %rcx
movq %rax, %r8
callq 0x285b
movl %eax, %r14d
movq -0x38(%rbp), %rax
movl %r14d, (%rax)
movl %r14d, %eax
xorl %r13d, %r13d
movb %r13b, (%rbx,%rax)
leaq -0x170(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x2120
leaq -0x70(%rbp), %r12
movb %r13b, 0x20(%r12)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%r12)
movaps %xmm0, (%r12)
movq %rbx, %rdi
movl %r14d, %esi
movq %r12, %rdx
callq 0x23fc
movq %r15, %rdi
callq 0x2240
movq -0x48(%rbp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x2270
testl %eax, %eax
je 0x2a06
leaq 0x6e6(%rip), %rdi       # 0x30c1
leaq -0x70(%rbp), %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2060
pushq $-0x1
popq %r13
jmp 0x2a44
leaq 0x695(%rip), %rdi       # 0x308b
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2060
pushq $-0x1
popq %r13
jmp 0x2a4c
leaq 0x6a0(%rip), %rdi       # 0x30ad
movq %r14, %rsi
xorl %eax, %eax
callq 0x2060
leaq 0x6ca(%rip), %rsi       # 0x30e8
leaq -0x170(%rbp), %r14
movq %r14, %rdi
movq -0x40(%rbp), %rdx
xorl %eax, %eax
callq 0x2070
movq -0x38(%rbp), %rax
movl (%rax), %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2551
movq %rbx, %rdi
callq 0x2220
movl %r13d, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x637(%rip), %rdi       # 0x309f
callq 0x21a0
jmp 0x2a00