// File: /workspace/github_c_makefile_star2_O0/markdryan[P]subtilis/common/lexer.c
// Function: prv_process_keyword
static bool prv_process_keyword(subtilis_lexer_t *l, char ch,
				subtilis_token_t *t, subtilis_error_t *err)
{
	subtilis_keyword_t *kw;
	subtilis_keyword_t key;
	const char *tbuf;
	bool possible_id = true;
	bool possible_fn = false;
	bool possible_proc = false;

	prv_set_first(l, t, SUBTILIS_TOKEN_UNKNOWN);

	/* TODO: We have very similar loops elsewhere */
	for (;;) {
		prv_ensure_input(l, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return false;
		if (l->index == l->buf_end)
			break;

		ch = l->buffer[l->index];
		if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')))
			break;
		prv_set_next_with_err(l, t, err);
		if (err->type != SUBTILIS_ERROR_OK)
			return false;
	}

	if (l->index < l->buf_end) {
		if (ch == '$') {
			prv_set_next_with_err(l, t, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return false;
			prv_ensure_input(l, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return false;
			if ((l->index < l->buf_end) &&
			    (l->buffer[l->index] == '#')) {
				possible_id = false;
				prv_set_next_with_err(l, t, err);
				if (err->type != SUBTILIS_ERROR_OK)
					return false;
			}
		} else if (ch == '#') {
			possible_id = false;
			prv_set_next_with_err(l, t, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return false;
		} else if (ch == '%' || ch == '&' || ch == '@') {
			prv_set_next_with_err(l, t, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return false;
		}
	}

	/* There's no need to check token length here.  If it's a keyword
	 * the length must be less than SUBTILIS_MAX_TOKEN_SIZE.  Otherwise
	 * the token size will be checked by prv_validate_identifier.
	 */

	/* This will zero terminate the token buffer.  This is what we need
	 * for performing the various string comparsions we're going to do
	 * but we'll need to remove the trailing zero if we don't have a
	 * keyword.
	 */

	tbuf = subtilis_token_get_text_with_err(t, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return false;

	if (possible_id) {
		possible_proc = strncmp(tbuf, "PROC", 4) == 0;
		possible_fn = strncmp(tbuf, "FN", 2) == 0;
		if (possible_proc || possible_fn) {
			subtilis_buffer_remove_terminator(&t->buf);
			if (ch != '$' && ch != '%' && ch != '&' && ch != '@') {
				ch = 0;
			} else if (possible_proc) {
				subtilis_error_set_bad_proc_name(
				    err, tbuf, l->stream->name, l->line);
				return false;
			}
			prv_process_call(l, t, possible_proc, ch, err);
			return false;
		}

		if (strncmp(tbuf, "REC", 3) == 0) {
			if (strcmp(tbuf, "RECTANGLE")) {
				subtilis_buffer_remove_terminator(&t->buf);
				prv_process_rec(l, t, tbuf, ch, err);
				return false;
			}
		}
	}

	// TODO: bsearch may not be efficient for large files.

	key.str = tbuf;
	kw = bsearch(&key, l->keywords, l->num_keywords, sizeof(*l->keywords),
		     prv_compare_keyword);

	if (kw) {
		if (kw->type == SUBTILIS_KEYWORD_REM) {
			prv_skip_line(l, err);
			if (err->type != SUBTILIS_ERROR_OK)
				return false;
			return true;
		}

		t->type = SUBTILIS_TOKEN_KEYWORD;
		t->tok.keyword.type = kw->type;
		t->tok.keyword.supported = kw->supported;
		return false;
	}

	subtilis_buffer_remove_terminator(&t->buf);
	if (possible_id) {
		t->type = SUBTILIS_TOKEN_IDENTIFIER;
		if (ch != '$' && ch != '%' && ch != '&' && ch != '@')
			ch = 0;
		prv_validate_identifier(l, t, ch, err);
		return false;
	}

	/* It's an invalid token */

	prv_check_token_buffer(l, t, err, SUBTILIS_ERROR_IDENTIFIER_TOO_LONG);
	if (err->type != SUBTILIS_ERROR_OK)
		return false;
	tbuf = subtilis_token_get_text(t);
	subtilis_error_set_unknown_token(err, tbuf, l->stream->name, l->line);

	return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movb %sil, %al
movq %rdi, -0x10(%rbp)
movb %al, -0x11(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movb $0x1, -0x49(%rbp)
movb $0x0, -0x4a(%rbp)
movb $0x0, -0x4b(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x7, %edx
callq 0x13b20
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x13890
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x140a7
movb $0x0, -0x1(%rbp)
jmp 0x14567
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x360(%rcx), %rax
jne 0x140be
jmp 0x14125
movq -0x10(%rbp), %rax
movq 0x368(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x40(%rcx), %rcx
movb (%rax,%rcx), %al
movb %al, -0x11(%rbp)
movsbl -0x11(%rbp), %eax
cmpl $0x41, %eax
jl 0x140e9
movsbl -0x11(%rbp), %eax
cmpl $0x5a, %eax
jle 0x140fd
movsbl -0x11(%rbp), %eax
cmpl $0x61, %eax
jl 0x140fb
movsbl -0x11(%rbp), %eax
cmpl $0x7a, %eax
jle 0x140fd
jmp 0x14125
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x14700
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x14120
movb $0x0, -0x1(%rbp)
jmp 0x14567
jmp 0x14088
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x360(%rcx), %rax
jae 0x14261
movsbl -0x11(%rbp), %eax
cmpl $0x24, %eax
jne 0x141e9
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x14700
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x1416e
movb $0x0, -0x1(%rbp)
jmp 0x14567
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x13890
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x1418d
movb $0x0, -0x1(%rbp)
jmp 0x14567
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x360(%rcx), %rax
jae 0x141e7
movq -0x10(%rbp), %rax
movq 0x368(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x40(%rcx), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x23, %eax
jne 0x141e7
movb $0x0, -0x49(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x14700
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x141e5
movb $0x0, -0x1(%rbp)
jmp 0x14567
jmp 0x141e7
jmp 0x1425f
movsbl -0x11(%rbp), %eax
cmpl $0x23, %eax
jne 0x1421b
movb $0x0, -0x49(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x14700
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x14219
movb $0x0, -0x1(%rbp)
jmp 0x14567
jmp 0x1425d
movsbl -0x11(%rbp), %eax
cmpl $0x25, %eax
je 0x14236
movsbl -0x11(%rbp), %eax
cmpl $0x26, %eax
je 0x14236
movsbl -0x11(%rbp), %eax
cmpl $0x40, %eax
jne 0x1425b
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x14700
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x14259
movb $0x0, -0x1(%rbp)
jmp 0x14567
jmp 0x1425b
jmp 0x1425d
jmp 0x1425f
jmp 0x14261
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x12de0
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x14284
movb $0x0, -0x1(%rbp)
jmp 0x14567
testb $0x1, -0x49(%rbp)
je 0x143f5
movq -0x48(%rbp), %rdi
leaq 0x95d82(%rip), %rsi     # 0xaa01b
movl $0x4, %edx
callq 0x12050
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movb %al, -0x4b(%rbp)
movq -0x48(%rbp), %rdi
leaq 0x96241(%rip), %rsi     # 0xaa4fa
movl $0x2, %edx
callq 0x12050
cmpl $0x0, %eax
sete %al
andb $0x1, %al
movb %al, -0x4a(%rbp)
testb $0x1, -0x4b(%rbp)
jne 0x142de
testb $0x1, -0x4a(%rbp)
je 0x1438e
movq -0x20(%rbp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x165d0
movsbl -0x11(%rbp), %eax
cmpl $0x24, %eax
je 0x14318
movsbl -0x11(%rbp), %eax
cmpl $0x25, %eax
je 0x14318
movsbl -0x11(%rbp), %eax
cmpl $0x26, %eax
je 0x14318
movsbl -0x11(%rbp), %eax
cmpl $0x40, %eax
je 0x14318
movb $0x0, -0x11(%rbp)
jmp 0x14365
testb $0x1, -0x4b(%rbp)
je 0x14363
movq -0x28(%rbp), %rdi
movq -0x48(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %r8
movq -0x10(%rbp), %rax
movl 0x38(%rax), %r9d
movl $0xb, %esi
leaq 0x96f34(%rip), %rcx     # 0xab276
leaq 0x94d3b(%rip), %rax     # 0xa9084
movq %rax, (%rsp)
movl $0x346, 0x8(%rsp)       # imm = 0x346
callq 0x156d0
movb $0x0, -0x1(%rbp)
jmp 0x14567
jmp 0x14365
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movb -0x4b(%rbp), %cl
movb -0x11(%rbp), %al
movq -0x28(%rbp), %r8
andb $0x1, %cl
movzbl %cl, %edx
movsbl %al, %ecx
callq 0x14d10
movb $0x0, -0x1(%rbp)
jmp 0x14567
movq -0x48(%rbp), %rdi
leaq 0x983fc(%rip), %rsi     # 0xac795
movl $0x3, %edx
callq 0x12050
cmpl $0x0, %eax
jne 0x143f3
movq -0x48(%rbp), %rdi
leaq 0x94d55(%rip), %rsi     # 0xa9108
callq 0x12140
cmpl $0x0, %eax
je 0x143f1
movq -0x20(%rbp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x165d0
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x48(%rbp), %rdx
movb -0x11(%rbp), %al
movq -0x28(%rbp), %r8
movsbl %al, %ecx
callq 0x14e60
movb $0x0, -0x1(%rbp)
jmp 0x14567
jmp 0x143f3
jmp 0x143f5
movq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq -0x40(%rbp), %rdi
movl $0x10, %ecx
leaq 0xba4(%rip), %r8        # 0x14fc0
callq 0x12270
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x14490
movq -0x30(%rbp), %rax
cmpl $0x3, 0x8(%rax)
jne 0x1445e
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x15000
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x14455
movb $0x0, -0x1(%rbp)
jmp 0x14567
movb $0x1, -0x1(%rbp)
jmp 0x14567
movq -0x20(%rbp), %rax
movl $0x1, (%rax)
movq -0x30(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x30(%rbp), %rax
movb 0xc(%rax), %cl
movq -0x20(%rbp), %rax
andb $0x1, %cl
movb %cl, 0xc(%rax)
movb $0x0, -0x1(%rbp)
jmp 0x14567
movq -0x20(%rbp), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x165d0
testb $0x1, -0x49(%rbp)
je 0x144f5
movq -0x20(%rbp), %rax
movl $0x6, (%rax)
movsbl -0x11(%rbp), %eax
cmpl $0x24, %eax
je 0x144d8
movsbl -0x11(%rbp), %eax
cmpl $0x25, %eax
je 0x144d8
movsbl -0x11(%rbp), %eax
cmpl $0x26, %eax
je 0x144d8
movsbl -0x11(%rbp), %eax
cmpl $0x40, %eax
je 0x144d8
movb $0x0, -0x11(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movb -0x11(%rbp), %al
movq -0x28(%rbp), %rcx
movsbl %al, %edx
callq 0x150e0
movb $0x0, -0x1(%rbp)
jmp 0x14567
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl $0x9, %ecx
callq 0x14bb0
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
je 0x1451a
movb $0x0, -0x1(%rbp)
jmp 0x14567
movq -0x20(%rbp), %rdi
callq 0x12d70
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rdi
movq -0x48(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %r8
movq -0x10(%rbp), %rax
movl 0x38(%rax), %r9d
movl $0xa, %esi
leaq 0x96d2b(%rip), %rcx     # 0xab276
leaq 0x94b32(%rip), %rax     # 0xa9084
movq %rax, (%rsp)
movl $0x379, 0x8(%rsp)       # imm = 0x379
callq 0x156d0
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)