// File: /workspace/github_cpp_makefile_star2_O3/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 0x3040
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 0x2e26
leaq 0x178(%rsp), %rdi
callq 0x2e9e
leaq 0x160(%rsp), %rdi
callq 0x2dc6
movq %r14, %rdi
callq 0x2df6
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, 0x70(%rsp)
movq %rbx, 0x68(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x3e80
movq 0x148(%rsp), %rax
movq 0x150(%rsp), %rcx
xorl %r12d, %r12d
cmpq %rcx, %rax
je 0x26d6
movl 0xc(%rax), %edx
cmpl %r12d, %edx
cmoval %edx, %r12d
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 0x2c68
movl %r12d, %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 %r12d
leaq (,%r12,4), %rbx
movq %rbx, %rdi
callq 0x2030
movq %rax, %r15
testq %r12, %r12
je 0x2738
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2150
movq 0x148(%rsp), %rax
movq 0x150(%rsp), %rcx
cmpq %rcx, %rax
je 0x275a
movl 0xc(%rax), %edx
incl (%r15,%rdx,4)
addq $0x28, %rax
jmp 0x2748
testl %r12d, %r12d
je 0x27a8
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 0x276a
leaq 0xa8(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x28ce(%rip), %rsi      # 0x508a
leaq 0x28cc(%rip), %rdx      # 0x508f
leaq 0x98(%rsp), %rdi
callq 0x2faa
leaq 0xb8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x3b76
movl %eax, %ebx
movq %r15, 0x60(%rsp)
movq 0x98(%rsp), %rdi
cmpq %r12, %rdi
je 0x2809
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 0x28ad(%rip), %rsi      # 0x50d6
movl $0x1, %edx
movq %rax, %rdi
callq 0x2240
movq 0x148(%rsp), %rbp
movq 0x150(%rsp), %rax
cmpq %rax, %rbp
je 0x288b
leaq 0x1d8(%rsp), %r12
leaq 0x287c(%rip), %r13      # 0x50d6
cmpl %ebx, 0x4(%rbp)
jne 0x2882
movl (%rbp), %esi
movq %r12, %rdi
callq 0x2160
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2240
movq 0x150(%rsp), %rax
addq $0x28, %rbp
cmpq %rax, %rbp
jne 0x285a
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), %r12
movq %r12, -0x10(%r12)
leaq 0x27c0(%rip), %rsi      # 0x5090
leaq 0x27bf(%rip), %rdx      # 0x5096
leaq 0x78(%rsp), %rdi
callq 0x2faa
leaq 0xb8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x3b76
movl %eax, %ebx
movq 0x78(%rsp), %rdi
cmpq %r12, %rdi
je 0x290f
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 0x27a7(%rip), %rsi      # 0x50d6
movl $0x1, %edx
movq %rax, %rdi
callq 0x2240
movq 0x148(%rsp), %rbp
movq 0x150(%rsp), %rax
cmpq %rax, %rbp
je 0x2991
leaq 0x1d8(%rsp), %r12
leaq 0x2776(%rip), %r13      # 0x50d6
cmpl %ebx, 0x4(%rbp)
jne 0x2988
movl (%rbp), %esi
movq %r12, %rdi
callq 0x2160
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2240
movq 0x150(%rsp), %rax
addq $0x28, %rbp
cmpq %rax, %rbp
jne 0x2960
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 0x3c88
cmpq $0x0, 0x1d0(%rsp)
je 0x2bbd
movl $0x1, %ebx
xorl %r14d, %r14d
leaq 0xb8(%rsp), %r13
leaq 0x40(%rsp), %rbp
cmpl $0x0, 0xc0(%rsp,%r14,4)
je 0x2baa
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %rsp, %r12
movq %r12, %rdi
leaq 0x2672(%rip), %rsi      # 0x508a
leaq 0x2670(%rip), %rdx      # 0x508f
callq 0x2faa
movq %r13, %rdi
movq %r12, %rsi
callq 0x3b76
leal -0x1(%rbx), %r12d
cmpl %eax, %r12d
jne 0x2a54
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2baa
movq 0x10(%rsp), %rsi
jmp 0x2ba2
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %rbp, %rdi
leaq 0x2628(%rip), %rsi      # 0x5090
leaq 0x2627(%rip), %rdx      # 0x5096
callq 0x2faa
movq %rbp, %rsi
movq %r13, %r15
movq %r13, %rdi
callq 0x3b76
movl %eax, %r13d
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x2aa1
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2210
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2abc
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2210
cmpl %r13d, %r12d
movq %r15, %r13
je 0x2baa
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0x3f3e
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
leaq 0x1d8(%rsp), %rdi
callq 0x2240
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0x25d5(%rip), %rsi      # 0x50d6
callq 0x2240
movl 0xc0(%rsp,%r14,4), %esi
movq %r12, %rdi
callq 0x2160
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0x25ae(%rip), %rsi      # 0x50d6
callq 0x2240
movl 0xfc(%rsp,%r14,4), %esi
movq %r12, %rdi
callq 0x2160
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2587(%rip), %rsi      # 0x50d6
callq 0x2240
movl 0x120(%rsp,%r14,4), %esi
movq %r12, %rdi
callq 0x2160
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x2baa
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2210
movl %ebx, %r14d
incl %ebx
cmpq %r14, 0x1d0(%rsp)
ja 0x29f3
leaq 0x1d8(%rsp), %rdi
callq 0x2280
movq 0x70(%rsp), %rdi
callq 0x2270
movq 0x60(%rsp), %rdi
callq 0x2270
movq 0x53eb(%rip), %rdi      # 0x7fd0
leaq 0x24ab(%rip), %rsi      # 0x5097
movl $0x28, %edx
callq 0x2240
movq 0x53d3(%rip), %rbx      # 0x7fd0
leaq 0x24bc(%rip), %rsi      # 0x50c0
movl $0x17, %edx
movq %rbx, %rdi
callq 0x2240
movq 0x68(%rsp), %r14
testq %r14, %r14
je 0x2c37
movq %r14, %rdi
callq 0x2110
movq 0x53a6(%rip), %rdi      # 0x7fd0
movq %r14, %rsi
movq %rax, %rdx
callq 0x2240
jmp 0x2c4e
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 0x5379(%rip), %rdi      # 0x7fd0
leaq 0x247a(%rip), %rsi      # 0x50d8
movl $0x5, %edx
callq 0x2240
movq 0x5359(%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, %r14
movq 0x78(%rsp), %rdi
cmpq %r12, %rdi
je 0x2d60
movq 0x88(%rsp), %rsi
jmp 0x2d53
jmp 0x2d5d
movq %rax, %r14
movq 0x98(%rsp), %rdi
cmpq %r12, %rdi
je 0x2d60
movq 0xa8(%rsp), %rsi
jmp 0x2d53
jmp 0x2d5d
jmp 0x2d5d
jmp 0x2d5d
jmp 0x2d33
jmp 0x2d33
jmp 0x2d5d
jmp 0x2d5d
movq %rax, %r14
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x2d1a
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2210
jmp 0x2d1a
jmp 0x2d17
movq %rax, %r14
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2d60
movq 0x10(%rsp), %rsi
jmp 0x2d53
jmp 0x2d5d
jmp 0x2d5d
movq %rax, %r14
jmp 0x2d6d
jmp 0x2d5d
jmp 0x2d5d
movq %rax, %r14
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x2d60
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2210
jmp 0x2d60
movq %rax, %r14
leaq 0x1d8(%rsp), %rdi
callq 0x22a0
leaq 0xb8(%rsp), %rdi
callq 0x2d82
movq %r14, %rdi
callq 0x2300