// File: /workspace/github_cpp_makefile_star2_O3/akw0088[P]openssl/rsync_upload.c
// Function: adler32_scan
int adler32_scan(unsigned char *data, unsigned int length, unsigned int block_size, unsigned int *hash_array, int num_hash, block_t *block, char **md5_array)
{
	unsigned int start_offset = 1;
	if (block_size > length)
	{
		printf("Block size larger than file\r\n");
		return -1;
	}

	// initialize output array
	for (int i = 0; i < num_hash; i++)
	{
		block[i].offset = -1;
		block[i].length = block_size;
	}


	// init checksum with adler32 of first block
	unsigned int checksum = adler32(&data[0], block_size);
	for (int i = 0; i < num_hash; i++)
	{
		// Checksum matches, do a strong hash to avoid false positives
		if (checksum == hash_array[i])
		{
			char hash[MD5_SIZE + 1] = { 0 };

			md5sum((char *)&data[i], block_size, (char *)&hash);

			if (strcmp(hash, md5_array[i]) == 0)
			{
				block[i].offset = 0;
				block[i].length = block_size;
				start_offset = block_size - 1;

				// Found a block with strong hash, skip past it then reinit adler32 for next block
				checksum = adler32(&data[start_offset - 1], block_size);
				break;
			}
		}
	}

	// Start the adler32 rolling hash (note ader32_roll is where majority of processing occurs)
	for(unsigned int i = start_offset; i < length - block_size + 1; i++)
	{
		checksum = adler32_roll(checksum,
			data[i + block_size - 1],
			data[i - 1],
			block_size);
#ifdef DEBUG
		// For debugging roll function compare it to the full adler32 calc of the block
		unsigned int full_hash = adler32(&data[i], block_size);
		//		checksum = full_hash;
#endif
		for (int j = 0; j < num_hash; j++)
		{
			// Checksum matches, do a strong hash to avoid false positives
			if (checksum == hash_array[j])
			{
				char hash[MD5_SIZE + 1] = { 0 };

				md5sum((char *)&data[i], block_size, (char *)&hash);

				if (strcmp(hash, md5_array[j]) == 0)
				{
					// Found a block with strong hash, skip past it then reinit adler32 for next block
					block[j].offset = i;
					block[j].length = block_size;
					i += block_size - 1;

					// be sure we arent skipping past end of data
					if (i < length - block_size + 1)
					{
						checksum = adler32(&data[i], block_size);
					}
				}
				break;
			}
		}

#ifdef DEBUG
		if (checksum != full_hash)
		{
			printf("adler32_roll failed at index %d\r\n", i);
			exit(0);
		}
#endif

	}
	return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
subl %edx, %esi
movl %esi, 0x4(%rsp)
jae 0x14d9
leaq 0x2e46(%rip), %rdi      # 0x4315
callq 0x1070
jmp 0x17f0
movl %edx, %r15d
movq %rdi, %r10
movq %rcx, 0x28(%rsp)
movl %r8d, %ebx
testl %r8d, %r8d
jle 0x1503
xorl %eax, %eax
movl $0xffffffff, (%r9,%rax,8) # imm = 0xFFFFFFFF
movl %r15d, 0x4(%r9,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x14ee
movq %r9, 0x38(%rsp)
movl %r15d, %eax
movq %rax, 0x18(%rsp)
movl $0x1, %ebp
testl %r15d, %r15d
je 0x156f
movl $0x1, %r11d
xorl %edx, %edx
movl $0x80078071, %esi       # imm = 0x80078071
xorl %eax, %eax
movq 0x18(%rsp), %r9
movzwl %r11w, %r11d
movzbl (%r10,%rdx), %edi
addl %r11d, %edi
movl %edi, %r11d
subl $0xfff1, %r11d          # imm = 0xFFF1
cmovbl %edi, %r11d
addl %r11d, %eax
movq %rax, %rdi
imulq %rsi, %rdi
shrq $0x2f, %rdi
imull $0xfff1, %edi, %edi     # imm = 0xFFF1
subl %edi, %eax
incq %rdx
cmpq %rdx, %r9
jne 0x152e
shll $0x10, %eax
movzwl %r11w, %ebp
orl %eax, %ebp
testl %r8d, %r8d
movl %r8d, 0x34(%rsp)
movq %r15, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq 0x28(%rsp), %rax
jle 0x1603
leal -0x1(%r15), %r12d
leal -0x2(%r15), %ecx
addq %r10, %rcx
movq %rcx, 0x20(%rsp)
xorl %r13d, %r13d
movq 0xb0(%rsp), %r15
cmpl (%rax,%r13,4), %ebp
jne 0x15eb
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movb $0x0, 0x70(%rsp)
leaq (%r10,%r13), %rdi
movq 0x8(%rsp), %rsi
leaq 0x50(%rsp), %r14
movq %r14, %rdx
callq 0x2b50
movq (%r15,%r13,8), %rsi
movq %r14, %rdi
callq 0x1140
testl %eax, %eax
je 0x160b
movq 0x10(%rsp), %r10
movq 0x28(%rsp), %rax
incq %r13
cmpq %r13, %rbx
jne 0x15a3
movl $0x1, %r12d
movq 0x8(%rsp), %r15
jmp 0x168a
movl $0x1, %r12d
jmp 0x168a
movq 0x38(%rsp), %rax
movl $0x0, (%rax,%r13,8)
movq 0x8(%rsp), %r15
movl %r15d, 0x4(%rax,%r13,8)
testl %r15d, %r15d
je 0x17f0
movl $0x1, %r9d
xorl %edx, %edx
movl $0x80078071, %esi       # imm = 0x80078071
xorl %eax, %eax
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r10
movzwl %r9w, %r9d
movzbl (%r10,%rdx), %edi
addl %r9d, %edi
movl %edi, %r9d
subl $0xfff1, %r9d           # imm = 0xFFF1
cmovbl %edi, %r9d
addl %r9d, %eax
movq %rax, %rdi
imulq %rsi, %rdi
shrq $0x2f, %rdi
imull $0xfff1, %edi, %edi     # imm = 0xFFF1
subl %edi, %eax
incq %rdx
cmpq %rdx, %r8
jne 0x1644
shll $0x10, %eax
movzwl %r9w, %ebp
orl %eax, %ebp
movq 0x10(%rsp), %r10
movl 0x4(%rsp), %eax
incl %eax
movl %eax, 0x4(%rsp)
cmpl %eax, %r12d
jae 0x17f0
leal -0x1(%r15), %eax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
leal (%rax,%r12), %r14d
movzbl (%r10,%r14), %esi
leal -0x1(%r12), %eax
movzbl (%r10,%rax), %edx
movl %ebp, %edi
movl %r15d, %ecx
movq %r10, %r13
callq 0x1407
movl %eax, %ebp
cmpl $0x0, 0x34(%rsp)
jle 0x16f6
leaq (%r14,%r13), %rcx
xorl %r15d, %r15d
movq %r13, %r10
movq 0x28(%rsp), %rax
cmpl (%rax,%r15,4), %ebp
je 0x16fe
incq %r15
cmpq %r15, %rbx
jne 0x16e3
jmp 0x1786
movq %r13, %r10
jmp 0x178b
movq %rcx, 0x40(%rsp)
movq %r14, 0x48(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movb $0x0, 0x70(%rsp)
movl %r12d, %edi
addq %r10, %rdi
movq 0x8(%rsp), %r14
movl %r14d, %esi
leaq 0x50(%rsp), %r13
movq %r13, %rdx
callq 0x2b50
movq 0xb0(%rsp), %rax
movq (%rax,%r15,8), %rsi
movq %r13, %rdi
callq 0x1140
testl %eax, %eax
je 0x174f
jmp 0x1781
testl %r14d, %r14d
sete %al
movq 0x38(%rsp), %rcx
movl %r12d, (%rcx,%r15,8)
movl %r14d, 0x4(%rcx,%r15,8)
movq 0x48(%rsp), %r12
cmpl 0x4(%rsp), %r12d
setae %cl
movl $0x1, %edx
cmovbl %edx, %ebp
orb %al, %cl
movq 0x40(%rsp), %r11
je 0x179b
movq 0x10(%rsp), %r10
movq 0x8(%rsp), %r15
incl %r12d
cmpl 0x4(%rsp), %r12d
jb 0x16a6
jmp 0x17f0
movl $0x1, %ecx
xorl %edx, %edx
xorl %eax, %eax
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %rdi
movl $0x80078071, %r9d       # imm = 0x80078071
movzwl %cx, %ecx
movzbl (%r11,%rdx), %esi
addl %ecx, %esi
movl %esi, %ecx
subl $0xfff1, %ecx           # imm = 0xFFF1
cmovbl %esi, %ecx
addl %ecx, %eax
movq %rax, %rsi
imulq %r9, %rsi
shrq $0x2f, %rsi
imull $0xfff1, %esi, %esi     # imm = 0xFFF1
subl %esi, %eax
incq %rdx
cmpq %rdx, %rdi
jne 0x17b4
shll $0x10, %eax
movzwl %cx, %ebp
orl %eax, %ebp
jmp 0x1786
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq