// File: /workspace/github_cpp_makefile_star2_O3/BU-DiSC[P]BF-Shared-Hashing/hash/sha-256.c
// Function: calc_sha_256(unsigned char*, void const*, unsigned long)
void calc_sha_256(uint8_t hash[32], const void * input, size_t len)
{
	/*
	 * Note 1: All integers (expect indexes) are 32-bit unsigned integers and addition is calculated modulo 2^32.
	 * Note 2: For each round, there is one round constant k[i] and one entry in the message schedule array w[i], 0 = i = 63
	 * Note 3: The compression function uses 8 working variables, a through h
	 * Note 4: Big-endian convention is used when expressing the constants in this pseudocode,
	 *     and when parsing message block data from bytes to words, for example,
	 *     the first word of the input message "abc" after padding is 0x61626380
	 */

	/*
	 * Initialize hash values:
	 * (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
	 */
	uint32_t h[] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
	unsigned i, j;

	/* 512-bit chunks is what we will operate on. */
	uint8_t chunk[64];

	struct buffer_state state;

	init_buf_state(&state, input, len);

	while (calc_chunk(chunk, &state)) {
		uint32_t ah[8];

		const uint8_t *p = chunk;

		/* Initialize working variables to current hash value: */
		for (i = 0; i < 8; i++)
			ah[i] = h[i];

		/* Compression function main loop: */
		for (i = 0; i < 4; i++) {
			/*
			 * The w-array is really w[64], but since we only need
			 * 16 of them at a time, we save stack by calculating
			 * 16 at a time.
			 *
			 * This optimization was not there initially and the
			 * rest of the comments about w[64] are kept in their
			 * initial state.
			 */

			/*
			 * create a 64-entry message schedule array w[0..63] of 32-bit words
			 * (The initial values in w[0..63] don't matter, so many implementations zero them here)
			 * copy chunk into first 16 words w[0..15] of the message schedule array
			 */
			uint32_t w[16];

			for (j = 0; j < 16; j++) {
				if (i == 0) {
					w[j] = (uint32_t) p[0] << 24 | (uint32_t) p[1] << 16 |
						(uint32_t) p[2] << 8 | (uint32_t) p[3];
					p += 4;
				} else {
					/* Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array: */
					const uint32_t s0 = right_rot(w[(j + 1) & 0xf], 7) ^ right_rot(w[(j + 1) & 0xf], 18) ^ (w[(j + 1) & 0xf] >> 3);
					const uint32_t s1 = right_rot(w[(j + 14) & 0xf], 17) ^ right_rot(w[(j + 14) & 0xf], 19) ^ (w[(j + 14) & 0xf] >> 10);
					w[j] = w[j] + s0 + w[(j + 9) & 0xf] + s1;
				}
				const uint32_t s1 = right_rot(ah[4], 6) ^ right_rot(ah[4], 11) ^ right_rot(ah[4], 25);
				const uint32_t ch = (ah[4] & ah[5]) ^ (~ah[4] & ah[6]);
				const uint32_t temp1 = ah[7] + s1 + ch + k[i << 4 | j] + w[j];
				const uint32_t s0 = right_rot(ah[0], 2) ^ right_rot(ah[0], 13) ^ right_rot(ah[0], 22);
				const uint32_t maj = (ah[0] & ah[1]) ^ (ah[0] & ah[2]) ^ (ah[1] & ah[2]);
				const uint32_t temp2 = s0 + maj;

				ah[7] = ah[6];
				ah[6] = ah[5];
				ah[5] = ah[4];
				ah[4] = ah[3] + temp1;
				ah[3] = ah[2];
				ah[2] = ah[1];
				ah[1] = ah[0];
				ah[0] = temp1 + temp2;
			}
		}

		/* Add the compressed chunk to the current hash value: */
		for (i = 0; i < 8; i++)
			h[i] += ah[i];
	}

	/* Produce the final hash value (big-endian): */
	for (i = 0, j = 0; i < 8; i++)
	{
		hash[j++] = (uint8_t) (h[i] >> 24);
		hash[j++] = (uint8_t) (h[i] >> 16);
		hash[j++] = (uint8_t) (h[i] >> 8);
		hash[j++] = (uint8_t) h[i];
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdx, %r14
movq %rsi, %rbx
movaps 0x694e(%rip), %xmm0     # 0x2a460
movaps %xmm0, 0x80(%rsp)
movdqa 0x692e(%rip), %xmm0     # 0x2a450
movdqa %xmm0, 0x70(%rsp)
leal (,%rdx,8), %eax
movl %eax, 0x38(%rsp)
movq %rdx, %rax
shrq $0x5, %rax
movq %rax, 0x58(%rsp)
movq $0x0, 0x40(%rsp)
movq %rdi, 0x60(%rsp)
cmpq $0x40, %r14
jb 0x23ba2
movdqu (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups 0x30(%rbx), %xmm3
movaps %xmm3, 0xc0(%rsp)
movaps %xmm2, 0xb0(%rsp)
movaps %xmm1, 0xa0(%rsp)
movdqa %xmm0, 0x90(%rsp)
addq $0x40, %rbx
movq %rbx, 0x48(%rsp)
addq $-0x40, %r14
movq %r14, 0x30(%rsp)
movb $0x1, %al
movq %rax, 0x28(%rsp)
jmp 0x23c53
leaq 0x90(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x5330
leaq (%rsp,%r14), %r13
addq $0x90, %r13
movl $0x40, %ebp
cmpl $0x0, 0x40(%rsp)
jne 0x23be3
movb $-0x80, (%r13)
incq %r13
movl $0x3f, %ebp
movl $0x1, %eax
movq %rax, 0x40(%rsp)
addq %r14, %rbx
subq %r14, %rbp
cmpq $0x8, %rbp
movq %rbx, 0x48(%rsp)
jb 0x23c36
leaq -0x8(%rbp), %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x5240
leaq -0x2(,%rbp), %rax
addq %r13, %rax
movl 0x38(%rsp), %ecx
movb %cl, 0x1(%rax)
movl $0x7, %ecx
movq 0x58(%rsp), %rdx
movb %dl, -0x7(%rax,%rcx)
shrq $0x8, %rdx
decq %rcx
jne 0x23c1e
movq $0x0, 0x28(%rsp)
jmp 0x23c4a
movq %r13, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x5240
movb $0x1, %al
movq %rax, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movdqa 0x70(%rsp), %xmm0
movaps 0x80(%rsp), %xmm1
movaps %xmm1, 0x10(%rsp)
movdqa %xmm0, (%rsp)
movl 0x10(%rsp), %ebp
movl 0x14(%rsp), %ecx
movl 0x18(%rsp), %r14d
movl 0x1c(%rsp), %edx
movl (%rsp), %r12d
movl 0x4(%rsp), %esi
movl 0x8(%rsp), %r15d
movl 0xc(%rsp), %edi
xorl %r13d, %r13d
leaq 0x90(%rsp), %rax
movq %rax, 0x50(%rsp)
movq %r13, %rax
shlq $0x4, %rax
movq %rax, 0x68(%rsp)
xorl %ebx, %ebx
movl %edx, %r11d
movl %edi, %r10d
movl %r15d, %edi
movl %esi, %r15d
movl %r12d, %esi
movl %r14d, %edx
movl %ecx, %r14d
movl %ebp, %ecx
testq %r13, %r13
je 0x23d68
movl %r10d, 0x3c(%rsp)
leaq 0x1(%rbx), %r10
movl %r10d, %r12d
andl $0xf, %r12d
movl 0xd0(%rsp,%r12,4), %r12d
movl %r12d, %ebp
roll $0x19, %ebp
movl %r12d, %r9d
roll $0xe, %r9d
xorl %ebp, %r9d
leal 0xe(%rbx), %ebp
andl $0xf, %ebp
movl 0xd0(%rsp,%rbp,4), %r8d
movl %edi, %eax
movl %r15d, %edi
movl %edx, %r15d
movl %r14d, %edx
movq %r13, %r14
movl %r8d, %r13d
roll $0xf, %r13d
shrl $0x3, %r12d
movl %r8d, %ebp
roll $0xd, %ebp
xorl %r9d, %r12d
xorl %r13d, %ebp
movq %r14, %r13
movl %edx, %r14d
movl %r15d, %edx
movl %edi, %r15d
movl %eax, %edi
movq %r10, %rax
movl 0x3c(%rsp), %r10d
shrl $0xa, %r8d
xorl %ebp, %r8d
addl 0xd0(%rsp,%rbx,4), %r12d
leal 0x9(%rbx), %r9d
andl $0xf, %r9d
addl 0xd0(%rsp,%r9,4), %r12d
addl %r8d, %r12d
movl %r12d, 0xd0(%rsp,%rbx,4)
jmp 0x23d88
movq 0x50(%rsp), %rax
movl (%rax), %r12d
bswapl %r12d
movl %r12d, 0xd0(%rsp,%rbx,4)
addq $0x4, %rax
movq %rax, 0x50(%rsp)
leaq 0x1(%rbx), %rax
movl %ecx, %r8d
roll $0x1a, %r8d
movl %ecx, %r9d
roll $0x15, %r9d
xorl %r8d, %r9d
movl %ecx, %r8d
roll $0x7, %r8d
xorl %r9d, %r8d
movl %r14d, %r9d
andl %ecx, %r9d
addl %r11d, %r9d
movl %ecx, %r11d
notl %r11d
andl %edx, %r11d
addl %r9d, %r11d
addl %r8d, %r11d
addq 0x68(%rsp), %rbx
leaq 0x66a9(%rip), %r8       # 0x2a470
addl (%r8,%rbx,4), %r11d
movl %esi, %r8d
roll $0x1e, %r8d
movl %esi, %r9d
roll $0x13, %r9d
addl %r12d, %r11d
movl %esi, %ebx
roll $0xa, %ebx
xorl %r8d, %r9d
xorl %r9d, %ebx
movl %edi, %r8d
xorl %r15d, %r8d
andl %esi, %r8d
movl %edi, %r12d
andl %r15d, %r12d
xorl %r8d, %r12d
addl %ebx, %r12d
movl %edx, 0x1c(%rsp)
movl %r14d, 0x18(%rsp)
movl %ecx, 0x14(%rsp)
movl %r10d, %ebp
addl %r11d, %ebp
movl %ebp, 0x10(%rsp)
movl %edi, 0xc(%rsp)
movl %r15d, 0x8(%rsp)
movl %esi, 0x4(%rsp)
addl %r11d, %r12d
movl %r12d, (%rsp)
movq %rax, %rbx
movl %edx, %r11d
movl %edi, %r10d
cmpq $0x10, %rax
jne 0x23cb1
incq %r13
cmpq $0x4, %r13
jne 0x23c9d
xorl %eax, %eax
movdqa 0x70(%rsp,%rax,4), %xmm0
paddd (%rsp,%rax,4), %xmm0
movdqa %xmm0, 0x70(%rsp,%rax,4)
addq $0x4, %rax
cmpq $0x8, %rax
jne 0x23e49
cmpb $0x0, 0x28(%rsp)
movq 0x60(%rsp), %rdi
movq 0x48(%rsp), %rbx
movq 0x30(%rsp), %r14
jne 0x23b4d
xorl %eax, %eax
movl 0x70(%rsp,%rax,4), %ecx
bswapl %ecx
movl %ecx, (%rdi,%rax,4)
incq %rax
cmpq $0x8, %rax
jne 0x23e80
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq