// File: /workspace/github_c_makefile_star2_O1/maandree[P]libkeccak/hmac/libkeccak_hmac_digest.c
// Function: libkeccak_hmac_digest
int
libkeccak_hmac_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);
	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_digest(&state->sponge, msg, msglen, bits, suffix, tmp) < 0)
			goto fail;
		goto stage_2;
	}

	if (libkeccak_hmac_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_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_update(state, NULL, 0) < 0)
		goto fail;

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

	if (libkeccak_hmac_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_digest(&state->sponge, leftover, newlen >> 3, newlen & 7, suffix, tmp) < 0)
		goto fail;

stage_3:
	my_explicit_bzero(tmp, (size_t)((state->sponge.n + 7) >> 3) * sizeof(char));
	free(tmp);
	return 0;
fail:
	my_explicit_bzero(tmp, (size_t)((state->sponge.n + 7) >> 3) * sizeof(char));
	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, %r12
movq %rdx, %rbp
movq %rsi, %r15
movq %rdi, %r14
movq 0xf0(%rdi), %r13
leaq 0x7(%r13), %rdi
sarq $0x3, %rdi
callq 0x2160
testq %rax, %rax
je 0x6c1b
movq %rax, %rbx
leaq 0x18(%r14), %rdi
testb $0x7, 0x10(%r14)
movq %rdi, 0x18(%rsp)
je 0x6c25
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x2070
testl %eax, %eax
js 0x6d0a
movb 0x148(%r14), %al
movb %al, 0xe(%rsp)
testq %r12, %r12
je 0x6bfb
movzbl (%r15,%rbp), %edx
movb 0x10(%r14), %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(%r14), %rcx
andl $0x7, %ecx
addq %r12, %rcx
movq %rcx, %rdx
shrq $0x3, %rdx
andl $0x7, %ecx
leaq 0xe(%rsp), %rsi
movq 0x18(%rsp), %rdi
jmp 0x6c2e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x6d34
movq %r15, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq 0x10(%rsp), %r8
movq %rbx, %r9
callq 0x2140
testl %eax, %eax
js 0x6d0a
movq (%r14), %rax
movq 0xf0(%r14), %r12
movq %rax, 0x8(%r14)
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2070
testl %eax, %eax
js 0x6d0a
sarq $0x3, %r13
andl $0x7, %r12d
testb $0x7, 0x10(%r14)
je 0x6ce5
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x2070
testl %eax, %eax
js 0x6d0a
movb 0x148(%r14), %al
movb %al, 0xe(%rsp)
testq %r12, %r12
je 0x6cbf
movzbl (%rbx,%r13), %edx
movb 0x10(%r14), %cl
andb $0x7, %cl
movl %edx, %esi
sarb %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(%r14), %rcx
andl $0x7, %ecx
addq %r12, %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 0x6cfd
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r12, %rcx
movq 0x10(%rsp), %r8
movq 0x20(%rsp), %r9
callq 0x2140
testl %eax, %eax
js 0x6d0a
xorl %ebp, %ebp
jmp 0x6d0f
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0xf0(%r14), %rdx
addq $0x7, %rdx
sarq $0x3, %rdx
movq 0x43e3(%rip), %rax      # 0xb108
movq %rbx, %rdi
xorl %esi, %esi
callq *%rax
movq %rbx, %rdi
callq 0x2040
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)