// File: kryffin[P]software-rasterizer/./parser.cpp
// Function: Model::Model(char const*)
Model (const char* filename) {
		std::ifstream in;
	    in.open(filename, std::ifstream::in);
	    if (in.fail()) {
	        std::cerr << "Failed to open " << filename << std::endl;
	        return;
	    }

	    std::string line;
	    while (!in.eof()) {

	        std::getline(in, line);
	        std::istringstream iss(line.c_str());
	        char trash;
	        if (!line.compare(0, 2, "v ")) { //vertices

	            iss >> trash;
	            Vec3f v;
	            iss >> v.x; iss >> v.y; iss >> v.z;
	            vertices.push_back(v);

	        } else if (!line.compare(0, 2, "f ")) { //triangles (f for faces)

	            Vec3i t;
	            std::string str, token;
	            int idx, cnt = 0;
	            iss >> trash;
	            while (iss >> str) {
	                token = str.std::string::substr(0, str.std::string::find('/'));
	                idx = std::stoi(token);
	                idx--; // in wavefront obj all indices start at 1, not zero
	                if (cnt == 0) t.x = idx;
	                if (cnt == 1) t.y = idx;
	                if (cnt == 2) t.z = idx;
	                cnt++;
	            }
	            if (cnt == 3) triangles.push_back(t);

	        } else if (!line.compare(0, 3, "vn ")) { //vertices normals

	            iss >> trash;
	            iss >> trash;
	            Vec3f n;
	            iss >> n.x; iss >> n.y; iss >> n.z;
	            normals.push_back(n);

	        }

	    }
	    std::cout << "# v# " << vertices.size() << " f# "  << triangles.size() << " vn# " << normals.size() << std::endl;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x550, %rsp            # imm = 0x550
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x490(%rbp)
movq %rdi, %rax
movq %rax, -0x4a0(%rbp)
callq 0x6a40
movq -0x490(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x498(%rbp)
callq 0x6a60
movq -0x490(%rbp), %rdi
addq $0x30, %rdi
movq %rdi, -0x488(%rbp)
callq 0x6a40
leaq -0x218(%rbp), %rdi
callq 0x2210
jmp 0x6019
movq -0x10(%rbp), %rsi
leaq -0x218(%rbp), %rdi
movl $0x8, %edx
callq 0x2130
jmp 0x6030
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x2030
movb %al, -0x4a1(%rbp)
jmp 0x6050
movb -0x4a1(%rbp), %al
testb $0x1, %al
jne 0x605f
jmp 0x60e6
movq 0x9f8a(%rip), %rdi      # 0xfff0
leaq 0x50dc(%rip), %rsi      # 0xb149
callq 0x21c0
movq %rax, -0x4b0(%rbp)
jmp 0x607b
movq -0x4b0(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x21c0
movq %rax, -0x4b8(%rbp)
jmp 0x6094
movq -0x4b8(%rbp), %rdi
movq 0x9f16(%rip), %rsi      # 0xffb8
callq 0x21f0
jmp 0x60a9
movl $0x1, -0x228(%rbp)
jmp 0x66d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x6706
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x66fa
leaq -0x248(%rbp), %rdi
callq 0x2290
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x2380
movb %al, -0x4b9(%rbp)
jmp 0x6112
movb -0x4b9(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x6123
jmp 0x65e6
leaq -0x218(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x2060
jmp 0x6138
leaq -0x248(%rbp), %rdi
callq 0x20d0
movq %rax, -0x4d0(%rbp)
leaq -0x3e9(%rbp), %rdi
movq %rdi, -0x4c8(%rbp)
callq 0x2360
movq -0x4d0(%rbp), %rsi
movq -0x4c8(%rbp), %rdx
leaq -0x3e8(%rbp), %rdi
callq 0x2280
jmp 0x617a
leaq -0x3c8(%rbp), %rdi
leaq -0x3e8(%rbp), %rsi
movl $0x8, %edx
callq 0x2320
jmp 0x6194
leaq -0x3e8(%rbp), %rdi
callq 0x2120
leaq -0x3e9(%rbp), %rdi
callq 0x2200
leaq 0x4fa6(%rip), %rcx      # 0xb159
xorl %eax, %eax
movl %eax, %esi
leaq -0x248(%rbp), %rdi
movl $0x2, %edx
callq 0x2270
movl %eax, -0x4d4(%rbp)
jmp 0x61d0
movl -0x4d4(%rbp), %eax
cmpl $0x0, %eax
jne 0x62cc
leaq -0x3c8(%rbp), %rdi
leaq -0x3ea(%rbp), %rsi
callq 0x2260
jmp 0x61f4
leaq -0x3f8(%rbp), %rdi
callq 0x55b0
jmp 0x6202
leaq -0x3c8(%rbp), %rdi
leaq -0x3f8(%rbp), %rsi
callq 0x2070
jmp 0x6217
leaq -0x3f4(%rbp), %rsi
leaq -0x3c8(%rbp), %rdi
callq 0x2070
jmp 0x622c
leaq -0x3f0(%rbp), %rsi
leaq -0x3c8(%rbp), %rdi
callq 0x2070
jmp 0x6241
movq -0x4a0(%rbp), %rdi
leaq -0x3f8(%rbp), %rsi
callq 0x6a80
jmp 0x6256
jmp 0x65c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x66ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x62a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x3e8(%rbp), %rdi
callq 0x2120
leaq -0x3e9(%rbp), %rdi
callq 0x2200
jmp 0x66ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x65d5
leaq 0x4e89(%rip), %rcx      # 0xb15c
xorl %eax, %eax
movl %eax, %esi
leaq -0x248(%rbp), %rdi
movl $0x2, %edx
callq 0x2270
movl %eax, -0x4d8(%rbp)
jmp 0x62f0
movl -0x4d8(%rbp), %eax
cmpl $0x0, %eax
jne 0x64fb
leaq -0x404(%rbp), %rdi
callq 0x6af0
jmp 0x630d
leaq -0x428(%rbp), %rdi
callq 0x2290
leaq -0x448(%rbp), %rdi
callq 0x2290
movl $0x0, -0x450(%rbp)
leaq -0x3c8(%rbp), %rdi
leaq -0x3ea(%rbp), %rsi
callq 0x2260
jmp 0x6344
jmp 0x6346
leaq -0x3c8(%rbp), %rdi
leaq -0x428(%rbp), %rsi
callq 0x2240
movq %rax, -0x4e0(%rbp)
jmp 0x6362
movq -0x4e0(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2230
movb %al, -0x4e1(%rbp)
jmp 0x6380
movb -0x4e1(%rbp), %al
testb $0x1, %al
jne 0x638f
jmp 0x64ba
xorl %eax, %eax
movl %eax, %edx
movq %rdx, -0x4f0(%rbp)
leaq -0x428(%rbp), %rdi
movq %rdi, -0x4f8(%rbp)
movl $0x2f, %esi
callq 0x2250
movq -0x4f8(%rbp), %rsi
movq -0x4f0(%rbp), %rdx
movq %rax, %rcx
leaq -0x470(%rbp), %rdi
callq 0x21e0
jmp 0x63d1
leaq -0x448(%rbp), %rdi
movq %rdi, -0x508(%rbp)
leaq -0x470(%rbp), %rsi
movq %rsi, -0x510(%rbp)
callq 0x2220
movq -0x510(%rbp), %rdi
callq 0x2120
movq -0x508(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x6b20
movl %eax, -0x4fc(%rbp)
jmp 0x641b
movl -0x4fc(%rbp), %eax
movl %eax, -0x44c(%rbp)
movl -0x44c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x44c(%rbp)
cmpl $0x0, -0x450(%rbp)
jne 0x647c
movl -0x44c(%rbp), %eax
movl %eax, -0x404(%rbp)
jmp 0x647c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x2120
leaq -0x428(%rbp), %rdi
callq 0x2120
jmp 0x65d5
cmpl $0x1, -0x450(%rbp)
jne 0x6491
movl -0x44c(%rbp), %eax
movl %eax, -0x400(%rbp)
cmpl $0x2, -0x450(%rbp)
jne 0x64a6
movl -0x44c(%rbp), %eax
movl %eax, -0x3fc(%rbp)
movl -0x450(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x450(%rbp)
jmp 0x6346
cmpl $0x3, -0x450(%rbp)
jne 0x64de
movq -0x4a0(%rbp), %rdi
addq $0x18, %rdi
leaq -0x404(%rbp), %rsi
callq 0x6b60
jmp 0x64dc
jmp 0x64de
leaq -0x448(%rbp), %rdi
callq 0x2120
leaq -0x428(%rbp), %rdi
callq 0x2120
jmp 0x65c2
leaq 0x4c5d(%rip), %rcx      # 0xb15f
xorl %eax, %eax
movl %eax, %esi
leaq -0x248(%rbp), %rdi
movl $0x3, %edx
callq 0x2270
movl %eax, -0x514(%rbp)
jmp 0x651f
movl -0x514(%rbp), %eax
cmpl $0x0, %eax
jne 0x65c0
leaq -0x3c8(%rbp), %rdi
leaq -0x3ea(%rbp), %rsi
callq 0x2260
jmp 0x6543
leaq -0x3c8(%rbp), %rdi
leaq -0x3ea(%rbp), %rsi
callq 0x2260
jmp 0x6558
leaq -0x47c(%rbp), %rdi
callq 0x55b0
jmp 0x6566
leaq -0x3c8(%rbp), %rdi
leaq -0x47c(%rbp), %rsi
callq 0x2070
jmp 0x657b
leaq -0x478(%rbp), %rsi
leaq -0x3c8(%rbp), %rdi
callq 0x2070
jmp 0x6590
leaq -0x474(%rbp), %rsi
leaq -0x3c8(%rbp), %rdi
callq 0x2070
jmp 0x65a5
movq -0x4a0(%rbp), %rdi
addq $0x30, %rdi
leaq -0x47c(%rbp), %rsi
callq 0x6a80
jmp 0x65be
jmp 0x65c0
jmp 0x65c2
jmp 0x65c4
leaq -0x3c8(%rbp), %rdi
callq 0x20e0
jmp 0x60f2
leaq -0x3c8(%rbp), %rdi
callq 0x20e0
jmp 0x66ee
movq 0x99db(%rip), %rdi      # 0xffc8
leaq 0x4b6f(%rip), %rsi      # 0xb163
callq 0x21c0
movq %rax, -0x520(%rbp)
jmp 0x6602
movq -0x4a0(%rbp), %rdi
callq 0x69f0
movq -0x520(%rbp), %rdi
movq %rax, %rsi
callq 0x2080
movq %rax, -0x528(%rbp)
jmp 0x6626
movq -0x528(%rbp), %rdi
leaq 0x4b35(%rip), %rsi      # 0xb169
callq 0x21c0
movq %rax, -0x530(%rbp)
jmp 0x6642
movq -0x4a0(%rbp), %rdi
addq $0x18, %rdi
callq 0x6960
movq -0x530(%rbp), %rdi
movq %rax, %rsi
callq 0x2080
movq %rax, -0x538(%rbp)
jmp 0x666a
movq -0x538(%rbp), %rdi
leaq 0x4af6(%rip), %rsi      # 0xb16e
callq 0x21c0
movq %rax, -0x540(%rbp)
jmp 0x6686
movq -0x4a0(%rbp), %rdi
addq $0x30, %rdi
callq 0x69f0
movq -0x540(%rbp), %rdi
movq %rax, %rsi
callq 0x2080
movq %rax, -0x548(%rbp)
jmp 0x66ae
movq -0x548(%rbp), %rdi
movq 0x98fc(%rip), %rsi      # 0xffb8
callq 0x21f0
jmp 0x66c3
leaq -0x248(%rbp), %rdi
callq 0x2120
movl $0x0, -0x228(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x2090
addq $0x550, %rsp            # imm = 0x550
popq %rbp
retq
leaq -0x248(%rbp), %rdi
callq 0x2120
leaq -0x218(%rbp), %rdi
callq 0x2090
movq -0x488(%rbp), %rdi
callq 0x6bd0
movq -0x498(%rbp), %rdi
callq 0x6c30
movq -0x490(%rbp), %rdi
callq 0x6bd0
movq -0x220(%rbp), %rdi
callq 0x2350
nopw %cs:(%rax,%rax)