// File: /workspace/github_c_makefile_star2_O0/System-Glitch[P]ESGILinter-C/src/parsing_operations.c
// Function: parse_ternary_condition
static type_t parse_ternary_condition(char *line, int length, int line_index, scope_t *scope, messages_t *messages) {
	type_t type;
	type_t condition_type;
	type_t left_operand_type;
	type_t right_operand_type;
	int rank                = 0;
	int index_operator      = 0;
	char has_question_mark  = 0;
	char has_colon          = 0;
	int index_question_mark = -1;
	int index_colon         = -1;
	char *question_mark     = NULL;
	char *colon             = NULL;
	char *condition         = NULL;
	char *left_operand      = NULL;
	char *right_operand     = NULL;
	char* occurrence        = NULL;
	const char* operator    = NULL;

	type.name = strduplicate("NULL");
	type.is_pointer = 0;
	type.is_literal = 0;

	//Condition until '?'
	question_mark = line;
	while((question_mark = strstr(question_mark, "?")) != NULL) {
		if(!check_quotes(line, question_mark, length) && !check_parenthesis(line, question_mark, length)) {
			has_question_mark = 1;
			break;
		}
		question_mark++;
	}
	if(!has_question_mark) return type;

	//Left operand until ':'
	colon = question_mark;
	while((colon = strstr(colon, ":")) != NULL) {
		if(!check_quotes(line, colon, length) && !check_parenthesis(line, colon, length)) {
			has_colon = 1;
			break;
		}
		colon++;
	}
	if(!has_colon) return type;

	index_question_mark = question_mark - line;
	index_colon         = colon - line;

	condition = strsubstr(line, 0, index_question_mark - 1);

	//If condition is assignation, isn't ternary condition
	while((operator = assignation_operators[index_operator++]) != NULL) {
		occurrence = strstr(line, operator);
		if(occurrence != NULL && *(occurrence + 1) != '=') {

			//Check if inside quotes
			while(check_quotes(line, occurrence, length) || check_parenthesis(line, occurrence, length)) {
				occurrence = strstr(occurrence + 1, operator);
				if(occurrence == NULL) break;
			}

			if(occurrence == NULL) continue;
			else {
				free(condition);
				return type;
			}
		}
	}

	left_operand  = strsubstr(line, index_question_mark + 1, index_colon - index_question_mark - 1);
	right_operand = strsubstr(line, index_colon + 1, length - index_colon - 1);

	left_operand_type  = parse_operand(left_operand, line_index, scope, messages);
	right_operand_type = parse_operand(right_operand, line_index, scope, messages);

	if(strcmp(left_operand_type.name,"NULL") && strcmp(right_operand_type.name,"NULL")) {
		condition_type = parse_operand(condition, line_index, scope, messages);

		rank = get_highest_rank(right_operand_type.name, left_operand_type.name);
		if(rank != -1) {
			free(type.name);
			type.name       = strduplicate((char*)type_rank[rank]);
			type.is_pointer = right_operand_type.is_pointer > left_operand_type.is_pointer ? 
									right_operand_type.is_pointer : left_operand_type.is_pointer;
		}
		if(!type_equals(&left_operand_type, &right_operand_type) && messages->ternary_types) {
			add_wrong_type_message(messages->ternary_types, &left_operand_type, &right_operand_type);
		}

		free(condition_type.name);
	}

	free(right_operand_type.name);
	free(left_operand_type.name);
	free(condition);
	free(left_operand);
	free(right_operand);

	return type;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x18(%rbp)
movl %esi, -0x1c(%rbp)
movl %edx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
movb $0x0, -0x69(%rbp)
movb $0x0, -0x6a(%rbp)
movl $0xffffffff, -0x70(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x74(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xa8(%rbp)
movq $0x0, -0xb0(%rbp)
leaq 0xb04e(%rip), %rdi      # 0x148a0
callq 0xa550
movq %rax, -0x10(%rbp)
movb $0x0, -0x8(%rbp)
movb $0x0, -0x7(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
leaq 0xb02f(%rip), %rsi      # 0x148a5
callq 0x2190
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x98c1
movq -0x18(%rbp), %rdi
movq -0x80(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0xae60
cmpb $0x0, %al
jne 0x98b3
movq -0x18(%rbp), %rdi
movq -0x80(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0xaf00
cmpb $0x0, %al
jne 0x98b3
movb $0x1, -0x69(%rbp)
jmp 0x98c1
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x986b
cmpb $0x0, -0x69(%rbp)
jne 0x98cc
jmp 0x9c7b
movq -0x80(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
leaq 0xac94(%rip), %rsi      # 0x14579
callq 0x2190
movq %rax, -0x88(%rbp)
cmpq $0x0, %rax
je 0x993f
movq -0x18(%rbp), %rdi
movq -0x88(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0xae60
cmpb $0x0, %al
jne 0x992b
movq -0x18(%rbp), %rdi
movq -0x88(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0xaf00
cmpb $0x0, %al
jne 0x992b
movb $0x1, -0x6a(%rbp)
jmp 0x993f
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x98d7
cmpb $0x0, -0x6a(%rbp)
jne 0x994a
jmp 0x9c7b
movq -0x80(%rbp), %rax
movq -0x18(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x70(%rbp)
movq -0x88(%rbp), %rax
movq -0x18(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x74(%rbp)
movq -0x18(%rbp), %rdi
movl -0x70(%rbp), %edx
subl $0x1, %edx
xorl %esi, %esi
callq 0xa5e0
movq %rax, -0x90(%rbp)
movl -0x68(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x68(%rbp)
movslq %eax, %rcx
leaq 0xea4a(%rip), %rax      # 0x183e0
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0x9a88
movq -0x18(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x2190
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
je 0x9a83
movq -0xa8(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x3d, %eax
je 0x9a83
jmp 0x99e6
movq -0x18(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0xae60
movzbl %al, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0xe1(%rbp)
jne 0x9a2b
movq -0x18(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0xaf00
movzbl %al, %eax
cmpl $0x0, %eax
setne %al
movb %al, -0xe1(%rbp)
movb -0xe1(%rbp), %al
testb $0x1, %al
jne 0x9a37
jmp 0x9a63
movq -0xa8(%rbp), %rdi
addq $0x1, %rdi
movq -0xb0(%rbp), %rsi
callq 0x2190
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
jne 0x9a61
jmp 0x9a63
jmp 0x99e6
cmpq $0x0, -0xa8(%rbp)
jne 0x9a72
jmp 0x9981
movq -0x90(%rbp), %rdi
callq 0x2030
jmp 0x9c7b
jmp 0x9981
movq -0x18(%rbp), %rdi
movl -0x70(%rbp), %esi
addl $0x1, %esi
movl -0x74(%rbp), %edx
subl -0x70(%rbp), %edx
subl $0x1, %edx
callq 0xa5e0
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rdi
movl -0x74(%rbp), %esi
addl $0x1, %esi
movl -0x1c(%rbp), %edx
subl -0x74(%rbp), %edx
subl $0x1, %edx
callq 0xa5e0
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rdi
movl -0x20(%rbp), %esi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x9f90
movq %rax, -0xc0(%rbp)
movq %rdx, -0xb8(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0xa0(%rbp), %rdi
movl -0x20(%rbp), %esi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x9f90
movq %rax, -0xd0(%rbp)
movq %rdx, -0xc8(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
leaq 0xad59(%rip), %rsi      # 0x148a0
callq 0x20f0
cmpl $0x0, %eax
je 0x9c45
movq -0x60(%rbp), %rdi
leaq 0xad40(%rip), %rsi      # 0x148a0
callq 0x20f0
cmpl $0x0, %eax
je 0x9c45
movq -0x90(%rbp), %rdi
movl -0x20(%rbp), %esi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x9f90
movq %rax, -0xe0(%rbp)
movq %rdx, -0xd8(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x60(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0xa140
movl %eax, -0x64(%rbp)
cmpl $-0x1, -0x64(%rbp)
je 0x9c0b
movq -0x10(%rbp), %rdi
callq 0x2030
movslq -0x64(%rbp), %rcx
leaq 0xe77d(%rip), %rax      # 0x18350
movq (%rax,%rcx,8), %rdi
callq 0xa550
movq %rax, -0x10(%rbp)
movsbl -0x58(%rbp), %eax
movsbl -0x48(%rbp), %ecx
cmpl %ecx, %eax
jle 0x9bf8
movsbl -0x58(%rbp), %eax
movl %eax, -0xe8(%rbp)
jmp 0x9c02
movsbl -0x48(%rbp), %eax
movl %eax, -0xe8(%rbp)
movl -0xe8(%rbp), %eax
movb %al, -0x8(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x8080
cmpb $0x0, %al
jne 0x9c3c
movq -0x30(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x9c3c
movq -0x30(%rbp), %rax
movq 0x30(%rax), %rdi
leaq -0x50(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0xa050
movq -0x40(%rbp), %rdi
callq 0x2030
movq -0x60(%rbp), %rdi
callq 0x2030
movq -0x50(%rbp), %rdi
callq 0x2030
movq -0x90(%rbp), %rdi
callq 0x2030
movq -0x98(%rbp), %rdi
callq 0x2030
movq -0xa0(%rbp), %rdi
callq 0x2030
movq -0x10(%rbp), %rax
movq -0x8(%rbp), %rdx
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax)