// File: /workspace/github_cpp_makefile_star2_O1/gauravtewari[P]netlist/main.cpp
// Function: main
int main(int argc, char **argv){
	class circuit c;

	if(argc != 2){
		cout<<"Error: No input file specified.\n";
		cout<<"Usage: ./parser <input_file_name>\n";
		return 1;
	}

	/* Parse the input file and generate the graph data structures */
	if(c.parse_input(argv[1]) != 0){
		cerr<<"Error while Parsing input \n";
		return 1;
	}

	/* Figuring the name of the outfile */
	char *ofile = strtok(argv[1],".");
	char *out_file = new char[strlen(ofile)+5];
	strncpy(out_file, ofile, strlen(ofile));
	strncpy(out_file+strlen(ofile), ".out",4);
	out_file[strlen(ofile)+4]='\0';

	/* Opening the output file for storing result */
	ofstream fout(out_file);
	if(!fout.is_open()){
		cerr<<"Can't open output file for writing\n";
		return -1;
	}
	
	/* Calculate the fan out for all the gates in the netlist */
	c.update_fan_out();
	
	/* Calculating the maximum fanout */
	u32 max_fout=0;
	for(vector<struct gate>::const_iterator i = c.gate_list.begin(); i != c.gate_list.end(); i++)
		if(i->fan_out > max_fout)
			max_fout = i->fan_out;
	fout<<max_fout<<endl;
	
	
	/* find the number of gates with similar fanouts */
	int *fan_count = new int[max_fout+1];
	for(u32 i=0; i<max_fout+1; i++) fan_count[i]=0;
	for(vector<struct gate>::const_iterator i = c.gate_list.begin(); i != c.gate_list.end(); i++)
		fan_count[i->fan_out]++;
	
	/* Printing result for number of gates with similar fanout */
	for(u32 i=0; i<max_fout+1; i++){
		fout<<fan_count[i]<<endl;
	}
	
	/* Generating the index of adjacency list entries for INPUT gates */
	u32 gtype = c.get_gate_type("INPUT");
	fout<<c.gate_type[gtype]<<" ";
	for(vector<struct gate>::const_iterator i = c.gate_list.begin(); i != c.gate_list.end(); i++)
		if(i->type==gtype) {
			fout<<i->id<<" ";
		}
	fout<<endl;

	/* Generating the index of adjacency list entries for OUTPUT gates */
	gtype=c.get_gate_type("OUTPUT");
	fout<<c.gate_type[gtype]<<" ";
	for(vector<struct gate>::const_iterator i = c.gate_list.begin(); i != c.gate_list.end(); i++)
		if(i->type==gtype){
			fout<<i->id<<" ";
		}
	fout<<endl;
	
	/* Calculate the number of gate types that drive the same type gate, and the fanin counterpart */
	c.update_same_faninout();
	for (u32 i=0; i<c.gate_t.size(); i++){
		if((c.gate_type[i]!=0) && (i != c.get_gate_type("INPUT")) && (i != c.get_gate_type("OUTPUT"))){
			fout<<c.get_gate_name(i)<<" "<<c.gate_type[i]<<" "<<c.same_fanout[i]<<" "<<c.same_fanin[i]<<endl;

		}
	}
	
	/* Clean up */
	fout.close();
	delete []out_file;
	delete []fan_count;
	cout<<"Statistics Generated, check output file\n";
	cout<<"Output file generated: "<<ofile<<".out\n";
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3d8, %rsp            # imm = 0x3D8
movq %rsi, %rbx
movl %edi, %ebp
xorl %eax, %eax
leaq 0x180(%rsp), %rcx
movq %rax, -0xc8(%rcx)
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
xorps %xmm0, %xmm0
movups %xmm0, -0x38(%rcx)
movups %xmm0, -0x28(%rcx)
movups %xmm0, -0x18(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq 0x1b0(%rsp), %rcx
movl %eax, (%rcx)
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
leaq 0xc0(%rsp), %rdi
movl $0x84, %edx
xorl %esi, %esi
callq 0x2150
cmpl $0x2, %ebp
jne 0x2575
movq 0x8(%rbx), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x3020
testl %eax, %eax
je 0x25f4
movl $0x1b, %edx
leaq 0x2adc(%rip), %rsi      # 0x5048
movq 0x5a7d(%rip), %r14      # 0x7ff0
jmp 0x259c
movq 0x5a54(%rip), %r14      # 0x7fd0
leaq 0x2a81(%rip), %rsi      # 0x5004
movl $0x20, %edx
movq %r14, %rdi
callq 0x2240
movl $0x22, %edx
leaq 0x2a89(%rip), %rsi      # 0x5025
movl $0x1, %ebx
movq %r14, %rdi
callq 0x2240
leaq 0x148(%rsp), %r14
leaq 0x1a8(%rsp), %rdi
callq 0x2e0c
leaq 0x178(%rsp), %rdi
callq 0x2e84
leaq 0x160(%rsp), %rdi
callq 0x2dac
movq %r14, %rdi
callq 0x2ddc
movl %ebx, %eax
addq $0x3d8, %rsp            # imm = 0x3D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rdi
leaq 0x2a65(%rip), %rsi      # 0x5064
callq 0x22c0
movq %rax, %rbx
movq %rax, %rdi
callq 0x2110
leaq 0x5(%rax), %rdi
callq 0x2030
movq %rax, %r14
movq %rbx, %rdi
callq 0x2110
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x2130
movq %rbx, %rdi
callq 0x2110
movl $0x74756f2e, (%r14,%rax) # imm = 0x74756F2E
movq %rbx, %rdi
callq 0x2110
movb $0x0, 0x4(%r14,%rax)
leaq 0x1d8(%rsp), %rdi
movq %r14, %rsi
movl $0x10, %edx
callq 0x2230
leaq 0x248(%rsp), %rdi
callq 0x20a0
testb %al, %al
je 0x26b4
movq %r14, 0x50(%rsp)
movq %rbx, 0x48(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x3eb0
movq 0x148(%rsp), %rax
movq 0x150(%rsp), %rcx
xorl %r14d, %r14d
cmpq %rcx, %rax
je 0x26d6
movl 0xc(%rax), %edx
cmpl %r14d, %edx
cmoval %edx, %r14d
addq $0x28, %rax
jmp 0x269f
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x5930(%rip), %rdi      # 0x7ff0
leaq 0x299f(%rip), %rsi      # 0x5066
movl $0x23, %edx
callq 0x2240
jmp 0x2c55
movl %r14d, %esi
leaq 0x1d8(%rsp), %rdi
callq 0x2160
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
incl %r14d
leaq (,%r14,4), %rdi
callq 0x2030
movq %rax, %r15
movl %r14d, %r12d
testl %r14d, %r14d
je 0x273d
leaq (,%r12,4), %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x2150
movq 0x148(%rsp), %rax
movq 0x150(%rsp), %rcx
cmpq %rcx, %rax
je 0x275f
movl 0xc(%rax), %edx
incl (%r15,%rdx,4)
addq $0x28, %rax
jmp 0x274d
testl %r14d, %r14d
je 0x27ad
xorl %r13d, %r13d
leaq 0x1d8(%rsp), %rbx
movl (%r15,%r13,4), %esi
movq %rbx, %rdi
callq 0x22f0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
incq %r13
cmpq %r13, %r12
jne 0x276f
leaq 0xa8(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x28ca(%rip), %rsi      # 0x508a
leaq 0x28c8(%rip), %rdx      # 0x508f
leaq 0x98(%rsp), %rdi
callq 0x2f90
leaq 0xb8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x3ba2
movl %eax, %ebx
movq 0x98(%rsp), %rdi
cmpq %r14, %rdi
je 0x2808
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x2210
movl %ebx, %eax
movl 0xc0(%rsp,%rax,4), %esi
leaq 0x1d8(%rsp), %r14
movq %r14, %rdi
callq 0x2160
leaq 0x28ae(%rip), %rsi      # 0x50d6
movl $0x1, %edx
movq %rax, %rdi
callq 0x2240
movq 0x148(%rsp), %rbp
cmpq 0x150(%rsp), %rbp
je 0x2884
leaq 0x1d8(%rsp), %r12
leaq 0x2880(%rip), %r13      # 0x50d6
cmpl %ebx, 0x4(%rbp)
jne 0x2876
movl (%rbp), %esi
movq %r12, %rdi
callq 0x2160
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2240
addq $0x28, %rbp
cmpq 0x150(%rsp), %rbp
jne 0x2856
movq 0x1d8(%rsp), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
leaq 0x1d8(%rsp), %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
leaq 0x88(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x27c8(%rip), %rsi      # 0x5090
leaq 0x27c7(%rip), %rdx      # 0x5096
leaq 0x78(%rsp), %rdi
callq 0x2f90
leaq 0xb8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x3ba2
movl %eax, %ebx
movq 0x78(%rsp), %rdi
cmpq %r14, %rdi
je 0x2907
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x2210
movl %ebx, %eax
movl 0xc0(%rsp,%rax,4), %esi
leaq 0x1d8(%rsp), %r14
movq %r14, %rdi
callq 0x2160
leaq 0x27af(%rip), %rsi      # 0x50d6
movl $0x1, %edx
movq %rax, %rdi
callq 0x2240
movq 0x148(%rsp), %rbp
cmpq 0x150(%rsp), %rbp
je 0x2983
leaq 0x1d8(%rsp), %r12
leaq 0x2781(%rip), %r13      # 0x50d6
cmpl %ebx, 0x4(%rbp)
jne 0x2975
movl (%rbp), %esi
movq %r12, %rdi
callq 0x2160
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2240
addq $0x28, %rbp
cmpq 0x150(%rsp), %rbp
jne 0x2955
movq 0x1d8(%rsp), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
leaq 0x1d8(%rsp), %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
leaq 0xb8(%rsp), %rdi
callq 0x3cb4
cmpq $0x0, 0x1d0(%rsp)
je 0x2bac
leaq 0x68(%rsp), %r13
xorl %r12d, %r12d
leaq 0x58(%rsp), %rbp
leaq 0x28(%rsp), %r14
movq %r14, %rbx
movl 0xc0(%rsp,%r12,4), %r14d
testl %r14d, %r14d
je 0x2a22
movq %r13, 0x58(%rsp)
movq %rbp, %rdi
leaq 0x2689(%rip), %rsi      # 0x508a
leaq 0x2687(%rip), %rdx      # 0x508f
callq 0x2f90
leaq 0xb8(%rsp), %rdi
movq %rbp, %rsi
callq 0x3ba2
cmpl %eax, %r12d
jne 0x2a28
xorl %eax, %eax
xorl %ebp, %ebp
jmp 0x2a64
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rbx, %rbp
movq %rbx, %rdi
leaq 0x2651(%rip), %rsi      # 0x5090
leaq 0x2650(%rip), %rdx      # 0x5096
callq 0x2f90
leaq 0xb8(%rsp), %rdi
movq %rbp, %rsi
callq 0x3ba2
cmpl %eax, %r12d
setne %bpl
movb $0x1, %al
testb %al, %al
je 0x2a84
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x2a84
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2210
testl %r14d, %r14d
je 0x2aa0
movq 0x58(%rsp), %rdi
cmpq %r13, %rdi
je 0x2aa0
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x2210
testb %bpl, %bpl
leaq 0x58(%rsp), %rbp
movq %rbx, %r14
je 0x2b9b
leaq 0x8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
movl %r12d, %edx
callq 0x3f84
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x1d8(%rsp), %rdi
callq 0x2240
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x25e7(%rip), %rsi      # 0x50d6
callq 0x2240
movl 0xc0(%rsp,%r12,4), %esi
movq %r14, %rdi
callq 0x2160
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x25c0(%rip), %rsi      # 0x50d6
callq 0x2240
movl 0xfc(%rsp,%r12,4), %esi
movq %r14, %rdi
callq 0x2160
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2599(%rip), %rsi      # 0x50d6
callq 0x2240
movl 0x120(%rsp,%r12,4), %esi
movq %r14, %rdi
callq 0x2160
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
movq %rbx, %r14
je 0x2b9b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2210
incl %r12d
cmpq %r12, 0x1d0(%rsp)
ja 0x29e2
leaq 0x1d8(%rsp), %rdi
callq 0x2280
movq 0x50(%rsp), %rdi
callq 0x2270
movq %r15, %rdi
callq 0x2270
movq 0x53fe(%rip), %rdi      # 0x7fd0
leaq 0x24be(%rip), %rsi      # 0x5097
movl $0x28, %edx
callq 0x2240
movq 0x53e6(%rip), %rbx      # 0x7fd0
leaq 0x24cf(%rip), %rsi      # 0x50c0
movl $0x17, %edx
movq %rbx, %rdi
callq 0x2240
movq 0x48(%rsp), %r14
testq %r14, %r14
je 0x2c24
movq %r14, %rdi
callq 0x2110
movq 0x53b9(%rip), %rdi      # 0x7fd0
movq %r14, %rsi
movq %rax, %rdx
callq 0x2240
jmp 0x2c3b
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x22e0
xorl %ebx, %ebx
movq 0x538c(%rip), %rdi      # 0x7fd0
leaq 0x248d(%rip), %rsi      # 0x50d8
movl $0x5, %edx
callq 0x2240
movq 0x536c(%rip), %rax      # 0x7fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x1e0(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x1d8(%rsp,%rcx)
callq 0x21c0
leaq 0x2d0(%rsp), %rdi
callq 0x20e0
jmp 0x25a9
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r14, %rdi
je 0x2d46
movq 0x88(%rsp), %rsi
jmp 0x2d3e
jmp 0x2d22
movq %rax, %rbx
movq 0x98(%rsp), %rdi
cmpq %r14, %rdi
je 0x2d46
movq 0xa8(%rsp), %rsi
jmp 0x2d3e
jmp 0x2d22
jmp 0x2d22
jmp 0x2d22
jmp 0x2d19
jmp 0x2d19
jmp 0x2d22
jmp 0x2d03
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x2d06
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2210
jmp 0x2d06
jmp 0x2d22
jmp 0x2d22
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r13, %rdi
je 0x2d46
movq 0x68(%rsp), %rsi
jmp 0x2d3e
jmp 0x2d22
movq %rax, %rbx
jmp 0x2d53
jmp 0x2d22
jmp 0x2d22
movq %rax, %rbx
jmp 0x2d46
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2d46
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2210
leaq 0x1d8(%rsp), %rdi
callq 0x22a0
leaq 0xb8(%rsp), %rdi
callq 0x2d68
movq %rbx, %rdi
callq 0x2300