// File: /workspace/github_cpp_makefile_star2_O1/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 0x94b8
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 0x6aa0(%rip), %r13      # 0xfd70
movq %r13, (%rax)
movl $0x200, %edi            # imm = 0x200
movl $0x1, %esi
callq 0x30f0
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x92f3
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 0x9331
movl $0x40, 0x10(%r12)
movl $0x0, 0x14(%r12)
movq %r12, 0x8(%rsp)
leaq 0x120(%rsp), %r12
xorl %r13d, %r13d
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
xorl %esi, %esi
callq 0x30d0
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
movq %rbx, %rsi
callq 0x30c0
leaq 0x11ac(%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 0x8eb4
testq %r12, %r12
je 0x948a
leaq 0x21(%rsp), %r14
xorl %r13d, %r13d
leaq 0x20(%rsp), %r15
movl %r13d, %ebx
movl $0x100, %edx            # imm = 0x100
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 0x1145(%rip), %rsi      # 0xa51c
callq 0x3230
movq %rax, 0x10(%rsp)
xorl $0x1, %r13d
movq %r13, 0x18(%rsp)
movq (%rsp,%r13,8), %r12
movq %r12, %rdi
callq 0x8f78
movq %r15, %rdi
callq 0x8d8c
cmpb $0x0, 0x20(%rsp)
je 0x9477
movl %ebx, %eax
movq (%rsp,%rax,8), %rbp
movslq 0x14(%rbp), %rbx
testq %rbx, %rbx
jle 0x9477
xorl %r13d, %r13d
movslq 0x14(%rbp), %rax
cmpq %rax, %r13
jge 0x942a
movq 0x8(%rbp), %rax
movq (%rax,%r13,8), %rdi
jmp 0x942c
xorl %edi, %edi
movzbl 0x20(%rsp), %eax
cmpl $0x2e, %eax
je 0x9444
cmpl $0x23, %eax
jne 0x945a
leaq 0x1a7e(%rip), %rsi      # 0xaec0
jmp 0x944b
leaq 0x1a78(%rip), %rsi      # 0xaec3
movq %r14, %rdx
movl $0x1, %ecx
callq 0x912d
jmp 0x9464
movq %r15, %rsi
xorl %edx, %edx
callq 0x8fef
movq %r12, %rdi
movq %rax, %rsi
callq 0x8f10
incq %r13
cmpq %r13, %rbx
jne 0x9417
movq 0x10(%rsp), %r12
testq %r12, %r12
movq 0x18(%rsp), %r13
jne 0x93ac
movl %r13d, %eax
xorl $0x1, %eax
movq (%rsp,%rax,8), %rdi
testq %rdi, %rdi
je 0x949f
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