// File: BU-DiSC[P]BF-Shared-Hashing/hash/sha-256.c
// Function: calc_chunk(unsigned char*, buffer_state*)
static int calc_chunk(uint8_t chunk[CHUNK_SIZE], struct buffer_state * state)
{
	size_t space_in_chunk;

	if (state->total_len_delivered) {
		return 0;
	}

	if (state->len >= CHUNK_SIZE) {
		memcpy(chunk, state->p, CHUNK_SIZE);
		state->p += CHUNK_SIZE;
		state->len -= CHUNK_SIZE;
		return 1;
	}

	memcpy(chunk, state->p, state->len);
	chunk += state->len;
	space_in_chunk = CHUNK_SIZE - state->len;
	state->p += state->len;
	state->len = 0;

	/* If we are here, space_in_chunk is one at minimum. */
	if (!state->single_one_delivered) {
		*chunk++ = 0x80;
		space_in_chunk -= 1;
		state->single_one_delivered = 1;
	}

	/*
	 * Now:
	 * - either there is enough space left for the total length, and we can conclude,
	 * - or there is too little space left, and we have to pad the rest of this chunk with zeroes.
	 * In the latter case, we will conclude at the next invokation of this function.
	 */
	if (space_in_chunk >= TOTAL_LEN_LEN) {
		const size_t left = space_in_chunk - TOTAL_LEN_LEN;
		size_t len = state->total_len;
		int i;
		memset(chunk, 0x00, left);
		chunk += left;

		/* Storing of len * 8 as a big endian 64-bit without overflow. */
		chunk[7] = (uint8_t) (len << 3);
		len >>= 5;
		for (i = 6; i >= 0; i--) {
			chunk[i] = (uint8_t) len;
			len >>= 8;
		}
		state->total_len_delivered = 1;
	} else {
		memset(chunk, 0x00, space_in_chunk);
	}

	return 1;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpl $0x0, 0x1c(%rax)
je 0x4ccb6
movl $0x0, 0x34(%rsp)
jmp 0x4ce6f
movq 0x20(%rsp), %rax
cmpq $0x40, 0x8(%rax)
jb 0x4cd06
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rax
movq (%rax), %rsi
movl $0x40, %edx
callq 0x6440
movq 0x20(%rsp), %rax
movq (%rax), %rcx
addq $0x40, %rcx
movq %rcx, (%rax)
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rcx
subq $0x40, %rcx
movq %rcx, 0x8(%rax)
movl $0x1, 0x34(%rsp)
jmp 0x4ce6f
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rax
movq (%rax), %rsi
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdx
callq 0x6440
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
addq 0x28(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x20(%rsp), %rcx
movl $0x40, %eax
subq 0x8(%rcx), %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rcx
movq 0x20(%rsp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq 0x20(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0x20(%rsp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x4cda1
movq 0x28(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x28(%rsp)
movb $-0x80, (%rax)
movq 0x18(%rsp), %rax
subq $0x1, %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movl $0x1, 0x18(%rax)
cmpq $0x8, 0x18(%rsp)
jb 0x4ce56
movq 0x18(%rsp), %rax
subq $0x8, %rax
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %rdx
xorl %esi, %esi
callq 0x62e0
movq 0x10(%rsp), %rax
addq 0x28(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x8(%rsp), %rax
shlq $0x3, %rax
movb %al, %cl
movq 0x28(%rsp), %rax
movb %cl, 0x7(%rax)
movq 0x8(%rsp), %rax
shrq $0x5, %rax
movq %rax, 0x8(%rsp)
movl $0x6, 0x4(%rsp)
cmpl $0x0, 0x4(%rsp)
jl 0x4ce48
movq 0x8(%rsp), %rax
movb %al, %dl
movq 0x28(%rsp), %rax
movslq 0x4(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x8(%rsp), %rax
shrq $0x8, %rax
movq %rax, 0x8(%rsp)
movl 0x4(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x4ce12
movq 0x20(%rsp), %rax
movl $0x1, 0x1c(%rax)
jmp 0x4ce67
movq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rdx
xorl %esi, %esi
callq 0x62e0
movl $0x1, 0x34(%rsp)
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq
nopl (%rax,%rax)