// File: /workspace/github_c_makefile_star2_O2/maandree[P]libkeccak/util/libkeccak_generalised_sum_fd.c
// Function: libkeccak_generalised_sum_fd
int
libkeccak_generalised_sum_fd(int fd, struct libkeccak_state *restrict state, const struct libkeccak_spec *restrict spec,
                             const char *restrict suffix, void *restrict hashsum)
{
	ssize_t got;
	size_t offset;
#ifndef _WIN32
	struct stat attr;
#endif
	size_t blksize = 4096;
	unsigned char *restrict chunk;
	size_t chunksize, extrasize, extrachunks;
	size_t chunks, chunkmod;

	if (spec && libkeccak_state_initialise(state, spec) < 0)
		return -1;

	chunksize = libkeccak_zerocopy_chunksize(state);
	extrasize = ((suffix ? strlen(suffix) : 0) + 2 + 7) >> 3;
	extrachunks = (extrasize + (chunksize - 1)) / chunksize;

#ifndef _WIN32
	if (fstat(fd, &attr) == 0)
		if (attr.st_blksize > 0)
			blksize = (size_t)attr.st_blksize;
#endif

	chunks = blksize / chunksize;
	chunkmod = blksize % chunksize;
	if (chunkmod) {
		blksize -= chunkmod;
		blksize += chunksize;
		chunks += 1;
	}
	if (chunks < extrachunks + 1)
		blksize = (extrachunks + 1) * chunksize;

#if ALLOCA_LIMIT > 0
	if (blksize > (size_t)ALLOCA_LIMIT) {
		blksize = (size_t)ALLOCA_LIMIT;
		blksize -= blksize % chunksize;
		if (!blksize)
			blksize = chunksize;
	}
# if defined(__clang__)
	/* We are using a limit so it's just like declaring an array
	 * in a function, except we might use less of the stack. */
#  pragma clang diagnostic push
#  pragma clang diagnostic ignored "-Walloca"
# endif
	chunk = alloca(blksize);
# if defined(__clang__)
#  pragma clang diagnostic pop
# endif
#else
	chunk = malloc(blksize);
	if (!chunk)
		return -1;
#endif

	offset = 0;
	for (;;) {
		got = read(fd, &chunk[offset], blksize - offset);
		if (got <= 0) {
			if (!got)
				break;
			if (errno == EINTR)
				continue;
			goto fail;
		}
		offset += (size_t)got;
		if (offset == blksize) {
			libkeccak_zerocopy_update(state, chunk, blksize);
			offset = 0;
		}
	}

	if (extrasize > blksize - offset) {
		chunkmod = offset % chunksize;
		libkeccak_zerocopy_update(state, chunk, offset - chunkmod);
		__builtin_memcpy(chunk, &chunk[offset - chunkmod], chunkmod * sizeof(char));
		offset = chunkmod;
	}

	libkeccak_zerocopy_digest(state, chunk, offset, 0, suffix, hashsum);
	return 0;

fail:
#if ALLOCA_LIMIT <= 0
	free(chunk);
#endif
	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r8, %rbx
movq %rcx, %r14
movq %rsi, %r15
movl %edi, %r13d
pushq $-0x1
popq %r12
testq %rdx, %rdx
je 0x7fdc
movq %r15, %rdi
movq %rdx, %rsi
callq 0x7948
testl %eax, %eax
js 0x8179
movq 0xc8(%r15), %r12
shrq $0x3, %r12
testq %r14, %r14
movq %rbx, -0x48(%rbp)
movq %r14, -0x40(%rbp)
je 0x8006
movq %r14, %rdi
callq 0x1070
addq $0x9, %rax
shrq $0x3, %rax
jmp 0x8009
pushq $0x1
popq %rax
movq %r15, -0x30(%rbp)
movq %rax, -0x58(%rbp)
addq %r12, %rax
decq %rax
xorl %edx, %edx
divq %r12
movq %rax, %rbx
leaq -0xe8(%rbp), %r14
movl %r13d, %edi
movq %r14, %rsi
callq 0x1130
movq 0x38(%r14), %rcx
testq %rcx, %rcx
movl $0x1000, %edx           # imm = 0x1000
cmovleq %rdx, %rcx
testl %eax, %eax
cmovneq %rdx, %rcx
movq %rcx, %rax
xorl %edx, %edx
divq %r12
movq %r12, %rsi
subq %rdx, %rsi
xorl %edi, %edi
testq %rdx, %rdx
setne %dil
cmoveq %rdx, %rsi
addq %rcx, %rsi
addq %rax, %rdi
incq %rbx
movq %rbx, %r14
imulq %r12, %r14
cmpq %rbx, %rdi
cmovaeq %rsi, %r14
cmpq $0x4001, %r14           # imm = 0x4001
jb 0x80a1
movl $0x4000, %ecx           # imm = 0x4000
movl $0x4000, %eax           # imm = 0x4000
xorl %edx, %edx
divq %r12
movq %rdx, %r14
subq %rcx, %r14
negq %r14
cmpq %rcx, %rdx
cmoveq %r12, %r14
movq %r12, -0x50(%rbp)
movq %rsp, %rbx
leaq 0xf(%r14), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
xorl %r15d, %r15d
movq %rbx, -0x38(%rbp)
addq %r15, %rbx
movq %r14, %r12
subq %r15, %r12
movl %r13d, %edi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x10b0
testq %rax, %rax
jg 0x80e7
je 0x8107
callq 0x1050
cmpl $0x4, (%rax)
je 0x80c6
jmp 0x8150
addq %rax, %r15
cmpq %r14, %r15
movq -0x38(%rbp), %rbx
jne 0x80bd
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x4864
xorl %r15d, %r15d
jmp 0x80bd
cmpq %r12, -0x58(%rbp)
jbe 0x8156
movq %r15, %rax
xorl %edx, %edx
divq -0x50(%rbp)
movq %rdx, %rbx
subq %rdx, %r15
movq -0x30(%rbp), %r14
movq %r14, %rdi
movq -0x38(%rbp), %r13
movq %r13, %rsi
movq %r15, %rdx
callq 0x4864
addq %r13, %r15
movq %r13, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x10d0
movq %rbx, %r15
movq -0x48(%rbp), %r9
movq -0x40(%rbp), %r8
jmp 0x8166
pushq $-0x1
popq %r12
jmp 0x8179
movq -0x48(%rbp), %r9
movq -0x40(%rbp), %r8
movq -0x30(%rbp), %r14
movq -0x38(%rbp), %r13
xorl %r12d, %r12d
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x5770
movl %r12d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop