// File: /workspace/github_c_makefile_star2_O0/Seclabs-htc[P]slip39/trezor-crypto/sha2.c
// Function: sha512_Update
void sha512_Update(SHA512_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[0] >> 3) % SHA512_BLOCK_LENGTH;
	if (usedspace > 0) {
		/* Calculate how much free space is available in the buffer */
		freespace = SHA512_BLOCK_LENGTH - usedspace;

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0xa7d0
jmp 0xab3d
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
shrq $0x3, %rax
andq $0x7f, %rax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jbe 0xa9a5
movl $0x80, %eax
subl -0x20(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
cmpq %rcx, %rax
jb 0xa93c
movq -0x8(%rbp), %rdi
addq $0x50, %rdi
movl -0x20(%rbp), %eax
addq %rax, %rdi
movq -0x10(%rbp), %rsi
movl -0x1c(%rbp), %eax
movl %eax, %edx
callq 0x8090
movl -0x1c(%rbp), %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x8(%rbp), %rax
addq 0x40(%rax), %rcx
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movl -0x1c(%rbp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0xa85f
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x48(%rax)
movl -0x1c(%rbp), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x18(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0x10, -0x24(%rbp)
jge 0xa925
movq -0x8(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq 0x50(%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
shrq $0x20, %rax
movq -0x30(%rbp), %rcx
shlq $0x20, %rcx
orq %rcx, %rax
movq %rax, -0x30(%rbp)
movabsq $-0xff00ff00ff0100, %rax # imm = 0xFF00FF00FF00FF00
andq -0x30(%rbp), %rax
shrq $0x8, %rax
movabsq $0xff00ff00ff00ff, %rcx # imm = 0xFF00FF00FF00FF
andq -0x30(%rbp), %rcx
shlq $0x8, %rcx
orq %rcx, %rax
movq %rax, -0x30(%rbp)
movabsq $-0xffff00010000, %rdx  # imm = 0xFFFF0000FFFF0000
andq -0x30(%rbp), %rdx
shrq $0x10, %rdx
movabsq $0xffff0000ffff, %rax   # imm = 0xFFFF0000FFFF
andq -0x30(%rbp), %rax
shlq $0x10, %rax
orq %rax, %rdx
movq -0x8(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq %rdx, 0x50(%rax,%rcx,8)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xa886
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x50, %rsi
movq -0x8(%rbp), %rdx
callq 0xa2b0
jmp 0xa9a3
movq -0x8(%rbp), %rdi
addq $0x50, %rdi
movl -0x20(%rbp), %eax
addq %rax, %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x8090
movq -0x18(%rbp), %rcx
shlq $0x3, %rcx
movq -0x8(%rbp), %rax
addq 0x40(%rax), %rcx
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x18(%rbp), %rcx
shlq $0x3, %rcx
cmpq %rcx, %rax
jae 0xa990
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x48(%rax)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
jmp 0xab3d
jmp 0xa9a5
jmp 0xa9a7
cmpq $0x80, -0x18(%rbp)
jb 0xaad8
movq -0x8(%rbp), %rdi
addq $0x50, %rdi
movq -0x10(%rbp), %rsi
movl $0x80, %edx
callq 0x8090
movl $0x0, -0x34(%rbp)
cmpl $0x10, -0x34(%rbp)
jge 0xaa71
movq -0x8(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq 0x50(%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
shrq $0x20, %rax
movq -0x40(%rbp), %rcx
shlq $0x20, %rcx
orq %rcx, %rax
movq %rax, -0x40(%rbp)
movabsq $-0xff00ff00ff0100, %rax # imm = 0xFF00FF00FF00FF00
andq -0x40(%rbp), %rax
shrq $0x8, %rax
movabsq $0xff00ff00ff00ff, %rcx # imm = 0xFF00FF00FF00FF
andq -0x40(%rbp), %rcx
shlq $0x8, %rcx
orq %rcx, %rax
movq %rax, -0x40(%rbp)
movabsq $-0xffff00010000, %rdx  # imm = 0xFFFF0000FFFF0000
andq -0x40(%rbp), %rdx
shrq $0x10, %rdx
movabsq $0xffff0000ffff, %rax   # imm = 0xFFFF0000FFFF
andq -0x40(%rbp), %rax
shlq $0x10, %rax
orq %rax, %rdx
movq -0x8(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq %rdx, 0x50(%rax,%rcx,8)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xa9d2
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x50, %rsi
movq -0x8(%rbp), %rdx
callq 0xa2b0
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rcx
addq $0x400, %rcx            # imm = 0x400
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rax
cmpq $0x400, 0x40(%rax)      # imm = 0x400
jae 0xaab7
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x48(%rax)
movq -0x18(%rbp), %rax
subq $0x80, %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
addq $0x80, %rax
movq %rax, -0x10(%rbp)
jmp 0xa9a7
cmpq $0x0, -0x18(%rbp)
jbe 0xab2f
movq -0x8(%rbp), %rdi
addq $0x50, %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x8090
movq -0x18(%rbp), %rcx
shlq $0x3, %rcx
movq -0x8(%rbp), %rax
addq 0x40(%rax), %rcx
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x18(%rbp), %rcx
shlq $0x3, %rcx
cmpq %rcx, %rax
jae 0xab2d
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x48(%rax)
jmp 0xab2f
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)