// File: /workspace/github_c_makefile_star2_O3/Seclabs-htc[P]slip39/trezor-crypto/sha2.c
// Function: sha1_Update
void sha1_Update(SHA1_CTX* context, const sha2_byte *data, size_t len) {
	unsigned int	freespace, usedspace;

	if (len == 0) {
		/* Calling with no data is valid - we do nothing */
		return;
	}

	usedspace = (context->bitcount >> 3) % SHA1_BLOCK_LENGTH;
	if (usedspace > 0) {
		/* Calculate how much free space is available in the buffer */
		freespace = SHA1_BLOCK_LENGTH - usedspace;

		if (len >= freespace) {
			/* Fill the buffer completely and process it */
			MEMCPY_BCOPY(((uint8_t*)context->buffer) + usedspace, data, freespace);
			context->bitcount += freespace << 3;
			len -= freespace;
			data += freespace;
#if BYTE_ORDER == LITTLE_ENDIAN
			/* Convert TO host byte order */
			for (int j = 0; j < 16; j++) {
				REVERSE32(context->buffer[j],context->buffer[j]);
			}
#endif
			sha1_Transform(context->state, context->buffer, context->state);
		} else {
			/* The buffer is not yet full */
			MEMCPY_BCOPY(((uint8_t*)context->buffer) + usedspace, data, len);
			context->bitcount += len << 3;
			/* Clean up: */
			usedspace = freespace = 0;
			return;
		}
	}
	while (len >= SHA1_BLOCK_LENGTH) {
		/* Process as many complete blocks as we can */
		MEMCPY_BCOPY(context->buffer, data, SHA1_BLOCK_LENGTH);
#if BYTE_ORDER == LITTLE_ENDIAN
		/* Convert TO host byte order */
		for (int j = 0; j < 16; j++) {
			REVERSE32(context->buffer[j],context->buffer[j]);
		}
#endif
		sha1_Transform(context->state, context->buffer, context->state);
		context->bitcount += SHA1_BLOCK_LENGTH << 3;
		len -= SHA1_BLOCK_LENGTH;
		data += SHA1_BLOCK_LENGTH;
	}
	if (len > 0) {
		/* There's left-overs, so save 'em */
		MEMCPY_BCOPY(context->buffer, data, len);
		context->bitcount += len << 3;
	}
	/* Clean up: */
	usedspace = freespace = 0;
}

// Assembly:
testq %rdx, %rdx
je 0x8bbf
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %rbx
movl 0x18(%rdi), %eax
shrl $0x3, %eax
andl $0x3f, %eax
je 0x8add
movl $0x40, %r12d
subl %eax, %r12d
movq %r13, %r15
movl %eax, %eax
leaq (%rbx,%rax), %rdi
addq $0x20, %rdi
subq %r12, %r15
jae 0x8ae2
movq %r14, %rsi
movq %r13, %rdx
callq 0x8080
jmp 0x8ba9
movq %r13, %r15
jmp 0x8b24
leaq 0x20(%rbx), %r13
movq %r14, %rsi
movq %r12, %rdx
callq 0x8080
leal (,%r12,8), %eax
addq %rax, 0x18(%rbx)
movl $0x8, %eax
movl (%rbx,%rax,4), %ecx
bswapl %ecx
movl %ecx, (%rbx,%rax,4)
incq %rax
cmpq $0x18, %rax
jne 0x8b02
addq %r12, %r14
movq %rbx, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x880b
cmpq $0x40, %r15
jb 0x8b92
leaq 0x20(%rbx), %r12
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups 0x30(%r14), %xmm3
movups %xmm3, 0x30(%r12)
movups %xmm2, 0x20(%r12)
movups %xmm1, 0x10(%r12)
movups %xmm0, (%r12)
movl $0x8, %eax
movl (%rbx,%rax,4), %ecx
bswapl %ecx
movl %ecx, (%rbx,%rax,4)
incq %rax
cmpq $0x18, %rax
jne 0x8b5d
movq %rbx, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x880b
addq $0x200, 0x18(%rbx)      # imm = 0x200
addq $-0x40, %r15
addq $0x40, %r14
cmpq $0x3f, %r15
ja 0x8b2e
testq %r15, %r15
je 0x8bb1
leaq 0x20(%rbx), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x8080
movq %r15, %r13
shlq $0x3, %r13
addq %r13, 0x18(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq