// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0xaca4
jmp 0xb1be
movq -0x10(%rbp), %rax
cmpl $0x4, 0x8(%rax)
jne 0xacbe
movq -0x20(%rbp), %rdi
leaq 0x16a6(%rip), %rsi      # 0xc35f
callq 0x2050
movq -0x10(%rbp), %rax
cmpl $0x6a, 0x8(%rax)
jne 0xaced
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
cmpl $0x7, 0x8(%rax)
jne 0xaced
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rsi
addq $0x1e, %rsi
callq 0x2050
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x40(%rbp)
subl $0x12, %eax
je 0xaf9d
jmp 0xad02
movl -0x40(%rbp), %eax
subl $0x16, %eax
jne 0xb08f
jmp 0xad10
movq -0x10(%rbp), %rax
cmpl $0x1, 0x10(%rax)
je 0xaf8d
movq -0x10(%rbp), %rax
cmpl $0x28, 0xc(%rax)
jne 0xade7
movl $0x0, 0x184ea(%rip)     # 0x23220
movq -0x18(%rbp), %rdi
movl 0x184e8(%rip), %edx     # 0x23228
leaq 0x27cc(%rip), %rsi      # 0xd513
movb $0x0, %al
callq 0x2100
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x9e20
movq -0x18(%rbp), %rdi
movl 0x184b3(%rip), %edx     # 0x23228
leaq 0x27a2(%rip), %rsi      # 0xd51e
movb $0x0, %al
callq 0x2100
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0xac80
movq -0x18(%rbp), %rdi
movl 0x18476(%rip), %edx     # 0x23228
leaq 0x278b(%rip), %rsi      # 0xd544
movb $0x0, %al
callq 0x2100
movq -0x18(%rbp), %rdi
movl 0x1845e(%rip), %edx     # 0x23228
leaq 0x2781(%rip), %rsi      # 0xd552
movb $0x0, %al
callq 0x2100
movl 0x1844a(%rip), %eax     # 0x23228
addl $0x1, %eax
movl %eax, 0x18441(%rip)     # 0x23228
movq -0x10(%rbp), %rax
cmpl $0x27, 0xc(%rax)
jne 0xaf8b
movl $0x0, 0x18421(%rip)     # 0x23220
movq -0x18(%rbp), %rdi
movl 0x1842b(%rip), %edx     # 0x23234
leaq 0x2794(%rip), %rsi      # 0xd5a4
movb $0x0, %al
callq 0x2100
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdx
addq $0x1e, %rdx
leaq 0x23d3(%rip), %rsi      # 0xd213
movb $0x0, %al
callq 0x2100
movq -0x18(%rbp), %rdi
leaq 0x270e(%rip), %rsi      # 0xd560
movb $0x0, %al
callq 0x2100
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq (%rax), %rdi
addq $0x1e, %rdi
callq 0x2170
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq (%rax), %rdi
addq $0x1e, %rdi
callq 0x2170
movl %eax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
movl -0x2c(%rbp), %edx
leaq 0x26b6(%rip), %rsi      # 0xd569
movb $0x0, %al
callq 0x2100
movq -0x18(%rbp), %rdi
leaq 0x26b1(%rip), %rsi      # 0xd576
movb $0x0, %al
callq 0x2100
movq -0x18(%rbp), %rdi
movl 0x1835e(%rip), %edx     # 0x23234
leaq 0x26a7(%rip), %rsi      # 0xd584
movb $0x0, %al
callq 0x2100
movq -0x18(%rbp), %rdi
movl -0x30(%rbp), %edx
leaq 0x2677(%rip), %rsi      # 0xd569
movb $0x0, %al
callq 0x2100
movq -0x18(%rbp), %rdi
leaq 0x2672(%rip), %rsi      # 0xd576
movb $0x0, %al
callq 0x2100
movq -0x18(%rbp), %rdi
movl 0x1831f(%rip), %edx     # 0x23234
leaq 0x2676(%rip), %rsi      # 0xd592
movb $0x0, %al
callq 0x2100
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0xac80
movq -0x18(%rbp), %rdi
movl 0x182de(%rip), %edx     # 0x23234
leaq 0x2643(%rip), %rsi      # 0xd5a0
movb $0x0, %al
callq 0x2100
movq -0x18(%rbp), %rdi
movl 0x182c6(%rip), %edx     # 0x23234
leaq 0x2637(%rip), %rsi      # 0xd5ac
movb $0x0, %al
callq 0x2100
movl 0x182b2(%rip), %eax     # 0x23234
addl $0x1, %eax
movl %eax, 0x182a9(%rip)     # 0x23234
jmp 0xaf8d
movq -0x10(%rbp), %rax
movl $0x1, 0x10(%rax)
jmp 0xb08f
movq -0x10(%rbp), %rax
cmpl $0x1, 0x10(%rax)
je 0xb084
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
cmpl $0x3f, 0x8(%rax)
jne 0xafe6
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq (%rax), %rdx
addq $0x1e, %rdx
leaq 0x25de(%rip), %rsi      # 0xd5b8
movb $0x0, %al
callq 0x2100
jmp 0xb082
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
cmpl $0x29, 0xc(%rax)
jne 0xb012
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
cmpl $0x2c, 0xc(%rax)
je 0xb080
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
cmpl $0x29, 0xc(%rax)
jne 0xb052
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq (%rax), %rdx
addq $0x1e, %rdx
leaq 0x2587(%rip), %rsi      # 0xd5d0
movb $0x0, %al
callq 0x2100
jmp 0xb07e
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movq (%rax), %rdx
addq $0x1e, %rdx
leaq 0x2559(%rip), %rsi      # 0xd5d0
movb $0x0, %al
callq 0x2100
jmp 0xb080
jmp 0xb082
jmp 0xb084
movq -0x10(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0xac80
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x44(%rbp)
subl $0x4, %eax
je 0xb0d0
jmp 0xb0b9
movl -0x44(%rbp), %eax
subl $0x64, %eax
je 0xb10f
jmp 0xb0c3
movl -0x44(%rbp), %eax
subl $0x6a, %eax
je 0xb0e5
jmp 0xb1a5
movq -0x20(%rbp), %rdi
leaq 0x1284(%rip), %rsi      # 0xc35f
callq 0x2050
jmp 0xb1a5
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
cmpl $0x7, 0x8(%rax)
jne 0xb10a
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rsi
addq $0x1e, %rsi
callq 0x2050
jmp 0xb1a5
movq -0x10(%rbp), %rax
cmpl $0x1, 0x10(%rax)
je 0xb1a3
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
cmpl $0x1c, 0x8(%rax)
jne 0xb1a1
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x30(%rax), %rax
movq 0x30(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x7670
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0xb196
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x9e20
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rdx
addq $0x1e, %rdx
leaq 0x246c(%rip), %rsi      # 0xd5fb
movb $0x0, %al
callq 0x2100
movq -0x10(%rbp), %rax
movl $0x1, 0x10(%rax)
jmp 0xb1a3
jmp 0xb1a5
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0xac80
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)