// File: /workspace/github_c_makefile_star2_O3/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), %r13
movl $0xfffff830, %ebp       # imm = 0xFFFFF830
testq %r13, %r13
je 0x31dd
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movzbl (%rdi), %eax
leaq 0x4acd(%rip), %rcx      # 0x7b10
testb $0x10, (%rcx,%rax)
jne 0x304e
xorl %r12d, %r12d
jmp 0x306c
cmpq $0x1, %r13
je 0x31dd
movzbl 0x1(%r15), %eax
incq %r15
decq %r13
testb $0x10, (%rcx,%rax)
jne 0x304e
movb $0x1, %r12b
movq %r13, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movzbl %al, %eax
cmpl $0x3a, %eax
jg 0x30c7
leal -0x22(%rax), %ecx
cmpl $0x5, %ecx
ja 0x30ed
leaq 0x4337(%rip), %rax      # 0x73c8
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
decq %r13
leaq 0x8(%rsp), %rdx
movq %r13, (%rdx)
incq %r15
leaq 0x244(%rip), %rsi       # 0x32f3
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
callq 0x31ee
movl $0x2, %r15d
jmp 0x31a0
cmpl $0x3d, %eax
jg 0x30f9
cmpl $0x3b, %eax
je 0x3168
cmpl $0x3c, %eax
jne 0x311a
decq %r13
movq %r13, 0x8(%rsp)
movl $0x80, %r15d
jmp 0x31b6
cmpl $0xa, %eax
je 0x3168
cmpl $0xd, %eax
je 0x3168
jmp 0x311a
cmpl $0x3e, %eax
je 0x31a8
cmpl $0x7c, %eax
jne 0x311a
decq %r13
movq %r13, 0x8(%rsp)
movl $0x100, %r15d           # imm = 0x100
jmp 0x31b6
leaq 0x2a1(%rip), %rsi       # 0x33c2
leaq 0x8(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
callq 0x31ee
movl $0x1, %r15d
jmp 0x31a0
decq %r13
movq %r13, 0x8(%rsp)
movl $0x200, %r15d           # imm = 0x200
jmp 0x31b6
leaq 0x1(%r15), %rdi
leaq -0x1(%r13), %rdx
movl $0xa, %esi
callq 0x2200
testq %rax, %rax
je 0x31dd
addq %r15, %r13
subq %rax, %r13
decq %r13
movq %r13, 0x8(%rsp)
movl $0x400, %r15d           # imm = 0x400
jmp 0x31b6
decq %r13
leaq 0x8(%rsp), %rdx
movq %r13, (%rdx)
incq %r15
leaq 0xd8(%rip), %rsi        # 0x3265
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
callq 0x31ee
movl $0x4, %r15d
testl %eax, %eax
jns 0x31b6
movl %eax, %ebp
jmp 0x31dd
decq %r13
movq %r13, 0x8(%rsp)
movl $0x40, %r15d
movl $0x20, %edi
callq 0x6540
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