// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rdi, %rbx
leaq 0x40(%rsp), %r14
movq %r14, -0x10(%r14)
movq $0x0, -0x8(%r14)
movb $0x0, (%r14)
leaq 0x90(%rsp), %rdi
movl $0x8, %edx
callq 0x2320
leaq 0x108(%rsp), %rdi
callq 0x20a0
testb %al, %al
je 0x338e
leaq 0x50(%rsp), %rbp
leaq 0x90(%rsp), %r14
movq 0x90(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %edx
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
callq 0x2330
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x33ca
leaq 0x30(%rsp), %rdi
leaq 0x1f9c(%rip), %rsi      # 0x5063
callq 0x20d0
testl %eax, %eax
je 0x3080
movq 0x30(%rsp), %r15
cmpb $0x23, (%r15)
je 0x3080
movq 0x38(%rsp), %r13
incq %r13
movq %r13, %rdi
callq 0x2030
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2130
movq %r12, %r15
decq %r15
movb 0x1(%r15), %r14b
incq %r15
movsbl %r14b, %edi
callq 0x2070
testl %eax, %eax
jne 0x3102
testb %r14b, %r14b
je 0x3381
movq %r15, %rdi
leaq 0x1ff7(%rip), %rsi      # 0x5120
callq 0x22c0
movq %rax, %rdi
callq 0x3541
movq %rax, %r15
movl $0x6, %edx
movq %rax, %rdi
leaq 0x1f42(%rip), %rsi      # 0x508a
callq 0x2120
testl %eax, %eax
leaq 0x90(%rsp), %r14
je 0x32df
movl $0x7, %edx
movq %r15, %rdi
leaq 0x1f24(%rip), %rsi      # 0x5090
callq 0x2120
testl %eax, %eax
je 0x331c
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x4008
movq %rbx, %rdi
movl $0x4, %esi
movq %r14, %rdx
callq 0x38e0
movl %eax, %r15d
xorl %edi, %edi
leaq 0x1fe4(%rip), %rsi      # 0x518c
callq 0x22c0
movq %rax, %rdi
callq 0x3541
leaq 0x70(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x4008
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3ba2
movq %rbx, %rdi
movl %eax, %esi
callq 0x3ab0
movl %eax, %r13d
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x3202
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2210
movq 0x90(%rbx), %r14
movl $0x18, %edi
callq 0x2200
movl %r13d, %ecx
leaq (%rcx,%rcx,4), %rcx
leaq (%r14,%rcx,8), %r14
addq $0x10, %r14
movl %r15d, 0x10(%rax)
movq %rax, %rdi
movq %r14, %rsi
callq 0x2060
incq 0x10(%r14)
xorl %edi, %edi
leaq 0x1f50(%rip), %rsi      # 0x518e
callq 0x22c0
testq %rax, %rax
leaq 0x90(%rsp), %r14
je 0x3358
movq %rax, %rdi
callq 0x3541
movq %rax, %r15
movq %rax, %rdi
callq 0x2110
cmpb $0x29, -0x1(%r15,%rax)
jne 0x3275
movb $0x0, -0x1(%r15,%rax)
movq %r15, %rdi
callq 0x3541
movq %rbp, %rdi
movq %rax, %rsi
leaq 0xf(%rsp), %rdx
callq 0x4008
movq %rbx, %rdi
movl $0x4, %esi
movq %rbp, %rdx
callq 0x38e0
movq %rbx, %rdi
movl %eax, %esi
movl %r13d, %edx
callq 0x3c3c
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x32c6
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2210
xorl %edi, %edi
leaq 0x1ebf(%rip), %rsi      # 0x518e
callq 0x22c0
testq %rax, %rax
jne 0x3254
jmp 0x3358
xorl %edi, %edi
leaq 0x1e53(%rip), %rsi      # 0x513b
callq 0x22c0
movq %rax, %rdi
callq 0x3541
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x4008
movq %rbx, %rdi
movq %r15, %rsi
callq 0x359a
testl %eax, %eax
je 0x3358
jmp 0x345f
xorl %edi, %edi
leaq 0x1e16(%rip), %rsi      # 0x513b
callq 0x22c0
movq %rax, %rdi
callq 0x3541
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x4008
movq %rbx, %rdi
movq %r15, %rsi
callq 0x37d0
testl %eax, %eax
jne 0x3474
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x3374
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2210
movq %r12, %rdi
callq 0x2270
jmp 0x3080
movq %r12, %rdi
callq 0x2270
jmp 0x3078
movq 0x4c5b(%rip), %rbx      # 0x7ff0
leaq 0x1d87(%rip), %rsi      # 0x5123
movl $0x17, %edx
movq %rbx, %rdi
callq 0x2240
testq %r15, %r15
je 0x33e0
movq %r15, %rdi
callq 0x2110
movq 0x4c33(%rip), %rdi      # 0x7ff0
movq %r15, %rsi
movq %rax, %rdx
callq 0x2240
jmp 0x33f7
xorl %ebx, %ebx
leaq 0x90(%rsp), %rdi
leaq 0x40(%rsp), %r14
callq 0x2090
jmp 0x3427
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x22e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21e0
movsbl %al, %esi
movq 0x4bdb(%rip), %rdi      # 0x7ff0
callq 0x2040
movl $0x1, %ebx
movq %rax, %rdi
callq 0x2170
leaq 0x90(%rsp), %rdi
callq 0x20b0
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x344b
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2210
movl %ebx, %eax
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4b8a(%rip), %rdi      # 0x7ff0
leaq 0x1cd0(%rip), %rsi      # 0x513d
callq 0x21f0
jmp 0x3487
movq 0x4b75(%rip), %rdi      # 0x7ff0
leaq 0x1ce2(%rip), %rsi      # 0x5164
callq 0x21f0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2260
jmp 0x34cb
jmp 0x34cb
jmp 0x34cb
jmp 0x34d0
movq %rax, %rbx
jmp 0x351d
jmp 0x34cb
jmp 0x34d0
jmp 0x34cb
jmp 0x34cb
jmp 0x34d0
jmp 0x34cb
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x34f4
movq 0x80(%rsp), %rsi
jmp 0x34ec
jmp 0x34d0
movq %rax, %rbx
jmp 0x34f4
movq %rax, %rbx
jmp 0x3510
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x34f4
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2210
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x3510
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2210
leaq 0x90(%rsp), %rdi
callq 0x20b0
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x3539
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2210
movq %rbx, %rdi
callq 0x2300