// File: /workspace/github_c_makefile_star2_O0/Seclabs-htc[P]slip39/trezor-crypto/sha2.c
// Function: sha256_Final
void sha256_Final(SHA256_CTX* context, sha2_byte digest[]) {
	unsigned int	usedspace;

	/* If no digest buffer is passed, we don't bother doing this: */
	if (digest != (sha2_byte*)0) {
		usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
		/* Begin padding with a 1 bit: */
		((uint8_t*)context->buffer)[usedspace++] = 0x80;
		
		if (usedspace > SHA256_SHORT_BLOCK_LENGTH) {
			memzero(((uint8_t*)context->buffer) + usedspace, SHA256_BLOCK_LENGTH - usedspace);

#if BYTE_ORDER == LITTLE_ENDIAN
			/* Convert TO host byte order */
			for (int j = 0; j < 16; j++) {
				REVERSE32(context->buffer[j],context->buffer[j]);
			}
#endif
			/* Do second-to-last transform: */
			sha256_Transform(context->state, context->buffer, context->state);
			
			/* And prepare the last transform: */
			usedspace = 0;
		}
		/* Set-up for the last transform: */
		memzero(((uint8_t*)context->buffer) + usedspace, SHA256_SHORT_BLOCK_LENGTH - usedspace);

#if BYTE_ORDER == LITTLE_ENDIAN
		/* Convert TO host byte order */
		for (int j = 0; j < 14; j++) {
			REVERSE32(context->buffer[j],context->buffer[j]);
		}
#endif
		/* Set the bit count: */
		context->buffer[14] = context->bitcount >> 32;
		context->buffer[15] = context->bitcount & 0xffffffff;

		/* Final transform: */
		sha256_Transform(context->state, context->buffer, context->state);

#if BYTE_ORDER == LITTLE_ENDIAN
		/* Convert FROM host byte order */
		for (int j = 0; j < 8; j++) {
			REVERSE32(context->state[j],context->state[j]);
		}
#endif
		MEMCPY_BCOPY(digest, context->state, SHA256_DIGEST_LENGTH);
	}

	/* Clean up state data: */
	memzero(context, sizeof(SHA256_CTX));
	usedspace = 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0xa0b2
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
shrq $0x3, %rax
andq $0x3f, %rax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rax
movl -0x14(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x14(%rbp)
movl %ecx, %ecx
movb $-0x80, 0x28(%rax,%rcx)
cmpl $0x38, -0x14(%rbp)
jbe 0x9f54
movq -0x8(%rbp), %rdi
addq $0x28, %rdi
movl -0x14(%rbp), %eax
addq %rax, %rdi
movl $0x40, %eax
subl -0x14(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
callq 0x81c0
movl $0x0, -0x18(%rbp)
cmpl $0x10, -0x18(%rbp)
jge 0x9f38
movq -0x8(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl 0x28(%rax,%rcx,4), %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
shrl $0x10, %eax
movl -0x1c(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movabsq $0xff00ff00, %rcx       # imm = 0xFF00FF00
andq %rcx, %rax
shrq $0x8, %rax
movl -0x1c(%rbp), %ecx
andq $0xff00ff, %rcx         # imm = 0xFF00FF
shlq $0x8, %rcx
orq %rcx, %rax
movl %eax, %edx
movq -0x8(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, 0x28(%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x9ed4
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x28, %rsi
movq -0x8(%rbp), %rdx
callq 0x97c0
movl $0x0, -0x14(%rbp)
movq -0x8(%rbp), %rdi
addq $0x28, %rdi
movl -0x14(%rbp), %eax
addq %rax, %rdi
movl $0x38, %eax
subl -0x14(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
callq 0x81c0
movl $0x0, -0x20(%rbp)
cmpl $0xe, -0x20(%rbp)
jge 0x9fde
movq -0x8(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl 0x28(%rax,%rcx,4), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
shrl $0x10, %eax
movl -0x24(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movabsq $0xff00ff00, %rcx       # imm = 0xFF00FF00
andq %rcx, %rax
shrq $0x8, %rax
movl -0x24(%rbp), %ecx
andq $0xff00ff, %rcx         # imm = 0xFF00FF
shlq $0x8, %rcx
orq %rcx, %rax
movl %eax, %edx
movq -0x8(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl %edx, 0x28(%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x9f7a
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
shrq $0x20, %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x60(%rax)
movq -0x8(%rbp), %rcx
movabsq $0xffffffff, %rax       # imm = 0xFFFFFFFF
andq 0x20(%rcx), %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x64(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x28, %rsi
movq -0x8(%rbp), %rdx
callq 0x97c0
movl $0x0, -0x28(%rbp)
cmpl $0x8, -0x28(%rbp)
jge 0xa08c
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shrl $0x10, %eax
movl -0x2c(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movabsq $0xff00ff00, %rcx       # imm = 0xFF00FF00
andq %rcx, %rax
shrq $0x8, %rax
movl -0x2c(%rbp), %ecx
andq $0xff00ff, %rcx         # imm = 0xFF00FF
shlq $0x8, %rcx
orq %rcx, %rax
movl %eax, %edx
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xa02a
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rax)
movq 0x18(%rcx), %rcx
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movl $0x68, %esi
callq 0x81c0
movl $0x0, -0x14(%rbp)
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)