// File: /workspace/github_c_makefile_star2_O2/Gaurang18[P]Compiler-Project-BITS-Pilani/./codegen.c
// Function: GenerateCodeStmts
void GenerateCodeStmts(FILE *fpdummy, parsetree head, FILE *fp, char *scope){

	if(head==NULL)
		return;

	if(head->value== 4)
		strcpy(scope, "driver");

	if(head->value==106 && head->parent->value==7)
		strcpy(scope, head->Lex->tk->value);

	switch(head->value){

		case 22:

		if(head->generated!=1){

				if(head->rule==40){

				expstate = 0;
				fprintf(fp,".while%d:\n",whilenum);
				ExpressionCode(fpdummy,head->child->right,fp,scope);
				fprintf(fp,"pop eax\ncmp eax,0000h\nje .endwhile%d\n", whilenum);

				GenerateCodeStmts(fpdummy,head->child->right->right->right, fp, scope);
				fprintf(fp,"jmp .while%d\n", whilenum);
				fprintf(fp,".endwhile%d:\n", whilenum);
				whilenum++;
			


			}

			if(head->rule==39){

				expstate = 0;
				fprintf(fp, ".for%d\n", fornum);
				parsetree id = head->child->right;

				fprintf(fp,"push dword[%s]\n",id->Lex->tk->value);
				fprintf(fp, "pop eax\n");
				// printf("%s\n", id->right->Lex->tk->value);
				// printf("%s\n", id->right->right->child->right->right->Lex->tk->value);

				int start = atoi(id->right->right->child->Lex->tk->value);
				int end = atoi(id->right->right->child->right->right->Lex->tk->value);
				// assert(0);
				fprintf(fp, "mov ecx, %d\n", start);
				fprintf(fp, "cmp eax, ecx\n");
				fprintf(fp, "jl .endfor%d\n", fornum);
				fprintf(fp, "mov ecx, %d\n", end);
				fprintf(fp, "cmp eax, ecx\n");
				fprintf(fp, "jg .endfor%d\n", fornum);
				GenerateCodeStmts(fpdummy,head->child->right->right->right->right, fp, scope);
				fprintf(fp,"jmp .for%d\n", fornum);
				fprintf(fp,".endfor%d:\n", fornum);
				fornum++;





			}

			

		}	head->generated = 1;

			break;
		case 18:
			if(head->generated!=1){
			if(head->child->value==63)
				 {
			fprintf(fp,"\n\tpush %s\n\tcall printf\n",head->child->right->Lex->tk->value);
			//fprintf(fp,"\n\tmov edi, numberout_form\n\tmov esi,[%s]\n\tmov al, 0\n\tcall printf\n",head->child->right->Lex->tk->value);
				 		

				 }			 				 
				 	// else
				 	// fprintf(fp,"call _UserInput\nmov dword[%s.%s], eax\n",stmt->children[1]->children[0]->str,stmt->children[1]->children[1]->str );
				 
			else
				 {	
				 	if(!(head->child->right->rule==41 && head->child->right->child->right->rule==44)){
				 		if(head->child->right->rule==41)
		fprintf(fp,"\n\tpush %s\n\tpush numberin_form\n\tcall scanf\n",head->child->right->child->Lex->tk->value);
				 		else
				 		{
		fprintf(fp,"\n\tpush %s\n\tpush numberin_form\n\tcall scanf\n",head->child->right->child->Lex->tk->value);
				 		}

				 	}	
				 }}

				 head->generated = 1;
				 break;

	}


	GenerateCodeStmts(fpdummy,head->child, fp, scope);

	switch(head->value){

		case 4: strcpy(scope, "driver");
				// assert(0);
				break;

		case 106: if(head->parent->value==7)
        			{
            			strcpy(scope, head->Lex->tk->value);
        			}				

        		break;

		case 100: 



			if(head->generated!=1){

			if(head->parent->value==28){

				parsetree id = head->parent->parent->parent->child;

				// printf("%s %s\n", id->Lex->tk->value, scope);

				int type = TypeChecker(fpdummy,id, scope);
				 // printf("%d\n", type);
				if(type==0){
					// assert(0);
					ExpressionCode(fpdummy,head->right, fp, scope);
					
					fprintf(fp,"pop eax\nmov dword[%s],eax\n",id->Lex->tk->value);
					//assert(0);

				}

				head->generated = 1;

				}


			}

			

			break;

		


	}



	GenerateCodeStmts(fpdummy,head->right, fp, scope);




}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r13
movq %rdi, -0x30(%rbp)
testq %r15, %r15
je 0x73ed
movl 0x8(%r15), %eax
cmpl $0x4, %eax
jne 0x704a
movl $0x726576, 0x3(%rbx)    # imm = 0x726576
movl $0x76697264, (%rbx)     # imm = 0x76697264
movl 0x8(%r15), %eax
cmpl $0x6a, %eax
jne 0x7073
movq 0x30(%r15), %rax
cmpl $0x7, 0x8(%rax)
jne 0x7319
movq (%r15), %rax
movq (%rax), %rsi
addq $0x1e, %rsi
movq %rbx, %rdi
callq 0x2060
movl 0x8(%r15), %eax
cmpl $0x12, %eax
je 0x72b7
cmpl $0x16, %eax
jne 0x7319
leaq 0x10(%r15), %r12
cmpl $0x1, 0x10(%r15)
je 0x7311
movl 0xc(%r15), %eax
cmpl $0x28, %eax
jne 0x7142
andl $0x0, 0x17e28(%rip)     # 0x1eed0
movl 0x17e2a(%rip), %edx     # 0x1eed8
movq %r14, %rdi
leaq 0x230d(%rip), %rsi      # 0x93c5
xorl %eax, %eax
callq 0x2130
movq 0x18(%r15), %rax
movq 0x28(%rax), %rsi
movq %r13, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x65ce
movl 0x17dfd(%rip), %edx     # 0x1eed8
movq %r14, %rdi
leaq 0x22eb(%rip), %rsi      # 0x93d0
xorl %eax, %eax
callq 0x2130
movq 0x18(%r15), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rsi
movq %r13, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x7006
movl 0x17dc8(%rip), %edx     # 0x1eed8
movq %r14, %rdi
leaq 0x22dc(%rip), %rsi      # 0x93f6
xorl %eax, %eax
callq 0x2130
movl 0x17db1(%rip), %edx     # 0x1eed8
movq %r14, %rdi
leaq 0x22d3(%rip), %rsi      # 0x9404
xorl %eax, %eax
callq 0x2130
incl 0x17d9a(%rip)           # 0x1eed8
movl 0xc(%r15), %eax
cmpl $0x27, %eax
jne 0x7311
andl $0x0, 0x17d7e(%rip)     # 0x1eed0
movl 0x17d8c(%rip), %edx     # 0x1eee4
movq %r14, %rdi
leaq 0x22f4(%rip), %rsi      # 0x9456
xorl %eax, %eax
callq 0x2130
movq 0x18(%r15), %rax
movq 0x28(%rax), %r13
movq (%r13), %rax
movq (%rax), %rdx
addq $0x1e, %rdx
movq %r14, %rdi
leaq 0x1f3f(%rip), %rsi      # 0x90c5
xorl %eax, %eax
callq 0x2130
leaq 0x227e(%rip), %rdi      # 0x9412
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x21e0
movq 0x28(%r13), %rax
movq 0x28(%rax), %rax
movq 0x18(%rax), %r13
movq (%r13), %rax
movq (%rax), %rdi
addq $0x1e, %rdi
callq 0x21a0
movl %eax, -0x34(%rbp)
movq 0x28(%r13), %rax
movq -0x30(%rbp), %r13
movq 0x28(%rax), %rax
movq (%rax), %rax
movq (%rax), %rdi
addq $0x1e, %rdi
callq 0x21a0
movl %eax, -0x38(%rbp)
movq %r14, %rdi
leaq 0x2232(%rip), %rsi      # 0x941b
movl -0x34(%rbp), %edx
xorl %eax, %eax
callq 0x2130
leaq 0x222e(%rip), %rdi      # 0x9428
pushq $0xd
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x21e0
movl 0x17cd6(%rip), %edx     # 0x1eee4
movq %r14, %rdi
leaq 0x221e(%rip), %rsi      # 0x9436
xorl %eax, %eax
callq 0x2130
movq %r14, %rdi
leaq 0x21f2(%rip), %rsi      # 0x941b
movl -0x38(%rbp), %edx
xorl %eax, %eax
callq 0x2130
leaq 0x21ee(%rip), %rdi      # 0x9428
pushq $0xd
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x21e0
movl 0x17c96(%rip), %edx     # 0x1eee4
movq %r14, %rdi
leaq 0x21ec(%rip), %rsi      # 0x9444
xorl %eax, %eax
callq 0x2130
movq 0x18(%r15), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rsi
movq %r13, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x7006
movl 0x17c5d(%rip), %edx     # 0x1eee4
movq %r14, %rdi
leaq 0x21c1(%rip), %rsi      # 0x9452
xorl %eax, %eax
callq 0x2130
movl 0x17c46(%rip), %edx     # 0x1eee4
movq %r14, %rdi
leaq 0x21b6(%rip), %rsi      # 0x945e
xorl %eax, %eax
callq 0x2130
incl 0x17c2f(%rip)           # 0x1eee4
jmp 0x7311
leaq 0x10(%r15), %r12
cmpl $0x1, 0x10(%r15)
je 0x7311
movq 0x18(%r15), %rax
cmpl $0x3f, 0x8(%rax)
movq 0x28(%rax), %rax
jne 0x72d9
leaq 0x2193(%rip), %rsi      # 0x946a
jmp 0x72fd
cmpl $0x29, 0xc(%rax)
movq 0x18(%rax), %rax
jne 0x72f6
movq 0x28(%rax), %rcx
cmpl $0x2c, 0xc(%rcx)
leaq 0x2190(%rip), %rsi      # 0x9482
jne 0x72fd
jmp 0x7311
leaq 0x2185(%rip), %rsi      # 0x9482
movq (%rax), %rax
movq (%rax), %rdx
addq $0x1e, %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2130
movl $0x1, (%r12)
movq 0x18(%r15), %rsi
movq %r13, %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x7006
movl 0x8(%r15), %eax
cmpl $0x6a, %eax
je 0x73c8
cmpl $0x64, %eax
je 0x7358
cmpl $0x4, %eax
jne 0x73e4
movl $0x726576, 0x3(%rbx)    # imm = 0x726576
movl $0x76697264, (%rbx)     # imm = 0x76697264
jmp 0x73e4
cmpl $0x1, 0x10(%r15)
je 0x73e4
movq 0x30(%r15), %rax
cmpl $0x1c, 0x8(%rax)
jne 0x73e4
movq 0x30(%rax), %rax
movq 0x30(%rax), %rax
movq %r13, %rdi
movq 0x18(%rax), %r13
movq %r13, %rsi
movq %rbx, %rdx
callq 0x4dc2
testl %eax, %eax
jne 0x73ba
movq 0x28(%r15), %rsi
movq -0x30(%rbp), %rdi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x65ce
movq (%r13), %rax
movq (%rax), %rdx
addq $0x1e, %rdx
movq %r14, %rdi
leaq 0x20fa(%rip), %rsi      # 0x94ad
xorl %eax, %eax
callq 0x2130
movl $0x1, 0x10(%r15)
movq -0x30(%rbp), %r13
jmp 0x73e4
movq 0x30(%r15), %rax
cmpl $0x7, 0x8(%rax)
jne 0x73e4
movq (%r15), %rax
movq (%rax), %rsi
addq $0x1e, %rsi
movq %rbx, %rdi
callq 0x2060
movq 0x28(%r15), %r15
jmp 0x7027
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq