// File: /workspace/github_c_makefile_star2_O0/qpfiffer[P]38-Moths/src/grengine.c
// Function: mmap_file_ol
static int mmap_file_ol(const char *file_path, m38_http_response *response,
				 const size_t *offset, const size_t *limit) {
	response->extra_data = calloc(1, sizeof(struct stat));

	if (stat(file_path, response->extra_data) == -1) {
		response->out = (unsigned char *)"<html><body><p>No such file.</p></body></html>";
		response->outsize= strlen("<html><body><p>No such file.</p></body></html>");
		free(response->extra_data);
		response->extra_data = NULL;
		return 404;
	}
	int fd = open(file_path, O_RDONLY);
	if (fd <= 0) {
		response->out = (unsigned char *)"<html><body><p>Could not open file.</p></body></html>";
		response->outsize= strlen("<html><body><p>could not open file.</p></body></html>");
		perror("mmap_file_ol: could not open file");
		free(response->extra_data);
		response->extra_data = NULL;
		close(fd);
		return 404;
	}


	const struct stat st = *(struct stat *)response->extra_data;

	const size_t c_offset = offset != NULL ? *offset : 0;
	const size_t c_limit = limit != NULL ? (*limit - c_offset) : (st.st_size - c_offset);

	response->out = mmap(NULL, c_limit, PROT_READ, MAP_PRIVATE, fd, c_offset);
	response->outsize = c_limit - c_offset;

	if (response->out == MAP_FAILED) {
		char buf[128] = {0};
		perror(buf);
		m38_log_msg(LOG_ERR, "Could not mmap file: %s", buf);

		response->out = (unsigned char *)"<html><body><p>Could not open file.</p></body></html>";
		response->outsize= strlen("<html><body><p>could not open file.</p></body></html>");
		close(fd);
		free(response->extra_data);
		response->extra_data = NULL;
		return 404;
	}
	close(fd);

	madvise(response->out, c_limit, MADV_SEQUENTIAL | MADV_WILLNEED);

	/* Figure out the mimetype for this resource: */
	char ending[16] = {0};
	unsigned int i = sizeof(ending);
	int found_dot = 0;
	const size_t res_len = strlen(file_path);
	for (i = res_len; i > (res_len - sizeof(ending)); i--) {
		if (file_path[i] == '.') {
			found_dot = 1;
			break;
		}
	}
	if (strlen(response->mimetype) == 0) {
		if (found_dot) {
			strncpy(ending, file_path + i, sizeof(ending) - 1);
			guess_mimetype(ending, sizeof(ending), response);
		} else {
			/* Fuck it, do something smart later with render_file. */
			strncpy(response->mimetype, "text/html", sizeof(response->mimetype));
		}
	}

	return 200;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl $0x1, %edi
movl $0x90, %esi
callq 0x3300
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x3350
cmpl $-0x1, %eax
jne 0x569a
movq -0x18(%rbp), %rax
leaq 0x6e92(%rip), %rcx      # 0xc4f8
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq $0x2e, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x3050
movq -0x18(%rbp), %rax
movq $0x0, 0x30(%rax)
movl $0x194, -0x4(%rbp)      # imm = 0x194
jmp 0x5985
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x34f0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jg 0x5703
movq -0x18(%rbp), %rax
leaq 0x6e6c(%rip), %rcx      # 0xc527
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq $0x35, 0x8(%rax)
leaq 0x6e8c(%rip), %rdi      # 0xc55d
callq 0x3520
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x3050
movq -0x18(%rbp), %rax
movq $0x0, 0x30(%rax)
movl -0x2c(%rbp), %edi
callq 0x32a0
movl $0x194, -0x4(%rbp)      # imm = 0x194
jmp 0x5985
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rsi
leaq -0xc0(%rbp), %rdi
movl $0x90, %edx
callq 0x3370
cmpq $0x0, -0x20(%rbp)
je 0x5733
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x178(%rbp)
jmp 0x573e
xorl %eax, %eax
movq %rax, -0x178(%rbp)
jmp 0x573e
movq -0x178(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x576a
movq -0x28(%rbp), %rax
movq (%rax), %rax
subq -0xc8(%rbp), %rax
movq %rax, -0x180(%rbp)
jmp 0x577f
movq -0x90(%rbp), %rax
subq -0xc8(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rsi
movl -0x2c(%rbp), %r8d
movq -0xc8(%rbp), %r9
xorl %eax, %eax
movl %eax, %edi
movl $0x1, %edx
movl $0x2, %ecx
callq 0x3170
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0xd0(%rbp), %rcx
subq -0xc8(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq $-0x1, %rcx
cmpq %rcx, (%rax)
jne 0x5866
leaq -0x150(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x3240
leaq -0x150(%rbp), %rdi
callq 0x3520
leaq -0x150(%rbp), %rdx
movl $0x3, %edi
leaq 0x6d67(%rip), %rsi      # 0xc57f
movb $0x0, %al
callq 0x3580
movq -0x18(%rbp), %rax
leaq 0x6cfd(%rip), %rcx      # 0xc527
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq $0x35, 0x8(%rax)
movl -0x2c(%rbp), %edi
callq 0x32a0
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x3050
movq -0x18(%rbp), %rax
movq $0x0, 0x30(%rax)
movl $0x194, -0x4(%rbp)      # imm = 0x194
jmp 0x5985
movl -0x2c(%rbp), %edi
callq 0x32a0
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0xd0(%rbp), %rsi
movl $0x3, %edx
callq 0x34b0
leaq -0x160(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3240
movl $0x10, -0x164(%rbp)
movl $0x0, -0x168(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3140
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rax
movl %eax, -0x164(%rbp)
movl -0x164(%rbp), %eax
movq -0x170(%rbp), %rcx
subq $0x10, %rcx
cmpq %rcx, %rax
jbe 0x5912
movq -0x10(%rbp), %rax
movl -0x164(%rbp), %ecx
movsbl (%rax,%rcx), %eax
cmpl $0x2e, %eax
jne 0x58ff
movl $0x1, -0x168(%rbp)
jmp 0x5912
jmp 0x5901
movl -0x164(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x164(%rbp)
jmp 0x58ca
movq -0x18(%rbp), %rdi
addq $0x10, %rdi
callq 0x3140
cmpq $0x0, %rax
jne 0x597e
cmpl $0x0, -0x168(%rbp)
je 0x5963
leaq -0x160(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x164(%rbp), %eax
addq %rax, %rsi
movl $0xf, %edx
callq 0x30a0
leaq -0x160(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl $0x10, %esi
callq 0x7160
jmp 0x597c
movq -0x18(%rbp), %rdi
addq $0x10, %rdi
leaq 0x67d2(%rip), %rsi      # 0xc144
movl $0x20, %edx
callq 0x30a0
jmp 0x597e
movl $0xc8, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
nopw %cs:(%rax,%rax)