// File: /workspace/github_c_makefile_star2_O1/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 0x23cb
movq %r14, %rdi
movq %r15, %rsi
callq 0x26a2
leaq 0x15b42(%rip), %r13     # 0x18348
movq (%r13), %rcx
leaq 0xdf42(%rip), %rdi      # 0x10753
movl $0x9, %esi
movl $0x1, %edx
callq 0x1170
leaq 0xdf36(%rip), %r14      # 0x1075d
movl $0xc, %edi
movq %r14, %rsi
callq 0x153a
movq (%r13), %rcx
leaq 0xdf39(%rip), %rdi      # 0x10778
movl $0x9, %esi
movl $0x1, %edx
callq 0x1170
movl $0xc, %edi
movq %r14, %rsi
callq 0x153a
cmpl $0x2, %ebx
jl 0x2ada
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 0xdef2(%rip), %rsi      # 0x10782
xorl %eax, %eax
callq 0x1100
movq (%r13), %rdi
leaq 0xdeef(%rip), %rsi      # 0x10791
movq %r14, %rdx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xdee8(%rip), %rdi      # 0x1079f
movl $0xd, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rdi
leaq 0xdab2(%rip), %r15      # 0x10383
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 0xdeb3(%rip), %rdi      # 0x107ad
movl $0xb, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rdi
leaq 0xdea5(%rip), %rsi      # 0x107b9
movl $0xa, %edx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xde9d(%rip), %rdi      # 0x107c8
movl $0xe, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rdi
leaq 0xde92(%rip), %rsi      # 0x107d7
movl $0xa, %edx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xde8a(%rip), %rdi      # 0x107e6
movl $0xa, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xde7b(%rip), %rdi      # 0x107f1
movl $0x17, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rdi
leaq 0xde79(%rip), %r15      # 0x10809
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 0xde5a(%rip), %rdi      # 0x10813
movl $0x9, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xde4a(%rip), %rdi      # 0x1081d
movl $0x1b, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xde4c(%rip), %rdi      # 0x10839
movl $0x9, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xde3c(%rip), %rdi      # 0x10843
movl $0x1a, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xde3d(%rip), %rdi      # 0x1085e
movl $0xa, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xde2e(%rip), %rdi      # 0x10869
movl $0xa, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xde1f(%rip), %rdi      # 0x10874
movl $0xe, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rdi
leaq 0xde14(%rip), %rsi      # 0x10883
movq %r14, %rdx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rdi
leaq 0xde09(%rip), %rsi      # 0x1088d
movq %r14, %rdx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xde00(%rip), %rdi      # 0x1089b
movl $0xf, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rcx
leaq 0xddf6(%rip), %rdi      # 0x108ab
movl $0xc, %esi
movl $0x1, %edx
callq 0x1170
leaq 0xdded(%rip), %rsi      # 0x108b8
movl $0x8, %edi
callq 0x153a
jmp 0x2bc6
movq (%r13), %rdi
leaq 0xdcd4(%rip), %rsi      # 0x107b9
movl $0xc, %edx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xdccc(%rip), %rdi      # 0x107c8
movl $0xe, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %rdi
leaq 0xdcc1(%rip), %rsi      # 0x107d7
movl $0xc, %edx
xorl %eax, %eax
callq 0x1100
movq (%r13), %r14
movl 0x1c(%r15), %edi
callq 0x158c
leaq 0xdd99(%rip), %rsi      # 0x108cf
leaq 0x15833(%rip), %rbx     # 0x18370
movq %r14, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xdcbe(%rip), %rdi      # 0x10813
movl $0x9, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %r14
movl 0x1c(%r15), %edi
addl $-0xa, %edi
callq 0x158c
leaq 0xdd68(%rip), %rsi      # 0x108e3
movq %r14, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1100
movq (%r13), %rcx
leaq 0xdca6(%rip), %rdi      # 0x10839
movl $0x9, %esi
movl $0x1, %edx
callq 0x1170
movq (%r13), %r14
movl 0x1c(%r15), %edi
addl $-0x8, %edi
callq 0x158c
leaq 0xdd3d(%rip), %rsi      # 0x108f6
movq %r14, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1100
callq 0x246c
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq