// File: /workspace/github_c_makefile_star2_O1/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
movq %rax, 0x8(%rsp)
movl $0xfffff830, %ebp       # imm = 0xFFFFF830
testq %rax, %rax
je 0x31da
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq 0x8(%rsp), %rax
movzbl (%rdi), %ecx
leaq 0x4ad3(%rip), %rdx      # 0x7b10
testb $0x10, (%rdx,%rcx)
jne 0x3048
xorl %r12d, %r12d
jmp 0x3066
cmpq $0x1, %rax
je 0x30e1
movzbl 0x1(%r15), %ecx
incq %r15
decq %rax
testb $0x10, (%rdx,%rcx)
jne 0x3048
movb $0x1, %r12b
movq %rax, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movzbl %cl, %eax
cmpl $0x3a, %eax
jg 0x30be
leal -0x22(%rax), %ecx
cmpl $0x5, %ecx
ja 0x30ef
leaq 0x433d(%rip), %rax      # 0x73c8
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
leaq 0x8(%rsp), %rdx
decq (%rdx)
incq %r15
leaq 0x242(%rip), %rsi       # 0x32e8
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
callq 0x31eb
movl $0x2, %r15d
jmp 0x31a0
cmpl $0x3d, %eax
jg 0x30fb
cmpl $0x3b, %eax
je 0x316e
cmpl $0x3c, %eax
jne 0x3119
decq 0x8(%rsp)
movl $0x80, %r15d
jmp 0x31b3
movq $0x0, 0x8(%rsp)
jmp 0x31da
cmpl $0xa, %eax
je 0x316e
cmpl $0xd, %eax
je 0x316e
jmp 0x3119
cmpl $0x3e, %eax
je 0x31a8
cmpl $0x7c, %eax
jne 0x3119
decq 0x8(%rsp)
movl $0x100, %r15d           # imm = 0x100
jmp 0x31b3
leaq 0x2a9(%rip), %rsi       # 0x33c9
leaq 0x8(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
callq 0x31eb
movl $0x1, %r15d
jmp 0x31a0
decq 0x8(%rsp)
movl $0x200, %r15d           # imm = 0x200
jmp 0x31b3
leaq 0x1(%r15), %rdi
movq 0x8(%rsp), %r13
leaq -0x1(%r13), %rdx
movl $0xa, %esi
callq 0x2200
testq %rax, %rax
je 0x31da
addq %r15, %r13
subq %rax, %r13
movq %r13, 0x8(%rsp)
decq 0x8(%rsp)
movl $0x400, %r15d           # imm = 0x400
jmp 0x31b3
leaq 0x8(%rsp), %rdx
decq (%rdx)
incq %r15
leaq 0xd5(%rip), %rsi        # 0x3262
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
callq 0x31eb
movl $0x4, %r15d
testl %eax, %eax
jns 0x31b3
movl %eax, %ebp
jmp 0x31da
decq 0x8(%rsp)
movl $0x40, %r15d
movl $0x20, %edi
callq 0x6754
movb %r12b, (%rax)
movl %r15d, 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