// File: Alcaro[P]bridges/arlib/zlibhead.cpp
// Function: inflator::zlibhead::adler32(arrayview<unsigned char>, unsigned int)
uint32_t inflator::zlibhead::adler32(bytesr by, uint32_t adler_prev)
{
	// could simd this, but not much point, it's not a bottleneck
	uint32_t a = adler_prev&0xFFFF;
	uint32_t b = adler_prev>>16;
	
	size_t i = 0;
	while (i < (by.size()&~4095)) // 5552 is safe, but 4096 is easier to deal with
	{
		do {
			a += by[i++]; b += a;
			a += by[i++]; b += a;
			a += by[i++]; b += a;
			a += by[i++]; b += a;
		} while (i&4095);
		a %= 65521;
		b %= 65521;
	}
	while (i < (by.size()&~3))
	{
		a += by[i++]; b += a;
		a += by[i++]; b += a;
		a += by[i++]; b += a;
		a += by[i++]; b += a;
	}
	while (i < by.size())
	{
		a += by[i++];
		b += a;
	}
	a %= 65521;
	b %= 65521;
	
	return a | (b<<16);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x8(%rbp)
movl %edx, -0x14(%rbp)
movl -0x14(%rbp), %eax
andl $0xffff, %eax           # imm = 0xFFFF
movl %eax, -0x18(%rbp)
movl -0x14(%rbp), %eax
shrl $0x10, %eax
movl %eax, -0x1c(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x6290
movq %rax, %rcx
movq -0x30(%rbp), %rax
andq $-0x1000, %rcx          # imm = 0xF000
cmpq %rcx, %rax
jae 0x46418
jmp 0x46339
movq -0x28(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x62e0
movzbl (%rax), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x28(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x62e0
movzbl (%rax), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x28(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x62e0
movzbl (%rax), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x28(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x62e0
movzbl (%rax), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x28(%rbp), %rax
andq $0xfff, %rax            # imm = 0xFFF
cmpq $0x0, %rax
jne 0x46339
movl -0x18(%rbp), %eax
movl $0xfff1, %ecx           # imm = 0xFFF1
xorl %edx, %edx
divl %ecx
movl %edx, -0x18(%rbp)
movl -0x1c(%rbp), %eax
movl $0xfff1, %ecx           # imm = 0xFFF1
xorl %edx, %edx
divl %ecx
movl %edx, -0x1c(%rbp)
jmp 0x4630f
jmp 0x4641a
movq -0x28(%rbp), %rax
movq %rax, -0x38(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x6290
movq %rax, %rcx
movq -0x38(%rbp), %rax
andq $-0x4, %rcx
cmpq %rcx, %rax
jae 0x464ec
movq -0x28(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x62e0
movzbl (%rax), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x28(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x62e0
movzbl (%rax), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x28(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x62e0
movzbl (%rax), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x28(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x62e0
movzbl (%rax), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
jmp 0x4641a
jmp 0x464ee
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x6290
movq %rax, %rcx
movq -0x40(%rbp), %rax
cmpq %rcx, %rax
jae 0x46537
movq -0x28(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x62e0
movzbl (%rax), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
jmp 0x464ee
movl -0x18(%rbp), %eax
movl $0xfff1, %ecx           # imm = 0xFFF1
xorl %edx, %edx
divl %ecx
movl %edx, -0x18(%rbp)
movl -0x1c(%rbp), %eax
movl $0xfff1, %ecx           # imm = 0xFFF1
xorl %edx, %edx
divl %ecx
movl %edx, -0x1c(%rbp)
movl -0x18(%rbp), %eax
movl -0x1c(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)