// File: /workspace/github_c_makefile_star2_O0/qpfiffer[P]38-Moths/src/parse.c
// Function: m38_parse_range_header
m38_range_header m38_parse_range_header(const char *range_query) {
	/* bytes=0- */
	/* bytes=12345-55555 */

	if (strncmp(range_query, "bytes=", strlen("bytes=")) != 0) {
		m38_log_msg(LOG_WARN, "Malformed range query: %s", range_query);
		m38_range_header rng = {
			.limit = 0,
			.offset = 0
		};
		return rng;
	}

	const char *actual_value = range_query + strlen("bytes=");
	size_t i;
	int found = 0;
	for (i = 0; i < strlen(actual_value); i++) {
		if (actual_value[i] == '-') {
			found = 1;
			break;
		}
	}

	if (!found) {
		m38_log_msg(LOG_WARN, "Malformed range query: %s", range_query);
		m38_range_header rng = {
			.limit = 0,
			.offset = 0
		};
		return rng;
	}

	long int _fn = strtol(actual_value, NULL, 10);
	if ((_fn == LONG_MIN || _fn == LONG_MAX) && errno == ERANGE) {
		m38_log_msg(LOG_WARN, "Malformed range query, could not parse first_num: %s", range_query);
		m38_range_header rng = {
			.limit = 0,
			.offset = 0
		};
		return rng;
	}

	const size_t first_num = (size_t)_fn;
	if (i == (strlen(actual_value) - 1)) {
		/* No second number, just return 0. */
		m38_range_header rng = {
			.limit = 0,
			.offset = first_num
		};
		return rng;
	}

	/* Otherwise, assume the rest of the value is the second number.
	 * The '+1' here is for the '-' between the two numbers.
	 */
	const long int _sn = strtol(actual_value + (i + 1), NULL, 10);
	if ((_sn == LONG_MIN || _sn == LONG_MAX) && errno == ERANGE) {
		/* No second number, just return 0. */
		m38_range_header rng = {
			.limit = 0,
			.offset = first_num
		};
		return rng;
	}
	const size_t second_num = _sn;

	if (second_num < first_num) {
		m38_range_header rng = {
			.limit = 0,
			.offset = first_num
		};
		return rng;
	}

	m38_range_header rng = {
		.limit = second_num,
		.offset = first_num
	};

	return rng;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x18(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x24f8(%rip), %rsi      # 0xdbef
movl $0x6, %edx
callq 0x30b0
cmpl $0x0, %eax
je 0xb732
movq -0x18(%rbp), %rdx
movl $0x2, %edi
leaq 0x24e0(%rip), %rsi      # 0xdbf6
movb $0x0, %al
callq 0x3580
leaq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3240
jmp 0xb901
movq -0x18(%rbp), %rax
addq $0x6, %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3140
movq %rax, %rcx
movq -0x58(%rbp), %rax
cmpq %rcx, %rax
jae 0xb794
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2d, %eax
jne 0xb784
movl $0x1, -0x2c(%rbp)
jmp 0xb794
jmp 0xb786
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xb74d
cmpl $0x0, -0x2c(%rbp)
jne 0xb7c6
movq -0x18(%rbp), %rdx
movl $0x2, %edi
leaq 0x244c(%rip), %rsi      # 0xdbf6
movb $0x0, %al
callq 0x3580
leaq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3240
jmp 0xb901
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x3360
movq %rax, -0x38(%rbp)
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
cmpq %rax, -0x38(%rbp)
je 0xb7fc
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
cmpq %rax, -0x38(%rbp)
jne 0xb832
callq 0x3090
cmpl $0x22, (%rax)
jne 0xb832
movq -0x18(%rbp), %rdx
movl $0x2, %edi
leaq 0x23fa(%rip), %rsi      # 0xdc10
movb $0x0, %al
callq 0x3580
leaq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3240
jmp 0xb901
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3140
movq %rax, %rcx
movq -0x60(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0xb870
movq $0x0, -0x10(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xb901
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rax
addq $0x1, %rax
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x3360
movq %rax, -0x48(%rbp)
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
cmpq %rax, -0x48(%rbp)
je 0xb8b1
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
cmpq %rax, -0x48(%rbp)
jne 0xb8cd
callq 0x3090
cmpl $0x22, (%rax)
jne 0xb8cd
movq $0x0, -0x10(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xb901
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0xb8f1
movq $0x0, -0x10(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xb901
movq -0x50(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rdx
addq $0x60, %rsp
popq %rbp
retq
nop