// File: jeffchan[P]blockchain-parser/SHA256.cpp
// Function: sha256_update(sha256_ctx_t*, void const*, unsigned int)
void sha256_update(sha256_ctx_t * sc, const void *data, uint32_t len)
{
	uint32_t bufferBytesLeft;
	uint32_t bytesToCopy;
	int needBurn = 0;

	if (sc->bufferLength) 
	{
		bufferBytesLeft = 64L - sc->bufferLength;
		bytesToCopy = bufferBytesLeft;
		if (bytesToCopy > len)
		{
			bytesToCopy = len;
		}
		memcpy(&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy);
		sc->totalLength += bytesToCopy * 8L;
		sc->bufferLength += bytesToCopy;
		data = ((uint8_t *) data) + bytesToCopy;
		len -= bytesToCopy;
		if (sc->bufferLength == 64L) 
		{
			SHA256Guts(sc, sc->buffer.words);
			needBurn = 1;
			sc->bufferLength = 0L;
		}
	}

	while (len > 63L) 
	{
		sc->totalLength += 512L;

		SHA256Guts(sc, (const uint32_t *)data);
		needBurn = 1;

		data = ((uint8_t *) data) + 64L;
		len -= 64L;
	}

	if (len) 
	{
		memcpy(&sc->buffer.bytes[sc->bufferLength], data, len);
		sc->totalLength += len * 8L;
		sc->bufferLength += len;
	}

	if (needBurn)
	{
		burnStack(sizeof(uint32_t[74]) + sizeof(uint32_t *[6]) +  sizeof(int));
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x28(%rax)
je 0x2848b
movq -0x8(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, %ecx
movl $0x40, %eax
subq %rcx, %rax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jbe 0x28400
movl -0x14(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
addq $0x2c, %rdi
movq -0x8(%rbp), %rax
movl 0x28(%rax), %eax
addq %rax, %rdi
movq -0x10(%rbp), %rsi
movl -0x1c(%rbp), %eax
movl %eax, %edx
callq 0x20f0
movl -0x1c(%rbp), %eax
movl %eax, %ecx
shlq $0x3, %rcx
movq -0x8(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movl -0x1c(%rbp), %ecx
movq -0x8(%rbp), %rax
addl 0x28(%rax), %ecx
movl %ecx, 0x28(%rax)
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movl -0x1c(%rbp), %ecx
movl -0x14(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rax
movl 0x28(%rax), %eax
cmpq $0x40, %rax
jne 0x28489
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x2c, %rsi
callq 0x28530
movl $0x1, -0x20(%rbp)
movq -0x8(%rbp), %rax
movl $0x0, 0x28(%rax)
jmp 0x2848b
jmp 0x2848d
movl -0x14(%rbp), %eax
cmpq $0x3f, %rax
jle 0x284d3
movq -0x8(%rbp), %rax
movq (%rax), %rcx
addq $0x200, %rcx            # imm = 0x200
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x28530
movl $0x1, -0x20(%rbp)
movq -0x10(%rbp), %rax
addq $0x40, %rax
movq %rax, -0x10(%rbp)
movl -0x14(%rbp), %eax
subq $0x40, %rax
movl %eax, -0x14(%rbp)
jmp 0x2848d
cmpl $0x0, -0x14(%rbp)
je 0x28519
movq -0x8(%rbp), %rdi
addq $0x2c, %rdi
movq -0x8(%rbp), %rax
movl 0x28(%rax), %eax
addq %rax, %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %eax
movl %eax, %edx
callq 0x20f0
movl -0x14(%rbp), %eax
movl %eax, %ecx
shlq $0x3, %rcx
movq -0x8(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movl -0x14(%rbp), %ecx
movq -0x8(%rbp), %rax
addl 0x28(%rax), %ecx
movl %ecx, 0x28(%rax)
cmpl $0x0, -0x20(%rbp)
je 0x28529
movl $0x15c, %edi            # imm = 0x15C
callq 0x2e450
addq $0x20, %rsp
popq %rbp
retq
nop