// File: /workspace/github_c_makefile_star2_O3/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, %r13
movq 0xf0(%rdi), %r12
leaq 0x7(%r12), %rdi
sarq $0x3, %rdi
callq 0x2160
testq %rax, %rax
je 0x7556
movq %rax, %rbx
leaq 0x18(%r13), %rdi
testb $0x7, 0x10(%r13)
movq %rdi, 0x18(%rsp)
je 0x7560
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x2030
testl %eax, %eax
js 0x7648
movb 0x148(%r13), %dl
movb %dl, 0xe(%rsp)
movq 0x10(%r13), %rax
testq %r15, %r15
je 0x7537
movzbl (%r14,%rbp), %esi
movl %eax, %ecx
andb $0x7, %cl
movl %esi, %edi
shrb %cl, %dil
orb %dl, %dil
movb %dil, 0xe(%rsp)
movb $0x8, %dl
subb %cl, %dl
movl %edx, %ecx
shll %cl, %esi
movb %sil, 0xf(%rsp)
andl $0x7, %eax
addq %r15, %rax
movq %rax, %rdx
shrq $0x3, %rdx
andl $0x7, %eax
leaq 0xe(%rsp), %rsi
movq 0x18(%rsp), %rdi
movq %rax, %rcx
jmp 0x7569
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x7655
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
movq 0x10(%rsp), %r8
movq %rbx, %r9
callq 0x2050
testl %eax, %eax
js 0x7648
movq (%r13), %rax
movq 0xf0(%r13), %r15
movq %rax, 0x8(%r13)
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2030
testl %eax, %eax
js 0x7648
shrq $0x3, %r12
andl $0x7, %r15d
testb $0x7, 0x10(%r13)
je 0x7623
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2030
testl %eax, %eax
js 0x7648
movb 0x148(%r13), %dl
movb %dl, 0xe(%rsp)
movq 0x10(%r13), %rax
testq %r15, %r15
je 0x75fe
movzbl (%rbx,%r12), %esi
movl %eax, %ecx
andb $0x7, %cl
movl %esi, %edi
shrb %cl, %dil
orb %dl, %dil
movb %dil, 0xe(%rsp)
movb $0x8, %dl
subb %cl, %dl
movl %edx, %ecx
shll %cl, %esi
movb %sil, 0xf(%rsp)
andl $0x7, %eax
addq %r15, %rax
movl %eax, %edx
shrl $0x3, %edx
andl $0x7, %eax
leaq 0xe(%rsp), %rsi
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movq 0x10(%rsp), %r8
movq %rbx, %r9
jmp 0x763b
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq 0x10(%rsp), %r8
movq 0x20(%rsp), %r9
callq 0x2050
testl %eax, %eax
js 0x7648
xorl %ebp, %ebp
jmp 0x764d
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
nop