// File: /workspace/github_c_makefile_star2_O3/anuwu[P]ERPLAG-Compiler/codegen.c
// Function: dynamicDeclaration
void dynamicDeclaration (moduleST *lst, varST *vst, int declCount)
{
	RSPAlign () ;
	dynamicDeclarationCheck (lst, vst) ;

	codePrint ("\t\tPUSH BX") ;
	codeComment (12, "saving register for malloc") ;
	codePrint ("\t\tPUSH AX") ;
	codeComment (12, "saving register for malloc") ;

	int RSPsub ;

	if (declCount > 1)
	{
		char counterReg[3], mallocReg[5] ;
		
		#if defined __linux__ || defined __MACH__
			strcpy (counterReg, "CX") ;
			strcpy (mallocReg, "RDI") ;
			RSPsub = 10 ;
		#endif
		#ifdef _WIN64
			strcpy (counterReg, "AX") ;
			strcpy (mallocReg, "RCX") ;
			RSPsub = 42 ;
		#endif

		codePrint ("\t\tMOV RBX, %d\n", -vst->offset) ;
		codePrint ("\n\t\tMOV %s, 0\n", counterReg) ;

		codePrint ("\n\t.declLoop:\n") ;

		codePrint ("\t\tPUSH %s\n", counterReg) ;
		codePrint ("\t\tPUSH %s\n", mallocReg) ;
		codePrint ("\t\tPUSH RBX\n") ;
		codePrint ("\t\tSUB RSP, %d\n", RSPsub) ;

		#if defined __linux__ || defined _WIN64
			codePrint ("\t\tCALL malloc\n") ;
		#endif
		#ifdef __MACH__
			codePrint ("\t\tCALL _malloc\n") ;
		#endif

		codePrint ("\t\tADD RSP, %d\n", RSPsub) ;	
		codePrint ("\t\tPOP RBX\n") ;
		codePrint ("\t\tMOV [RBP + RBX], RAX\n") ;
		codePrint ("\t\tPOP %s\n", mallocReg) ;
		codePrint ("\t\tPOP %s\n", counterReg) ;
		
		codePrint ("\t\tPOP SI\n") ;
		codePrint ("\t\tMOV [RBP + RBX + 10], SI\n") ;
		codePrint ("\t\tPOP DI\n") ;
		codePrint ("\t\tMOV [RBP + RBX + 8], DI\n") ;
		codePrint ("\t\tPUSH DI\n") ;
		codePrint ("\t\tPUSH SI\n") ;
		codePrint ("\t\tADD RBX, 12\n") ;

		codePrint ("\t\tINC %s\n", counterReg) ;
		codePrint ("\t\tCMP %s, %d\n", counterReg, declCount) ;

		codePrint ("\t\tJL .declLoop\n") ;

		codePrint ("\t\tADD RSP, 4") ;
		codeComment (8, "Restoring array limits") ;
	}
	else
	{
		#if defined __linux__ || defined __MACH__
			RSPsub = 12 ;
		#endif
		#ifdef _WIN64
			RSPsub = 44 ;
		#endif

		codePrint ("\t\tSUB RSP, %d\n", RSPsub) ;

		#if defined __linux__ || defined _WIN64
			codePrint ("\t\tCALL malloc\n") ;
		#endif
		#ifdef __MACH__
			codePrint ("\t\tCALL _malloc\n") ;
		#endif

		codePrint ("\t\tADD RSP, %d\n", RSPsub) ;

		codePrint ("\t\tMOV [RBP%s], RAX\n", getOffsetStr(vst->offset)) ;
		codePrint ("\t\tPOP SI\n") ;
		codePrint ("\t\tMOV [RBP%s], SI\n", getOffsetStr(vst->offset - 10)) ;
		codePrint ("\t\tPOP DI\n") ;
		codePrint ("\t\tMOV [RBP%s], DI\n\n", getOffsetStr(vst->offset - 8)) ;
	}

	RSPRestore () ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r14
callq 0x23bf
movq %r14, %rdi
movq %r15, %rsi
callq 0x2696
leaq 0x14b4e(%rip), %r13     # 0x17348
movq (%r13), %rcx
leaq 0xcf4e(%rip), %rdi      # 0xf753
movl $0x9, %esi
movl $0x1, %edx
callq 0x1170
leaq 0xcf42(%rip), %r14      # 0xf75d
movl $0xc, %edi
movq %r14, %rsi
callq 0x152e
movq (%r13), %rcx
leaq 0xcf45(%rip), %rdi      # 0xf778
movl $0x9, %esi
movl $0x1, %edx
callq 0x1170
movl $0xc, %edi
movq %r14, %rsi
callq 0x152e
cmpl $0x2, %ebx
jl 0x2ace
leaq -0x2c(%rbp), %r14
movw $0x5843, (%r14)         # imm = 0x5843
movb $0x0, 0x2(%r14)
leaq -0x34(%rbp), %r12
movl $0x494452, (%r12)       # imm = 0x494452
movq (%r13), %rdi
xorl %edx, %edx
subl 0x1c(%r15), %edx
leaq 0xcefe(%rip), %rsi      # 0xf782
xorl %eax, %eax
callq 0x1100
movq (%r13), %rdi
leaq 0xcefb(%rip), %rsi      # 0xf791
movq %r14, %rdx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xcef4(%rip), %rdi      # 0xf79f
movl $0xd, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rdi
leaq 0xcabe(%rip), %r15      # 0xf383
movq %r15, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xcebf(%rip), %rdi      # 0xf7ad
movl $0xb, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rdi
leaq 0xceb1(%rip), %rsi      # 0xf7b9
movl $0xa, %edx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xcea9(%rip), %rdi      # 0xf7c8
movl $0xe, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rdi
leaq 0xce9e(%rip), %rsi      # 0xf7d7
movl $0xa, %edx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xce96(%rip), %rdi      # 0xf7e6
movl $0xa, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xce87(%rip), %rdi      # 0xf7f1
movl $0x17, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rdi
leaq 0xce85(%rip), %r15      # 0xf809
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rdi
movq %r15, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xce66(%rip), %rdi      # 0xf813
movl $0x9, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xce56(%rip), %rdi      # 0xf81d
movl $0x1b, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xce58(%rip), %rdi      # 0xf839
movl $0x9, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xce48(%rip), %rdi      # 0xf843
movl $0x1a, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xce49(%rip), %rdi      # 0xf85e
movl $0xa, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xce3a(%rip), %rdi      # 0xf869
movl $0xa, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xce2b(%rip), %rdi      # 0xf874
movl $0xe, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rdi
leaq 0xce20(%rip), %rsi      # 0xf883
movq %r14, %rdx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rdi
leaq 0xce15(%rip), %rsi      # 0xf88d
movq %r14, %rdx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xce0c(%rip), %rdi      # 0xf89b
movl $0xf, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xce02(%rip), %rdi      # 0xf8ab
movl $0xc, %esi
movl $0x1, %edx
callq 0x1170
leaq 0xcdf9(%rip), %rsi      # 0xf8b8
movl $0x8, %edi
callq 0x152e
jmp 0x2bba
movq (%r13), %rdi
leaq 0xcce0(%rip), %rsi      # 0xf7b9
movl $0xc, %edx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xccd8(%rip), %rdi      # 0xf7c8
movl $0xe, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rdi
leaq 0xcccd(%rip), %rsi      # 0xf7d7
movl $0xc, %edx
xorl %eax, %eax
callq 0x1100
movq (%r13), %r14
movl 0x1c(%r15), %edi
callq 0x1580
leaq 0xcda5(%rip), %rsi      # 0xf8cf
leaq 0x1483f(%rip), %rbx     # 0x17370
movq %r14, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xccca(%rip), %rdi      # 0xf813
movl $0x9, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %r14
movl 0x1c(%r15), %edi
addl $-0xa, %edi
callq 0x1580
leaq 0xcd74(%rip), %rsi      # 0xf8e3
movq %r14, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xccb2(%rip), %rdi      # 0xf839
movl $0x9, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %r14
movl 0x1c(%r15), %edi
addl $-0x8, %edi
callq 0x1580
leaq 0xcd49(%rip), %rsi      # 0xf8f6
movq %r14, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1100
callq 0x2460
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq