// File: /workspace/github_c_makefile_star2_O3/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 0x272a3
cmpq $0x0, 0x10(%r15)
je 0x272a3
movq %rsi, %r13
movq %r15, %rdi
movq %r14, %rdx
callq 0x1ec4c
cmpl $0x0, (%r14)
jne 0x27554
movq %rax, %rbx
movq %rax, %rdi
callq 0x37534
testb %al, %al
je 0x2721f
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x367cf
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x274ba
movl (%rbx), %eax
cmpl $0x1, %eax
je 0x27271
cmpl $0x3, %eax
jne 0x272df
leaq 0x148(%rbx), %rdi
callq 0x14db1
movq %rax, %r12
movq %rax, %rdi
callq *0x10(%r15)
cmpq $-0x1, %rax
je 0x27487
movq %rax, %rbp
movq %rbx, %rdi
callq 0x2b166
movl %ebp, %edi
movq %r14, %rsi
callq 0x2b7af
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x274ba
movq %r13, %rdi
callq 0x12ad0
movl (%r13), %ecx
cmpl $0x2, %ecx
jne 0x27317
cmpb $0x2c, (%rax)
jne 0x27295
cmpb $0x0, 0x1(%rax)
je 0x27563
movq $0x0, 0x8(%rsp)
jmp 0x27380
movq (%r15), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x4c7dc(%rip), %rax     # 0x73a91
leaq 0x4c7c2(%rip), %rdx     # 0x73a7e
leaq 0x4cb52(%rip), %rcx     # 0x73e15
movq %r14, %rdi
movl $0x13, %esi
pushq $0x11a                  # imm = 0x11A
pushq %rax
callq 0x143da
addq $0x48, %rsp
jmp 0x27558
movq %rbx, %rdi
callq 0x2f5ad
movq (%r15), %rcx
movq 0x30(%rcx), %r8
movl 0x38(%rcx), %r9d
leaq 0x4c798(%rip), %r10     # 0x73a91
leaq 0x4c7a6(%rip), %rdx     # 0x73aa6
movq %r14, %rdi
movl $0x1d, %esi
movq %rax, %rcx
pushq $0x138                  # imm = 0x138
pushq %r10
jmp 0x274b1
movq $0x0, 0x8(%rsp)
cmpl $0x1, %ecx
jne 0x27380
cmpl $0x86, 0x8(%r13)
jne 0x27380
leaq 0x30(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %r8
callq 0x27994
movq %rax, %rbp
cmpl $0x0, (%r14)
jne 0x27509
cmpq $0x0, 0x30(%rsp)
jne 0x27382
movq (%r15), %rax
movq 0x30(%rax), %rdx
movl 0x38(%rax), %ecx
leaq 0x4c724(%rip), %r8      # 0x73a91
movq %r14, %rdi
movl $0x4d, %esi
movl $0x15c, %r9d            # imm = 0x15C
jmp 0x27504
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 0x2744b
movl 0x14(%rsp), %r12d
movl 0x1c(%rsp), %eax
notl %eax
testl %r12d, %eax
jne 0x274c4
movl 0x10(%rsp), %ecx
movl 0x18(%rsp), %eax
notl %eax
testl %ecx, %eax
jne 0x274e5
movl %ecx, 0x2c(%rsp)
movq %r13, %rdi
callq 0x12ad0
movq %rax, %rcx
movq $-0x1, %rax
cmpl $0x2, (%r13)
jne 0x273f6
cmpb $0x3b, (%rcx)
jne 0x273f6
cmpb $0x0, 0x1(%rcx)
je 0x275de
xorl %ecx, %ecx
movq 0x8(%rsp), %rdx
movq 0x48(%r15), %rdi
movslq 0x148(%rbx), %rsi
subq $0x8, %rsp
movzbl %cl, %r10d
andl $0x1, %r10d
movq %rbp, %rcx
movl %r12d, %r8d
movl 0x34(%rsp), %r9d
pushq %r14
pushq %r10
pushq %rax
callq 0x2cd9b
addq $0x20, %rsp
cmpl $0x0, (%r14)
jne 0x274ba
cmpb $0x1, 0x6(%rsp)
jne 0x274ba
movq %r15, %rdi
movq %r14, %rsi
callq 0x2b01f
jmp 0x274ba
movq (%r15), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x4c634(%rip), %rax     # 0x73a91
leaq 0x4fd19(%rip), %rdx     # 0x7717d
leaq 0x4c9aa(%rip), %rcx     # 0x73e15
movq %r14, %rdi
movl $0x13, %esi
pushq $0x165                  # imm = 0x165
pushq %rax
callq 0x143da
addq $0x10, %rsp
jmp 0x27509
movq (%r15), %rax
movq 0x30(%rax), %r8
movl 0x38(%rax), %r9d
leaq 0x4c5f8(%rip), %rax     # 0x73a91
leaq 0x4c975(%rip), %rcx     # 0x73e15
movq %r14, %rdi
movl $0x5a, %esi
movq %r12, %rdx
pushq $0x12d                  # imm = 0x12D
pushq %rax
callq 0x143da
addq $0x10, %rsp
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %ebp, %ebp
jmp 0x27511
movq (%r15), %rax
movq 0x30(%rax), %rdx
movl 0x38(%rax), %ecx
leaq 0x4c5bc(%rip), %r8      # 0x73a91
movq %r14, %rdi
movl $0x4d, %esi
movl $0x16b, %r9d            # imm = 0x16B
jmp 0x27504
movq (%r15), %rax
movq 0x30(%rax), %rdx
movl 0x38(%rax), %ecx
leaq 0x4c59b(%rip), %r8      # 0x73a91
movq %r14, %rdi
movl $0x4d, %esi
movl $0x171, %r9d            # imm = 0x171
callq 0x145c8
xorl %r12d, %r12d
movq 0x8(%rsp), %r14
movq %rbx, %rdi
callq 0x2b166
movq %rbp, %rdi
callq 0x122c0
movq %r14, %rdi
callq 0x122c0
testq %r12, %r12
je 0x27554
movq 0x20(%rsp), %rbx
testq %rbx, %rbx
je 0x2754c
xorl %r14d, %r14d
movq (%r12,%r14,8), %rdi
callq 0x2b166
incq %r14
cmpq %r14, %rbx
jne 0x2753b
movq %r12, %rdi
callq 0x122c0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %rdx
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rcx
callq 0x27612
movq %rax, %r12
cmpl $0x0, (%r14)
jne 0x274bd
movq 0x20(%rsp), %rdx
testq %rdx, %rdx
je 0x275b5
leaq 0x14(%rsp), %rcx
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %r8
callq 0x27770
movq %rax, 0x8(%rsp)
cmpl $0x0, (%r14)
je 0x27609
xorl %r12d, %r12d
xorl %ebp, %ebp
jmp 0x2750c
movq (%r15), %rax
movq 0x30(%rax), %rdx
movl 0x38(%rax), %ecx
leaq 0x4c4cb(%rip), %r8      # 0x73a91
movq %r14, %rdi
movl $0x4d, %esi
movl $0x148, %r9d            # imm = 0x148
callq 0x145c8
jmp 0x274bd
leaq 0x7(%rsp), %rdx
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rcx
callq 0x27bab
cmpl $0x0, (%r14)
jne 0x27509
movb $0x0, 0x6(%rsp)
movb 0x7(%rsp), %cl
jmp 0x273f8
movl (%r13), %ecx
jmp 0x27320