// File: logicx24[P]RayTracer/raytracer.cpp
// Function: parseObj(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::vector<Polygon*, std::allocator<Polygon*>>*, std::vector<mat4, std::allocator<mat4>>*, Material&)
void parseObj(string fileName, vector<Polygon*> *sceneObjects, vector<mat4> *transforms, Material &curMat) {
	vector<vec3> verts;
	vector<vec3>normals;
	verts.push_back(vec3(0, 0, 0)); // Kill index 0, since vertices are numbered 1 ... n
	normals.push_back(vec3(0, 0, 0)); // Kill index 0, since normals are numbered 1 ... n
	ifstream objFile(fileName);
	string currentLine;
	string argument;
    if (!objFile.is_open()) {
    	cerr << "Error opening obj file" << endl;
    	return;
    }
    while (getline(objFile, currentLine)) {
    	istringstream iss;
    	iss.str(currentLine);
    	iss >> argument;
        if (currentLine.empty() || argumentMatches(argument, "#")) {
        	continue;
        } else if (argumentMatches(argument, "v")) {
        	float args[3];
            parseArgs(args, &iss, 3, "Incorrect number of arguments for vertex in obj file.");
            verts.push_back(vec3(args[0], args[1], args[2]));
        } else if (argumentMatches(argument, "vn")) {
        	float args[3];
            parseArgs(args, &iss, 3, "Incorrect number of arguments for vertex normal in obj file.");
            vec3 norm = vec3(args[0], args[1], args[2]);
            normals.push_back(norm);
        } else if (argumentMatches(argument, "f")) {
        	bool normalSpecified = false;
            int vertArgs[3];
            int normalArgs[3];
            string temp;
            int index = 0;
            while (iss >> temp) {
            	normalSpecified = (temp.find("//") != string::npos);
                /*
            	std::string s = "scott>=tiger>=mushroom";
            	std::string delimiter = ">=";

            	size_t pos = 0;
            	std::string token;
                while ((pos = s.find(delimiter)) != std::string::npos) {
                	    token = s.substr(0, pos);
                	        std::cout << token << std::endl;
                	            s.erase(0, pos + delimiter.length());

                }
                std::cout << s << std::endl;
                */
                if (normalSpecified) {
                	string delimeter = "//";
                	int pos = temp.find(delimeter);
                    string face = temp.substr(0, pos);
                    vertArgs[index] = stoi(face);
                    temp.erase(0, pos + delimeter.length());
                    normalArgs[index] = stoi(temp);
                } else {
                    vertArgs[index] = atoi(temp.c_str());
                }
                index++;
            }
            Triangle *t;
            mat4 currentTransform = reduceTransforms(transforms);
            if (normalSpecified) {
            	vec3 n1 = normals[normalArgs[0]];
            	vec3 n2 = normals[normalArgs[1]];
            	vec3 n3 = normals[normalArgs[2]];
                t = new Triangle(verts[vertArgs[0]], verts[vertArgs[1]], verts[vertArgs[2]], 
                              curMat, currentTransform, n1, n2, n3);
            } else {
                t = new Triangle(verts[vertArgs[0]], verts[vertArgs[1]], verts[vertArgs[2]], 
                                    curMat, currentTransform);
            }
            sceneObjects->push_back(t);
        } else {
        	cerr << "Unknown input in obj file: " << fileName << "." << endl;
        }
    }
    objFile.close();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x920, %rsp            # imm = 0x920
movq %rdi, -0x7f8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x7c70
leaq -0x50(%rbp), %rdi
callq 0x7c70
leaq -0x68(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x6860
jmp 0x3e78
leaq -0x38(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x7c90
jmp 0x3e87
leaq -0x90(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x6860
jmp 0x3e9e
leaq -0x50(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x7c90
jmp 0x3eb0
movq -0x7f8(%rbp), %rsi
leaq -0x298(%rbp), %rdi
movl $0x8, %edx
callq 0x3460
jmp 0x3eca
leaq -0x2b8(%rbp), %rdi
callq 0x33b0
leaq -0x2d8(%rbp), %rdi
callq 0x33b0
leaq -0x298(%rbp), %rdi
callq 0x3450
movb %al, -0x7f9(%rbp)
jmp 0x3ef6
movb -0x7f9(%rbp), %al
testb $0x1, %al
jne 0x3f64
jmp 0x3f02
movq 0x2c0e7(%rip), %rdi     # 0x2fff0
leaq 0x2116e(%rip), %rsi     # 0x2507e
callq 0x3220
movq %rax, -0x808(%rbp)
jmp 0x3f1e
movq -0x808(%rbp), %rdi
movq 0x2c08c(%rip), %rsi     # 0x2ffb8
callq 0x3270
jmp 0x3f33
movl $0x1, -0x2dc(%rbp)
jmp 0x493f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x49a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x497e
jmp 0x3f66
leaq -0x298(%rbp), %rdi
leaq -0x2b8(%rbp), %rsi
callq 0x3070
movq %rax, -0x810(%rbp)
jmp 0x3f82
movq -0x810(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3340
movb %al, -0x811(%rbp)
jmp 0x3fa0
movb -0x811(%rbp), %al
testb $0x1, %al
jne 0x3faf
jmp 0x4927
leaq -0x460(%rbp), %rdi
callq 0x31a0
jmp 0x3fbd
leaq -0x460(%rbp), %rdi
leaq -0x2b8(%rbp), %rsi
callq 0x32e0
jmp 0x3fd2
leaq -0x460(%rbp), %rdi
leaq -0x2d8(%rbp), %rsi
callq 0x3360
jmp 0x3fe7
leaq -0x2b8(%rbp), %rdi
callq 0x34a0
movb %al, %cl
movb $0x0, -0x481(%rbp)
movb $0x1, %al
testb $0x1, %cl
movb %al, -0x812(%rbp)
jne 0x403e
leaq -0x480(%rbp), %rdi
leaq -0x2d8(%rbp), %rsi
callq 0x3100
jmp 0x401e
movb $0x1, -0x481(%rbp)
leaq -0x480(%rbp), %rdi
leaq 0x21062(%rip), %rsi     # 0x25095
callq 0x3d50
movb %al, -0x812(%rbp)
movb -0x812(%rbp), %al
movb %al, -0x813(%rbp)
testb $0x1, -0x481(%rbp)
jne 0x4055
jmp 0x4061
leaq -0x480(%rbp), %rdi
callq 0x3160
movb -0x813(%rbp), %al
testb $0x1, %al
jne 0x406d
jmp 0x408d
movl $0x2, -0x2dc(%rbp)
jmp 0x48f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x4919
leaq -0x4a8(%rbp), %rdi
leaq -0x2d8(%rbp), %rsi
callq 0x3100
jmp 0x40a2
leaq -0x4a8(%rbp), %rdi
leaq 0x20fe7(%rip), %rsi     # 0x25097
callq 0x3d50
movb %al, -0x814(%rbp)
leaq -0x4a8(%rbp), %rdi
callq 0x3160
movb -0x814(%rbp), %al
testb $0x1, %al
jne 0x40d6
jmp 0x41cc
leaq -0x4b4(%rbp), %rax
movq %rax, -0x828(%rbp)
leaq -0x4d9(%rbp), %rdi
movq %rdi, -0x820(%rbp)
callq 0x3500
movq -0x820(%rbp), %rdx
leaq 0x20f94(%rip), %rsi     # 0x25099
leaq -0x4d8(%rbp), %rdi
callq 0x7cc0
jmp 0x4113
movq -0x828(%rbp), %rdi
leaq -0x460(%rbp), %rsi
movl $0x3, %edx
leaq -0x4d8(%rbp), %rcx
callq 0x3b60
jmp 0x4134
leaq -0x4d8(%rbp), %rdi
callq 0x3160
leaq -0x4d9(%rbp), %rdi
callq 0x3290
movss -0x4b4(%rbp), %xmm0
movss -0x4b0(%rbp), %xmm1
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm1, %xmm1
movss -0x4ac(%rbp), %xmm2
cvtss2sd %xmm2, %xmm2
leaq -0x4f8(%rbp), %rdi
callq 0x6860
jmp 0x417e
leaq -0x38(%rbp), %rdi
leaq -0x4f8(%rbp), %rsi
callq 0x7c90
jmp 0x4190
jmp 0x48eb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x41bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x4d8(%rbp), %rdi
callq 0x3160
leaq -0x4d9(%rbp), %rdi
callq 0x3290
jmp 0x4919
leaq -0x518(%rbp), %rdi
leaq -0x2d8(%rbp), %rsi
callq 0x3100
jmp 0x41e1
leaq -0x518(%rbp), %rdi
leaq 0x20ee0(%rip), %rsi     # 0x250cf
callq 0x3d50
movb %al, -0x829(%rbp)
leaq -0x518(%rbp), %rdi
callq 0x3160
movb -0x829(%rbp), %al
testb $0x1, %al
jne 0x4215
jmp 0x430b
leaq -0x524(%rbp), %rax
movq %rax, -0x840(%rbp)
leaq -0x549(%rbp), %rdi
movq %rdi, -0x838(%rbp)
callq 0x3500
movq -0x838(%rbp), %rdx
leaq 0x20e8e(%rip), %rsi     # 0x250d2
leaq -0x548(%rbp), %rdi
callq 0x7cc0
jmp 0x4252
movq -0x840(%rbp), %rdi
leaq -0x460(%rbp), %rsi
movl $0x3, %edx
leaq -0x548(%rbp), %rcx
callq 0x3b60
jmp 0x4273
leaq -0x548(%rbp), %rdi
callq 0x3160
leaq -0x549(%rbp), %rdi
callq 0x3290
movss -0x524(%rbp), %xmm0
movss -0x520(%rbp), %xmm1
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm1, %xmm1
movss -0x51c(%rbp), %xmm2
cvtss2sd %xmm2, %xmm2
leaq -0x568(%rbp), %rdi
callq 0x6860
jmp 0x42bd
leaq -0x50(%rbp), %rdi
leaq -0x568(%rbp), %rsi
callq 0x7d70
jmp 0x42cf
jmp 0x48e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x42fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x548(%rbp), %rdi
callq 0x3160
leaq -0x549(%rbp), %rdi
callq 0x3290
jmp 0x4919
leaq -0x588(%rbp), %rdi
leaq -0x2d8(%rbp), %rsi
callq 0x3100
jmp 0x4320
leaq -0x588(%rbp), %rdi
leaq 0x20de1(%rip), %rsi     # 0x2510f
callq 0x3d50
movb %al, -0x841(%rbp)
leaq -0x588(%rbp), %rdi
callq 0x3160
movb -0x841(%rbp), %al
testb $0x1, %al
jne 0x4354
jmp 0x487c
movb $0x0, -0x589(%rbp)
leaq -0x5c8(%rbp), %rdi
callq 0x33b0
movl $0x0, -0x5cc(%rbp)
leaq -0x460(%rbp), %rdi
leaq -0x5c8(%rbp), %rsi
callq 0x3360
movq %rax, -0x850(%rbp)
jmp 0x438d
movq -0x850(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3340
movb %al, -0x851(%rbp)
jmp 0x43ab
movb -0x851(%rbp), %al
testb $0x1, %al
jne 0x43ba
jmp 0x45c8
leaq -0x5c8(%rbp), %rdi
leaq 0x20d49(%rip), %rsi     # 0x25111
xorl %eax, %eax
movl %eax, %edx
callq 0x3590
cmpq $-0x1, %rax
setne %al
andb $0x1, %al
movb %al, -0x589(%rbp)
testb $0x1, -0x589(%rbp)
je 0x4590
leaq -0x5f1(%rbp), %rdi
movq %rdi, -0x860(%rbp)
callq 0x3500
movq -0x860(%rbp), %rdx
leaq 0x20d03(%rip), %rsi     # 0x25111
leaq -0x5f0(%rbp), %rdi
callq 0x7cc0
jmp 0x441c
leaq -0x5f1(%rbp), %rdi
callq 0x3290
xorl %eax, %eax
movl %eax, %edx
movq %rdx, -0x868(%rbp)
leaq -0x5c8(%rbp), %rdi
movq %rdi, -0x870(%rbp)
leaq -0x5f0(%rbp), %rsi
callq 0x35a0
movq -0x870(%rbp), %rsi
movq -0x868(%rbp), %rdx
movl %eax, -0x5f8(%rbp)
movslq -0x5f8(%rbp), %rcx
leaq -0x618(%rbp), %rdi
callq 0x3260
jmp 0x4476
xorl %eax, %eax
movl %eax, %esi
leaq -0x618(%rbp), %rdi
movl $0xa, %edx
callq 0x7de0
movl %eax, -0x874(%rbp)
jmp 0x4493
movl -0x874(%rbp), %ecx
movslq -0x5cc(%rbp), %rax
movl %ecx, -0x598(%rbp,%rax,4)
movslq -0x5f8(%rbp), %rax
movq %rax, -0x880(%rbp)
leaq -0x5f0(%rbp), %rdi
callq 0x3410
movq -0x880(%rbp), %rdx
addq %rax, %rdx
xorl %eax, %eax
movl %eax, %esi
leaq -0x5c8(%rbp), %rdi
callq 0x30a0
jmp 0x44dd
xorl %eax, %eax
movl %eax, %esi
leaq -0x5c8(%rbp), %rdi
movl $0xa, %edx
callq 0x7de0
movl %eax, -0x884(%rbp)
jmp 0x44fa
movl -0x884(%rbp), %ecx
movslq -0x5cc(%rbp), %rax
movl %ecx, -0x5a4(%rbp,%rax,4)
leaq -0x618(%rbp), %rdi
callq 0x3160
leaq -0x5f0(%rbp), %rdi
callq 0x3160
jmp 0x45b4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x486b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x5f1(%rbp), %rdi
callq 0x3290
jmp 0x486b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x457f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x618(%rbp), %rdi
callq 0x3160
leaq -0x5f0(%rbp), %rdi
callq 0x3160
jmp 0x486b
leaq -0x5c8(%rbp), %rdi
callq 0x30f0
movq %rax, %rdi
callq 0x3440
movl %eax, %ecx
movslq -0x5cc(%rbp), %rax
movl %ecx, -0x598(%rbp,%rax,4)
movl -0x5cc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5cc(%rbp)
jmp 0x4371
movq -0x18(%rbp), %rsi
leaq -0x6a0(%rbp), %rdi
callq 0x3d80
jmp 0x45da
testb $0x1, -0x589(%rbp)
je 0x4758
movslq -0x5a4(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x7e20
movq %rax, %rsi
leaq -0x6b8(%rbp), %rdi
callq 0x7e40
jmp 0x4608
movslq -0x5a0(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x7e20
movq %rax, %rsi
leaq -0x6d0(%rbp), %rdi
callq 0x7e40
jmp 0x4629
movslq -0x59c(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x7e20
movq %rax, %rsi
leaq -0x6e8(%rbp), %rdi
callq 0x7e40
jmp 0x464a
movl $0x2b8, %edi            # imm = 0x2B8
callq 0x3240
movq %rax, -0x890(%rbp)
jmp 0x465d
movslq -0x598(%rbp), %rsi
leaq -0x38(%rbp), %rdi
movq %rdi, -0x8b0(%rbp)
callq 0x7e20
movq -0x8b0(%rbp), %rdi
movq %rax, -0x8b8(%rbp)
movslq -0x594(%rbp), %rsi
callq 0x7e20
movq -0x8b0(%rbp), %rdi
movq %rax, -0x8a8(%rbp)
movslq -0x590(%rbp), %rsi
callq 0x7e20
movq %rax, -0x8a0(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x898(%rbp)
leaq -0x768(%rbp), %rdi
leaq -0x6a0(%rbp), %rsi
callq 0x7e80
jmp 0x46cf
movq -0x898(%rbp), %r8
movq -0x8a0(%rbp), %rcx
movq -0x8a8(%rbp), %rdx
movq -0x8b8(%rbp), %rsi
movq -0x890(%rbp), %rdi
movq %rsp, %rax
leaq -0x6e8(%rbp), %r9
movq %r9, 0x10(%rax)
leaq -0x6d0(%rbp), %r9
movq %r9, 0x8(%rax)
leaq -0x6b8(%rbp), %r9
movq %r9, (%rax)
leaq -0x768(%rbp), %r9
callq 0x7f20
jmp 0x4723
movq -0x890(%rbp), %rax
movq %rax, -0x620(%rbp)
jmp 0x483d
movq -0x890(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
movl $0x2b8, %esi            # imm = 0x2B8
callq 0x3250
jmp 0x486b
movl $0x2b8, %edi            # imm = 0x2B8
callq 0x3240
movq %rax, -0x8c0(%rbp)
jmp 0x476b
movslq -0x598(%rbp), %rsi
leaq -0x38(%rbp), %rdi
movq %rdi, -0x8e0(%rbp)
callq 0x7e20
movq -0x8e0(%rbp), %rdi
movq %rax, -0x8e8(%rbp)
movslq -0x594(%rbp), %rsi
callq 0x7e20
movq -0x8e0(%rbp), %rdi
movq %rax, -0x8d8(%rbp)
movslq -0x590(%rbp), %rsi
callq 0x7e20
movq %rax, -0x8d0(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x8c8(%rbp)
leaq -0x7e8(%rbp), %rdi
leaq -0x6a0(%rbp), %rsi
callq 0x7e80
jmp 0x47dd
movq -0x8c8(%rbp), %r8
movq -0x8d0(%rbp), %rcx
movq -0x8d8(%rbp), %rdx
movq -0x8e8(%rbp), %rsi
movq -0x8c0(%rbp), %rdi
leaq -0x7e8(%rbp), %r9
callq 0x80f0
jmp 0x480e
movq -0x8c0(%rbp), %rax
movq %rax, -0x620(%rbp)
jmp 0x483d
movq -0x8c0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
movl $0x2b8, %esi            # imm = 0x2B8
callq 0x3250
jmp 0x486b
movq -0x10(%rbp), %rdi
movq -0x620(%rbp), %rax
movq %rax, -0x7f0(%rbp)
leaq -0x7f0(%rbp), %rsi
callq 0x8270
jmp 0x485d
leaq -0x5c8(%rbp), %rdi
callq 0x3160
jmp 0x48e7
leaq -0x5c8(%rbp), %rdi
callq 0x3160
jmp 0x4919
movq 0x2b76d(%rip), %rdi     # 0x2fff0
leaq 0x2088a(%rip), %rsi     # 0x25114
callq 0x3220
movq %rax, -0x8f0(%rbp)
jmp 0x4898
movq -0x7f8(%rbp), %rsi
movq -0x8f0(%rbp), %rdi
callq 0x3210
movq %rax, -0x8f8(%rbp)
jmp 0x48b4
movq -0x8f8(%rbp), %rdi
leaq 0x20af4(%rip), %rsi     # 0x253b6
callq 0x3220
movq %rax, -0x900(%rbp)
jmp 0x48d0
movq -0x900(%rbp), %rdi
movq 0x2b6da(%rip), %rsi     # 0x2ffb8
callq 0x3270
jmp 0x48e5
jmp 0x48e7
jmp 0x48e9
jmp 0x48eb
jmp 0x48ed
movl $0x0, -0x2dc(%rbp)
leaq -0x460(%rbp), %rdi
callq 0x3120
movl -0x2dc(%rbp), %eax
testl %eax, %eax
je 0x4914
jmp 0x490f
jmp 0x3f66
jmp 0x3f66
leaq -0x460(%rbp), %rdi
callq 0x3120
jmp 0x497e
leaq -0x298(%rbp), %rdi
callq 0x3080
jmp 0x4935
movl $0x0, -0x2dc(%rbp)
leaq -0x2d8(%rbp), %rdi
callq 0x3160
leaq -0x2b8(%rbp), %rdi
callq 0x3160
leaq -0x298(%rbp), %rdi
callq 0x30b0
leaq -0x50(%rbp), %rdi
callq 0x82a0
leaq -0x38(%rbp), %rdi
callq 0x82a0
addq $0x920, %rsp            # imm = 0x920
popq %rbp
retq
leaq -0x2d8(%rbp), %rdi
callq 0x3160
leaq -0x2b8(%rbp), %rdi
callq 0x3160
leaq -0x298(%rbp), %rdi
callq 0x30b0
leaq -0x50(%rbp), %rdi
callq 0x82a0
leaq -0x38(%rbp), %rdi
callq 0x82a0
movq -0x70(%rbp), %rdi
callq 0x34f0
nopl (%rax)