// File: /workspace/github_c_makefile_star2_O0/Seclabs-htc[P]slip39/trezor-crypto/sha2.c
// Function: sha512_Last
static void sha512_Last(SHA512_CTX* context) {
	unsigned int	usedspace;

	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
	/* Begin padding with a 1 bit: */
	((uint8_t*)context->buffer)[usedspace++] = 0x80;
	
	if (usedspace > SHA512_SHORT_BLOCK_LENGTH) {
		memzero(((uint8_t*)context->buffer) + usedspace, SHA512_BLOCK_LENGTH - usedspace);

#if BYTE_ORDER == LITTLE_ENDIAN
		/* Convert TO host byte order */
		for (int j = 0; j < 16; j++) {
			REVERSE64(context->buffer[j],context->buffer[j]);
		}
#endif
		/* Do second-to-last transform: */
		sha512_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, SHA512_SHORT_BLOCK_LENGTH - usedspace);

#if BYTE_ORDER == LITTLE_ENDIAN
	/* Convert TO host byte order */
	for (int j = 0; j < 14; j++) {
		REVERSE64(context->buffer[j],context->buffer[j]);
	}
#endif
	/* Store the length of input data (in bits): */
	context->buffer[14] = context->bitcount[1];
	context->buffer[15] = context->bitcount[0];

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
shrq $0x3, %rax
andq $0x7f, %rax
movl %eax, -0xc(%rbp)
movq -0x8(%rbp), %rax
movl -0xc(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0xc(%rbp)
movl %ecx, %ecx
movb $-0x80, 0x50(%rax,%rcx)
cmpl $0x70, -0xc(%rbp)
jbe 0xad60
movq -0x8(%rbp), %rdi
addq $0x50, %rdi
movl -0xc(%rbp), %eax
addq %rax, %rdi
movl $0x80, %eax
subl -0xc(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
callq 0x81c0
movl $0x0, -0x10(%rbp)
cmpl $0x10, -0x10(%rbp)
jge 0xad44
movq -0x8(%rbp), %rax
movslq -0x10(%rbp), %rcx
movq 0x50(%rax,%rcx,8), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
shrq $0x20, %rax
movq -0x18(%rbp), %rcx
shlq $0x20, %rcx
orq %rcx, %rax
movq %rax, -0x18(%rbp)
movabsq $-0xff00ff00ff0100, %rax # imm = 0xFF00FF00FF00FF00
andq -0x18(%rbp), %rax
shrq $0x8, %rax
movabsq $0xff00ff00ff00ff, %rcx # imm = 0xFF00FF00FF00FF
andq -0x18(%rbp), %rcx
shlq $0x8, %rcx
orq %rcx, %rax
movq %rax, -0x18(%rbp)
movabsq $-0xffff00010000, %rdx  # imm = 0xFFFF0000FFFF0000
andq -0x18(%rbp), %rdx
shrq $0x10, %rdx
movabsq $0xffff0000ffff, %rax   # imm = 0xFFFF0000FFFF
andq -0x18(%rbp), %rax
shlq $0x10, %rax
orq %rax, %rdx
movq -0x8(%rbp), %rax
movslq -0x10(%rbp), %rcx
movq %rdx, 0x50(%rax,%rcx,8)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xaca5
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x50, %rsi
movq -0x8(%rbp), %rdx
callq 0xa2b0
movl $0x0, -0xc(%rbp)
movq -0x8(%rbp), %rdi
addq $0x50, %rdi
movl -0xc(%rbp), %eax
addq %rax, %rdi
movl $0x70, %eax
subl -0xc(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
callq 0x81c0
movl $0x0, -0x1c(%rbp)
cmpl $0xe, -0x1c(%rbp)
jge 0xae25
movq -0x8(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq 0x50(%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
shrq $0x20, %rax
movq -0x28(%rbp), %rcx
shlq $0x20, %rcx
orq %rcx, %rax
movq %rax, -0x28(%rbp)
movabsq $-0xff00ff00ff0100, %rax # imm = 0xFF00FF00FF00FF00
andq -0x28(%rbp), %rax
shrq $0x8, %rax
movabsq $0xff00ff00ff00ff, %rcx # imm = 0xFF00FF00FF00FF
andq -0x28(%rbp), %rcx
shlq $0x8, %rcx
orq %rcx, %rax
movq %rax, -0x28(%rbp)
movabsq $-0xffff00010000, %rdx  # imm = 0xFFFF0000FFFF0000
andq -0x28(%rbp), %rdx
shrq $0x10, %rdx
movabsq $0xffff0000ffff, %rax   # imm = 0xFFFF0000FFFF
andq -0x28(%rbp), %rax
shlq $0x10, %rax
orq %rax, %rdx
movq -0x8(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq %rdx, 0x50(%rax,%rcx,8)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xad86
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0xc0(%rax)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0xc8(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x50, %rsi
movq -0x8(%rbp), %rdx
callq 0xa2b0
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)