// File: /workspace/github_cpp_makefile_star2_O3/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:
testq %rsi, %rsi
je 0x92d0
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x328, %rsp            # imm = 0x328
movq %rsi, %rbx
movq %rdi, %r14
movl $0x18, %edi
callq 0x3130
movq %rax, %r15
leaq 0x6c6e(%rip), %r13      # 0xfd70
movq %r13, (%rax)
movl $0x200, %edi            # imm = 0x200
movl $0x1, %esi
callq 0x30f0
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x9125
movl $0x40, 0x10(%r15)
movl $0x0, 0x14(%r15)
movq %r15, (%rsp)
movl $0x18, %edi
callq 0x3130
movq %rax, %r12
movq %r13, (%rax)
movl $0x200, %edi            # imm = 0x200
movl $0x1, %esi
callq 0x30f0
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0x9163
movl $0x40, 0x10(%r12)
movl $0x0, 0x14(%r12)
movq %r12, 0x8(%rsp)
leaq 0x120(%rsp), %r12
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
movq %rbx, %rsi
callq 0x30c0
leaq 0x138c(%rip), %rsi      # 0xa51c
movq %r12, %rdi
callq 0x3230
movq %rax, %r12
movl $0x8, %edi
callq 0x3130
movq (%r14), %rcx
movq %rcx, (%rax)
movq %r15, %rdi
movq %rax, %rsi
callq 0x8ce6
xorl %r13d, %r13d
testq %r12, %r12
je 0x92a2
leaq 0x21(%rsp), %r14
movl %r13d, %ebx
movl $0x100, %edx            # imm = 0x100
leaq 0x20(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x30d0
movl $0x100, %edx            # imm = 0x100
movq %r15, %rdi
movq %r12, %rsi
callq 0x30c0
xorl %edi, %edi
leaq 0x1325(%rip), %rsi      # 0xa51c
callq 0x3230
movq %rax, 0x18(%rsp)
xorl $0x1, %r13d
movq (%rsp,%r13,8), %r12
movq %r12, %rdi
callq 0x8da4
movq %r15, %rdi
callq 0x8bbc
cmpb $0x0, 0x20(%rsp)
je 0x9294
movl %ebx, %eax
movq (%rsp,%rax,8), %rbp
movslq 0x14(%rbp), %rbx
testq %rbx, %rbx
jle 0x9294
xorl %r15d, %r15d
movslq 0x14(%rbp), %rax
cmpq %rax, %r15
jge 0x9245
movq 0x8(%rbp), %rax
movq (%rax,%r15,8), %rdi
jmp 0x9247
xorl %edi, %edi
movzbl 0x20(%rsp), %eax
cmpl $0x2e, %eax
je 0x925f
cmpl $0x23, %eax
jne 0x9275
leaq 0x1c4b(%rip), %rsi      # 0xaea8
jmp 0x9266
leaq 0x1c45(%rip), %rsi      # 0xaeab
movq %r14, %rdx
movl $0x1, %ecx
callq 0x8f5d
jmp 0x9281
leaq 0x20(%rsp), %rsi
xorl %edx, %edx
callq 0x8e1d
movq %r12, %rdi
movq %rax, %rsi
callq 0x8d3c
incq %r15
cmpq %r15, %rbx
jne 0x9232
movq 0x18(%rsp), %r12
testq %r12, %r12
jne 0x91c7
movl %r13d, %eax
xorl $0x1, %eax
movq (%rsp,%rax,8), %rdi
testq %rdi, %rdi
je 0x92b7
movq (%rdi), %rax
callq *0x8(%rax)
movl %r13d, %eax
movq (%rsp,%rax,8), %rax
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
retq
nop