// File: /workspace/github_c_makefile_star2_O1/maandree[P]libkeccak/hmac/libkeccak_hmac_fast_digest.c
// Function: libkeccak_hmac_fast_digest
int
libkeccak_hmac_fast_digest(struct libkeccak_hmac_state *restrict state, const void *restrict msg_, size_t msglen,
                           size_t bits, const char *restrict suffix, void *restrict hashsum)
{
	const unsigned char *restrict msg = msg_;
	size_t hashsize = (size_t)state->sponge.n >> 3;
	unsigned char *tmp = malloc((size_t)((state->sponge.n + 7) >> 3) * sizeof(char));
	unsigned char leftover[2];
	size_t newlen;

	if (!tmp)
		return -1;

	if (!(state->key_length & 7)) {
		if (libkeccak_fast_digest(&state->sponge, msg, msglen, bits, suffix, tmp) < 0)
			goto fail;
		goto stage_2;
	}

	if (libkeccak_hmac_fast_update(state, msg, msglen) < 0)
		goto fail;
	leftover[0] = state->leftover;
	if (bits) {
		leftover[0] |= (unsigned char)(msg[msglen] >> (state->key_length & 7));
		leftover[1] = (unsigned char)(msg[msglen] << (8 - (state->key_length & 7)));
	}
	newlen = (state->key_length & 7) + bits;
	if (libkeccak_fast_digest(&state->sponge, leftover, newlen >> 3, newlen & 7, suffix, tmp) < 0)
		goto fail;

stage_2:
	bits = state->sponge.n & 7;
	state->key_ipad = state->key_opad;
	if (libkeccak_hmac_fast_update(state, NULL, 0) < 0)
		goto fail;

	if (!(state->key_length & 7)) {
		if (libkeccak_fast_digest(&state->sponge, tmp, hashsize, bits, suffix, hashsum) < 0)
			goto fail;
		goto stage_3;
	}

	if (libkeccak_hmac_fast_update(state, tmp, hashsize) < 0)
		goto fail;
	leftover[0] = state->leftover;
	if (bits) {
		leftover[0] |= (unsigned char)(tmp[hashsize] >> (state->key_length & 7));
		leftover[1] = (unsigned char)(tmp[hashsize] << (8 - (state->key_length & 7)));
	}
	newlen = (state->key_length & 7) + bits;
	if (libkeccak_fast_digest(&state->sponge, leftover, newlen >> 3, newlen & 7, suffix, tmp) < 0)
		goto fail;

stage_3:
	free(tmp);
	return 0;
fail:
	free(tmp);
	return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, 0x20(%rsp)
movq %r8, 0x10(%rsp)
movq %rcx, %r15
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r12
movq 0xf0(%rdi), %r13
leaq 0x7(%r13), %rdi
sarq $0x3, %rdi
callq 0x2160
testq %rax, %rax
je 0x6ec8
movq %rax, %rbx
leaq 0x18(%r12), %rdi
testb $0x7, 0x10(%r12)
movq %rdi, 0x18(%rsp)
je 0x6ed2
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x2030
testl %eax, %eax
js 0x6fbe
movb 0x148(%r12), %al
movb %al, 0xe(%rsp)
testq %r15, %r15
je 0x6ea7
movzbl (%r14,%rbp), %edx
movb 0x10(%r12), %cl
andb $0x7, %cl
movl %edx, %esi
shrb %cl, %sil
orb %al, %sil
movb %sil, 0xe(%rsp)
movb $0x8, %al
subb %cl, %al
movl %eax, %ecx
shll %cl, %edx
movb %dl, 0xf(%rsp)
movq 0x10(%r12), %rcx
andl $0x7, %ecx
addq %r15, %rcx
movq %rcx, %rdx
shrq $0x3, %rdx
andl $0x7, %ecx
leaq 0xe(%rsp), %rsi
movq 0x18(%rsp), %rdi
jmp 0x6edb
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x6fcb
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
movq 0x10(%rsp), %r8
movq %rbx, %r9
callq 0x2050
testl %eax, %eax
js 0x6fbe
movq (%r12), %rax
movq 0xf0(%r12), %r15
movq %rax, 0x8(%r12)
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2030
testl %eax, %eax
js 0x6fbe
shrq $0x3, %r13
andl $0x7, %r15d
testb $0x7, 0x10(%r12)
je 0x6f99
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x2030
testl %eax, %eax
js 0x6fbe
movb 0x148(%r12), %al
movb %al, 0xe(%rsp)
testq %r15, %r15
je 0x6f72
movzbl (%rbx,%r13), %edx
movb 0x10(%r12), %cl
andb $0x7, %cl
movl %edx, %esi
shrb %cl, %sil
orb %al, %sil
movb %sil, 0xe(%rsp)
movb $0x8, %al
subb %cl, %al
movl %eax, %ecx
shll %cl, %edx
movb %dl, 0xf(%rsp)
movq 0x10(%r12), %rcx
andl $0x7, %ecx
addq %r15, %rcx
movl %ecx, %edx
shrl $0x3, %edx
andl $0x7, %ecx
leaq 0xe(%rsp), %rsi
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r8
movq %rbx, %r9
jmp 0x6fb1
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r15, %rcx
movq 0x10(%rsp), %r8
movq 0x20(%rsp), %r9
callq 0x2050
testl %eax, %eax
js 0x6fbe
xorl %ebp, %ebp
jmp 0x6fc3
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %rbx, %rdi
callq 0x2040
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq