// File: /workspace/github_c_makefile_star2_O1/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, %r12d
testq %rdx, %rdx
je 0x9014
movq %r15, %rdi
movq %rdx, %rsi
callq 0x88f0
testl %eax, %eax
js 0x9142
movq 0xc8(%r15), %r13
shrq $0x3, %r13
testq %r14, %r14
movq %rbx, -0x58(%rbp)
movq %r14, -0x40(%rbp)
je 0x903e
movq %r14, %rdi
callq 0x1070
addq $0x9, %rax
shrq $0x3, %rax
jmp 0x9043
movl $0x1, %eax
movq %r15, -0x30(%rbp)
movq %rax, -0x50(%rbp)
addq %r13, %rax
decq %rax
xorl %edx, %edx
divq %r13
movq %rax, %r14
leaq -0xe8(%rbp), %rbx
movl %r12d, %edi
movq %rbx, %rsi
callq 0x1130
movq 0x38(%rbx), %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 %r13
movq %r13, %rsi
subq %rdx, %rsi
xorl %edi, %edi
testq %rdx, %rdx
setne %dil
cmoveq %rdx, %rsi
addq %rcx, %rsi
addq %rax, %rdi
incq %r14
movq %r14, %rbx
imulq %r13, %rbx
cmpq %r14, %rdi
cmovaeq %rsi, %rbx
cmpq $0x4001, %rbx           # imm = 0x4001
jb 0x90db
movl $0x4000, %ecx           # imm = 0x4000
movl $0x4000, %eax           # imm = 0x4000
xorl %edx, %edx
divq %r13
movq %rdx, %rbx
subq %rcx, %rbx
negq %rbx
cmpq %rcx, %rdx
cmoveq %r13, %rbx
movq %r13, -0x48(%rbp)
movq %rsp, %r14
leaq 0xf(%rbx), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
xorl %r15d, %r15d
movq %r14, -0x38(%rbp)
leaq (%r14,%r15), %r13
movq %rbx, %r14
subq %r15, %r14
movl %r12d, %edi
movq %r13, %rsi
movq %r14, %rdx
callq 0x10b0
testq %rax, %rax
jg 0x9122
je 0x9149
callq 0x1050
cmpl $0x4, (%rax)
je 0x9101
jmp 0x9142
addq %rax, %r15
cmpq %rbx, %r15
movq -0x38(%rbp), %r14
jne 0x90f7
movq -0x30(%rbp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4c3c
xorl %r15d, %r15d
jmp 0x90f7
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x91b0
cmpq %r14, -0x50(%rbp)
jbe 0x918e
movq %r15, %rax
xorl %edx, %edx
divq -0x48(%rbp)
movq %rdx, %rbx
subq %rdx, %r15
movq -0x30(%rbp), %r14
movq %r14, %rdi
movq -0x38(%rbp), %r12
movq %r12, %rsi
movq %r15, %rdx
callq 0x4c3c
addq %r12, %r15
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x10d0
movq %rbx, %r15
movq -0x40(%rbp), %r8
jmp 0x919a
movq -0x40(%rbp), %r8
movq -0x30(%rbp), %r14
movq -0x38(%rbp), %r12
xorl %ebx, %ebx
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
movq -0x58(%rbp), %r9
callq 0x1150
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)