// File: /workspace/github_c_makefile_star2_O0/qpfiffer[P]38-Moths/src/parse.c
// Function: m38_parse_request
int m38_parse_request(const unsigned char *to_read, const size_t num_read, m38_http_request *out) {
	/* Find the verb */
	const char *verb_end = strnstr((char *)to_read, " ", MAX_READ_LEN);
	if (verb_end == NULL)
		goto error;

	const size_t c_verb_size = verb_end - (char *)to_read;
	const size_t verb_size = c_verb_size >= sizeof(out->verb) ? sizeof(out->verb) - 1: c_verb_size;
	strncpy(out->verb, (char *)to_read, verb_size);

	const char *res_offset = verb_end + strlen(" ");
	const char *resource_end = strnstr(res_offset, " ", sizeof(out->resource));
	if (resource_end == NULL)
		goto error;

	const size_t c_resource_size = resource_end - res_offset;
	const size_t resource_size = c_resource_size >= sizeof(out->resource) ? sizeof(out->resource) : c_resource_size;
	strncpy(out->resource, res_offset, resource_size);

	/* Parse full header here. */
	size_t header_length = 0;
	int found_end = 0;
	while (header_length + 3 <= num_read) {
		if (to_read[header_length] == '\r' &&
		    to_read[header_length + 1] == '\n' &&
		    to_read[header_length + 2] == '\r' &&
		    to_read[header_length + 3] == '\n') {
			header_length += 4;
			found_end = 1;
			break;
		}
		header_length++;
	}

	if (!found_end) {
		m38_log_msg(LOG_DEBUG, "Could not find end of HTTP header.");
		goto error;
	}

	out->full_header = strndup((char *)to_read, header_length);
	out->header_len = header_length;

	return 0;

error:
	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x218b(%rip), %rsi      # 0xdbed
movl $0x400, %edx            # imm = 0x400
callq 0x32c0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xba7c
jmp 0xbc0d
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x30(%rbp)
cmpq $0x10, -0x30(%rbp)
jb 0xba9d
movl $0xf, %eax
movq %rax, -0x70(%rbp)
jmp 0xbaa5
movq -0x30(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x30a0
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
leaq 0x2118(%rip), %rsi      # 0xdbed
movl $0x200, %edx            # imm = 0x200
callq 0x32c0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0xbaef
jmp 0xbc0d
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x50(%rbp)
cmpq $0x200, -0x50(%rbp)     # imm = 0x200
jb 0xbb13
movl $0x200, %eax            # imm = 0x200
movq %rax, -0x78(%rbp)
jmp 0xbb1b
movq -0x50(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rdi
addq $0x10, %rdi
movq -0x40(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x30a0
movq $0x0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movq -0x60(%rbp), %rax
addq $0x3, %rax
cmpq -0x18(%rbp), %rax
ja 0xbbbf
movq -0x10(%rbp), %rax
movq -0x60(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0xd, %eax
jne 0xbbb1
movq -0x10(%rbp), %rax
movq -0x60(%rbp), %rcx
movzbl 0x1(%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0xbbb1
movq -0x10(%rbp), %rax
movq -0x60(%rbp), %rcx
movzbl 0x2(%rax,%rcx), %eax
cmpl $0xd, %eax
jne 0xbbb1
movq -0x10(%rbp), %rax
movq -0x60(%rbp), %rcx
movzbl 0x3(%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0xbbb1
movq -0x60(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x60(%rbp)
movl $0x1, -0x64(%rbp)
jmp 0xbbbf
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xbb47
cmpl $0x0, -0x64(%rbp)
jne 0xbbda
movl $0x1, %edi
leaq 0x2074(%rip), %rsi      # 0xdc45
movb $0x0, %al
callq 0x3580
jmp 0xbc0d
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x32f0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x230(%rax)
movq -0x60(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x238(%rax)
movl $0x0, -0x4(%rbp)
jmp 0xbc14
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq