// File: /workspace/github_c_makefile_star2_O3/TurkeyMcMac[P]ts3d/src/read-lines.c
// Function: read_lines
struct string *read_lines(FILE *file, size_t *nlines)
{
	int errnum;
	size_t lines_cap;
	struct string *lines;
	*nlines = 0;
	lines_cap = 16;
	lines = xmalloc(lines_cap * sizeof(*lines));
	char buf[BUFSIZ];
	size_t nread = sizeof(buf);
	char *head = buf + nread;
	errno = 0;
	do {
		size_t line_cap = 16;
		struct string *line = GROWE(lines, *nlines, lines_cap);
		line->len = 0;
		line->text = xmalloc(line_cap);
		for (bool last_one = false; !last_one;) {
			if (head >= buf + nread) {
				nread = fread(buf, 1, sizeof(buf), file);
				last_one = nread < sizeof(buf);
				head = buf;
			}
			char *nl = memchr(head, '\n', nread + buf - head);
			if (nl) {
				last_one = true;
			} else if (head == buf + nread
			        && line->len == 0
				&& feof(file))
			{
				--*nlines;
				free(line->text);
				goto done;
			} else {
				nl = buf + nread;
			}
			char *added = string_grow(line, &line_cap, nl - head);
			memcpy(added, head, nl - head);
			head = nl + 1;
		}
		if (line->len != 0) string_shrink_to_fit(line);
	} while (!errno && !(feof(file) && head >= buf + nread));
done:
	if (errno) {
		for (size_t i = 0; i < *nlines; ++i) {
			free(lines[i].text);
		}
		free(lines);
		lines = NULL;
	}
	errnum = errno;
	fclose(file);
	errno = errnum;
	return lines;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2048, %rsp           # imm = 0x2048
movq %rdi, 0x10(%rsp)
movq %rsi, 0x18(%rsp)
movq $0x0, (%rsi)
leaq 0x38(%rsp), %rax
movq $0x10, (%rax)
movl $0x100, %edi            # imm = 0x100
callq 0xb080
leaq 0x8(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x2040(%rsp), %r14
callq 0x2320
movl $0x0, (%rax)
movl $0x2000, %r13d          # imm = 0x2000
movq %rax, 0x28(%rsp)
movq $0x10, 0x20(%rsp)
movl $0x10, %ecx
leaq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x4c2c
movq %rax, %rbx
movq %rax, 0x30(%rsp)
movq $0x0, (%rax)
movq 0x20(%rsp), %rdi
callq 0xb080
movq %rax, 0x8(%rbx)
leaq (%rsp,%r13), %rax
addq $0x40, %rax
cmpq %rax, %r14
jae 0xa09f
xorl %r15d, %r15d
jmp 0xa0c8
movl $0x1, %esi
movl $0x2000, %edx           # imm = 0x2000
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq 0x10(%rsp), %rcx
callq 0x22e0
movq %rax, %r13
cmpq $0x2000, %rax           # imm = 0x2000
setb %r15b
leaq (%rsp,%r13), %rbx
addq $0x40, %rbx
movq %rbx, %rbp
subq %r14, %rbp
movq %r14, %rdi
movl $0xa, %esi
movq %rbp, %rdx
callq 0x20d0
testq %rax, %rax
jne 0xa13b
cmpq %rbx, %r14
movq 0x30(%rsp), %r12
jne 0xa10e
cmpq $0x0, (%r12)
jne 0xa10e
movq 0x10(%rsp), %rdi
callq 0x23d0
testl %eax, %eax
jne 0xa1a7
movq %r12, %rdi
leaq 0x20(%rsp), %rsi
movq %rbp, %rdx
callq 0xa569
movq %rax, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x2500
leaq 0x1(%rbx), %r14
testb %r15b, %r15b
je 0xa08d
jmp 0xa170
movq %rax, %r12
movq %rax, %rbp
subq %r14, %rbp
movq 0x30(%rsp), %r15
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
movq %rbp, %rdx
callq 0xa569
movq %rax, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x2500
incq %r12
movq %r12, %r14
movq %r15, %r12
cmpq $0x0, (%r12)
je 0xa17f
movq %r12, %rdi
callq 0xa57b
movq 0x28(%rsp), %r15
cmpl $0x0, (%r15)
jne 0xa1c4
movq 0x10(%rsp), %rdi
callq 0x23d0
testl %eax, %eax
je 0xa04e
cmpq %rbx, %r14
jb 0xa04e
jmp 0xa1be
movq 0x18(%rsp), %rax
decq (%rax)
movq 0x8(%r12), %rdi
callq 0x2570
movq 0x28(%rsp), %r15
cmpl $0x0, (%r15)
je 0xa20b
movq 0x18(%rsp), %r12
cmpq $0x0, (%r12)
je 0xa1f3
movl $0x8, %ebx
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
movq (%rax,%rbx), %rdi
callq 0x2570
incq %r14
addq $0x10, %rbx
cmpq (%r12), %r14
jb 0xa1d8
movq 0x8(%rsp), %rdi
callq 0x2570
movq $0x0, 0x8(%rsp)
movl (%r15), %ebx
jmp 0xa20d
xorl %ebx, %ebx
movq 0x10(%rsp), %rdi
callq 0x23e0
movl %ebx, (%r15)
movq 0x8(%rsp), %rax
addq $0x2048, %rsp           # imm = 0x2048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)