// File: /workspace/github_cpp_makefile_star2_O2/gauravtewari[P]netlist/netlist.cpp
// Function: circuit::parse_input(char const*)
u32 circuit::parse_input(const char *file){
	
	char in_seps[]="(=";		/* For normal lines the first separater encountered will be either one of these two */
	char *str, *str_ptr, *token;
	string line;
	u32 inputs =0, outputs=0, gates=0;
	
	/* Open the input file and check for error */
	ifstream in_file(file);
	if(!in_file.is_open()){
		cerr<<"Can't open input file: "<<file<<endl;
		return 1;
	}
	
	/* Read lines from the file */
	while(getline(in_file, line)){
		
		/* Leave empty lines */
		if(line=="") continue;				
		
		/* Leave comments */
		if(line[0] == '#') continue;
		
		/* get c type string for further processing */
		str_ptr = new char [line.size()+1];
		str = str_ptr;									/* Since the pointer is moved while removing the spaces, we might lost the count of allocated memory
															two pointers are kept. One can be moved around	 while the other can be used to free memory. */
		strncpy (str, line.c_str(), line.size()+1);

		/* Check if string is all spaces */
		while(isspace(*str)) str++;
			if(*str == 0) { delete []str_ptr; continue;}	

		/* Now we got rid of all empty and blank lines */
		/* Extract the first tokens from the input line */
		token = strtok( str, in_seps );
		token=trim(token);
	
		if(strncmp(token,"INPUT",6)==0){				/* An input net is found */
			token = strtok( NULL, ")" );
			token=trim(token);
			string t(token);
			if(add_input_edge(t) != 0){
				cerr<<"Error Adding Input Gate after parsing\n";
				exit(-1);
			}
			++inputs;
		}else if(strncmp(token,"OUTPUT",7)==0){ 		/* An output net if found */
				token = strtok( NULL, ")" );
				token=trim(token);
				string t(token);
				if(add_output_edge(t) != 0){
					cerr<<"Error Adding Output Gate after parsing\n";
					exit(-1);
				}
			++outputs;
		}else{											
			/* if neither input nor output then gate description is found */
				
				/* find an entry in net_list for this output net of the gate */
				string t(token);
				u32 onet = add_net(INTERNAL_NET, t);

				/* Getting the gate name (type) */
				token=strtok(NULL, "(");
				token=trim(token);

				/* Add an entry for this gate */
				u32 _gate = add_gate(get_gate_type(token));
				
				/* Add the net as output to the gate */
				add_outnet_to_gate(_gate, onet);

				/* This logic extracts all the input nets from the line */
				/* It can handle some spaces if they are inserted in between */
				/* Now the tokens are separated by comma, last token will either have a ')' character or some spaces followed by ')' at the end*/
				while((token = strtok( NULL, "," )) != NULL ){
					token=trim(token);
					if(token[strlen(token)-1]==')') token[strlen(token)-1]=0;
					token=trim(token);

					/* Get an edge for this net and add the gate to its gates list*/
					string t(token);
					u32 innet = add_net(INTERNAL_NET, t);
					add_gate_to_net(innet, _gate);
				}
				++gates;
		}
		delete []str_ptr;
	}
	in_file.close();
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x90(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x22f0
leaq 0x108(%rsp), %rdi
callq 0x20a0
testb %al, %al
je 0x3068
leaq 0x30(%rsp), %r15
leaq 0x70(%rsp), %r13
leaq 0x90(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x2080
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x3093
leaq 0x10(%rsp), %rdi
leaq 0x2220(%rip), %rsi      # 0x5063
callq 0x3975
testb %al, %al
jne 0x2e13
movq 0x10(%rsp), %r12
cmpb $0x23, (%r12)
je 0x2e13
movq 0x18(%rsp), %rbp
incq %rbp
movq %rbp, %rdi
callq 0x2030
movq %rax, %r14
movq %rax, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x2140
movq %r14, %r12
decq %r12
movb 0x1(%r12), %bpl
incq %r12
movsbl %bpl, %edi
callq 0x2060
testl %eax, %eax
jne 0x2e7f
testb %bpl, %bpl
je 0x305b
movq %r12, %rdi
leaq 0x2261(%rip), %rsi      # 0x5108
callq 0x22a0
movq %rax, %rdi
callq 0x3162
movq %rax, %r12
movq %rax, %rdi
leaq 0x21c9(%rip), %rsi      # 0x508a
pushq $0x6
popq %rdx
callq 0x2120
testl %eax, %eax
je 0x2fde
movq %r12, %rdi
leaq 0x21b5(%rip), %rsi      # 0x5090
pushq $0x7
popq %rdx
callq 0x2120
testl %eax, %eax
je 0x3019
movq %r15, %rbp
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2aaa
movq %rbx, %rdi
pushq $0x4
popq %rsi
movq %rbp, %rdx
callq 0x33fe
movl %eax, %r12d
xorl %edi, %edi
leaq 0x225e(%rip), %rsi      # 0x5174
callq 0x22a0
movq %rax, %rdi
callq 0x3162
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x2aaa
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x35d4
movq %rbx, %rdi
movl %eax, %esi
callq 0x353c
movl %eax, %ebp
leaq 0x50(%rsp), %rdi
callq 0x2110
movq %rbx, %rdi
movl %ebp, %esi
movl %r12d, %edx
callq 0x362c
movq %r15, %r12
xorl %edi, %edi
leaq 0x2206(%rip), %rsi      # 0x5176
callq 0x22a0
testq %rax, %rax
je 0x3053
movq %rax, %rdi
callq 0x3162
movq %rax, %r12
movq %rax, %rdi
callq 0x2100
cmpb $0x29, -0x1(%r12,%rax)
jne 0x2f9f
movb $0x0, -0x1(%r12,%rax)
movq %r12, %rdi
callq 0x3162
movq %r13, %rdi
movq %rax, %rsi
leaq 0xf(%rsp), %rdx
callq 0x2aaa
movq %rbx, %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
callq 0x33fe
movq %r15, %r12
movq %rbx, %rdi
movl %eax, %esi
movl %ebp, %edx
callq 0x3656
movq %r13, %rdi
callq 0x2110
jmp 0x2f67
xorl %edi, %edi
leaq 0x213c(%rip), %rsi      # 0x5123
callq 0x22a0
movq %rax, %rdi
callq 0x3162
movq %r15, %r12
movq %r15, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x2aaa
movq %rbx, %rdi
movq %r12, %rsi
callq 0x31ba
testl %eax, %eax
je 0x3053
jmp 0x30cd
xorl %edi, %edi
leaq 0x2101(%rip), %rsi      # 0x5123
callq 0x22a0
movq %rax, %rdi
callq 0x3162
movq %r15, %r12
movq %r15, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x2aaa
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3328
testl %eax, %eax
jne 0x30e2
movq %r12, %rdi
callq 0x2110
movq %r14, %rdi
callq 0x2240
jmp 0x2e13
movq 0x4f81(%rip), %rdi      # 0x7ff0
leaq 0x2095(%rip), %rsi      # 0x510b
callq 0x21d0
movq %rax, %rdi
movq %r14, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x2130
pushq $0x1
popq %rbx
jmp 0x30a2
xorl %ebx, %ebx
leaq 0x90(%rsp), %rdi
callq 0x2090
leaq 0x90(%rsp), %rdi
callq 0x20b0
leaq 0x10(%rsp), %rdi
callq 0x2110
movl %ebx, %eax
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4f1c(%rip), %rdi      # 0x7ff0
leaq 0x204a(%rip), %rsi      # 0x5125
callq 0x21d0
jmp 0x30f5
movq 0x4f07(%rip), %rdi      # 0x7ff0
leaq 0x205c(%rip), %rsi      # 0x514c
callq 0x21d0
pushq $-0x1
popq %rdi
callq 0x2230
jmp 0x3127
jmp 0x3127
jmp 0x3127
jmp 0x3122
jmp 0x3127
jmp 0x3122
jmp 0x3122
jmp 0x3127
jmp 0x3127
jmp 0x3127
movq %rax, %rbx
jmp 0x3150
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
jmp 0x3134
jmp 0x3122
movq %rax, %rbx
jmp 0x3143
movq %rax, %rbx
jmp 0x3139
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x2110
leaq 0x30(%rsp), %rdi
callq 0x2110
leaq 0x90(%rsp), %rdi
callq 0x20b0
leaq 0x10(%rsp), %rdi
callq 0x2110
movq %rbx, %rdi
callq 0x22d0