// File: /workspace/github_cpp_makefile_star2_O2/smallmuou[P]tinyhtml/tinyhtml.cpp
// Function: TiHtmlElement::Query(char const*)
TiHtmlResult* TiHtmlElement::Query( const char* query ) {
    if (!query) return NULL;

    TiHtmlResult* results[2] = { new TiHtmlResult(), new TiHtmlResult()};
    char aQuery[512] = {0};
    strncpy(aQuery, query, sizeof(aQuery));
    const char* token = strtok(aQuery, "/");

    int index = 0;
    int aIndex = 0;
    results[index]->Append(new TiHtmlElement(this));
    while (token) {
        char name[256] = {0};
        strncpy(name, token, sizeof(name));
        token = strtok(NULL, "/");
        aIndex = index ? 0 : 1;

        results[aIndex]->Clear();

        /* trim */
        strtrim(name);
        if (name[0]) {
            int count = results[index]->Count();
            for (int i = 0; i < count; i++) {
                TiHtmlElement* element = results[index]->Element(i);
                TiHtmlResult* aResult = NULL;
                switch(name[0]) {
                    case '#':
                        aResult = FindByAttribute(element, "id", &name[1], true);
                        break;
                    case '.':
                        aResult = FindByAttribute(element, "class", &name[1], true);
                        break;
                    default:
                        aResult = FindByTag(element, name, false);
                        break;
                }
                results[aIndex]->Append(aResult);
            }
        }

        index = aIndex;
    }

    aIndex = index ? 0 : 1;
    delete(results[aIndex]);
    return results[index];
}

// Assembly:
xorl %eax, %eax
testq %rsi, %rsi
je 0x8098
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x328, %rsp            # imm = 0x328
movq %rsi, %r15
movq %rdi, %rbx
pushq $0x18
popq %r12
movq %r12, %rdi
callq 0x3130
movq %rax, %r14
movq %rax, %rdi
callq 0x7ada
movq %r14, (%rsp)
movq %r12, %rdi
callq 0x3130
movq %rax, %r12
movq %rax, %rdi
callq 0x7ada
movq %r12, 0x8(%rsp)
leaq 0x120(%rsp), %r12
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
movq %r15, %rsi
callq 0x30c0
leaq 0x15c4(%rip), %rsi      # 0x951c
movq %r12, %rdi
callq 0x3230
movq %rax, %r15
pushq $0x8
popq %rdi
callq 0x3130
movq (%rbx), %rcx
movq %rcx, (%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x7b74
leaq 0x21(%rsp), %r12
pushq $0x1
popq %r14
xorl %ebp, %ebp
movl %ebp, %ebx
testq %r15, %r15
je 0x8071
movl $0x100, %edx            # imm = 0x100
leaq 0x20(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x30d0
movl $0x100, %edx            # imm = 0x100
movq %r13, %rdi
movq %r15, %rsi
callq 0x30c0
xorl %edi, %edi
leaq 0x155d(%rip), %rsi      # 0x951c
callq 0x3230
movq %rax, 0x18(%rsp)
xorl $0x1, %ebp
movq %rbp, 0x10(%rsp)
movq (%rsp,%rbp,8), %r15
movq %r15, %rdi
callq 0x7c36
movq %r13, %rdi
callq 0x7a5c
cmpb $0x0, 0x20(%rsp)
je 0x8062
movq (%rsp,%rbx,8), %rbx
movl 0x14(%rbx), %ebp
testl %ebp, %ebp
movl $0x0, %eax
cmovlel %eax, %ebp
xorl %r13d, %r13d
cmpq %r13, %rbp
je 0x8062
movslq 0x14(%rbx), %rax
cmpq %rax, %r13
jge 0x8018
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %rdi
jmp 0x801a
xorl %edi, %edi
movzbl 0x20(%rsp), %eax
cmpl $0x2e, %eax
je 0x8032
cmpl $0x23, %eax
jne 0x8046
leaq 0x1ec8(%rip), %rsi      # 0x9ef8
jmp 0x8039
leaq 0x1ec2(%rip), %rsi      # 0x9efb
movq %r12, %rdx
movl %r14d, %ecx
callq 0x7db2
jmp 0x8052
leaq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x7cad
movq %r15, %rdi
movq %rax, %rsi
callq 0x7bca
incq %r13
jmp 0x8000
movq 0x18(%rsp), %r15
movq 0x10(%rsp), %rbp
jmp 0x7f87
xorl $0x1, %ebp
movq (%rsp,%rbp,8), %rdi
testq %rdi, %rdi
je 0x8083
movq (%rdi), %rax
callq *0x8(%rax)
movq (%rsp,%rbx,8), %rax
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop