// File: /workspace/github_cpp_makefile_star2_O2/wangyu-[P]pwdgen/lib/pbkdf2-sha1.cpp
// Function: PKCS5_PBKDF2_HMAC_SHA1(unsigned char const*, unsigned long, unsigned char const*, unsigned long, unsigned long, unsigned long, unsigned char*)
void PKCS5_PBKDF2_HMAC_SHA1(const unsigned char *password, size_t plen,
    const unsigned char *salt, size_t slen,
    const unsigned long iteration_count, const unsigned long key_length,
    unsigned char *output)
{
	sha1_context ctx;
	sha1_starts(&ctx);

	// Size of the generated digest
	unsigned char md_size = 20;
	unsigned char md1[20];
	unsigned char work[20];

	unsigned long counter = 1;
	unsigned long generated_key_length = 0;
	while (generated_key_length < key_length) {
		// U1 ends up in md1 and work
		unsigned char c[4];
		c[0] = (counter >> 24) & 0xff;
		c[1] = (counter >> 16) & 0xff;
		c[2] = (counter >> 8) & 0xff;
		c[3] = (counter >> 0) & 0xff;

		sha1_hmac_starts(&ctx, password, plen);
		sha1_hmac_update(&ctx, salt, slen);
		sha1_hmac_update(&ctx, c, 4);
		sha1_hmac_finish(&ctx, md1);
		memcpy(work, md1, md_size);

		unsigned long ic = 1;
		for (ic = 1; ic < iteration_count; ic++) {
			// U2 ends up in md1
			sha1_hmac_starts(&ctx, password, plen);
			sha1_hmac_update(&ctx, md1, md_size);
			sha1_hmac_finish(&ctx, md1);
			// U1 xor U2
			unsigned long i = 0;
			for (i = 0; i < md_size; i++) {
				work[i] ^= md1[i];
			}
			// and so on until iteration_count
		}

		// Copy the generated bytes to the key
		unsigned long bytes_to_write =
		    min((key_length - generated_key_length), md_size);
		memcpy(output + generated_key_length, work, bytes_to_write);
		generated_key_length += bytes_to_write;
		++counter;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %r9, 0x28(%rsp)
movq %r8, %r14
movq %rcx, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, %r13
movq %rdi, %rbp
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %r15
movaps %xmm0, (%r15)
movq $0x67452301, 0x10(%r15) # imm = 0x67452301
movl $0x98badcfe, %eax       # imm = 0x98BADCFE
leaq 0x5712ce8b(%rax), %rcx
movq %rcx, 0x18(%r15)
movq %rax, 0x20(%r15)
movq $0x10325476, 0x28(%r15) # imm = 0x10325476
addq $0x2b1804f2, %rax       # imm = 0x2B1804F2
movq %rax, 0x30(%r15)
pushq $0x1
popq %rax
xorl %edx, %edx
leaq 0x60(%rsp), %r12
movq 0x28(%rsp), %rcx
subq %rdx, %rcx
movq %rcx, 0x38(%rsp)
jbe 0x40545f
movq %rdx, 0x10(%rsp)
movq %rax, 0x30(%rsp)
bswapl %eax
movl %eax, 0xc(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
movl %r13d, %edx
callq 0x405103
movq %r15, %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x403315
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x403315
movq %r15, %rdi
movq %r12, %rsi
callq 0x4051e4
movl 0x70(%rsp), %eax
movl %eax, 0x50(%rsp)
movaps 0x60(%rsp), %xmm0
movaps %xmm0, 0x40(%rsp)
pushq $0x1
popq %rbx
cmpq %r14, %rbx
jae 0x40541c
movq %r15, %rdi
movq %rbp, %rsi
movl %r13d, %edx
callq 0x405103
movq %r15, %rdi
movq %r12, %rsi
pushq $0x14
popq %rdx
callq 0x403315
movq %r15, %rdi
movq %r12, %rsi
callq 0x4051e4
xorl %eax, %eax
cmpq $0x14, %rax
je 0x405417
movb 0x60(%rsp,%rax), %cl
xorb %cl, 0x40(%rsp,%rax)
incq %rax
jmp 0x405404
incq %rbx
jmp 0x4053d6
movq 0x38(%rsp), %rbx
cmpq $0x14, %rbx
pushq $0x14
popq %rax
cmovaeq %rax, %rbx
movq 0x1b0(%rsp), %rax
movq 0x10(%rsp), %rcx
leaq (%rax,%rcx), %rdi
leaq 0x40(%rsp), %rsi
movq %rbx, %rdx
callq 0x401040
movq 0x10(%rsp), %rdx
addq %rbx, %rdx
movq 0x30(%rsp), %rax
incq %rax
jmp 0x405363
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)