// File: /workspace/github_c_makefile_star2_O1/System-Glitch[P]ESGILinter-C/src/parsing_functions.c
// Function: parse_function_parameters
static arraylist_t *parse_function_parameters(char *line, unsigned char is_prototype, int line_index) {
	arraylist_t *list            = arraylist_init(5);
	unsigned int start_index     = 0;
	unsigned int index           = 0;
	unsigned int sub_index       = 0;
	unsigned int length          = strlen(line);
	unsigned int type_length     = 0;
	unsigned int star_count_type = 0;
	unsigned int star_count_name = 0;
	unsigned int array_count     = 0;
	unsigned int type_sub_index;
	unsigned char c;
	char *tmp           = NULL;
	char *tmp_name      = NULL;
	char *type          = NULL;
	char *name          = NULL;
	match_t *match_type = NULL;
	match_t *match_name = NULL;
	field_t *field      = NULL;

	do {
		SKIP_WHITESPACES

		start_index = index;

		//Until comma
		while((c = line[index]) != ',' && index < length) {
			index++;
		}

		tmp = strsubstr(line, start_index, index - start_index);
		if(strlen(tmp) == 0) {
			free(tmp);
			break;
		}

		//Parse type
		match_type = parse_type(tmp);
		if(match_type == NULL) {
			free(tmp);
			field_list_free(list);
			return NULL;
		}

		type = substr_match(tmp, *match_type);
		free(match_type);
		type_length     = strlen(type);
		star_count_type = strcount(type, '*');
		type_sub_index  = strcountuntil(type, '*', 1, 1);

		//Remove stars
		type[type_length - type_sub_index] = '\0';

		//If no name and not prototype, invalid syntax
		match_name = parse_variable_name(tmp + type_length, &sub_index, &array_count, NULL);
		if(match_name != NULL) {
			tmp_name = substr_match(tmp + type_length, *match_name);
			free(match_name);

			if(strlen(tmp_name) == 0 && !is_prototype) { //Parameter names compulsory when not prototype
				free(tmp);
				field_list_free(list);
				return NULL;
			}

			star_count_name = strcount(tmp_name, '*');
			sub_index       = strcountuntil(tmp_name, '*', 0, 1);

			//Remove stars
			name = strsubstr(tmp_name, sub_index, strlen(tmp_name) - star_count_name);
			free(tmp_name);
		} else if(!is_prototype) { //Parameter names compulsory when not prototype
			free(tmp);
			field_list_free(list);
			return NULL;
		}

		field = field_init(name, type, star_count_type + star_count_name + array_count, line_index);
		field->is_param = 1;
		arraylist_add(list, field);

		free(tmp);

		index++;
		
	} while(index < length);

	return list;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, 0x30(%rsp)
movl %esi, 0x1c(%rsp)
movq %rdi, %r15
movl $0x5, %edi
callq 0x6ac8
movq %rax, %rbx
xorl %ebp, %ebp
leaq 0x24(%rsp), %rax
movl %ebp, (%rax)
movq %r15, %rdi
callq 0x20b0
movq %rax, %r13
leaq 0x20(%rsp), %rax
movl %ebp, (%rax)
movl %r13d, %r14d
xorl %eax, %eax
movq $0x0, 0x38(%rsp)
movq %rbx, 0x10(%rsp)
movl %eax, 0xc(%rsp)
movl %ebp, %r12d
decl %ebp
movl %ebp, %ebx
movsbl (%r15,%r12), %edi
callq 0x7ed1
incl %ebx
testb %al, %al
je 0xaf74
leaq 0x1(%r12), %rax
cmpq %r14, %r12
movq %rax, %r12
jb 0xaf57
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %ebx, %eax
movl %eax, %ecx
leal 0x1(%rax), %ebp
incl %edx
cmpb $0x2c, (%r15,%rcx)
je 0xaf90
cmpl %r13d, %eax
movl %ebp, %eax
jb 0xaf7b
movq %r15, %rdi
movl %ebx, %esi
callq 0x7db1
movq %rax, %r12
cmpb $0x0, (%rax)
je 0xb114
movq %r12, %rdi
callq 0x6d50
testq %rax, %rax
je 0xb123
movq %rax, %rbx
movq (%rax), %rsi
movq %r12, %rdi
callq 0x8149
movq %r12, 0x28(%rsp)
movq %rax, %r12
movq %rbx, %rdi
callq 0x2030
movq %r12, %rdi
callq 0x20b0
movq %rax, %rbx
movq %r12, %rdi
movl $0x2a, %esi
callq 0x7e07
movl %eax, 0x34(%rsp)
movq %r12, %rdi
movl $0x2a, %esi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x7e3f
movl %ebx, %ecx
subl %eax, %ebx
movq %r12, 0x40(%rsp)
movb $0x0, (%r12,%rbx)
movq 0x28(%rsp), %r12
addq %rcx, %r12
movq %r12, %rdi
leaq 0x24(%rsp), %rsi
leaq 0x20(%rsp), %rdx
xorl %ecx, %ecx
callq 0x8508
testq %rax, %rax
je 0xb0b3
movq %rax, %rbx
movq (%rax), %rsi
movq %r12, %rdi
callq 0x8149
movq %rax, %r12
movq %rbx, %rdi
callq 0x2030
movb (%r12), %al
orb 0x1c(%rsp), %al
je 0xb128
movq %r12, %rdi
movl $0x2a, %esi
callq 0x7e07
movl %eax, 0xc(%rsp)
movq %r12, %rdi
movl $0x2a, %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x7e3f
movl %eax, %ebx
movl %eax, 0x24(%rsp)
movq %r12, %rdi
callq 0x20b0
subl 0xc(%rsp), %eax
movq %r12, %rdi
movl %ebx, %esi
movl %eax, %edx
callq 0x7db1
movq %rax, %rbx
movq %r12, %rdi
movl 0xc(%rsp), %r12d
callq 0x2030
jmp 0xb0c4
cmpb $0x0, 0x1c(%rsp)
movl 0xc(%rsp), %r12d
movq 0x38(%rsp), %rbx
je 0xb128
movl 0x34(%rsp), %eax
addl %r12d, %eax
addl 0x20(%rsp), %eax
movsbl %al, %edx
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
movl 0x30(%rsp), %ecx
callq 0x6090
movb $0x1, 0x24(%rax)
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6b96
movq 0x28(%rsp), %rdi
callq 0x2030
cmpl %r13d, %ebp
movl %r12d, %eax
jb 0xaf4c
jmp 0xb13e
movq %r12, %rdi
callq 0x2030
movq 0x10(%rsp), %rbx
jmp 0xb13e
movq %r12, %rdi
jmp 0xb12d
movq 0x28(%rsp), %rdi
callq 0x2030
movq 0x10(%rsp), %rdi
callq 0x6899
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq