// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq %rsi, 0x8(%rsp)
movq %rdi, 0x58(%rsp)
movaps 0x639b(%rip), %xmm0     # 0x2a460
movaps %xmm0, 0x80(%rsp)
movdqa 0x637b(%rip), %xmm0     # 0x2a450
movdqa %xmm0, 0x70(%rsp)
leal (,%rdx,8), %eax
movl %eax, 0x38(%rsp)
movq %rdx, %rax
shrq $0x5, %rax
movq %rax, 0x60(%rsp)
movq $0x0, 0x40(%rsp)
xorl %r15d, %r15d
testl %r15d, %r15d
jne 0x24150
cmpq $0x40, %rbx
jb 0x24349
movq 0x8(%rsp), %rax
movdqu (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups 0x30(%rax), %xmm3
movaps %xmm3, 0x100(%rsp)
movaps %xmm2, 0xf0(%rsp)
movaps %xmm1, 0xe0(%rsp)
movdqa %xmm0, 0xd0(%rsp)
addq $0x40, %rax
movq %rax, 0x8(%rsp)
addq $-0x40, %rbx
movl %r15d, %eax
testl %r15d, %r15d
jne 0x243ef
movl %eax, 0x3c(%rsp)
movq %rbx, 0x68(%rsp)
movdqa 0x70(%rsp), %xmm0
movaps 0x80(%rsp), %xmm1
movaps %xmm1, 0x20(%rsp)
movdqa %xmm0, 0x10(%rsp)
movl 0x20(%rsp), %r15d
movl 0x24(%rsp), %ecx
movl 0x28(%rsp), %ebx
movl 0x2c(%rsp), %edx
movl 0x10(%rsp), %r10d
movl 0x14(%rsp), %esi
movl 0x18(%rsp), %r12d
movl 0x1c(%rsp), %r14d
leaq 0x62c7(%rip), %rax      # 0x2a470
movq %rax, 0x50(%rsp)
xorl %r9d, %r9d
leaq 0xd0(%rsp), %rax
movq %rax, 0x48(%rsp)
movl $0xe, %r13d
movl %edx, %ebp
movl %r14d, %edi
movl %r12d, %r14d
movl %esi, %r12d
movl %r10d, %esi
movl %ebx, %edx
movl %ecx, %ebx
movl %r15d, %ecx
testq %r9, %r9
je 0x2424a
leal -0xd(%r13), %r10d
andl $0xf, %r10d
movl 0x90(%rsp,%r10,4), %r10d
movl %r10d, %r15d
roll $0x19, %r15d
movl %r10d, %eax
roll $0xe, %eax
xorl %r15d, %eax
movl %r13d, %r15d
andl $0xf, %r15d
movl 0x90(%rsp,%r15,4), %r11d
movl %r11d, %r8d
roll $0xf, %r8d
shrl $0x3, %r10d
movl %r11d, %r15d
roll $0xd, %r15d
xorl %eax, %r10d
xorl %r8d, %r15d
shrl $0xa, %r11d
xorl %r15d, %r11d
addl 0x58(%rsp,%r13,4), %r10d
leal -0x5(%r13), %eax
andl $0xf, %eax
addl 0x90(%rsp,%rax,4), %r10d
addl %r11d, %r10d
movl %r10d, 0x58(%rsp,%r13,4)
jmp 0x24263
movq 0x48(%rsp), %rax
movl (%rax), %r10d
bswapl %r10d
movl %r10d, 0x58(%rsp,%r13,4)
addq $0x4, %rax
movq %rax, 0x48(%rsp)
movl %ecx, %eax
roll $0x1a, %eax
movl %ecx, %r8d
roll $0x15, %r8d
xorl %eax, %r8d
movl %ecx, %eax
roll $0x7, %eax
xorl %r8d, %eax
movl %ebx, %r8d
andl %ecx, %r8d
addl %ebp, %r8d
movl %ecx, %ebp
notl %ebp
andl %edx, %ebp
addl %r8d, %ebp
addl %eax, %ebp
movq 0x50(%rsp), %rax
addl -0x38(%rax,%r13,4), %ebp
addl 0x58(%rsp,%r13,4), %ebp
movl %esi, %eax
roll $0x1e, %eax
movl %esi, %r8d
roll $0x13, %r8d
movl %esi, %r11d
roll $0xa, %r11d
xorl %eax, %r8d
xorl %r8d, %r11d
movl %r14d, %eax
xorl %r12d, %eax
andl %esi, %eax
movl %r14d, %r10d
andl %r12d, %r10d
xorl %eax, %r10d
addl %r11d, %r10d
movl %edx, 0x2c(%rsp)
movl %ebx, 0x28(%rsp)
movl %ecx, 0x24(%rsp)
movl %edi, %r15d
addl %ebp, %r15d
movl %r15d, 0x20(%rsp)
movl %r14d, 0x1c(%rsp)
movl %r12d, 0x18(%rsp)
movl %esi, 0x14(%rsp)
addl %ebp, %r10d
movl %r10d, 0x10(%rsp)
incq %r13
movl %edx, %ebp
movl %r14d, %edi
cmpq $0x1e, %r13
jne 0x241c9
incq %r9
addq $0x40, 0x50(%rsp)
cmpq $0x4, %r9
jne 0x241be
xorl %ecx, %ecx
movq 0x68(%rsp), %rbx
movdqa 0x70(%rsp,%rcx,4), %xmm0
paddd 0x10(%rsp,%rcx,4), %xmm0
movdqa %xmm0, 0x70(%rsp,%rcx,4)
addq $0x4, %rcx
cmpq $0x8, %rcx
jne 0x24323
movl 0x3c(%rsp), %r15d
jmp 0x240fe
leaq 0xd0(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
callq 0x5330
leaq (%rsp,%rbx), %r13
addq $0xd0, %r13
movl $0x40, %ebp
cmpl $0x0, 0x40(%rsp)
jne 0x2438c
movb $-0x80, (%r13)
incq %r13
movl $0x3f, %ebp
movl $0x1, %eax
movq %rax, 0x40(%rsp)
addq %rbx, 0x8(%rsp)
subq %rbx, %rbp
cmpq $0x8, %rbp
jb 0x243d8
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 0x60(%rsp), %rdx
movb %dl, -0x7(%rax,%rcx)
shrq $0x8, %rdx
decq %rcx
jne 0x243c4
movl $0x1, %eax
jmp 0x243e8
movq %r13, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x5240
movl %r15d, %eax
xorl %ebx, %ebx
jmp 0x24153
xorl %eax, %eax
movq 0x58(%rsp), %rdx
movl 0x70(%rsp,%rax,4), %ecx
bswapl %ecx
movl %ecx, (%rdx,%rax,4)
incq %rax
cmpq $0x8, %rax
jne 0x243f6
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop