// File: /workspace/github_cpp_makefile_star2_O3/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 0x33ae
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 0x33ea
leaq 0x30(%rsp), %rdi
leaq 0x1f7c(%rip), %rsi      # 0x5063
callq 0x20d0
testl %eax, %eax
je 0x30a0
movq 0x30(%rsp), %r15
cmpb $0x23, (%r15)
je 0x30a0
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 0x3122
testb %r14b, %r14b
je 0x33a1
movq %r15, %rdi
leaq 0x1fbf(%rip), %rsi      # 0x5108
callq 0x22c0
movq %rax, %rdi
callq 0x3561
movq %rax, %r15
movl $0x6, %edx
movq %rax, %rdi
leaq 0x1f22(%rip), %rsi      # 0x508a
callq 0x2120
testl %eax, %eax
leaq 0x90(%rsp), %r14
je 0x32ff
movl $0x7, %edx
movq %r15, %rdi
leaq 0x1f04(%rip), %rsi      # 0x5090
callq 0x2120
testl %eax, %eax
je 0x333c
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x3fac
movq %rbx, %rdi
movl $0x4, %esi
movq %r14, %rdx
callq 0x38e0
movl %eax, %r15d
xorl %edi, %edi
leaq 0x1fac(%rip), %rsi      # 0x5174
callq 0x22c0
movq %rax, %rdi
callq 0x3561
leaq 0x70(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x3fac
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3b76
movq %rbx, %rdi
movl %eax, %esi
callq 0x3a9a
movl %eax, %r13d
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x3222
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 0x1f18(%rip), %rsi      # 0x5176
callq 0x22c0
testq %rax, %rax
leaq 0x90(%rsp), %r14
je 0x3378
movq %rax, %rdi
callq 0x3561
movq %rax, %r15
movq %rax, %rdi
callq 0x2110
cmpb $0x29, -0x1(%r15,%rax)
jne 0x3295
movb $0x0, -0x1(%r15,%rax)
movq %r15, %rdi
callq 0x3561
movq %rbp, %rdi
movq %rax, %rsi
leaq 0xf(%rsp), %rdx
callq 0x3fac
movq %rbx, %rdi
movl $0x4, %esi
movq %rbp, %rdx
callq 0x38e0
movq %rbx, %rdi
movl %eax, %esi
movl %r13d, %edx
callq 0x3c10
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x32e6
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2210
xorl %edi, %edi
leaq 0x1e87(%rip), %rsi      # 0x5176
callq 0x22c0
testq %rax, %rax
jne 0x3274
jmp 0x3378
xorl %edi, %edi
leaq 0x1e1b(%rip), %rsi      # 0x5123
callq 0x22c0
movq %rax, %rdi
callq 0x3561
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x3fac
movq %rbx, %rdi
movq %r15, %rsi
callq 0x35ba
testl %eax, %eax
je 0x3378
jmp 0x347f
xorl %edi, %edi
leaq 0x1dde(%rip), %rsi      # 0x5123
callq 0x22c0
movq %rax, %rdi
callq 0x3561
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
movq %rbp, %rdx
callq 0x3fac
movq %rbx, %rdi
movq %r15, %rsi
callq 0x37d0
testl %eax, %eax
jne 0x3494
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x3394
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2210
movq %r12, %rdi
callq 0x2270
jmp 0x30a0
movq %r12, %rdi
callq 0x2270
jmp 0x3098
movq 0x4c3b(%rip), %rbx      # 0x7ff0
leaq 0x1d4f(%rip), %rsi      # 0x510b
movl $0x17, %edx
movq %rbx, %rdi
callq 0x2240
testq %r15, %r15
je 0x3400
movq %r15, %rdi
callq 0x2110
movq 0x4c13(%rip), %rdi      # 0x7ff0
movq %r15, %rsi
movq %rax, %rdx
callq 0x2240
jmp 0x3417
xorl %ebx, %ebx
leaq 0x90(%rsp), %rdi
leaq 0x40(%rsp), %r14
callq 0x2090
jmp 0x3447
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 0x4bbb(%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 0x346b
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 0x4b6a(%rip), %rdi      # 0x7ff0
leaq 0x1c98(%rip), %rsi      # 0x5125
callq 0x21f0
jmp 0x34a7
movq 0x4b55(%rip), %rdi      # 0x7ff0
leaq 0x1caa(%rip), %rsi      # 0x514c
callq 0x21f0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2260
jmp 0x34eb
jmp 0x34eb
jmp 0x34eb
jmp 0x34f0
movq %rax, %rbx
jmp 0x353d
jmp 0x34eb
jmp 0x34f0
jmp 0x34eb
jmp 0x34eb
jmp 0x34f0
jmp 0x34eb
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x3514
movq 0x80(%rsp), %rsi
jmp 0x350c
jmp 0x34f0
movq %rax, %rbx
jmp 0x3514
movq %rax, %rbx
jmp 0x3530
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x3514
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2210
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x3530
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 0x3559
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2210
movq %rbx, %rdi
callq 0x2300