// File: /workspace/github_c_makefile_star2_O3/System-Glitch[P]ESGILinter-C/src/parsing_functions.c
// Function: get_function_from_declaration
function_t *get_function_from_declaration(int line_index, char *line) {
	unsigned int star_count_type;
	unsigned int star_count_name;
	unsigned int type_sub_index;
	unsigned int length;
	unsigned int type_length;
	unsigned int type_start_index   = 0;
	unsigned int params_start_index = 0;
	unsigned int index              = 0;
	function_t *function = NULL;
	arraylist_t *params  = NULL;
	match_t *match_type  = NULL;
	char *tmp_name;
	char *tmp_params;
	char *name = NULL;
	char *type = NULL;
	char is_prototype;

	line = str_remove_comments(line);

	is_prototype = function_is_prototype(line);
	if(is_prototype == -1) {
		free(line);
		free(match_type);
		return NULL;
	}

	match_type = parse_type(line);
	if(match_type == NULL) {
		free(line);
		return NULL;
	}

	type = substr_match(line, *match_type);
	type_start_index = match_type->index_start;
	free(match_type);

	if(type != NULL) {

		type_length = strlen(type);
		tmp_name    = strsubstr(line, type_start_index + type_length, strlen(line) - type_length);
		length      = strlen(tmp_name);

		if(length > 0) {
			star_count_type = strcount(type, '*');
			type_sub_index  = strcountuntil(type, '*', 1, 1);

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

			//No array modifier in type

			if(strcount(type, '*')) { //Contains stars inside the type name -> invalid type
				free(tmp_name);
				free(type);
				free(line);
				return NULL;
			}

			name = parse_function_name(tmp_name, &star_count_name, &params_start_index);

			if(name != NULL) {

				index = params_start_index;
				//Find closing parenthesis
				while(tmp_name[index] != ')' && index < length) {
					index++;
				}

				if(tmp_name[index] != ')') { //Syntax error, no closing parenthesis found
					free(tmp_name);
					free(name);
					free(type);
					free(line);
					return NULL;
				}

				tmp_params = strsubstr(tmp_name, params_start_index, index - params_start_index);

				params = parse_function_parameters(tmp_params, is_prototype, line_index);
				free(tmp_params);
				free(tmp_name);

				if(params != NULL) {
					function = function_init(name, is_prototype, strduplicate(type), star_count_type + star_count_name, params, line_index);
				} else {
					free(line);
					return NULL;
				}
			} else {
				free(tmp_name);
				free(type);
				free(line);
				return NULL;
			}

		}

		free(type);
	}

	free(line);

	return function;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edi, %ebp
movq %rsi, %rdi
callq 0x7c54
movq %rax, %rbx
movq %rax, %rdi
callq 0x20b0
decl %eax
movslq %eax, %r14
movsbl (%rbx,%r14), %edi
callq 0x7f3a
movl %r14d, %ecx
decq %r14
testl %ecx, %ecx
js 0xacab
testb %al, %al
jne 0xac93
movzbl 0x1(%rbx,%r14), %eax
cmpl $0x7b, %eax
setne %cl
negb %cl
cmpl $0x3b, %eax
movzbl %cl, %ecx
movl $0x1, %r13d
cmovnel %ecx, %r13d
orl $0x40, %eax
cmpl $0x7b, %eax
jne 0xae04
movq %rbx, %rdi
callq 0x6de4
testq %rax, %rax
je 0xae04
movq %rax, %r12
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x81b3
movq %rax, %r14
movl (%r12), %r15d
movq %r12, %rdi
callq 0x2030
testq %r14, %r14
je 0xad98
movq %r14, %rdi
callq 0x20b0
movq %rax, %r12
addl %r12d, %r15d
movq %rbx, %rdi
callq 0x20b0
subl %r12d, %eax
movq %rbx, %rdi
movl %r15d, %esi
movl %eax, %edx
callq 0x7e1a
movq %rax, %r15
movq %rax, %rdi
callq 0x20b0
testl %eax, %eax
je 0xafc2
movq %rax, 0x10(%rsp)
movq %r14, %rdi
movl $0x2a, %esi
callq 0x7e70
movl %eax, 0x24(%rsp)
movq %r14, %rdi
movl $0x2a, %esi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x7ea8
subl %eax, %r12d
movb $0x0, (%r14,%r12)
movq %r14, %rdi
movl $0x2a, %esi
callq 0x7e70
testl %eax, %eax
je 0xada0
movq %r15, %rdi
callq 0x2030
movq %r14, %rdi
jmp 0xadff
xorl %r15d, %r15d
jmp 0xafcd
movq %r14, 0x8(%rsp)
movl %ebp, 0x20(%rsp)
movq %r15, %rdi
callq 0x20b0
movl %eax, %ebp
xorl %r14d, %r14d
movsbl (%r15,%r14), %edi
callq 0x7f3a
leaq 0x1(%r14), %r12
testb %al, %al
je 0xadd0
cmpq %rbp, %r14
movq %r12, %r14
jb 0xadb6
xorl %eax, %eax
callq 0x82d2
testq %rax, %rax
je 0xadf2
movq %rax, %rdi
movzbl -0x1(%r15,%r12), %eax
orl $0x40, %eax
cmpl $0x7b, %eax
jne 0xae14
callq 0x2030
movq %r15, %rdi
callq 0x2030
movq 0x8(%rsp), %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
xorl %r15d, %r15d
jmp 0xafd5
leaq -0x1(%r12), %rax
addq $-0x2, %r12
movq %rdi, (%rsp)
movl %eax, (%rdi)
movsbl 0x1(%r15,%r12), %edi
incq %r12
cmpl $0x2a, %edi
setne %r14b
callq 0x82a7
testb %al, %al
sete %al
testb %al, %r14b
jne 0xae47
cmpq %rbp, %r12
jb 0xae23
movq (%rsp), %rax
movl %r12d, 0x4(%rax)
movsbl (%r15,%r12), %edi
callq 0x7f3a
leaq 0x1(%r12), %r14
testb %al, %al
je 0xae6a
cmpq %rbp, %r12
movq %r14, %r12
jb 0xae4f
cmpb $0x28, -0x1(%r15,%r14)
jne 0xaf2b
movq (%rsp), %r12
movq (%r12), %rsi
movq %r15, %rdi
callq 0x81b3
movq %rax, %rbp
movq %r12, %rdi
callq 0x2030
movq %rbp, %rdi
movl $0x2a, %esi
callq 0x7e70
movl %eax, %r12d
movq %rbp, %rdi
movl $0x2a, %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x7ea8
movl %eax, (%rsp)
movq %rbp, %rdi
callq 0x20b0
movl %r12d, 0x1c(%rsp)
subl %r12d, %eax
movq %rbp, %rdi
movq %rbp, %r12
movl (%rsp), %esi
movl %eax, %edx
callq 0x7e1a
movq %rax, %rbp
movq %r12, %rdi
callq 0x2030
testq %rbp, %rbp
je 0xadf2
movq %rbp, (%rsp)
movl %r14d, %eax
movl 0x10(%rsp), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movb (%r15,%rax), %sil
incl %edx
cmpq %rcx, %rax
jae 0xaf10
incq %rax
cmpb $0x29, %sil
jne 0xaefc
cmpb $0x29, %sil
movl 0x20(%rsp), %ebp
je 0xaf34
movq %r15, %rdi
callq 0x2030
movq (%rsp), %rdi
jmp 0xadf5
movq (%rsp), %rdi
jmp 0xaded
movq %r15, %rdi
movl %r14d, %esi
callq 0x7e1a
movq %rax, %r12
movzbl %r13b, %esi
movq %rax, %rdi
movl %ebp, %edx
callq 0xafe7
movq %r12, %rdi
movq %rax, %r12
callq 0x2030
movq %r15, %rdi
callq 0x2030
testq %r12, %r12
je 0xae04
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x7dc1
movq %rax, 0x10(%rsp)
movl $0x30, %edi
callq 0x2140
testq %rax, %rax
je 0xafc2
movq %rax, %r15
movl 0x1c(%rsp), %eax
addl 0x24(%rsp), %eax
movq (%rsp), %rcx
movq %rcx, (%r15)
movb %r13b, 0x18(%r15)
movq 0x10(%rsp), %rcx
movq %rcx, 0x8(%r15)
movb %al, 0x10(%r15)
movq %r12, 0x20(%r15)
movl %ebp, 0x28(%r15)
movl $0x0, 0x2c(%r15)
jmp 0xafc5
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq