// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb $0x0, -0x41(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movb $0x0, -0x55(%rbp)
movq $-0x1, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x31574
movq -0x8(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x315be
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x38(%rax), %r9d
movl $0x13, %esi
leaq 0x79506(%rip), %rdx     # 0xaaaa0
leaq 0x79cd5(%rip), %rcx     # 0xab276
leaq 0x7950b(%rip), %rax     # 0xaaab3
movq %rax, (%rsp)
movl $0x11a, 0x8(%rsp)       # imm = 0x11A
callq 0x156d0
jmp 0x31a9b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x24a20
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x315e1
jmp 0x31a9b
movq -0x20(%rbp), %rdi
callq 0x49d10
testb $0x1, %al
jne 0x315f0
jmp 0x31615
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x488b0
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x31613
jmp 0x31a3c
jmp 0x31615
movq -0x20(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x316c2
movq -0x20(%rbp), %rdi
addq $0x148, %rdi            # imm = 0x148
callq 0x16760
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rdi
callq *%rax
movq %rax, -0x40(%rbp)
cmpq $-0x1, -0x40(%rbp)
jne 0x31696
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rdx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x38(%rax), %r9d
movl $0x5a, %esi
leaq 0x79bfd(%rip), %rcx     # 0xab276
leaq 0x79433(%rip), %rax     # 0xaaab3
movq %rax, (%rsp)
movl $0x12d, 0x8(%rsp)       # imm = 0x12D
callq 0x156d0
jmp 0x31a3c
movq -0x20(%rbp), %rdi
callq 0x36ee0
movq -0x40(%rbp), %rax
movl %eax, %edi
movq -0x18(%rbp), %rsi
callq 0x37800
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x316c0
jmp 0x31a3c
jmp 0x3172a
movq -0x20(%rbp), %rax
cmpl $0x1, (%rax)
je 0x31728
movq -0x18(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3e190
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x38(%rax), %r9d
movl $0x1d, %esi
leaq 0x793bd(%rip), %rdx     # 0xaaac8
leaq 0x793a1(%rip), %rax     # 0xaaab3
movq %rax, (%rsp)
movl $0x138, 0x8(%rsp)       # imm = 0x138
callq 0x156d0
jmp 0x31a3c
jmp 0x3172a
movq -0x10(%rbp), %rdi
callq 0x12d70
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x317f5
movq -0x28(%rbp), %rdi
leaq 0x78e5a(%rip), %rsi     # 0xaa5a9
callq 0x12140
cmpl $0x0, %eax
jne 0x317f5
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rcx
leaq -0x68(%rbp), %rdx
callq 0x31ab0
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x31784
jmp 0x31a3c
cmpq $0x0, -0x68(%rbp)
jne 0x317c0
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %rdx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x38(%rax), %ecx
movl $0x4d, %esi
leaq 0x79303(%rip), %r8      # 0xaaab3
movl $0x148, %r9d            # imm = 0x148
callq 0x15930
jmp 0x31a3c
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x18(%rbp), %r8
leaq -0x50(%rbp), %rcx
callq 0x31cc0
movq %rax, -0x80(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x317f3
jmp 0x31a3c
jmp 0x317f5
movq -0x10(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x31877
movq -0x10(%rbp), %rax
cmpl $0x86, 0x8(%rax)
jne 0x31877
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %r8
leaq -0x70(%rbp), %rdx
leaq -0x54(%rbp), %rcx
callq 0x32000
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x31839
jmp 0x31a3c
cmpq $0x0, -0x70(%rbp)
jne 0x31875
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %rdx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x38(%rax), %ecx
movl $0x4d, %esi
leaq 0x7924e(%rip), %r8      # 0xaaab3
movl $0x15c, %r9d            # imm = 0x15C
callq 0x15930
jmp 0x31a3c
jmp 0x31877
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x20(%rbp), %rcx
movslq 0x148(%rcx), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x4c(%rbp), %rdx
leaq -0x41(%rbp), %rcx
callq *%rax
testb $0x1, %al
jne 0x318e6
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %r8
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x38(%rax), %r9d
movl $0x13, %esi
leaq 0x7ccc7(%rip), %rdx     # 0xae589
leaq 0x799ad(%rip), %rcx     # 0xab276
leaq 0x791e3(%rip), %rax     # 0xaaab3
movq %rax, (%rsp)
movl $0x165, 0x8(%rsp)       # imm = 0x165
callq 0x156d0
jmp 0x31a3c
movl -0x50(%rbp), %eax
orl -0x48(%rbp), %eax
cmpl -0x48(%rbp), %eax
je 0x31926
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %rdx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x38(%rax), %ecx
movl $0x4d, %esi
leaq 0x7919d(%rip), %r8      # 0xaaab3
movl $0x16b, %r9d            # imm = 0x16B
callq 0x15930
jmp 0x31a3c
movl -0x54(%rbp), %eax
orl -0x4c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
je 0x31966
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %rdx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x38(%rax), %ecx
movl $0x4d, %esi
leaq 0x7915d(%rip), %r8      # 0xaaab3
movl $0x171, %r9d            # imm = 0x171
callq 0x15930
jmp 0x31a3c
movq -0x10(%rbp), %rdi
callq 0x12d70
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x319bc
movq -0x28(%rbp), %rdi
leaq 0x790d6(%rip), %rsi     # 0xaaa5d
callq 0x12140
cmpl $0x0, %eax
jne 0x319bc
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rcx
leaq -0x55(%rbp), %rdx
callq 0x32290
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x319b8
jmp 0x31a3c
movb $0x0, -0x41(%rbp)
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x20(%rbp), %rax
movslq 0x148(%rax), %rsi
movq -0x80(%rbp), %rdx
movq -0x88(%rbp), %rcx
movl -0x50(%rbp), %r8d
movl -0x54(%rbp), %r9d
movq -0x60(%rbp), %r11
movb -0x55(%rbp), %r10b
movq -0x18(%rbp), %rax
andb $0x1, %r10b
movq %r11, (%rsp)
movzbl %r10b, %r10d
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x39ae0
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x31a27
jmp 0x31a3c
testb $0x1, -0x41(%rbp)
je 0x31a3a
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x36ce0
jmp 0x31a3c
movq -0x20(%rbp), %rdi
callq 0x36ee0
movq -0x88(%rbp), %rdi
callq 0x12290
movq -0x80(%rbp), %rdi
callq 0x12290
cmpq $0x0, -0x78(%rbp)
je 0x31a9b
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0x31a92
movq -0x78(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x36ee0
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x31a69
movq -0x78(%rbp), %rdi
callq 0x12290
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)