// File: /workspace/github_c_makefile_star2_O1/markdryan[P]subtilis/frontend/parser_os.c
// Function: subtilis_parser_sys
void subtilis_parser_sys(subtilis_parser_t *p, subtilis_token_t *t,
			 subtilis_error_t *err)
{
	subtilis_exp_t *call_name;
	const char *tbuf;
	size_t i;
	const char *swi_name;
	size_t translated;
	bool handle_errors = false;
	uint32_t expected_in_regs = 0;
	uint32_t expected_out_regs = 0;
	uint32_t found_in_regs = 0;
	uint32_t found_out_regs = 0;
	bool flags_local = false;
	size_t flags_reg = SIZE_MAX;
	size_t no_inputs = 0;
	size_t no_outputs = 0;
	subtilis_exp_t **input_args = NULL;
	size_t *in_regs = NULL;
	subtilis_ir_sys_out_reg_t *out_regs = NULL;

	if (!p->backend.sys_check || !p->backend.sys_trans) {
		subtilis_error_set_not_supported(
		    err, "SYS on this target", p->l->stream->name, p->l->line);
		return;
	}

	call_name = subtilis_parser_expression(p, t, err);
	if (err->type != SUBTILIS_ERROR_OK)
		return;

	if (subtilis_type_if_is_numeric(&call_name->type)) {
		call_name = subtilis_type_if_to_int(p, call_name, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
	}

	if (call_name->type.type == SUBTILIS_TYPE_CONST_STRING) {
		swi_name = subtilis_buffer_get_string(&call_name->exp.str);
		translated = p->backend.sys_trans(swi_name);
		if (translated == SIZE_MAX) {
			subtilis_error_set_sys_call_unknown(
			    err, swi_name, p->l->stream->name, p->l->line);
			goto cleanup;
		}
		subtilis_exp_delete(call_name);
		call_name = subtilis_exp_new_int32((int32_t)translated, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
	} else if (call_name->type.type != SUBTILIS_TYPE_CONST_INTEGER) {
		subtilis_error_set_expected(
		    err, "String or Integer constant expected",
		    subtilis_type_name(&call_name->type), p->l->stream->name,
		    p->l->line);
		goto cleanup;
	}

	/*
	 * At this point call_name must be a constant integer.
	 */

	tbuf = subtilis_token_get_text(t);
	if ((t->type == SUBTILIS_TOKEN_OPERATOR) && !strcmp(tbuf, ",")) {
		input_args = prv_get_args(p, t, &no_inputs, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

		if (no_inputs == 0) {
			subtilis_error_set_sys_bad_args(err, p->l->stream->name,
							p->l->line);
			goto cleanup;
		}

		in_regs = prv_process_input_args(p, input_args, no_inputs,
						 &found_in_regs, err);
		input_args = NULL;
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
	}

	if ((t->type == SUBTILIS_TOKEN_KEYWORD) &&
	    (t->tok.keyword.type == SUBTILIS_KEYWORD_TO)) {
		out_regs = prv_get_output_args(p, t, &no_outputs,
					       &found_out_regs, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;

		if (no_outputs == 0) {
			subtilis_error_set_sys_bad_args(err, p->l->stream->name,
							p->l->line);
			goto cleanup;
		}
	}

	if (!p->backend.sys_check(call_name->exp.ir_op.integer,
				  &expected_in_regs, &expected_out_regs,
				  &handle_errors)) {
		subtilis_error_set_not_supported(err, "SWI", p->l->stream->name,
						 p->l->line);
		goto cleanup;
	}

	if ((found_in_regs | expected_in_regs) != expected_in_regs) {
		subtilis_error_set_sys_bad_args(err, p->l->stream->name,
						p->l->line);
		goto cleanup;
	}

	if ((found_out_regs | expected_out_regs) != expected_out_regs) {
		subtilis_error_set_sys_bad_args(err, p->l->stream->name,
						p->l->line);
		goto cleanup;
	}

	tbuf = subtilis_token_get_text(t);
	if ((t->type == SUBTILIS_TOKEN_OPERATOR) && !strcmp(tbuf, ";")) {
		flags_reg = prv_handle_flags(p, t, &flags_local, err);
		if (err->type != SUBTILIS_ERROR_OK)
			goto cleanup;
		handle_errors = false;
	}

	/*
	 * TODO,
	 * 5. Allow unknown constant integer call_names
	 * 9. Add missing SWIs
	 */

	subtilis_ir_section_add_sys_call(
	    p->current, call_name->exp.ir_op.integer, in_regs, out_regs,
	    found_in_regs, found_out_regs, flags_reg, flags_local, err);
	in_regs = NULL;
	out_regs = NULL;
	if (err->type != SUBTILIS_ERROR_OK)
		goto cleanup;

	if (handle_errors)
		subtilis_exp_handle_errors(p, err);

cleanup:

	subtilis_exp_delete(call_name);

	free(out_regs);
	free(in_regs);

	if (input_args) {
		for (i = 0; i < no_inputs; i++)
			subtilis_exp_delete(input_args[i]);
		free(input_args);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %r14
movq %rdi, %r15
xorl %eax, %eax
movb %al, 0x6(%rsp)
movl %eax, 0x1c(%rsp)
movl %eax, 0x18(%rsp)
movl %eax, 0x14(%rsp)
movl %eax, 0x10(%rsp)
movb %al, 0x7(%rsp)
movq %rax, 0x20(%rsp)
movq %rax, 0x30(%rsp)
cmpq %rax, 0x18(%rdi)
je 0x274ba
cmpq $0x0, 0x10(%r15)
je 0x274ba
movq %rsi, %r13
movq %r15, %rdi
movq %r14, %rdx
callq 0x1eb88
cmpl $0x0, (%r14)
jne 0x2775f
movq %rax, %rbx
movq %rax, %rdi
callq 0x37676
testb %al, %al
je 0x273cb
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3691f
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x2766f
movl (%rbx), %eax
cmpl $0x1, %eax
je 0x2741d
cmpl $0x3, %eax
jne 0x274f6
leaq 0x148(%rbx), %rdi
callq 0x14dfb
movq %rax, %r12
movq %rax, %rdi
callq *0x10(%r15)
cmpq $-0x1, %rax
je 0x2763c
movq %rax, %rbp
movq %rbx, %rdi
callq 0x2b4c3
movl %ebp, %edi
movq %r14, %rsi
callq 0x2bb29
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x2766f
movq %r13, %rdi
callq 0x12ad0
cmpl $0x2, (%r13)
jne 0x27443
leaq 0x4c165(%rip), %rsi     # 0x73598
movq %rax, %rdi
callq 0x12160
testl %eax, %eax
je 0x2767c
movq $0x0, 0x8(%rsp)
cmpl $0x1, (%r13)
jne 0x2752e
cmpl $0x86, 0x8(%r13)
jne 0x2752e
leaq 0x30(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %r8
callq 0x27b88
movq %rax, %rbp
cmpl $0x0, (%r14)
jne 0x27714
cmpq $0x0, 0x30(%rsp)
jne 0x27530
movq (%r15), %rax
movq 0x30(%rax), %rdx
movl 0x38(%rax), %ecx
leaq 0x4c5cc(%rip), %r8      # 0x73a73
movq %r14, %rdi
movl $0x4d, %esi
movl $0x15c, %r9d            # imm = 0x15C
jmp 0x2770f
movq (%r15), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x4c5a7(%rip), %rax     # 0x73a73
leaq 0x4c58d(%rip), %rdx     # 0x73a60
leaq 0x4c91b(%rip), %rcx     # 0x73df5
movq %r14, %rdi
movl $0x13, %esi
pushq $0x11a                  # imm = 0x11A
pushq %rax
callq 0x1442a
addq $0x48, %rsp
jmp 0x27763
movq %rbx, %rdi
callq 0x2f882
movq (%r15), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x4c563(%rip), %r10     # 0x73a73
leaq 0x4c571(%rip), %rdx     # 0x73a88
movq %r14, %rdi
movl $0x1d, %esi
movq %rax, %rcx
pushq $0x138                  # imm = 0x138
pushq %r10
jmp 0x27666
xorl %ebp, %ebp
movslq 0x148(%rbx), %rdi
leaq 0x1c(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x6(%rsp), %rcx
callq *0x18(%r15)
testb %al, %al
je 0x27600
movl 0x14(%rsp), %r12d
movl 0x1c(%rsp), %eax
notl %eax
testl %r12d, %eax
jne 0x276cf
movl 0x10(%rsp), %ecx
movl 0x18(%rsp), %eax
notl %eax
testl %ecx, %eax
jne 0x276f0
movl %ecx, 0x2c(%rsp)
movq %r13, %rdi
callq 0x12ad0
movq $-0x1, %r10
cmpl $0x2, (%r13)
jne 0x275b0
leaq 0x4c484(%rip), %rsi     # 0x73a1d
movq %rax, %rdi
callq 0x12160
movq $-0x1, %r10
testl %eax, %eax
je 0x27797
movq 0x48(%r15), %rdi
movslq 0x148(%rbx), %rsi
movzbl 0x7(%rsp), %eax
subq $0x8, %rsp
movq 0x10(%rsp), %rdx
movq %rbp, %rcx
movl %r12d, %r8d
movl 0x34(%rsp), %r9d
pushq %r14
pushq %rax
pushq %r10
callq 0x2d10c
addq $0x20, %rsp
cmpl $0x0, (%r14)
jne 0x2766f
cmpb $0x1, 0x6(%rsp)
jne 0x2766f
movq %r15, %rdi
movq %r14, %rsi
callq 0x2b37c
jmp 0x2766f
movq (%r15), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x4c461(%rip), %rax     # 0x73a73
leaq 0x4fb00(%rip), %rdx     # 0x77119
leaq 0x4c7d5(%rip), %rcx     # 0x73df5
movq %r14, %rdi
movl $0x13, %esi
pushq $0x165                  # imm = 0x165
pushq %rax
callq 0x1442a
addq $0x10, %rsp
jmp 0x27714
movq (%r15), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x4c425(%rip), %rax     # 0x73a73
leaq 0x4c7a0(%rip), %rcx     # 0x73df5
movq %r14, %rdi
movl $0x5a, %esi
movq %r12, %rdx
pushq $0x12d                  # imm = 0x12D
pushq %rax
callq 0x1442a
addq $0x10, %rsp
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %ebp, %ebp
jmp 0x2771c
leaq 0x20(%rsp), %rdx
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rcx
callq 0x277c1
movq %rax, %r12
cmpl $0x0, (%r14)
jne 0x27672
movq 0x20(%rsp), %rdx
testq %rdx, %rdx
je 0x2776e
leaq 0x14(%rsp), %rcx
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %r8
callq 0x2794e
movq %rax, 0x8(%rsp)
cmpl $0x0, (%r14)
je 0x2744c
xorl %r12d, %r12d
xorl %ebp, %ebp
jmp 0x27717
movq (%r15), %rax
movq 0x30(%rax), %rdx
movl 0x38(%rax), %ecx
leaq 0x4c393(%rip), %r8      # 0x73a73
movq %r14, %rdi
movl $0x4d, %esi
movl $0x16b, %r9d            # imm = 0x16B
jmp 0x2770f
movq (%r15), %rax
movq 0x30(%rax), %rdx
movl 0x38(%rax), %ecx
leaq 0x4c372(%rip), %r8      # 0x73a73
movq %r14, %rdi
movl $0x4d, %esi
movl $0x171, %r9d            # imm = 0x171
callq 0x14618
xorl %r12d, %r12d
movq 0x8(%rsp), %r14
movq %rbx, %rdi
callq 0x2b4c3
movq %rbp, %rdi
callq 0x122c0
movq %r14, %rdi
callq 0x122c0
testq %r12, %r12
je 0x2775f
movq 0x20(%rsp), %rbx
testq %rbx, %rbx
je 0x27757
xorl %r14d, %r14d
movq (%r12,%r14,8), %rdi
callq 0x2b4c3
incq %r14
cmpq %r14, %rbx
jne 0x27746
movq %r12, %rdi
callq 0x122c0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rax
movq 0x30(%rax), %rdx
movl 0x38(%rax), %ecx
leaq 0x4c2f4(%rip), %r8      # 0x73a73
movq %r14, %rdi
movl $0x4d, %esi
movl $0x148, %r9d            # imm = 0x148
callq 0x14618
jmp 0x27672
leaq 0x7(%rsp), %rdx
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rcx
callq 0x27d92
cmpl $0x0, (%r14)
jne 0x27714
movq %rax, %r10
movb $0x0, 0x6(%rsp)
jmp 0x275b0