// File: /workspace/github_c_makefile_star2_O2/anuwu[P]ERPLAG-Compiler/codegen.c
// Function: switchCaseLabels
int switchCaseLabels (astNode *node, moduleST *lst, int caseCount , int *caseLabels)
{
	int i, def_label = -1 ;

	for (i = 0 ; i < caseCount ; i++)
		caseLabels[i] = get_label (LABEL_SWITCH) ;

	varST *switchVar = searchVar (lst, node->next->tok->lexeme) ;
	codePrint ("\n\t\tMOV AX, [RBP%s]", getOffsetStr(switchVar->offset)) ;
	codeComment (10, "loading switch variable") ;

	astNode *caseValNode =  node->next->next->next ;
	i = 0 ;

	if (switchVar->datatype == TK_INTEGER)
	{
		while (caseValNode != NULL)
		{
			codePrint ("\n\t\tCMP AX, %s", caseValNode->tok->lexeme) ;
			codeComment (11, "switch case") ;
			codePrint ("\t\tJE @SWITCH%d\n", caseLabels[i]) ;

			if (caseValNode->next->next->next->id == statements)
			{
				def_label = get_label (LABEL_SWITCH) ;
				codePrint ("\n\t\tJMP @SWITCH%d", def_label) ;
				codeComment (11, "default case") ;
				break ;
			}

			i++ ;
			caseValNode = caseValNode->next->next->next ;
		}
	}
	else
	{		
		codePrint ("\n\t\tCMP AX, 0\n") ;
		if (caseValNode->tok->lexeme[0] == 't')
		{
			codePrint ("\t\tJNE @SWITCH%d", caseLabels[0]) ;
			codeComment (11, "true case") ;
		}
		else
		{
			codePrint ("\t\tJE @SWITCH%d", caseLabels[0]) ;
			codeComment (11, "false case") ;
		}

		codePrint ("\t\tJMP @SWITCH%d", caseLabels[1]) ;
		if (caseValNode->tok->lexeme[0] == 't')
			codeComment (11, "false case") ;
		else
			codeComment (11, "true case") ;
	}

	return def_label ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %rbx
movq %rsi, %rax
movq %rdi, %r14
xorl %ecx, %ecx
testl %edx, %edx
cmovlel %ecx, %edx
cmpq %rcx, %rdx
je 0x3598
movl 0x12db8(%rip), %esi     # 0x16340
incl %esi
movl %esi, 0x12db0(%rip)     # 0x16340
movl %esi, (%rbx,%rcx,4)
incq %rcx
jmp 0x357d
movq 0x38(%r14), %rcx
movq 0x8(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0xac19
movq %rax, %r13
leaq 0x12d82(%rip), %r15     # 0x16338
movq (%r15), %r12
movl 0x1c(%rax), %edi
callq 0x156f
leaq 0xb342(%rip), %rsi      # 0xe90a
leaq 0x12d91(%rip), %rdx     # 0x16360
movq %r12, %rdi
xorl %eax, %eax
callq 0x10f0
leaq 0xb33d(%rip), %rsi      # 0xe91d
pushq $0xa
popq %rdi
callq 0x151a
movq 0x38(%r14), %rax
movq 0x38(%rax), %rax
movq 0x38(%rax), %r12
cmpl $0x37, 0x8(%r13)
jne 0x3690
pushq $0xb
popq %r13
leaq 0xb345(%rip), %r14      # 0xe94f
testq %r12, %r12
je 0x3727
movq (%r15), %rdi
movq 0x8(%r12), %rax
movq 0x8(%rax), %rdx
leaq 0xb30f(%rip), %rsi      # 0xe935
xorl %eax, %eax
callq 0x10f0
movl %r13d, %edi
leaq 0xb30c(%rip), %rsi      # 0xe943
callq 0x151a
movq (%r15), %rdi
movl (%rbx), %edx
movq %r14, %rsi
xorl %eax, %eax
callq 0x10f0
movq 0x38(%r12), %rax
movq 0x38(%rax), %rax
movq 0x38(%rax), %r12
addq $0x4, %rbx
cmpl $0xc, (%r12)
jne 0x360a
movl 0x12cd7(%rip), %ebx     # 0x16340
incl %ebx
movl %ebx, 0x12ccf(%rip)     # 0x16340
movq (%r15), %rdi
leaq 0xb2e4(%rip), %rsi      # 0xe95f
movl %ebx, %edx
xorl %eax, %eax
callq 0x10f0
leaq 0xb2e5(%rip), %r13      # 0xe970
jmp 0x371a
movq (%r15), %rcx
leaq 0xb2e3(%rip), %rdi      # 0xe97d
pushq $0xd
popq %rsi
pushq $0x1
popq %rdx
callq 0x1160
movq 0x8(%r12), %rax
movq 0x8(%rax), %rax
cmpb $0x74, (%rax)
movq (%r15), %rdi
movl (%rbx), %edx
leaq 0xb2ce(%rip), %rax      # 0xe98b
leaq 0xb2e1(%rip), %rsi      # 0xe9a5
cmoveq %rax, %rsi
leaq 0xb2cc(%rip), %r13      # 0xe99b
leaq 0xb2de(%rip), %r14      # 0xe9b4
cmoveq %r13, %r14
xorl %eax, %eax
callq 0x10f0
pushq $0xb
popq %rdi
movq %r14, %rsi
callq 0x151a
movq (%r15), %rdi
movl 0x4(%rbx), %edx
leaq 0xb267(%rip), %rsi      # 0xe960
xorl %eax, %eax
callq 0x10f0
movq 0x8(%r12), %rax
movq 0x8(%rax), %rax
cmpb $0x74, (%rax)
leaq 0xb2a1(%rip), %rax      # 0xe9b4
cmoveq %rax, %r13
pushq $-0x1
popq %rbx
pushq $0xb
popq %rdi
movq %r13, %rsi
callq 0x151a
jmp 0x372a
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq