// File: /workspace/github_c_makefile_star2_O0/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
subq $0x110, %rsp            # imm = 0x110
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq $0x1000, -0xd0(%rbp) # imm = 0x1000
cmpq $0x0, -0x18(%rbp)
je 0xbfbe
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xafd0
cmpl $0x0, %eax
jge 0xbfbe
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xc2b5
movq -0x10(%rbp), %rdi
callq 0xc390
movq %rax, -0xe0(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xbfe7
movq -0x20(%rbp), %rdi
callq 0x1050
movq %rax, -0x108(%rbp)
jmp 0xbff2
xorl %eax, %eax
movq %rax, -0x108(%rbp)
jmp 0xbff2
movq -0x108(%rbp), %rax
addq $0x2, %rax
addq $0x7, %rax
shrq $0x3, %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq -0xe0(%rbp), %rcx
subq $0x1, %rcx
addq %rcx, %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0xe0(%rbp)
movq %rax, -0xf0(%rbp)
movl -0x8(%rbp), %edi
leaq -0xc8(%rbp), %rsi
callq 0x1110
cmpl $0x0, %eax
jne 0xc061
cmpq $0x0, -0x90(%rbp)
jle 0xc05f
movq -0x90(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0xc061
movq -0xd0(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0xe0(%rbp)
movq %rax, -0xf8(%rbp)
movq -0xd0(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0xe0(%rbp)
movq %rdx, -0x100(%rbp)
cmpq $0x0, -0x100(%rbp)
je 0xc0dc
movq -0x100(%rbp), %rcx
movq -0xd0(%rbp), %rax
subq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0xe0(%rbp), %rax
addq -0xd0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xf8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
movq -0xf0(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jae 0xc10d
movq -0xf0(%rbp), %rax
addq $0x1, %rax
imulq -0xe0(%rbp), %rax
movq %rax, -0xd0(%rbp)
cmpq $0x4000, -0xd0(%rbp) # imm = 0x4000
jbe 0xc162
movq $0x4000, -0xd0(%rbp) # imm = 0x4000
movq -0xd0(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0xe0(%rbp)
movq -0xd0(%rbp), %rax
subq %rdx, %rax
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0xd0(%rbp)
jne 0xc160
movq -0xe0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0xc162
movq -0xd0(%rbp), %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rax
subq %rcx, %rax
movq %rax, %rsp
movq %rax, -0xd8(%rbp)
movq $0x0, -0x38(%rbp)
movl -0x8(%rbp), %edi
movq -0xd8(%rbp), %rsi
addq -0x38(%rbp), %rsi
movq -0xd0(%rbp), %rdx
subq -0x38(%rbp), %rdx
callq 0x1090
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jg 0xc1cc
cmpq $0x0, -0x30(%rbp)
jne 0xc1bb
jmp 0xc206
callq 0x1040
cmpl $0x4, (%rax)
jne 0xc1c7
jmp 0xc189
jmp 0xc2ae
movq -0x30(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jne 0xc204
movq -0x10(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movq -0xd0(%rbp), %rdx
callq 0x54e0
movq $0x0, -0x38(%rbp)
jmp 0xc189
movq -0xe8(%rbp), %rax
movq -0xd0(%rbp), %rcx
subq -0x38(%rbp), %rcx
cmpq %rcx, %rax
jbe 0xc285
movq -0x38(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0xe0(%rbp)
movq %rdx, -0x100(%rbp)
movq -0x10(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movq -0x38(%rbp), %rdx
subq -0x100(%rbp), %rdx
callq 0x54e0
movq -0xd8(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movq -0x38(%rbp), %rax
subq -0x100(%rbp), %rax
addq %rax, %rsi
movq -0x100(%rbp), %rdx
shlq $0x0, %rdx
callq 0x10b0
movq -0x100(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq -0xd8(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x20(%rbp), %r8
movq -0x28(%rbp), %r9
xorl %eax, %eax
movl %eax, %ecx
callq 0x6b20
movl $0x0, -0x4(%rbp)
jmp 0xc2b5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax)