// File: /workspace/github_cpp_makefile_star2_O1/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, 0x40(%rsp)
xorl %eax, %eax
leaq 0x50(%rsp), %rcx
movq %rax, 0x10(%rcx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rcx)
leaq 0x10(%rsp), %rcx
movq %rax, 0x10(%rcx)
movaps %xmm0, (%rcx)
leaq 0x2e9c(%rip), %r15      # 0x71d7
leaq 0x90(%rsp), %rdx
movl $0x0, 0x34(%rsp)
xorl %ebx, %ebx
xorl %ebp, %ebp
movq %r14, %rdi
movq %r15, %rsi
movq %rdx, %r12
xorl %eax, %eax
callq 0x21d0
cmpl $-0x1, %eax
je 0x46e5
incl %ebp
movzbl 0x90(%rsp), %eax
cmpl $0x23, %eax
je 0x438f
cmpl $0x66, %eax
je 0x43a4
cmpl $0x76, %eax
jne 0x438f
cmpb $0x0, 0x91(%rsp)
je 0x4476
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x2180
movq %r12, %rdx
jmp 0x434f
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x21d0
cmpl $0x1, %eax
jne 0x4938
movl %ebx, %r15d
movq %r12, %rbx
movq %r12, %rdi
leaq 0x2e41(%rip), %rsi      # 0x720e
callq 0x2060
testq %rax, %rax
je 0x44d1
movq %rbx, %rdi
leaq 0x2e2c(%rip), %rsi      # 0x7211
leaq 0x4(%rsp), %rdx
leaq 0x38(%rsp), %r13
movq %r13, %rcx
xorl %eax, %eax
callq 0x20f0
cmpl $0x2, %eax
movl %r15d, %ebx
leaq 0x2dd1(%rip), %r15      # 0x71d7
jne 0x4938
movq %r14, %rdi
leaq 0x2dfb(%rip), %rsi      # 0x7211
leaq 0x8(%rsp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x21d0
cmpl $0x2, %eax
jne 0x4938
movq %r14, %rdi
leaq 0x2dd9(%rip), %rsi      # 0x7211
leaq 0xc(%rsp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x21d0
cmpl $0x2, %eax
jne 0x4938
incl %ebx
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
jne 0x46b7
leaq 0x10(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x4bfc
jmp 0x439f
movq %r14, %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movq $0x0, 0x80(%rsp)
leaq 0x2d46(%rip), %rsi      # 0x71da
leaq 0x70(%rsp), %rdx
leaq 0x78(%rsp), %rcx
leaq 0x80(%rsp), %r8
xorl %eax, %eax
callq 0x21d0
movl %eax, %r13d
cmpl $0x3, %eax
jne 0x4583
leaq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x49e4
incl 0x34(%rsp)
jmp 0x4593
movq %rbx, %rdi
leaq 0x2d3d(%rip), %rsi      # 0x7218
leaq 0x3c(%rsp), %rdx
leaq 0x4c(%rsp), %r13
movq %r13, %rcx
leaq 0x38(%rsp), %r8
xorl %eax, %eax
callq 0x20f0
cmpl $0x3, %eax
jne 0x45a9
movl 0x3c(%rsp), %eax
movl %eax, 0x4(%rsp)
movq %r14, %rdi
leaq 0x2d09(%rip), %rsi      # 0x7218
leaq 0x8(%rsp), %rdx
movq %r13, %rcx
leaq 0x38(%rsp), %r8
xorl %eax, %eax
callq 0x21d0
movl %r15d, %ebx
leaq 0x2ceb(%rip), %rsi      # 0x7218
leaq 0x38(%rsp), %r8
leaq 0x2c9e(%rip), %r15      # 0x71d7
cmpl $0x3, %eax
jne 0x4938
movq %r14, %rdi
leaq 0xc(%rsp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x21d0
cmpl $0x3, %eax
jne 0x4938
incl %ebx
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
jne 0x46b7
leaq 0x10(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x4bfc
jmp 0x439f
leaq 0x2c5c(%rip), %rdi      # 0x71e6
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
cmpl $0x3, %r13d
leaq 0x2c39(%rip), %r15      # 0x71d7
je 0x439f
jmp 0x4995
movq %rbx, %rdi
movq %r13, %rcx
leaq 0x2c65(%rip), %r13      # 0x721b
movq %r13, %rsi
leaq 0x3c(%rsp), %rdx
xorl %eax, %eax
callq 0x20f0
cmpl $0x2, %eax
jne 0x4642
movl 0x3c(%rsp), %eax
movl %eax, 0x4(%rsp)
movq %r14, %rdi
movq %r13, %rsi
leaq 0x8(%rsp), %rdx
leaq 0x4c(%rsp), %rcx
xorl %eax, %eax
callq 0x21d0
movl %r15d, %ebx
leaq 0x4c(%rsp), %rcx
leaq 0x2bdf(%rip), %r15      # 0x71d7
cmpl $0x2, %eax
jne 0x4938
movq %r14, %rdi
movq %r13, %rsi
leaq 0xc(%rsp), %rdx
xorl %eax, %eax
callq 0x21d0
cmpl $0x2, %eax
jne 0x4938
incl %ebx
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
jne 0x46b7
leaq 0x10(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x4bfc
jmp 0x439f
movq %rbx, %rdi
leaq 0x2bc9(%rip), %rsi      # 0x7215
leaq 0x4(%rsp), %rdx
xorl %eax, %eax
callq 0x20f0
cmpl $0x1, %eax
movl %r15d, %ebx
leaq 0x2b72(%rip), %r15      # 0x71d7
jne 0x4938
movq %r14, %rdi
leaq 0x2ba0(%rip), %rsi      # 0x7215
leaq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x21d0
cmpl $0x1, %eax
jne 0x4938
movq %r14, %rdi
leaq 0x2b81(%rip), %rsi      # 0x7215
leaq 0xc(%rsp), %rdx
xorl %eax, %eax
callq 0x21d0
cmpl $0x1, %eax
jne 0x4938
incl %ebx
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x46d1
movl 0xc(%rsp), %eax
movl %eax, 0x8(%rsi)
movq 0x4(%rsp), %rax
movq %rax, (%rsi)
addq $0xc, 0x18(%rsp)
jmp 0x439f
leaq 0x10(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x4bfc
jmp 0x439f
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x47b8
xorl %esi, %esi
movq %rsi, 0x88(%rsp)
leaq (%rsi,%rsi,2), %rcx
movl (%rax,%rcx,4), %r13d
movl 0x4(%rax,%rcx,4), %r15d
movl 0x8(%rax,%rcx,4), %r14d
xorl %r12d, %r12d
movl %r13d, %eax
testl %r12d, %r12d
je 0x473f
movl %r14d, %eax
cmpl $0x2, %r12d
je 0x473f
movl %r15d, %eax
cmpl $0x1, %r12d
je 0x473f
leaq 0x2b2b(%rip), %rdi      # 0x7260
xorl %eax, %eax
callq 0x2040
movl %r13d, %eax
testl %eax, %eax
jle 0x4941
movl %r13d, %eax
testl %r12d, %r12d
je 0x4772
movl %r14d, %eax
cmpl $0x2, %r12d
je 0x4772
movl %r15d, %eax
cmpl $0x1, %r12d
je 0x4772
leaq 0x2af8(%rip), %rdi      # 0x7260
xorl %eax, %eax
callq 0x2040
movl %r13d, %eax
cmpl 0x34(%rsp), %eax
jg 0x4941
incl %r12d
cmpl $0x3, %r12d
jne 0x4714
movq 0x88(%rsp), %rsi
incq %rsi
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
cmpq %rsi, %rcx
ja 0x46f7
movl %ebx, %ebp
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x420e
movl 0x34(%rsp), %r12d
movl %r12d, (%rbx)
movl %ebp, 0x4(%rbx)
movslq %r12d, %r15
movl $0x18, %ecx
movq %r15, %rax
mulq %rcx
movq $-0x1, %rdi
cmovnoq %rax, %rdi
callq 0x2030
movq %rax, %r14
testl %r12d, %r12d
je 0x482b
leaq (%r15,%r15,2), %rax
leaq -0x18(,%rax,8), %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rcx
shrq $0x4, %rdx
leaq (%rdx,%rdx,2), %rax
leaq 0x18(,%rax,8), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x20c0
movq 0x40(%rsp), %rbx
movq %r14, 0x8(%rbx)
movslq %ebp, %rax
movl $0xc, %ecx
mulq %rcx
movq $-0x1, %rdi
cmovnoq %rax, %rdi
callq 0x2030
movq %rax, 0x10(%rbx)
cmpl $0x0, (%rbx)
movq %rbx, %rax
jle 0x4889
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x50(%rsp), %rsi
addq %rbx, %rsi
movq 0x8(%rax), %rdi
addq %rbx, %rdi
movq %rax, %r15
callq 0x5f6a
incq %r14
movslq (%r15), %rax
addq $0x18, %rbx
cmpq %rax, %r14
movq %r15, %rax
jl 0x4860
movb $0x1, %bl
cmpl $0x0, 0x4(%rax)
jle 0x4953
leaq 0x29c4(%rip), %r15      # 0x7260
xorl %r12d, %r12d
leaq (%r12,%r12,2), %r13
shlq $0x2, %r13
xorl %ebp, %ebp
movq 0x10(%rsp), %rbx
addq %r13, %rbx
testl %ebp, %ebp
je 0x48da
cmpl $0x2, %ebp
je 0x48c5
cmpl $0x1, %ebp
jne 0x48cb
addq $0x4, %rbx
jmp 0x48da
addq $0x8, %rbx
jmp 0x48da
movq %r15, %rdi
xorl %eax, %eax
callq 0x2040
movq 0x40(%rsp), %rax
movl (%rbx), %r14d
movq 0x10(%rax), %rbx
addq %r13, %rbx
testl %ebp, %ebp
je 0x490d
cmpl $0x2, %ebp
je 0x48f8
cmpl $0x1, %ebp
jne 0x48fe
addq $0x4, %rbx
jmp 0x490d
addq $0x8, %rbx
jmp 0x490d
movq %r15, %rdi
xorl %eax, %eax
callq 0x2040
movq 0x40(%rsp), %rax
decl %r14d
movl %r14d, (%rbx)
incl %ebp
cmpl $0x3, %ebp
jne 0x48a9
incq %r12
movq 0x40(%rsp), %rax
movslq 0x4(%rax), %rax
cmpq %rax, %r12
movq 0x40(%rsp), %rax
jl 0x489f
movb $0x1, %bl
jmp 0x4953
leaq 0x28a7(%rip), %rdi      # 0x71e6
jmp 0x4948
leaq 0x28d9(%rip), %rdi      # 0x7221
xorl %ebx, %ebx
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x496a
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x4981
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movl %ebx, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebx, %ebx
jmp 0x4953
jmp 0x499f
jmp 0x499f
jmp 0x499f
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x49b9
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x49d0
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2120
movq %rbx, %rdi
callq 0x21c0