// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movl %edi, %ebp
movq %rsi, %rdi
callq 0x7be4
movq %rax, %rbx
movq %rax, %rdi
callq 0x20b0
decl %eax
movslq %eax, %r14
movsbl (%rbx,%r14), %edi
callq 0x7ed1
movl %r14d, %ecx
decq %r14
testl %ecx, %ecx
js 0xabaf
testb %al, %al
jne 0xab97
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 0xaedb
movq %rbx, %rdi
callq 0x6d50
testq %rax, %rax
je 0xaedb
movq %rax, %r12
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x8149
movq %rax, %r14
movl (%r12), %r15d
movq %r12, %rdi
callq 0x2030
testq %r14, %r14
je 0xaedb
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 0x7db1
movq %rax, %r15
movq %rax, %rdi
callq 0x20b0
testl %eax, %eax
je 0xac93
movq %rax, 0x30(%rsp)
movq %r14, %rdi
movl $0x2a, %esi
callq 0x7e07
movl %eax, 0x10(%rsp)
movq %r14, %rdi
movl $0x2a, %esi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x7e3f
subl %eax, %r12d
movb $0x0, (%r14,%r12)
movq %r14, %rdi
movl $0x2a, %esi
callq 0x7e07
testl %eax, %eax
je 0xaca3
movq %r15, %rdi
callq 0x2030
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x2030
jmp 0xaede
movl %r13d, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movl %ebp, 0x1c(%rsp)
movq %r15, %rdi
callq 0x20b0
movq %rax, 0x28(%rsp)
movl %eax, %r14d
movl $0xfffffffe, %r12d      # imm = 0xFFFFFFFE
xorl %r13d, %r13d
movsbl (%r15,%r13), %edi
callq 0x7ed1
leaq 0x1(%r13), %rbp
incl %r12d
testb %al, %al
je 0xace7
cmpq %r14, %r13
movq %rbp, %r13
jb 0xacca
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x8287
testq %rax, %rax
je 0xad0f
movq %rax, %rdi
movzbl -0x1(%r15,%rbp), %eax
orl $0x40, %eax
cmpl $0x7b, %eax
jne 0xad20
callq 0x2030
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x20(%rsp), %r14
movl 0x10(%rsp), %ebp
jmp 0xae08
leal 0x1(%r12), %eax
movq %rdi, 0x8(%rsp)
movl %eax, (%rdi)
movq 0x28(%rsp), %r13
incl %r12d
movsbl (%r15,%r12), %edi
cmpl $0x2a, %edi
setne %bpl
callq 0x825c
testb %al, %al
sete %al
testb %al, %bpl
jne 0xad54
cmpl %r13d, %r12d
jb 0xad31
movq 0x8(%rsp), %rax
movl %r12d, 0x4(%rax)
movl 0x10(%rsp), %ebp
movsbl (%r15,%r12), %edi
callq 0x7ed1
leaq 0x1(%r12), %r13
testb %al, %al
je 0xad7c
cmpq %r14, %r12
movq %r13, %r12
jb 0xad61
cmpb $0x28, -0x1(%r15,%r13)
jne 0xadf3
movq 0x8(%rsp), %r12
movq (%r12), %rsi
movq %r15, %rdi
callq 0x8149
movq %rax, %r14
movq %r12, %rdi
callq 0x2030
movq %r14, %rdi
movl $0x2a, %esi
callq 0x7e07
movl %eax, %r12d
movq %r14, %rdi
movl $0x2a, %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x7e3f
movl %eax, 0x8(%rsp)
movq %r14, %rdi
callq 0x20b0
movl %r12d, 0x28(%rsp)
subl %r12d, %eax
movq %r14, %rdi
movl 0x8(%rsp), %esi
movl %eax, %edx
callq 0x7db1
movq %rax, %r12
movq %r14, %rdi
callq 0x2030
jmp 0xae03
movq 0x8(%rsp), %rdi
callq 0x2030
xorl %r13d, %r13d
xorl %r12d, %r12d
movq 0x20(%rsp), %r14
testq %r12, %r12
je 0xac8b
movl %r13d, %eax
movl 0x30(%rsp), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movb (%r15,%rax), %sil
incl %edx
cmpq %rcx, %rax
jae 0xae31
incq %rax
cmpb $0x29, %sil
jne 0xae1d
cmpb $0x29, %sil
je 0xae47
movq %r15, %rdi
callq 0x2030
movq %r12, %rdi
jmp 0xac8e
movq %r12, 0x10(%rsp)
movq %r15, %rdi
movl %r13d, %esi
callq 0x7db1
movq %rax, %r13
movzbl 0x18(%rsp), %esi
movq %rax, %rdi
movl 0x1c(%rsp), %edx
callq 0xaef8
movq %rax, %r12
movq %r13, %rdi
callq 0x2030
movq %r15, %rdi
callq 0x2030
testq %r12, %r12
je 0xaedb
movq %r14, %rdi
callq 0x7d58
movq %rax, %r13
movl $0x30, %edi
callq 0x2140
movq %rax, %r15
testq %rax, %rax
je 0xac96
movl 0x28(%rsp), %ecx
addl %ebp, %ecx
movq 0x10(%rsp), %rax
movq %rax, (%r15)
movl 0x18(%rsp), %eax
movb %al, 0x18(%r15)
movq %r13, 0x8(%r15)
movb %cl, 0x10(%r15)
movq %r12, 0x20(%r15)
movl 0x1c(%rsp), %eax
movl %eax, 0x28(%r15)
movl $0x0, 0x2c(%r15)
jmp 0xac96
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0x2030
movq %r15, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq