// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movl %edx, %r15d
movq %rsi, %rbx
movq %rdi, %r14
callq 0x2372
movq %r14, %rdi
movq %rbx, -0x40(%rbp)
movq %rbx, %rsi
callq 0x2639
leaq 0x13b9f(%rip), %r14     # 0x16338
movq (%r14), %rcx
leaq 0xbfb0(%rip), %rdi      # 0xe753
pushq $0x9
popq %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
callq 0x1160
leaq 0xbfa4(%rip), %r13      # 0xe75d
pushq $0xc
popq %rbx
movl %ebx, %edi
movq %r13, %rsi
callq 0x151a
movq (%r14), %rcx
leaq 0xbfa8(%rip), %rdi      # 0xe778
pushq $0x9
popq %rsi
movq %r12, %rdx
callq 0x1160
movl %ebx, %edi
movq %r13, %rsi
callq 0x151a
movl %r15d, -0x2c(%rbp)
cmpl $0x2, %r15d
jl 0x2a27
leaq -0x44(%rbp), %r14
movw $0x5843, (%r14)         # imm = 0x5843
movb $0x0, 0x2(%r14)
leaq -0x34(%rbp), %r13
movl $0x494452, (%r13)       # imm = 0x494452
leaq 0x13b23(%rip), %r15     # 0x16338
movq (%r15), %rdi
xorl %edx, %edx
movq -0x40(%rbp), %rax
subl 0x1c(%rax), %edx
leaq 0xbf5a(%rip), %rsi      # 0xe782
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rdi
leaq 0xbf58(%rip), %rsi      # 0xe791
movq %r14, %rdx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rcx
leaq 0xbf52(%rip), %rdi      # 0xe79f
pushq $0xd
popq %rsi
movq %r12, %rdx
callq 0x1160
movq (%r15), %rdi
leaq 0xbb21(%rip), %rbx      # 0xe383
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rdi
movq %rbx, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rcx
leaq 0xbf24(%rip), %rdi      # 0xe7ad
pushq $0xb
popq %rsi
movq %r12, %rdx
callq 0x1160
movq (%r15), %rdi
leaq 0xbf1b(%rip), %rsi      # 0xe7b9
pushq $0xa
popq %rbx
movl %ebx, %edx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rcx
leaq 0xbf14(%rip), %rdi      # 0xe7c8
pushq $0xe
popq %rsi
movq %r12, %rdx
callq 0x1160
movq (%r15), %rdi
leaq 0xbf0e(%rip), %rsi      # 0xe7d7
movl %ebx, %edx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rcx
leaq 0xbf0a(%rip), %rdi      # 0xe7e6
pushq $0xa
popq %r13
movq %r13, %rsi
movq %r12, %rdx
callq 0x1160
movq (%r15), %rcx
leaq 0xbefc(%rip), %rdi      # 0xe7f1
pushq $0x17
popq %rsi
movq %r12, %rdx
callq 0x1160
movq (%r15), %rdi
leaq 0xbeff(%rip), %rbx      # 0xe809
movq %rbx, %rsi
leaq -0x34(%rbp), %rdx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rdi
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rcx
leaq 0xbee1(%rip), %rdi      # 0xe813
pushq $0x9
popq %rbx
movq %rbx, %rsi
movq %r12, %rdx
callq 0x1160
movq (%r15), %rcx
leaq 0xbed3(%rip), %rdi      # 0xe81d
pushq $0x1b
popq %rsi
movq %r12, %rdx
callq 0x1160
movq (%r15), %rcx
leaq 0xbeda(%rip), %rdi      # 0xe839
movq %rbx, %rsi
movq %r12, %rdx
callq 0x1160
movq (%r15), %rcx
leaq 0xbecf(%rip), %rdi      # 0xe843
pushq $0x1a
popq %rsi
movq %r12, %rdx
callq 0x1160
movq (%r15), %rcx
leaq 0xbed5(%rip), %rdi      # 0xe85e
movq %r13, %rsi
movq %r12, %rdx
callq 0x1160
movq (%r15), %rcx
leaq 0xbecb(%rip), %rdi      # 0xe869
movq %r13, %rsi
movq %r12, %rdx
callq 0x1160
movq (%r15), %rcx
leaq 0xbec1(%rip), %rdi      # 0xe874
pushq $0xe
popq %rsi
movq %r12, %rdx
callq 0x1160
movq (%r15), %rdi
leaq 0xbebb(%rip), %rsi      # 0xe883
movq %r14, %rdx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rdi
leaq 0xbeb1(%rip), %rsi      # 0xe88d
movq %r14, %rdx
movl -0x2c(%rbp), %ecx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rcx
leaq 0xbea8(%rip), %rdi      # 0xe89b
pushq $0xf
popq %rsi
movq %r12, %rdx
callq 0x1160
movq (%r15), %rcx
leaq 0xbea3(%rip), %rdi      # 0xe8ab
pushq $0xc
popq %rsi
movq %r12, %rdx
callq 0x1160
leaq 0xbe9e(%rip), %rsi      # 0xe8b8
pushq $0x8
popq %rdi
callq 0x151a
jmp 0x2b15
leaq 0x1390a(%rip), %r15     # 0x16338
movq (%r15), %rdi
leaq 0xbd81(%rip), %rsi      # 0xe7b9
pushq $0xc
popq %r14
movl %r14d, %edx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rcx
leaq 0xbd78(%rip), %rdi      # 0xe7c8
pushq $0xe
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x1160
movq (%r15), %rdi
leaq 0xbd6f(%rip), %rsi      # 0xe7d7
movl %r14d, %edx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %r12
movq -0x40(%rbp), %r14
movl 0x1c(%r14), %edi
callq 0x156f
leaq 0xbe46(%rip), %rsi      # 0xe8cf
leaq 0x138d0(%rip), %rdx     # 0x16360
movq %r12, %rdi
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rcx
leaq 0xbd6f(%rip), %rdi      # 0xe813
pushq $0x9
popq %r12
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1160
movq (%r15), %r13
movl 0x1c(%r14), %edi
addl $-0xa, %edi
callq 0x156f
leaq 0xbe1a(%rip), %rsi      # 0xe8e3
movq %r13, %rdi
leaq 0x1388d(%rip), %r13     # 0x16360
movq %r13, %rdx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rcx
leaq 0xbd52(%rip), %rdi      # 0xe839
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1160
movq (%r15), %rbx
movl 0x1c(%r14), %edi
addl $-0x8, %edi
callq 0x156f
leaq 0xbdee(%rip), %rsi      # 0xe8f6
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x10f0
callq 0x240c
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq