// File: 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
movq %rsp, %rbp
subq $0x470, %rsp            # imm = 0x470
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x358(%rbp)
movb $0x0, -0x1a(%rbp)
movw $0x3d28, -0x1c(%rbp)    # imm = 0x3D28
leaq -0x58(%rbp), %rdi
callq 0x2320
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x270(%rbp), %rdi
movl $0x8, %edx
callq 0x23f0
jmp 0x4bbd
leaq -0x270(%rbp), %rdi
callq 0x2380
movb %al, -0x359(%rbp)
jmp 0x4bd1
movb -0x359(%rbp), %al
testb $0x1, %al
jne 0x4c6f
jmp 0x4be1
movq 0xc408(%rip), %rdi      # 0x10ff0
leaq 0x6521(%rip), %rsi      # 0xb110
callq 0x2220
movq %rax, -0x368(%rbp)
jmp 0x4bfd
movq -0x368(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2220
movq %rax, -0x370(%rbp)
jmp 0x4c16
movq -0x370(%rbp), %rdi
movq 0xc39c(%rip), %rsi      # 0x10fc0
callq 0x2260
jmp 0x4c2b
movl $0x1, -0x4(%rbp)
movl $0x1, -0x280(%rbp)
jmp 0x5427
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
jmp 0x5454
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
jmp 0x5448
jmp 0x4c71
leaq -0x270(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x2090
movq %rax, -0x378(%rbp)
jmp 0x4c8a
movq -0x378(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x22f0
movb %al, -0x379(%rbp)
jmp 0x4ca8
movb -0x379(%rbp), %al
testb $0x1, %al
jne 0x4cb7
jmp 0x5408
leaq 0x63a5(%rip), %rsi      # 0xb063
leaq -0x58(%rbp), %rdi
callq 0x6700
movb %al, -0x37a(%rbp)
jmp 0x4ccf
movb -0x37a(%rbp), %al
testb $0x1, %al
jne 0x4cdb
jmp 0x4cdd
jmp 0x4c71
xorl %eax, %eax
movl %eax, %esi
leaq -0x58(%rbp), %rdi
callq 0x2410
movq %rax, -0x388(%rbp)
jmp 0x4cf3
movq -0x388(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x4d07
jmp 0x4c71
leaq -0x58(%rbp), %rdi
callq 0x21a0
movq %rax, %rdi
incq %rdi
callq 0x2030
movq %rax, -0x390(%rbp)
jmp 0x4d24
movq -0x390(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x3a0(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x2100
movq %rax, -0x398(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x21a0
movq -0x3a0(%rbp), %rdi
movq -0x398(%rbp), %rsi
movq %rax, %rdx
addq $0x1, %rdx
callq 0x2140
movq -0x28(%rbp), %rax
movsbl (%rax), %edi
callq 0x2060
cmpl $0x0, %eax
je 0x4d94
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x4d75
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x4dc2
movq -0x30(%rbp), %rax
movq %rax, -0x3a8(%rbp)
cmpq $0x0, %rax
je 0x4dbd
movq -0x3a8(%rbp), %rdi
callq 0x2300
jmp 0x4c71
movq -0x28(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x2370
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x5470
movq %rax, -0x3b0(%rbp)
jmp 0x4de5
movq -0x3b0(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
leaq 0x6294(%rip), %rsi      # 0xb08f
movl $0x6, %edx
callq 0x2130
cmpl $0x0, %eax
jne 0x4f2a
leaq 0x6313(%rip), %rsi      # 0xb128
xorl %eax, %eax
movl %eax, %edi
callq 0x2370
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x5470
movq %rax, -0x3b8(%rbp)
jmp 0x4e34
movq -0x3b8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x3c8(%rbp)
leaq -0x2a1(%rbp), %rdi
movq %rdi, -0x3c0(%rbp)
callq 0x23d0
movq -0x3c8(%rbp), %rsi
movq -0x3c0(%rbp), %rdx
leaq -0x2a0(%rbp), %rdi
callq 0x34f0
jmp 0x4e79
leaq -0x2a1(%rbp), %rdi
callq 0x2280
movq -0x358(%rbp), %rdi
leaq -0x2a0(%rbp), %rsi
callq 0x5530
movl %eax, -0x3cc(%rbp)
jmp 0x4ea0
movl -0x3cc(%rbp), %eax
cmpl $0x0, %eax
je 0x4f10
movq 0xc13e(%rip), %rdi      # 0x10ff0
leaq 0x6271(%rip), %rsi      # 0xb12a
callq 0x2220
jmp 0x4ec0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x22d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x2a1(%rbp), %rdi
callq 0x2280
jmp 0x5448
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x2a0(%rbp), %rdi
callq 0x2170
jmp 0x5448
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
leaq -0x2a0(%rbp), %rdi
callq 0x2170
jmp 0x53e6
movq -0x38(%rbp), %rdi
leaq 0x6160(%rip), %rsi      # 0xb095
movl $0x7, %edx
callq 0x2130
cmpl $0x0, %eax
jne 0x5064
leaq 0x61d9(%rip), %rsi      # 0xb128
xorl %eax, %eax
movl %eax, %edi
callq 0x2370
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x5470
movq %rax, -0x3d8(%rbp)
jmp 0x4f6e
movq -0x3d8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x3e8(%rbp)
leaq -0x2c9(%rbp), %rdi
movq %rdi, -0x3e0(%rbp)
callq 0x23d0
movq -0x3e8(%rbp), %rsi
movq -0x3e0(%rbp), %rdx
leaq -0x2c8(%rbp), %rdi
callq 0x34f0
jmp 0x4fb3
leaq -0x2c9(%rbp), %rdi
callq 0x2280
movq -0x358(%rbp), %rdi
leaq -0x2c8(%rbp), %rsi
callq 0x57b0
movl %eax, -0x3ec(%rbp)
jmp 0x4fda
movl -0x3ec(%rbp), %eax
cmpl $0x0, %eax
je 0x504a
movq 0xc004(%rip), %rdi      # 0x10ff0
leaq 0x615e(%rip), %rsi      # 0xb151
callq 0x2220
jmp 0x4ffa
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x22d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x2c9(%rbp), %rdi
callq 0x2280
jmp 0x5448
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x2c8(%rbp), %rdi
callq 0x2170
jmp 0x5448
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
leaq -0x2c8(%rbp), %rdi
callq 0x2170
jmp 0x53e4
movq -0x38(%rbp), %rax
movq %rax, -0x400(%rbp)
leaq -0x2f1(%rbp), %rdi
movq %rdi, -0x3f8(%rbp)
callq 0x23d0
movq -0x400(%rbp), %rsi
movq -0x3f8(%rbp), %rdx
leaq -0x2f0(%rbp), %rdi
callq 0x34f0
jmp 0x509e
leaq -0x2f1(%rbp), %rdi
callq 0x2280
movq -0x358(%rbp), %rdi
movl $0x4, %esi
leaq -0x2f0(%rbp), %rdx
callq 0x5930
movl %eax, -0x404(%rbp)
jmp 0x50ca
movl -0x404(%rbp), %eax
movl %eax, -0x2f8(%rbp)
leaq 0x609c(%rip), %rsi      # 0xb179
xorl %eax, %eax
movl %eax, %edi
callq 0x2370
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x5470
movq %rax, -0x410(%rbp)
jmp 0x50fc
movq -0x410(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x420(%rbp)
leaq -0x321(%rbp), %rdi
movq %rdi, -0x418(%rbp)
callq 0x23d0
movq -0x420(%rbp), %rsi
movq -0x418(%rbp), %rdx
leaq -0x320(%rbp), %rdi
callq 0x34f0
jmp 0x5141
movq -0x358(%rbp), %rdi
leaq -0x320(%rbp), %rsi
callq 0x5c30
movl %eax, -0x424(%rbp)
jmp 0x515c
movl -0x424(%rbp), %esi
movq -0x358(%rbp), %rdi
callq 0x5b50
movl %eax, -0x428(%rbp)
jmp 0x5176
leaq -0x320(%rbp), %rdi
callq 0x2170
leaq -0x321(%rbp), %rdi
callq 0x2280
movl -0x428(%rbp), %eax
movq -0x358(%rbp), %rdi
movl %eax, -0x2fc(%rbp)
movl -0x2fc(%rbp), %esi
movl -0x2f8(%rbp), %edx
callq 0x5ce0
jmp 0x51b4
jmp 0x51b6
xorl %eax, %eax
movl %eax, %edi
leaq 0x5fba(%rip), %rsi      # 0xb17b
callq 0x2370
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
je 0x53bf
movq -0x38(%rbp), %rdi
callq 0x5470
movq %rax, -0x430(%rbp)
jmp 0x51e6
movq -0x430(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x438(%rbp)
movq -0x38(%rbp), %rdi
callq 0x2120
movq %rax, %rcx
movq -0x438(%rbp), %rax
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x29, %eax
jne 0x52c5
movq -0x38(%rbp), %rax
movq %rax, -0x440(%rbp)
movq -0x38(%rbp), %rdi
callq 0x2120
movq %rax, %rcx
movq -0x440(%rbp), %rax
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
jmp 0x52c5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x2f1(%rbp), %rdi
callq 0x2280
jmp 0x5448
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
jmp 0x53d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
jmp 0x52b4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x320(%rbp), %rdi
callq 0x2170
leaq -0x321(%rbp), %rdi
callq 0x2280
jmp 0x53d6
movq -0x38(%rbp), %rdi
callq 0x5470
movq %rax, -0x448(%rbp)
jmp 0x52d7
movq -0x448(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x458(%rbp)
leaq -0x349(%rbp), %rdi
movq %rdi, -0x450(%rbp)
callq 0x23d0
movq -0x458(%rbp), %rsi
movq -0x450(%rbp), %rdx
leaq -0x348(%rbp), %rdi
callq 0x34f0
jmp 0x531c
leaq -0x349(%rbp), %rdi
callq 0x2280
movq -0x358(%rbp), %rdi
movl $0x4, %esi
leaq -0x348(%rbp), %rdx
callq 0x5930
movl %eax, -0x45c(%rbp)
jmp 0x5348
movq -0x358(%rbp), %rdi
movl -0x45c(%rbp), %eax
movl %eax, -0x350(%rbp)
movl -0x350(%rbp), %esi
movl -0x2fc(%rbp), %edx
callq 0x5d20
jmp 0x536e
leaq -0x348(%rbp), %rdi
callq 0x2170
jmp 0x51b6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x349(%rbp), %rdi
callq 0x2280
jmp 0x53d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x278(%rbp)
movl %eax, -0x27c(%rbp)
leaq -0x348(%rbp), %rdi
callq 0x2170
jmp 0x53d6
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
leaq -0x2f0(%rbp), %rdi
callq 0x2170
jmp 0x53e4
leaq -0x2f0(%rbp), %rdi
callq 0x2170
jmp 0x5448
jmp 0x53e6
movq -0x30(%rbp), %rax
movq %rax, -0x468(%rbp)
cmpq $0x0, %rax
je 0x5403
movq -0x468(%rbp), %rdi
callq 0x2300
jmp 0x4c71
leaq -0x270(%rbp), %rdi
callq 0x20a0
jmp 0x5416
movl $0x0, -0x4(%rbp)
movl $0x1, -0x280(%rbp)
leaq -0x270(%rbp), %rdi
callq 0x20b0
leaq -0x58(%rbp), %rdi
callq 0x2170
movl -0x4(%rbp), %eax
addq $0x470, %rsp            # imm = 0x470
popq %rbp
retq
leaq -0x270(%rbp), %rdi
callq 0x20b0
leaq -0x58(%rbp), %rdi
callq 0x2170
movq -0x278(%rbp), %rdi
callq 0x23c0
nopl (%rax)