// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rdi, 0x50(%rsp)
movaps 0x5bb5(%rip), %xmm0     # 0x23410
movaps %xmm0, 0x80(%rsp)
movaps 0x5b96(%rip), %xmm0     # 0x23400
movaps %xmm0, 0x70(%rsp)
leal (,%rdx,8), %eax
movl %eax, 0x30(%rsp)
movq %rdx, %rax
shrq $0x5, %rax
movq %rax, 0x48(%rsp)
movq $0x0, 0x38(%rsp)
xorl %eax, %eax
testl %eax, %eax
jne 0x1dba4
cmpq $0x40, %r14
jb 0x1d8e4
movups (%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)
movaps %xmm0, 0x90(%rsp)
addq $0x40, %rbx
addq $-0x40, %r14
movq $0x0, 0x28(%rsp)
jmp 0x1d988
leaq 0x90(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6350
leaq (%rsp,%r14), %r13
addq $0x90, %r13
cmpl $0x0, 0x38(%rsp)
je 0x1d90d
pushq $0x40
jmp 0x1d91f
movb $-0x80, (%r13)
incq %r13
pushq $0x1
popq %rax
movq %rax, 0x38(%rsp)
pushq $0x3f
popq %rbp
addq %r14, %rbx
subq %r14, %rbp
cmpq $0x8, %rbp
jb 0x1d965
leaq -0x8(%rbp), %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x6260
leaq -0x8(,%rbp), %rax
addq %r13, %rax
movl 0x30(%rsp), %ecx
movb %cl, 0x7(%rax)
movq 0x48(%rsp), %rcx
pushq $0x6
popq %rdx
testl %edx, %edx
js 0x1d97d
movl %edx, %esi
movb %cl, (%rax,%rsi)
shrq $0x8, %rcx
decl %edx
jmp 0x1d954
movq $0x0, 0x28(%rsp)
movq %r13, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x6260
jmp 0x1d985
pushq $0x1
popq %rax
movq %rax, 0x28(%rsp)
xorl %r14d, %r14d
xorl %eax, %eax
cmpq $0x8, %rax
je 0x1d99c
movl 0x70(%rsp,%rax,4), %ecx
movl %ecx, (%rsp,%rax,4)
incq %rax
jmp 0x1d98a
movq %r14, 0x58(%rsp)
movq %rbx, 0x60(%rsp)
movl 0x10(%rsp), %r8d
movl 0x14(%rsp), %ebp
movl 0x18(%rsp), %ecx
movl 0x1c(%rsp), %r10d
movl (%rsp), %r15d
movl 0x4(%rsp), %edx
movl 0x8(%rsp), %edi
movl 0xc(%rsp), %r11d
xorl %r13d, %r13d
leaq 0x90(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpq $0x4, %r13
je 0x1db7c
movq %r13, %rax
shlq $0x4, %rax
movq %rax, 0x68(%rsp)
xorl %ebx, %ebx
cmpq $0x10, %rbx
je 0x1db74
testq %r13, %r13
je 0x1da97
leaq 0x1(%rbx), %r14
movl %r14d, %eax
andl $0xf, %eax
movl 0xd0(%rsp,%rax,4), %r12d
movl %r12d, %eax
roll $0x19, %eax
movl %r12d, %r9d
roll $0xe, %r9d
xorl %eax, %r9d
leal 0xe(%rbx), %eax
andl $0xf, %eax
movl %r10d, 0x34(%rsp)
movl 0xd0(%rsp,%rax,4), %r10d
movl %edx, %esi
movl %ecx, %edx
movl %ebp, %ecx
movl %r11d, %ebp
movq %r13, %r11
movl %r10d, %r13d
roll $0xf, %r13d
shrl $0x3, %r12d
movl %r10d, %eax
roll $0xd, %eax
xorl %r9d, %r12d
xorl %r13d, %eax
movq %r11, %r13
movl %ebp, %r11d
movl %ecx, %ebp
movl %edx, %ecx
movl %esi, %edx
shrl $0xa, %r10d
xorl %eax, %r10d
addl 0xd0(%rsp,%rbx,4), %r12d
leal 0x9(%rbx), %eax
andl $0xf, %eax
addl 0xd0(%rsp,%rax,4), %r12d
addl %r10d, %r12d
movl 0x34(%rsp), %r10d
movl %r12d, 0xd0(%rsp,%rbx,4)
jmp 0x1dab7
movq 0x40(%rsp), %rax
movl (%rax), %r12d
bswapl %r12d
movl %r12d, 0xd0(%rsp,%rbx,4)
addq $0x4, %rax
movq %rax, 0x40(%rsp)
leaq 0x1(%rbx), %r14
movl %r8d, %eax
movl %r8d, %r9d
roll $0x1a, %r9d
roll $0x15, %eax
xorl %r9d, %eax
movl %r8d, %r9d
roll $0x7, %r9d
xorl %eax, %r9d
movl %ebp, %eax
andl %r8d, %eax
addl %r10d, %eax
movl %r8d, %r10d
notl %r10d
andl %ecx, %r10d
addl %r10d, %eax
orq 0x68(%rsp), %rbx
addl %r9d, %eax
leaq 0x592c(%rip), %r9       # 0x23420
addl (%r9,%rbx,4), %eax
addl %r12d, %eax
movl %r15d, %r9d
movl %r15d, %r10d
roll $0x1e, %r10d
roll $0x13, %r9d
movl %r15d, %ebx
xorl %r10d, %r9d
movl %edi, %r10d
xorl %edx, %r10d
andl %r15d, %r10d
roll $0xa, %ebx
xorl %r9d, %ebx
movl %edi, %r12d
andl %edx, %r12d
xorl %r10d, %r12d
addl %ebx, %r12d
movl %r11d, %r9d
addl %eax, %r9d
addl %eax, %r12d
movl %ecx, 0x1c(%rsp)
movl %ebp, 0x18(%rsp)
movl %r8d, 0x14(%rsp)
movl %ecx, %r10d
movl %ebp, %ecx
movl %r8d, %ebp
movl %r9d, 0x10(%rsp)
movl %edi, 0xc(%rsp)
movl %edx, 0x8(%rsp)
movl %r15d, 0x4(%rsp)
movl %edi, %r11d
movl %edx, %edi
movl %r15d, %edx
movl %r9d, %r8d
movl %r12d, (%rsp)
movq %r14, %rbx
movl %r12d, %r15d
jmp 0x1d9f1
incq %r13
jmp 0x1d9d9
xorl %eax, %eax
movq 0x60(%rsp), %rbx
movq 0x58(%rsp), %r14
cmpq $0x8, %rax
je 0x1db9a
movl (%rsp,%rax,4), %ecx
addl %ecx, 0x70(%rsp,%rax,4)
incq %rax
jmp 0x1db88
movq 0x28(%rsp), %rax
jmp 0x1d891
xorl %eax, %eax
movq 0x50(%rsp), %rdx
cmpq $0x8, %rax
je 0x1dbbf
movl 0x70(%rsp,%rax,4), %ecx
bswapl %ecx
movl %ecx, (%rdx,%rax,4)
incq %rax
jmp 0x1dbab
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)