// File: /workspace/github_c_makefile_star2_O2/ebiggers[P]mysh/mysh_lex.c
// Function: lex_next_token
int lex_next_token(const char *p, size_t *bytes_remaining_p,
		   struct token **tok_ret)
{
	struct token *tok;
	enum token_type type;
	char *tok_data;
	bool found_whitespace = false;
	size_t bytes_remaining = *bytes_remaining_p;
	int ret;

	if (*bytes_remaining_p == 0) /* No bytes remaining */
		return LEX_NOT_ENOUGH_INPUT;

	/* Ignore whitespace between tokens */
	while (shell_char_type(*p) & SHELL_LEX_WHITESPACE) {
		found_whitespace = true;
		p++;
		if (--bytes_remaining == 0)
			return LEX_NOT_ENOUGH_INPUT;
	}

	/* Choose the token type based on the next character, then parse the
	 * token. */
	tok_data = NULL;
	switch (*p) {
	case '&':
		type = TOK_AMPERSAND;
		--bytes_remaining;
		break;
	case '|':
		type = TOK_PIPE;
		--bytes_remaining;
		break;
	case '<':
		type = TOK_LESS_THAN;
		--bytes_remaining;
		break;
	case '>':
		type = TOK_GREATER_THAN;
		--bytes_remaining;
		break;
	case '#':
		{ /* everything after '#' up until the next newline is a comment */
			const char *newline;

			newline = memchr(p + 1, '\n', bytes_remaining - 1);
			if (!newline)
				return LEX_NOT_ENOUGH_INPUT;
			bytes_remaining -= newline - p;
		}
		/* fall through */
	case '\r':
	case '\n':
	case ';':
		type = TOK_END_OF_SHELL_STATEMENT;
		--bytes_remaining;
		break;
	case '\'':
		type = TOK_SINGLE_QUOTED_STRING;
		--bytes_remaining;
		ret = lex_string(p + 1, scan_single_quoted_string,
				 &bytes_remaining, &tok_data);
		if (ret < 0)
			return ret;
		break;
	case '"':
		type = TOK_DOUBLE_QUOTED_STRING;
		--bytes_remaining;
		ret = lex_string(p + 1, scan_double_quoted_string,
				 &bytes_remaining, &tok_data);
		if (ret < 0)
			return ret;
		break;
	default:
		/* anything that didn't match one of the special characters is
		 * treated as the beginning of an unquoted string */
		type = TOK_UNQUOTED_STRING;
		ret = lex_string(p, scan_unquoted_string,
				 &bytes_remaining, &tok_data);
		if (ret < 0)
			return ret;
		break;
	}
	/* Allocate and initialize the token */
	tok = xmalloc(sizeof(struct token));
	tok->preceded_by_whitespace = found_whitespace;
	tok->type = type;
	/* tok_data defaults to NULL if not explicitly set for a string token */
	tok->tok_data = tok_data;
	/* Return the token and set the bytes remaining in the input according
	 * to how much was consumed */
	*bytes_remaining_p = bytes_remaining;
	*tok_ret = tok;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq (%rsi), %rax
movl $0xfffff830, %ebp       # imm = 0xFFFFF830
testq %rax, %rax
je 0x2f0f
movq %rdx, %rbx
movq %rsi, %r14
xorl %edx, %edx
leaq 0x4da7(%rip), %rsi      # 0x7b10
xorl %r12d, %r12d
movzbl (%rdi,%rdx), %ecx
testb $0x10, (%rsi,%rcx)
je 0x2d86
incq %rdx
movb $0x1, %r12b
cmpq %rdx, %rax
jne 0x2d6c
jmp 0x2f0f
movq %rax, %r13
subq %rdx, %r13
andq $0x0, 0x10(%rsp)
movq %r13, 0x8(%rsp)
leaq (%rdi,%rdx), %r15
leal -0x22(%rcx), %esi
cmpl $0x5, %esi
jbe 0x2ddb
cmpl $0xa, %ecx
je 0x2e98
cmpl $0xd, %ecx
je 0x2e98
cmpl $0x7c, %ecx
je 0x2eb9
cmpl $0x3c, %ecx
je 0x2edb
cmpl $0x3e, %ecx
je 0x2ecb
cmpl $0x3b, %ecx
je 0x2e98
jmp 0x2e1e
leaq 0x45e6(%rip), %rcx      # 0x73c8
movslq (%rcx,%rsi,4), %rsi
addq %rcx, %rsi
jmpq *%rsi
addq %rdx, %rdi
incq %rdi
notq %rdx
addq %rax, %rdx
leaq 0x8(%rsp), %rax
movq %rdx, (%rax)
leaq 0x21d(%rip), %rsi       # 0x3023
leaq 0x10(%rsp), %rcx
movq %rax, %rdx
callq 0x2f20
testl %eax, %eax
js 0x2e72
pushq $0x2
jmp 0x2ed8
leaq 0x2c5(%rip), %rsi       # 0x30ea
leaq 0x8(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
callq 0x2f20
testl %eax, %eax
js 0x2e72
pushq $0x1
jmp 0x2ed8
addq %rdx, %rdi
incq %rdi
notq %rdx
addq %rax, %rdx
leaq 0x8(%rsp), %rax
movq %rdx, (%rax)
leaq 0x13a(%rip), %rsi       # 0x2f97
leaq 0x10(%rsp), %rcx
movq %rax, %rdx
callq 0x2f20
testl %eax, %eax
js 0x2e72
pushq $0x4
jmp 0x2ed8
movl %eax, %ebp
jmp 0x2f0f
addq %rdx, %rdi
incq %rdi
notq %rdx
addq %rax, %rdx
pushq $0xa
popq %rsi
callq 0x2200
testq %rax, %rax
je 0x2f0f
addq %r15, %r13
subq %rax, %r13
decq %r13
movq %r13, 0x8(%rsp)
movl $0x400, %ebp            # imm = 0x400
jmp 0x2eeb
notq %rdx
addq %rax, %rdx
movq %rdx, 0x8(%rsp)
movl $0x200, %ebp            # imm = 0x200
jmp 0x2eeb
notq %rdx
addq %rax, %rdx
movq %rdx, 0x8(%rsp)
movl $0x100, %ebp            # imm = 0x100
jmp 0x2eeb
notq %rdx
addq %rax, %rdx
movq %rdx, 0x8(%rsp)
pushq $0x40
popq %rbp
jmp 0x2eeb
notq %rdx
addq %rax, %rdx
movq %rdx, 0x8(%rsp)
movl $0x80, %ebp
pushq $0x20
popq %rdi
callq 0x6099
movb %r12b, (%rax)
movl %ebp, 0x4(%rax)
movq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x8(%rsp), %rcx
movq %rcx, (%r14)
movq %rax, (%rbx)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq