// File: SNUCSE-CTA[P]GI/src/graph.cpp
// Function: Graph::checkFormat(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int32_t Graph::checkFormat(string aFileName)
{
	ifstream infile(aFileName);
	if(infile.fail()) {
		cerr << "Error: input file does not exist." << endl;
		nofile = true;
		return -1;
	}
	else {
		nofile = false;
	}

	char tag;
	int64_t vid, vlabel, left, right, elabel, gid;
	int64_t numNode = -1;
	int64_t numEdge = 0;

	bool flag_error = false; //turns true when file format is wrong

	string line;
	int64_t lineNumber = 0;
	//1. check the first line (t ID #nodes)
	if( getline(infile, line) ) {
		++lineNumber; //add 1 after getline
		istringstream ss(line);	//read values from this stream
		
		//check the number of words
		string oneWord;
		int32_t count = 0;
		while(ss >> oneWord)
			++count;
		if(count != 3) { //line does not consist of exactly 3 values
			cerr << "File Format Error: in line " << lineNumber << endl;
			flag_error = true;
		}

		//check the values
		if(flag_error == false) {
			ss.clear();
			ss.seekg(0);
			ss >> tag >> gid >> numNode;
			if( tag != 't' || numNode < 1 || numNode > INT32_MAX ) {
				cerr << "File Format Error: in line " << lineNumber << endl;
				flag_error = true;
			}
		}
	}
	else { //empty file!
		cerr << "Error: File is empty" << endl;
		flag_error = true;
	}
	
	//2. check the vertices line (v ID label)
	//	 vertex ID should be in the range of [0, #nodes)
	//	 vertices should appear in the increasing order of vertex ID
	if(flag_error == false) {
		for(int32_t i = 0; i < numNode; ++i) {
			if( getline(infile, line) ) {
				++lineNumber; //add 1 after getline
				istringstream ss(line);

				//check the number of words
				string oneWord;
				int32_t count = 0;
				while(ss >> oneWord)
					++count;
				if(count != 3) { //line does not consist of exactly 3 values
					cerr << "File Format Error: in line " << lineNumber << endl;
					flag_error = true;
					break;
				}

				ss.clear();
				ss.seekg(0);
				ss >> tag >> vid >> vlabel;
				if( tag != 'v' || vid != i || vlabel > INT32_MAX ) {
					cerr << "File Format Error: in line " << lineNumber << endl;
					flag_error = true;
					break;
				}
			}
			else {
				cerr << "File Format Error: not enough vertices" << endl;
				flag_error = true;
				break;
			}
		}
	}
	
	//3. check the edges line (e ID ID label)
	if(flag_error == false) {
		while( getline(infile, line) ) {
			++lineNumber; //add 1 after getline
			istringstream ss(line);

			//check the number of words
			string oneWord;
			int32_t count = 0;
			while(ss >> oneWord)
				++count;
			if(count != 4) { //line does not consist of exactly 4 values
				cerr << "File Format Error: in line " << lineNumber << endl;
				flag_error = true;
				break;
			}

			ss.clear();
			ss.seekg(0);
			ss >> tag >> left >> right >> elabel;
			if( tag != 'e' || left >= numNode || right >= numNode || elabel > INT32_MAX) {
				cerr << "File Format Error: in line " << lineNumber << endl;
				flag_error = true;
				break;
			}

			++numEdge;
		}
	}

	infile.close();

	if(numEdge > INT32_MAX) {
		cerr << "File Format Error: #edges == " << numEdge << " > " << INT32_MAX << endl;
		flag_error = true;
	}

	if(flag_error) {
		errfile = true;
		return -1;
	}
	else {
		return 0;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x920, %rsp            # imm = 0x920
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x7e8(%rbp)
leaq -0x220(%rbp), %rdi
movl $0x8, %edx
callq 0x3300
movq -0x220(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x220(%rbp,%rax), %rdi
callq 0x3030
movb %al, -0x7d9(%rbp)
jmp 0x4c6f
movb -0x7d9(%rbp), %al
testb $0x1, %al
jne 0x4c7b
jmp 0x4ce4
movq 0x1b36e(%rip), %rdi     # 0x1fff0
leaq 0x135b6(%rip), %rsi     # 0x1823f
callq 0x31b0
movq %rax, -0x7f0(%rbp)
jmp 0x4c97
movq -0x7f0(%rbp), %rdi
movq 0x1b313(%rip), %rsi     # 0x1ffb8
callq 0x31e0
jmp 0x4cac
movq -0x7e8(%rbp), %rax
movb $0x1, 0x8(%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x579d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
jmp 0x57b5
movq -0x7e8(%rbp), %rax
movb $0x0, 0x8(%rax)
movq $-0x1, -0x270(%rbp)
movq $0x0, -0x278(%rbp)
movb $0x0, -0x279(%rbp)
leaq -0x2a0(%rbp), %rdi
movq %rdi, -0x800(%rbp)
callq 0x32a0
movq -0x800(%rbp), %rsi
movq $0x0, -0x2a8(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x3070
movq %rax, -0x7f8(%rbp)
jmp 0x4d46
movq -0x7f8(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3250
movb %al, -0x801(%rbp)
jmp 0x4d64
movb -0x801(%rbp), %al
testb $0x1, %al
jne 0x4d73
jmp 0x4fec
movq -0x2a8(%rbp), %rax
incq %rax
movq %rax, -0x2a8(%rbp)
leaq -0x428(%rbp), %rdi
leaq -0x2a0(%rbp), %rsi
movl $0x8, %edx
callq 0x3330
jmp 0x4d9e
leaq -0x448(%rbp), %rdi
callq 0x32a0
movl $0x0, -0x44c(%rbp)
leaq -0x428(%rbp), %rdi
leaq -0x448(%rbp), %rsi
callq 0x3270
movq %rax, -0x810(%rbp)
jmp 0x4dd0
movq -0x810(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3250
movb %al, -0x811(%rbp)
jmp 0x4dee
movb -0x811(%rbp), %al
testb $0x1, %al
jne 0x4dfa
jmp 0x4e51
movl -0x44c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44c(%rbp)
jmp 0x4db4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
jmp 0x578f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x3140
leaq -0x428(%rbp), %rdi
callq 0x30f0
jmp 0x578f
cmpl $0x3, -0x44c(%rbp)
je 0x4eae
movq 0x1b18f(%rip), %rdi     # 0x1fff0
leaq 0x133f9(%rip), %rsi     # 0x18261
callq 0x31b0
movq %rax, -0x820(%rbp)
jmp 0x4e76
movq -0x820(%rbp), %rdi
movq -0x2a8(%rbp), %rsi
callq 0x33a0
movq %rax, -0x828(%rbp)
jmp 0x4e92
movq -0x828(%rbp), %rdi
movq 0x1b118(%rip), %rsi     # 0x1ffb8
callq 0x31e0
jmp 0x4ea7
movb $0x1, -0x279(%rbp)
movb -0x279(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x4fd2
movq -0x428(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x428(%rbp,%rax), %rdi
xorl %esi, %esi
callq 0x3340
jmp 0x4ede
xorl %eax, %eax
movl %eax, %esi
leaq -0x460(%rbp), %rdi
callq 0x7480
jmp 0x4ef0
movq -0x460(%rbp), %rsi
movq -0x458(%rbp), %rdx
leaq -0x428(%rbp), %rdi
callq 0x3260
jmp 0x4f0c
leaq -0x428(%rbp), %rdi
leaq -0x231(%rbp), %rsi
callq 0x3290
movq %rax, -0x830(%rbp)
jmp 0x4f28
movq -0x830(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x3120
movq %rax, -0x838(%rbp)
jmp 0x4f44
movq -0x838(%rbp), %rdi
leaq -0x270(%rbp), %rsi
callq 0x3120
jmp 0x4f59
movsbl -0x231(%rbp), %eax
cmpl $0x74, %eax
jne 0x4f7c
cmpq $0x1, -0x270(%rbp)
jl 0x4f7c
cmpq $0x7fffffff, -0x270(%rbp) # imm = 0x7FFFFFFF
jle 0x4fd0
movq 0x1b06d(%rip), %rdi     # 0x1fff0
leaq 0x132d7(%rip), %rsi     # 0x18261
callq 0x31b0
movq %rax, -0x840(%rbp)
jmp 0x4f98
movq -0x840(%rbp), %rdi
movq -0x2a8(%rbp), %rsi
callq 0x33a0
movq %rax, -0x848(%rbp)
jmp 0x4fb4
movq -0x848(%rbp), %rdi
movq 0x1aff6(%rip), %rsi     # 0x1ffb8
callq 0x31e0
jmp 0x4fc9
movb $0x1, -0x279(%rbp)
jmp 0x4fd2
leaq -0x448(%rbp), %rdi
callq 0x3140
leaq -0x428(%rbp), %rdi
callq 0x30f0
jmp 0x5024
movq 0x1affd(%rip), %rdi     # 0x1fff0
leaq 0x13283(%rip), %rsi     # 0x1827d
callq 0x31b0
movq %rax, -0x850(%rbp)
jmp 0x5008
movq -0x850(%rbp), %rdi
movq 0x1afa2(%rip), %rsi     # 0x1ffb8
callq 0x31e0
jmp 0x501d
movb $0x1, -0x279(%rbp)
movb -0x279(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x5379
movl $0x0, -0x464(%rbp)
movslq -0x464(%rbp), %rax
cmpq -0x270(%rbp), %rax
jge 0x5377
leaq -0x220(%rbp), %rdi
leaq -0x2a0(%rbp), %rsi
callq 0x3070
movq %rax, -0x858(%rbp)
jmp 0x5072
movq -0x858(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3250
movb %al, -0x859(%rbp)
jmp 0x5090
movb -0x859(%rbp), %al
testb $0x1, %al
jne 0x509f
jmp 0x5327
movq -0x2a8(%rbp), %rax
incq %rax
movq %rax, -0x2a8(%rbp)
leaq -0x5e8(%rbp), %rdi
leaq -0x2a0(%rbp), %rsi
movl $0x8, %edx
callq 0x3330
jmp 0x50ca
leaq -0x608(%rbp), %rdi
callq 0x32a0
movl $0x0, -0x60c(%rbp)
leaq -0x5e8(%rbp), %rdi
leaq -0x608(%rbp), %rsi
callq 0x3270
movq %rax, -0x868(%rbp)
jmp 0x50fc
movq -0x868(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3250
movb %al, -0x869(%rbp)
jmp 0x511a
movb -0x869(%rbp), %al
testb $0x1, %al
jne 0x5126
jmp 0x5166
movl -0x60c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60c(%rbp)
jmp 0x50e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
leaq -0x608(%rbp), %rdi
callq 0x3140
leaq -0x5e8(%rbp), %rdi
callq 0x30f0
jmp 0x578f
cmpl $0x3, -0x60c(%rbp)
je 0x51d2
movq 0x1ae7a(%rip), %rdi     # 0x1fff0
leaq 0x130e4(%rip), %rsi     # 0x18261
callq 0x31b0
movq %rax, -0x878(%rbp)
jmp 0x518b
movq -0x878(%rbp), %rdi
movq -0x2a8(%rbp), %rsi
callq 0x33a0
movq %rax, -0x880(%rbp)
jmp 0x51a7
movq -0x880(%rbp), %rdi
movq 0x1ae03(%rip), %rsi     # 0x1ffb8
callq 0x31e0
jmp 0x51bc
movb $0x1, -0x279(%rbp)
movl $0x4, -0x230(%rbp)
jmp 0x52ff
movq -0x5e8(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x5e8(%rbp,%rax), %rdi
xorl %esi, %esi
callq 0x3340
jmp 0x51ee
xorl %eax, %eax
movl %eax, %esi
leaq -0x620(%rbp), %rdi
callq 0x7480
jmp 0x5200
movq -0x620(%rbp), %rsi
movq -0x618(%rbp), %rdx
leaq -0x5e8(%rbp), %rdi
callq 0x3260
jmp 0x521c
leaq -0x5e8(%rbp), %rdi
leaq -0x231(%rbp), %rsi
callq 0x3290
movq %rax, -0x888(%rbp)
jmp 0x5238
movq -0x888(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0x3120
movq %rax, -0x890(%rbp)
jmp 0x5254
movq -0x890(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x3120
jmp 0x5269
movsbl -0x231(%rbp), %eax
cmpl $0x76, %eax
jne 0x5295
movq -0x240(%rbp), %rax
movslq -0x464(%rbp), %rcx
cmpq %rcx, %rax
jne 0x5295
cmpq $0x7fffffff, -0x248(%rbp) # imm = 0x7FFFFFFF
jle 0x52f5
movq 0x1ad54(%rip), %rdi     # 0x1fff0
leaq 0x12fbe(%rip), %rsi     # 0x18261
callq 0x31b0
movq %rax, -0x898(%rbp)
jmp 0x52b1
movq -0x898(%rbp), %rdi
movq -0x2a8(%rbp), %rsi
callq 0x33a0
movq %rax, -0x8a0(%rbp)
jmp 0x52cd
movq -0x8a0(%rbp), %rdi
movq 0x1acdd(%rip), %rsi     # 0x1ffb8
callq 0x31e0
jmp 0x52e2
movb $0x1, -0x279(%rbp)
movl $0x4, -0x230(%rbp)
jmp 0x52ff
movl $0x0, -0x230(%rbp)
leaq -0x608(%rbp), %rdi
callq 0x3140
leaq -0x5e8(%rbp), %rdi
callq 0x30f0
movl -0x230(%rbp), %eax
testl %eax, %eax
je 0x5325
jmp 0x5323
jmp 0x5377
jmp 0x5361
movq 0x1acc2(%rip), %rdi     # 0x1fff0
leaq 0x12f5d(%rip), %rsi     # 0x18292
callq 0x31b0
movq %rax, -0x8a8(%rbp)
jmp 0x5343
movq -0x8a8(%rbp), %rdi
movq 0x1ac67(%rip), %rsi     # 0x1ffb8
callq 0x31e0
jmp 0x5358
movb $0x1, -0x279(%rbp)
jmp 0x5377
jmp 0x5363
movl -0x464(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x464(%rbp)
jmp 0x5042
jmp 0x5379
movb -0x279(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x56a0
jmp 0x538f
leaq -0x220(%rbp), %rdi
leaq -0x2a0(%rbp), %rsi
callq 0x3070
movq %rax, -0x8b0(%rbp)
jmp 0x53ab
movq -0x8b0(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3250
movb %al, -0x8b1(%rbp)
jmp 0x53c9
movb -0x8b1(%rbp), %al
testb $0x1, %al
jne 0x53d8
jmp 0x569e
movq -0x2a8(%rbp), %rax
incq %rax
movq %rax, -0x2a8(%rbp)
leaq -0x7a0(%rbp), %rdi
leaq -0x2a0(%rbp), %rsi
movl $0x8, %edx
callq 0x3330
jmp 0x5403
leaq -0x7c0(%rbp), %rdi
callq 0x32a0
movl $0x0, -0x7c4(%rbp)
leaq -0x7a0(%rbp), %rdi
leaq -0x7c0(%rbp), %rsi
callq 0x3270
movq %rax, -0x8c0(%rbp)
jmp 0x5435
movq -0x8c0(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3250
movb %al, -0x8c1(%rbp)
jmp 0x5453
movb -0x8c1(%rbp), %al
testb $0x1, %al
jne 0x545f
jmp 0x549f
movl -0x7c4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c4(%rbp)
jmp 0x5419
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
leaq -0x7c0(%rbp), %rdi
callq 0x3140
leaq -0x7a0(%rbp), %rdi
callq 0x30f0
jmp 0x578f
cmpl $0x4, -0x7c4(%rbp)
je 0x550b
movq 0x1ab41(%rip), %rdi     # 0x1fff0
leaq 0x12dab(%rip), %rsi     # 0x18261
callq 0x31b0
movq %rax, -0x8d0(%rbp)
jmp 0x54c4
movq -0x8d0(%rbp), %rdi
movq -0x2a8(%rbp), %rsi
callq 0x33a0
movq %rax, -0x8d8(%rbp)
jmp 0x54e0
movq -0x8d8(%rbp), %rdi
movq 0x1aaca(%rip), %rsi     # 0x1ffb8
callq 0x31e0
jmp 0x54f5
movb $0x1, -0x279(%rbp)
movl $0xa, -0x230(%rbp)
jmp 0x5673
movq -0x7a0(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x7a0(%rbp,%rax), %rdi
xorl %esi, %esi
callq 0x3340
jmp 0x5527
xorl %eax, %eax
movl %eax, %esi
leaq -0x7d8(%rbp), %rdi
callq 0x7480
jmp 0x5539
movq -0x7d8(%rbp), %rsi
movq -0x7d0(%rbp), %rdx
leaq -0x7a0(%rbp), %rdi
callq 0x3260
jmp 0x5555
leaq -0x7a0(%rbp), %rdi
leaq -0x231(%rbp), %rsi
callq 0x3290
movq %rax, -0x8e0(%rbp)
jmp 0x5571
movq -0x8e0(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x3120
movq %rax, -0x8e8(%rbp)
jmp 0x558d
movq -0x8e8(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x3120
movq %rax, -0x8f0(%rbp)
jmp 0x55a9
movq -0x8f0(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x3120
jmp 0x55be
movsbl -0x231(%rbp), %eax
cmpl $0x65, %eax
jne 0x55f7
movq -0x250(%rbp), %rax
cmpq -0x270(%rbp), %rax
jge 0x55f7
movq -0x258(%rbp), %rax
cmpq -0x270(%rbp), %rax
jge 0x55f7
cmpq $0x7fffffff, -0x260(%rbp) # imm = 0x7FFFFFFF
jle 0x5657
movq 0x1a9f2(%rip), %rdi     # 0x1fff0
leaq 0x12c5c(%rip), %rsi     # 0x18261
callq 0x31b0
movq %rax, -0x8f8(%rbp)
jmp 0x5613
movq -0x8f8(%rbp), %rdi
movq -0x2a8(%rbp), %rsi
callq 0x33a0
movq %rax, -0x900(%rbp)
jmp 0x562f
movq -0x900(%rbp), %rdi
movq 0x1a97b(%rip), %rsi     # 0x1ffb8
callq 0x31e0
jmp 0x5644
movb $0x1, -0x279(%rbp)
movl $0xa, -0x230(%rbp)
jmp 0x5673
movq -0x278(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x278(%rbp)
movl $0x0, -0x230(%rbp)
leaq -0x7c0(%rbp), %rdi
callq 0x3140
leaq -0x7a0(%rbp), %rdi
callq 0x30f0
movl -0x230(%rbp), %eax
testl %eax, %eax
je 0x5699
jmp 0x5697
jmp 0x569e
jmp 0x538f
jmp 0x56a0
leaq -0x220(%rbp), %rdi
callq 0x3080
jmp 0x56ae
cmpq $0x7fffffff, -0x278(%rbp) # imm = 0x7FFFFFFF
jle 0x5749
movq 0x1a92a(%rip), %rdi     # 0x1fff0
leaq 0x12bec(%rip), %rsi     # 0x182b9
callq 0x31b0
movq %rax, -0x908(%rbp)
jmp 0x56db
movq -0x908(%rbp), %rdi
movq -0x278(%rbp), %rsi
callq 0x33a0
movq %rax, -0x910(%rbp)
jmp 0x56f7
movq -0x910(%rbp), %rdi
leaq 0x12bd2(%rip), %rsi     # 0x182d7
callq 0x31b0
movq %rax, -0x918(%rbp)
jmp 0x5713
movq -0x918(%rbp), %rdi
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
callq 0x3350
movq %rax, -0x920(%rbp)
jmp 0x572d
movq -0x920(%rbp), %rdi
movq 0x1a87d(%rip), %rsi     # 0x1ffb8
callq 0x31e0
jmp 0x5742
movb $0x1, -0x279(%rbp)
testb $0x1, -0x279(%rbp)
je 0x5770
movq -0x7e8(%rbp), %rax
movb $0x1, 0x9(%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x5781
movl $0x0, -0x4(%rbp)
movl $0x1, -0x230(%rbp)
leaq -0x2a0(%rbp), %rdi
callq 0x3140
jmp 0x579d
leaq -0x2a0(%rbp), %rdi
callq 0x3140
jmp 0x57b5
leaq -0x220(%rbp), %rdi
callq 0x3090
movl -0x4(%rbp), %eax
addq $0x920, %rsp            # imm = 0x920
popq %rbp
retq
leaq -0x220(%rbp), %rdi
callq 0x3090
movq -0x228(%rbp), %rdi
callq 0x3370
nopl (%rax)