// File: /workspace/github_cpp_makefile_star2_O2/Aries-orz[P]Ari-RayTracing/src/ObjParser.cpp
// Function: rt::ObjParser::parse(_IO_FILE*)
bool ObjParser::parse(FILE* fp) {
    char buf[256];
    int nVertices = 0, nFaces = 0;
    int lineNum = 0;
    std::vector<Vec3d> vecVertices;
    std::vector<TriArray> vecFaces;
    
    while (fscanf(fp, "%s", buf) != EOF) {
        ++lineNum;
        switch(buf[0]) {
            case '#':
                fgets(buf, sizeof(buf), fp);
                break;
            case 'v':
                switch(buf[1]) {
                    case '\0': {
                        Vec3d point;
                        if (fscanf(fp, "%lf %lf %lf", &point.x, &point.y, &point.z) == 3) {
                            ++nVertices;
                            vecVertices.push_back(point);
                        } else {
                            printf("RTError: Wrong data format at line %d.\n", lineNum);
                            return false;
                        }
                        break;
                    }
                    default:
                        fgets(buf, sizeof(buf), fp);
                        break;
                }
                break;
            case 'f':
                int v, n, t;
                TriArray indice;
                if (fscanf(fp, "%s", buf) != 1) {
                    printf("RTError: Wrong data format at line %d.\n", lineNum);
                    return false;
                }
                
                if (strstr(buf, "//")) {
                    if ( sscanf(buf, "%d//%d", &indice[0], &n) == 2 &&
                         fscanf(fp, "%d//%d", &indice[1], &n) == 2 &&
                         fscanf(fp, "%d//%d", &indice[2], &n) == 2 ) {
                        ++nFaces;
                        vecFaces.push_back(indice);
                    } else {
                        printf("RTError: Wrong data format at line %d.\n", lineNum);
                        return false;
                    }
                }
                
                else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3) {
                    indice[0] = v;
                    if ( fscanf(fp, "%d/%d/%d", &indice[1], &t, &n) == 3 &&
                         fscanf(fp, "%d/%d/%d", &indice[2], &t, &n) == 3 ) {
                        ++nFaces;
                        vecFaces.push_back(indice);
                        }
                    else {
                        printf("RTError: Wrong data format at line %d.\n", lineNum);
                        return false;
                    }
                }
                else if (sscanf(buf, "%d/%d", &v, &t) == 2) {
                    indice[0] = v;
                    if ( fscanf(fp, "%d/%d", &indice[1], &t) == 2 &&
                         fscanf(fp, "%d/%d", &indice[2], &t) == 2 ) {
                        ++nFaces;
                        vecFaces.push_back(indice);
                    }
                    else {
                        printf("RTError: Wrong data format at line %d.\n", lineNum);
                        return false;
                    }
                }
                else {
                    if ( sscanf(buf, "%d", &indice[0]) == 1 &&
                         fscanf(fp, "%d", &indice[1]) == 1 &&
                         fscanf(fp, "%d", &indice[2]) == 1 ) {
                        ++nFaces;
                        vecFaces.push_back(indice);
                    }
                    else {
                        printf("RTError: Wrong data format at line %d.\n", lineNum);
                        return false;
                    }
                }
                break;
            default:
                fgets(buf, sizeof(buf), fp);
                break;
        }
    }
    
    for (int i=0; i<vecFaces.size(); ++i) {
        TriArray tri = vecFaces[i];
        for (int j=0; j<3; ++j) {
            if (tri[j] <=0 || tri[j] > nVertices) {
                printf("RTError: The vertex index of face at line %d is out of range!\n", lineNum);
                return false;
            }
        }
    }
    destroy();
    numOfVertices = nVertices;
    numOfFaces = nFaces;
    verticeList = new Vec3d[numOfVertices];
    faceList = new TriArray[numOfFaces];
    for (int i=0; i<numOfVertices; ++i) {
        verticeList[i] = vecVertices[i];
    }
    for (int i=0; i<numOfFaces; ++i) {
        for (int j=0; j<3; ++j)
            faceList[i][j] = vecFaces[i][j] - 1;
    }
    
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rsi, %r14
movq %rdi, 0x60(%rsp)
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x20(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x217c(%rip), %r15      # 0x62c7
leaq 0x90(%rsp), %r13
leaq 0x6c(%rsp), %rbx
xorl %r12d, %r12d
movl $0x0, 0x4(%rsp)
xorl %ebp, %ebp
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x21d0
cmpl $-0x1, %eax
je 0x446f
movzbl 0x90(%rsp), %eax
cmpl $0x23, %eax
je 0x41a3
cmpl $0x66, %eax
je 0x41b8
cmpl $0x76, %eax
jne 0x41a3
cmpb $0x0, 0x91(%rsp)
je 0x4263
movq %r13, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x2180
jmp 0x4468
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x21d0
cmpl $0x1, %eax
jne 0x44dd
movq %r13, %rdi
leaq 0x2123(%rip), %rsi      # 0x62fe
callq 0x2060
testq %rax, %rax
je 0x42b1
movq %r13, %rdi
leaq 0x210e(%rip), %rsi      # 0x6301
leaq 0x8(%rsp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x20f0
cmpl $0x2, %eax
jne 0x44dd
movq %r14, %rdi
leaq 0x20ec(%rip), %rsi      # 0x6301
leaq 0xc(%rsp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x21d0
cmpl $0x2, %eax
jne 0x44dd
movq %r14, %rdi
leaq 0x20ca(%rip), %rsi      # 0x6301
leaq 0x10(%rsp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x21d0
cmpl $0x2, %eax
jne 0x44dd
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x46b0
jmp 0x4464
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
movq %r14, %rdi
leaq 0x204f(%rip), %rsi      # 0x62ca
leaq 0x40(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x50(%rsp), %r8
xorl %eax, %eax
callq 0x21d0
cmpl $0x3, %eax
jne 0x44dd
leaq 0x70(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x4652
incl %r12d
jmp 0x4468
movl %r12d, 0x14(%rsp)
movq %r13, %rdi
leaq 0x2048(%rip), %rsi      # 0x6308
leaq 0x1c(%rsp), %r15
movq %r15, %rdx
movq %rbx, %r12
leaq 0x18(%rsp), %rbx
movq %rbx, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x20f0
cmpl $0x3, %eax
jne 0x435b
movl 0x1c(%rsp), %eax
movl %eax, 0x8(%rsp)
movq %r14, %rdi
leaq 0x2014(%rip), %rsi      # 0x6308
leaq 0xc(%rsp), %rdx
movq %rbx, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x21d0
movl 0x14(%rsp), %ecx
leaq 0x1fb6(%rip), %r15      # 0x62c7
cmpl $0x3, %eax
jne 0x44dd
movq %r12, %rbx
movl %ecx, %r12d
movq %r14, %rdi
leaq 0x1fde(%rip), %rsi      # 0x6308
leaq 0x10(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %rbx, %r8
xorl %eax, %eax
callq 0x21d0
cmpl $0x3, %eax
jne 0x44dd
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x46b0
jmp 0x4464
movq %r13, %rdi
leaq 0x1fa6(%rip), %rsi      # 0x630b
movq %r15, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x20f0
cmpl $0x2, %eax
jne 0x43e7
movl 0x1c(%rsp), %eax
movl %eax, 0x8(%rsp)
movq %r14, %rdi
leaq 0x1f82(%rip), %rsi      # 0x630b
leaq 0xc(%rsp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x21d0
movl 0x14(%rsp), %ecx
leaq 0x1f24(%rip), %r15      # 0x62c7
cmpl $0x2, %eax
jne 0x44dd
movq %r12, %rbx
movl %ecx, %r12d
movq %r14, %rdi
leaq 0x1f4f(%rip), %rsi      # 0x630b
leaq 0x10(%rsp), %rdx
leaq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x21d0
cmpl $0x2, %eax
jne 0x44dd
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x46b0
jmp 0x4464
movq %r13, %rdi
leaq 0x1f14(%rip), %rsi      # 0x6305
leaq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x20f0
cmpl $0x1, %eax
movl 0x14(%rsp), %eax
leaq 0x1ebc(%rip), %r15      # 0x62c7
jne 0x44dd
movq %r12, %rbx
movl %eax, %r12d
movq %r14, %rdi
leaq 0x1ee4(%rip), %rsi      # 0x6305
leaq 0xc(%rsp), %rdx
xorl %eax, %eax
callq 0x21d0
cmpl $0x1, %eax
jne 0x44dd
movq %r14, %rdi
leaq 0x1ec5(%rip), %rsi      # 0x6305
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x21d0
cmpl $0x1, %eax
jne 0x44dd
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x46b0
incl 0x4(%rsp)
incl %ebp
jmp 0x4165
xorl %r13d, %r13d
pushq $0xc
popq %rbx
leaq 0x40(%rsp), %r14
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rax
subq %rcx, %rax
cqto
idivq %rbx
cmpq %r13, %rax
jbe 0x4522
imulq $0xc, %r13, %rax
movl 0x8(%rcx,%rax), %edx
movl %edx, 0x48(%rsp)
movq (%rcx,%rax), %rax
movq %rax, 0x40(%rsp)
xorl %r15d, %r15d
cmpl $0x3, %r15d
je 0x44d8
movq %r14, %rdi
movl %r15d, %esi
callq 0x467e
cmpl $0x0, (%rax)
jle 0x44e8
movq %r14, %rdi
movl %r15d, %esi
callq 0x467e
incl %r15d
cmpl %r12d, (%rax)
jle 0x44ad
jmp 0x44e8
incq %r13
jmp 0x447a
incl %ebp
leaq 0x1df0(%rip), %rdi      # 0x62d6
jmp 0x44ef
leaq 0x1e22(%rip), %rdi      # 0x6311
xorl %ebx, %ebx
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
leaq 0x20(%rsp), %rdi
callq 0x4710
leaq 0x70(%rsp), %rdi
callq 0x46d4
movl %ebx, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x60(%rsp), %rbx
movq %rbx, %rdi
callq 0x4022
movl %r12d, (%rbx)
movl 0x4(%rsp), %eax
movl %eax, 0x4(%rbx)
movslq %r12d, %rax
pushq $0x18
popq %r13
mulq %r13
movq %rax, %r15
pushq $-0x1
popq %r14
movq %rax, %rdi
cmovoq %r14, %rdi
callq 0x2030
movq %rax, %rbp
testl %r12d, %r12d
pushq $0xc
popq %rbx
je 0x4581
addq $-0x18, %r15
movq %r15, %rax
xorl %edx, %edx
divq %r13
subq %rdx, %r15
addq $0x18, %r15
movq %rbp, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x20c0
movslq 0x4(%rsp), %rax
mulq %rbx
movq 0x60(%rsp), %rbx
movq %rbp, 0x8(%rbx)
cmovnoq %rax, %r14
movq %r14, %rdi
callq 0x2030
movq %rax, 0x10(%rbx)
xorl %r14d, %r14d
xorl %r15d, %r15d
movslq %r12d, %rax
cmpq %rax, %r15
jge 0x45d0
movq 0x70(%rsp), %rsi
addq %r14, %rsi
movq 0x8(%rbx), %rdi
addq %r14, %rdi
callq 0x5c7a
incq %r15
movl (%rbx), %r12d
addq $0x18, %r14
jmp 0x45a8
xorl %r14d, %r14d
movslq 0x4(%rbx), %rax
cmpq %rax, %r14
jge 0x4616
xorl %ebp, %ebp
imulq $0xc, %r14, %r15
cmpl $0x3, %ebp
je 0x4611
movq 0x20(%rsp), %rdi
addq %r15, %rdi
movl %ebp, %esi
callq 0x467e
movl (%rax), %r12d
movq 0x10(%rbx), %rdi
addq %r15, %rdi
movl %ebp, %esi
callq 0x467e
decl %r12d
movl %r12d, (%rax)
incl %ebp
jmp 0x45e2
incq %r14
jmp 0x45d3
movb $0x1, %bl
jmp 0x44fa
jmp 0x4627
jmp 0x4627
jmp 0x4627
jmp 0x4627
jmp 0x4627
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x4710
leaq 0x70(%rsp), %rdi
callq 0x46d4
movq %rbx, %rdi
callq 0x21c0