// File: /workspace/github_c_makefile_star2_O2/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 $0x78, %rsp
movl %edi, %r13d
movq %rsi, %rdi
callq 0x77dc
movq %rax, %rbx
movq %rax, %rdi
callq 0x20b0
decl %eax
movslq %eax, %r14
movsbl (%rbx,%r14), %edi
callq 0x7ace
movl %r14d, %ecx
decq %r14
testl %ecx, %ecx
js 0xa4fb
testb %al, %al
jne 0xa4e3
movzbl 0x1(%rbx,%r14), %eax
cmpl $0x7b, %eax
setne %cl
negb %cl
cmpl $0x3b, %eax
movzbl %cl, %ecx
pushq $0x1
popq %r12
cmovnel %ecx, %r12d
orl $0x40, %eax
cmpl $0x7b, %eax
jne 0xa65c
movq %rbx, %rdi
callq 0x6a30
testq %rax, %rax
je 0xa65c
movq %rax, %r15
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x7d20
movq %rax, %r14
movl (%r15), %ebp
movq %r15, %rdi
callq 0x2030
testq %r14, %r14
je 0xa5da
movl %r12d, 0xc(%rsp)
movq %r14, %rdi
callq 0x20b0
movq %rax, %r12
addl %r12d, %ebp
movq %rbx, %rdi
callq 0x20b0
subl %r12d, %eax
movq %rbx, %rdi
movl %ebp, %esi
movl %eax, %edx
callq 0x79a6
movq %rax, %r15
movq %rax, %rdi
callq 0x20b0
testl %eax, %eax
je 0xa5df
movq %rax, 0x10(%rsp)
pushq $0x2a
popq %rbp
movq %r14, %rdi
movl %ebp, %esi
callq 0x79fc
movl %eax, 0x5c(%rsp)
pushq $0x1
popq %rcx
movq %r14, %rdi
movl %ebp, %esi
movl %ecx, %edx
callq 0x7a2e
subl %eax, %r12d
movb $0x0, (%r14,%r12)
movq %r14, %rdi
movl %ebp, %esi
callq 0x79fc
testl %eax, %eax
je 0xa5f4
movq %r15, %rdi
callq 0x2030
movq %r14, %rdi
jmp 0xa657
xorl %r15d, %r15d
jmp 0xa5ea
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
jmp 0xa667
movq %r14, 0x48(%rsp)
movl %r13d, 0x24(%rsp)
movq %r15, %rdi
callq 0x20b0
movl %eax, %ebp
xorl %r13d, %r13d
movsbl (%r15,%r13), %edi
callq 0x7ace
leaq 0x1(%r13), %r12
testb %al, %al
je 0xa625
cmpq %rbp, %r13
movq %r12, %r13
jb 0xa60b
xorl %eax, %eax
callq 0x7e26
testq %rax, %rax
je 0xa64a
movq %rax, %r14
movzbl -0x1(%r15,%r12), %eax
orl $0x40, %eax
cmpl $0x7b, %eax
jne 0xa679
movq %r14, %rdi
callq 0x2030
movq %r15, %rdi
callq 0x2030
movq 0x48(%rsp), %rdi
callq 0x2030
movq %rbx, %rdi
callq 0x2030
xorl %r15d, %r15d
movq %r15, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x1(%r12), %rax
addq $-0x2, %r12
movl %eax, (%r14)
movsbl 0x1(%r15,%r12), %edi
incq %r12
cmpl $0x2a, %edi
setne %r13b
callq 0x7dfb
testb %al, %al
sete %al
testb %al, %r13b
jne 0xa6a9
cmpq %rbp, %r12
jb 0xa685
movl %r12d, 0x4(%r14)
movsbl (%r15,%r12), %edi
callq 0x7ace
leaq 0x1(%r12), %r13
testb %al, %al
je 0xa6c8
cmpq %rbp, %r12
movq %r13, %r12
jb 0xa6ad
cmpb $0x28, -0x1(%r15,%r13)
jne 0xa642
movq (%r14), %rsi
movq %r15, %rdi
callq 0x7d20
movq %rax, %r12
movq %r14, %rdi
callq 0x2030
pushq $0x2a
popq %rbp
movq %r12, %rdi
movl %ebp, %esi
callq 0x79fc
movl %eax, %r14d
pushq $0x1
popq %rcx
movq %r12, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x7a2e
movl %eax, %ebp
movq %r12, %rdi
callq 0x20b0
subl %r14d, %eax
movq %r12, %rdi
movl %ebp, %esi
movl %eax, %edx
callq 0x79a6
movq %rax, %rbp
movq %r12, %rdi
callq 0x2030
testq %rbp, %rbp
je 0xa64a
movl %r13d, %eax
movl 0x10(%rsp), %ecx
pushq $-0x1
popq %rdx
movb (%r15,%rax), %sil
incl %edx
cmpq %rcx, %rax
jae 0xa754
incq %rax
cmpb $0x29, %sil
jne 0xa740
cmpb $0x29, %sil
je 0xa76a
movq %r15, %rdi
callq 0x2030
movq %rbp, %rdi
jmp 0xa64d
movq %rbp, 0x68(%rsp)
movl %r14d, 0x54(%rsp)
movq %r15, %rdi
movl %r13d, %esi
callq 0x79a6
movq %rax, %r12
pushq $0x5
popq %rdi
callq 0x67e4
movq %rax, 0x18(%rsp)
leaq 0x2c(%rsp), %rax
andl $0x0, (%rax)
movq %r12, %r14
movq %r12, %rdi
callq 0x20b0
leaq 0x28(%rsp), %rcx
andl $0x0, (%rcx)
movq %rax, 0x70(%rsp)
movl %eax, %ebp
xorl %r13d, %r13d
movl $0x0, 0x10(%rsp)
xorl %r12d, %r12d
movq %r12, 0x30(%rsp)
movl %r13d, %r12d
decq %r12
movsbl 0x1(%r14,%r12), %edi
incq %r12
callq 0x7ace
testb %al, %al
je 0xa7e1
cmpq %rbp, %r12
jb 0xa7ca
pushq $-0x1
popq %rdx
movq %r12, %rax
leaq 0x1(%rax), %r13
incl %edx
cmpb $0x2c, (%r14,%rax)
je 0xa7fc
cmpq %rbp, %rax
movq %r13, %rax
jb 0xa7e7
movq %r14, %rdi
movl %r12d, %esi
callq 0x79a6
movq %rax, %r12
cmpb $0x0, (%rax)
je 0xa99b
movq %r12, %rdi
callq 0x6a30
movq %r12, %rdi
testq %rax, %rax
je 0xa9cd
movq %rax, %r12
movq (%rax), %rsi
movq %rdi, 0x38(%rsp)
callq 0x7d20
movq %rax, 0x60(%rsp)
movq %r12, %rdi
callq 0x2030
movq 0x60(%rsp), %r12
movq %r12, %rdi
callq 0x20b0
movq %rax, 0x40(%rsp)
movq %r12, %rdi
pushq $0x2a
popq %rsi
callq 0x79fc
movl %eax, 0x58(%rsp)
movq %r12, %rdi
pushq $0x2a
popq %rsi
pushq $0x1
popq %rcx
movl %ecx, %edx
callq 0x7a2e
movq 0x40(%rsp), %rdx
movl %edx, %ecx
subl %eax, %edx
movb $0x0, (%r12,%rdx)
movq 0x38(%rsp), %r12
addq %rcx, %r12
movq %r12, %rdi
leaq 0x2c(%rsp), %rsi
leaq 0x28(%rsp), %rdx
xorl %ecx, %ecx
callq 0x8014
testq %rax, %rax
je 0xa938
movq (%rax), %rsi
movq %r12, %rdi
movq %rax, 0x10(%rsp)
callq 0x7d20
movq %rax, %r12
movq 0x10(%rsp), %rdi
callq 0x2030
movq %r12, %rcx
movb (%r12), %al
movl 0xc(%rsp), %r12d
orb %r12b, %al
je 0xa9af
movq %rcx, %rdi
movq %rcx, 0x40(%rsp)
pushq $0x2a
popq %rsi
callq 0x79fc
movl %eax, 0x10(%rsp)
movq 0x40(%rsp), %r12
movq %r12, %rdi
pushq $0x2a
popq %rsi
xorl %edx, %edx
pushq $0x1
popq %rcx
callq 0x7a2e
movl %eax, 0x30(%rsp)
movl %eax, 0x2c(%rsp)
movq %r12, %rdi
callq 0x20b0
subl 0x10(%rsp), %eax
movq %r12, %rdi
movl 0x30(%rsp), %esi
movl %eax, %edx
callq 0x79a6
movq %rax, 0x30(%rsp)
movq %r12, %rdi
callq 0x2030
movl 0x10(%rsp), %ecx
jmp 0xa947
cmpb $0x0, 0xc(%rsp)
movl 0x10(%rsp), %ecx
je 0xa9c8
movl %ecx, 0x10(%rsp)
movl 0x58(%rsp), %eax
addl %ecx, %eax
addl 0x28(%rsp), %eax
movsbl %al, %edx
movq 0x30(%rsp), %r12
movq %r12, %rdi
movq 0x60(%rsp), %rsi
movl 0x24(%rsp), %ecx
callq 0x5ee4
movb $0x1, 0x24(%rax)
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x68b5
movq 0x38(%rsp), %rdi
callq 0x2030
cmpl 0x70(%rsp), %r13d
jb 0xa7bf
movq 0x18(%rsp), %r13
jmp 0xa9df
movq %r12, %rdi
callq 0x2030
movl 0xc(%rsp), %r12d
movq 0x18(%rsp), %r13
jmp 0xa9e4
movq 0x38(%rsp), %rdi
callq 0x2030
movq 0x18(%rsp), %rdi
callq 0x6605
xorl %r13d, %r13d
jmp 0xa9e4
movq 0x38(%rsp), %rdi
callq 0x2030
movq 0x18(%rsp), %rdi
callq 0x6605
xorl %r13d, %r13d
movl 0xc(%rsp), %r12d
movq %r14, %rdi
callq 0x2030
movq %r15, %rdi
callq 0x2030
testq %r13, %r13
movl 0x24(%rsp), %ebp
movq 0x48(%rsp), %r14
movl 0x5c(%rsp), %r15d
je 0xa65c
movq %r14, %rdi
callq 0x794d
movl 0x54(%rsp), %ecx
addl %r15d, %ecx
movzbl %cl, %ecx
movzbl %r12b, %esi
movq 0x68(%rsp), %rdi
movq %rax, %rdx
movq %r13, %r8
movl %ebp, %r9d
callq 0xa468
movq %rax, %r15
jmp 0xa5e2