// File: /workspace/github_cpp_makefile_star2_O3/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 $0x188, %rsp            # imm = 0x188
movq %rsi, %r14
movq %rdi, 0x78(%rsp)
xorl %eax, %eax
leaq 0x40(%rsp), %rcx
movq %rax, 0x10(%rcx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rcx)
leaq 0x20(%rsp), %rcx
movq %rax, 0x10(%rcx)
movaps %xmm0, (%rcx)
leaq 0x20d0(%rip), %r15      # 0x62c7
leaq 0x80(%rsp), %r12
leaq 0x1c(%rsp), %rbx
xorl %r13d, %r13d
movl $0x0, 0x14(%rsp)
xorl %ebp, %ebp
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x21d0
cmpl $-0x1, %eax
je 0x4593
movzbl 0x80(%rsp), %eax
cmpl $0x23, %eax
je 0x424f
cmpl $0x66, %eax
je 0x4263
cmpl $0x76, %eax
jne 0x424f
cmpb $0x0, 0x81(%rsp)
je 0x4328
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x2180
incl %ebp
jmp 0x4211
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x21d0
cmpl $0x1, %eax
jne 0x475a
movq %r12, %rdi
leaq 0x2078(%rip), %rsi      # 0x62fe
callq 0x2060
testq %rax, %rax
je 0x4379
movq %r12, %rdi
leaq 0x2063(%rip), %rsi      # 0x6301
leaq 0x8(%rsp), %rdx
leaq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x20f0
cmpl $0x2, %eax
jne 0x475a
movq %r14, %rdi
leaq 0x203f(%rip), %rsi      # 0x6301
leaq 0xc(%rsp), %rdx
leaq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x21d0
cmpl $0x2, %eax
jne 0x475a
movq %r14, %rdi
leaq 0x201b(%rip), %rsi      # 0x6301
leaq 0x10(%rsp), %rdx
leaq 0x18(%rsp), %rcx
xorl %eax, %eax
callq 0x21d0
cmpl $0x2, %eax
jne 0x475a
incl 0x14(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
jne 0x4565
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x4a22
jmp 0x425f
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
movq %r14, %rdi
leaq 0x1f87(%rip), %rsi      # 0x62ca
leaq 0x60(%rsp), %rdx
leaq 0x68(%rsp), %rcx
leaq 0x70(%rsp), %r8
xorl %eax, %eax
callq 0x21d0
cmpl $0x3, %eax
jne 0x475a
leaq 0x40(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4806
incl %r13d
jmp 0x425f
movl %r13d, 0x38(%rsp)
movq %r12, %rdi
leaq 0x1f80(%rip), %rsi      # 0x6308
movq %rbx, %rdx
leaq 0x3c(%rsp), %r15
movq %r15, %rcx
movq %r12, %r13
leaq 0x18(%rsp), %r12
movq %r12, %r8
xorl %eax, %eax
callq 0x20f0
cmpl $0x3, %eax
jne 0x443d
movl 0x1c(%rsp), %eax
movl %eax, 0x8(%rsp)
movq %r14, %rdi
leaq 0x1f48(%rip), %rsi      # 0x6308
leaq 0xc(%rsp), %rdx
movq %r15, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x21d0
movl 0x38(%rsp), %ecx
leaq 0x1eea(%rip), %r15      # 0x62c7
cmpl $0x3, %eax
jne 0x475a
movq %r13, %r12
movl %ecx, %r13d
movq %r14, %rdi
leaq 0x1f12(%rip), %rsi      # 0x6308
leaq 0x10(%rsp), %rdx
leaq 0x3c(%rsp), %rcx
leaq 0x18(%rsp), %r8
xorl %eax, %eax
callq 0x21d0
cmpl $0x3, %eax
jne 0x475a
incl 0x14(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
jne 0x4565
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x4a22
jmp 0x425f
movq %r13, %rdi
leaq 0x1ec4(%rip), %rsi      # 0x630b
movq %rbx, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x20f0
cmpl $0x2, %eax
movq %r13, %r12
jne 0x44e7
movl 0x1c(%rsp), %eax
movl %eax, 0x8(%rsp)
movq %r14, %rdi
leaq 0x1e99(%rip), %rsi      # 0x630b
leaq 0xc(%rsp), %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x21d0
movl 0x38(%rsp), %r13d
leaq 0x1e3a(%rip), %r15      # 0x62c7
leaq 0x1c(%rsp), %rbx
cmpl $0x2, %eax
jne 0x475a
movq %r14, %rdi
leaq 0x1e66(%rip), %rsi      # 0x630b
leaq 0x10(%rsp), %rdx
leaq 0x3c(%rsp), %rcx
xorl %eax, %eax
callq 0x21d0
cmpl $0x2, %eax
jne 0x475a
incl 0x14(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
jne 0x4565
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x4a22
jmp 0x425f
movq %r12, %rdi
leaq 0x1e14(%rip), %rsi      # 0x6305
leaq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x20f0
cmpl $0x1, %eax
movl 0x38(%rsp), %r13d
leaq 0x1dbb(%rip), %r15      # 0x62c7
leaq 0x1c(%rsp), %rbx
jne 0x475a
movq %r14, %rdi
leaq 0x1de4(%rip), %rsi      # 0x6305
leaq 0xc(%rsp), %rdx
xorl %eax, %eax
callq 0x21d0
cmpl $0x1, %eax
jne 0x475a
movq %r14, %rdi
leaq 0x1dc5(%rip), %rsi      # 0x6305
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x21d0
cmpl $0x1, %eax
jne 0x475a
incl 0x14(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x457f
movl 0x10(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x28(%rsp)
jmp 0x425f
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x4a22
jmp 0x425f
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rdx
subq %rax, %rdx
movq 0x78(%rsp), %rbx
je 0x4623
sarq $0x2, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %edx, %edx
leaq (%rdx,%rdx,2), %r8
movl (%rax,%r8,4), %esi
movl 0x4(%rax,%r8,4), %edi
movl 0x8(%rax,%r8,4), %r8d
xorl %r9d, %r9d
movl %esi, %r10d
testl %r9d, %r9d
je 0x45ec
movl %r8d, %r10d
cmpl $0x2, %r9d
je 0x45ec
movl %edi, %r10d
testl %r10d, %r10d
jle 0x4765
movl %esi, %r10d
testl %r9d, %r9d
je 0x4609
movl %r8d, %r10d
cmpl $0x2, %r9d
je 0x4609
movl %edi, %r10d
cmpl %r13d, %r10d
jg 0x4765
incl %r9d
cmpl $0x3, %r9d
jne 0x45d8
incq %rdx
cmpq %rcx, %rdx
jne 0x45c3
movq %rbx, %rdi
callq 0x40ca
movl %r13d, (%rbx)
movl 0x14(%rsp), %ebp
movl %ebp, 0x4(%rbx)
movslq %r13d, %rax
movl $0x18, %ecx
mulq %rcx
movq %rax, %r15
movq $-0x1, %rdi
cmovnoq %rax, %rdi
callq 0x2030
movq %rax, %r14
testl %r13d, %r13d
je 0x4689
addq $-0x18, %r15
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
movq %r15, %rax
mulq %rcx
shrq $0x4, %rdx
leaq (%rdx,%rdx,2), %rax
leaq 0x18(,%rax,8), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x20c0
movq %r14, 0x8(%rbx)
movslq %ebp, %rax
movl $0xc, %ecx
mulq %rcx
movq $-0x1, %rdi
cmovnoq %rax, %rdi
callq 0x2030
movq %rax, 0x10(%rbx)
testl %r13d, %r13d
jle 0x46dd
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x40(%rsp), %rsi
addq %r14, %rsi
movq 0x8(%rbx), %rdi
addq %r14, %rdi
callq 0x5d7a
incq %r15
movslq (%rbx), %rax
addq $0x18, %r14
cmpq %rax, %r15
jl 0x46b7
movl 0x4(%rbx), %ebp
movb $0x1, %r14b
testl %ebp, %ebp
jle 0x4778
movq 0x20(%rsp), %rax
xorl %ecx, %ecx
leaq (,%rcx,4), %rdx
leaq (%rdx,%rdx,2), %r9
leaq (%rax,%r9), %rsi
movq 0x10(%rbx), %r10
leaq (%r10,%r9), %rdx
addq $0x8, %rdx
leaq (%r10,%r9), %rdi
leaq (%rax,%r9), %r8
addq $0x4, %r8
addq %r10, %r9
addq $0x4, %r9
xorl %r10d, %r10d
movq %rsi, %r15
movq %rdi, %r11
testl %r10d, %r10d
je 0x4738
movq %r8, %r15
movq %r9, %r11
cmpl $0x1, %r10d
jne 0x4745
movl (%r15), %ebp
decl %ebp
movl %ebp, (%r11)
incl %r10d
jmp 0x4721
movl 0x8(%rsi), %esi
decl %esi
movl %esi, (%rdx)
incq %rcx
movslq 0x4(%rbx), %rdx
cmpq %rdx, %rcx
jl 0x46ef
jmp 0x4778
incl %ebp
leaq 0x1b73(%rip), %rdi      # 0x62d6
jmp 0x476c
leaq 0x1ba5(%rip), %rdi      # 0x6311
xorl %r14d, %r14d
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x478f
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x47a6
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movl %r14d, %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x47c1
jmp 0x47c1
jmp 0x47c1
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x47db
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x47f2
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq %rbx, %rdi
callq 0x21c0