// File: 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:
pushq %rbp
movq %rsp, %rbp
subq $0x3c0, %rsp            # imm = 0x3C0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x370(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0xcb52
movq $0x0, -0x8(%rbp)
jmp 0xcee5
movl $0x18, %edi
callq 0x3130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x378(%rbp)
callq 0xc2d0
jmp 0xcb70
movq -0x378(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rax
movq %rax, -0x388(%rbp)
movl $0x18, %edi
callq 0x3130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x380(%rbp)
callq 0xc2d0
jmp 0xcba4
movq -0x388(%rbp), %rax
movq -0x380(%rbp), %rcx
movq %rcx, (%rax)
leaq -0x240(%rbp), %rdi
movq %rdi, -0x3a0(%rbp)
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
movq %rdx, -0x3a8(%rbp)
callq 0x30d0
movq -0x3a8(%rbp), %rdx
movq -0x3a0(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x30c0
movq -0x3a0(%rbp), %rdi
leaq 0x1921(%rip), %rsi      # 0xe51c
callq 0x3220
movq %rax, -0x248(%rbp)
movl $0x0, -0x24c(%rbp)
movl $0x0, -0x250(%rbp)
movslq -0x24c(%rbp), %rax
movq -0x30(%rbp,%rax,8), %rax
movq %rax, -0x398(%rbp)
movl $0x8, %edi
callq 0x3130
movq -0x370(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x390(%rbp)
callq 0xc620
jmp 0xcc53
movq -0x390(%rbp), %rsi
movq -0x398(%rbp), %rdi
callq 0xc400
cmpq $0x0, -0x248(%rbp)
je 0xce96
leaq -0x350(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x30d0
leaq -0x350(%rbp), %rdi
movq -0x248(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x30c0
xorl %eax, %eax
movl %eax, %edi
leaq 0x1872(%rip), %rsi      # 0xe51c
callq 0x3220
movq %rax, -0x248(%rbp)
movl -0x24c(%rbp), %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x250(%rbp)
movslq -0x250(%rbp), %rax
movq -0x30(%rbp,%rax,8), %rdi
callq 0xc570
leaq -0x350(%rbp), %rdi
callq 0xc1a0
cmpb $0x0, -0x350(%rbp)
je 0xce85
movslq -0x24c(%rbp), %rax
movq -0x30(%rbp,%rax,8), %rdi
callq 0xc500
movl %eax, -0x354(%rbp)
movl $0x0, -0x358(%rbp)
movl -0x358(%rbp), %eax
cmpl -0x354(%rbp), %eax
jge 0xce83
movslq -0x24c(%rbp), %rax
movq -0x30(%rbp,%rax,8), %rdi
movl -0x358(%rbp), %esi
callq 0xc520
movq %rax, -0x360(%rbp)
movq $0x0, -0x368(%rbp)
movsbl -0x350(%rbp), %eax
movl %eax, -0x3ac(%rbp)
subl $0x23, %eax
je 0xcde3
jmp 0xcd69
movl -0x3ac(%rbp), %eax
subl $0x2e, %eax
je 0xce0f
jmp 0xce3b
movq -0x378(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x18, %esi
callq 0x3140
jmp 0xcef2
movq -0x380(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x18, %esi
callq 0x3140
jmp 0xcef2
movq -0x390(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x8, %esi
callq 0x3140
jmp 0xcef2
movq -0x360(%rbp), %rdi
leaq -0x350(%rbp), %rdx
addq $0x1, %rdx
leaq 0x21c4(%rip), %rsi      # 0xefc0
movl $0x1, %ecx
callq 0xc8e0
movq %rax, -0x368(%rbp)
jmp 0xce57
movq -0x360(%rbp), %rdi
leaq -0x350(%rbp), %rdx
addq $0x1, %rdx
leaq 0x219b(%rip), %rsi      # 0xefc3
movl $0x1, %ecx
callq 0xc8e0
movq %rax, -0x368(%rbp)
jmp 0xce57
movq -0x360(%rbp), %rdi
leaq -0x350(%rbp), %rsi
xorl %edx, %edx
callq 0xc6d0
movq %rax, -0x368(%rbp)
movslq -0x250(%rbp), %rax
movq -0x30(%rbp,%rax,8), %rdi
movq -0x368(%rbp), %rsi
callq 0xc4a0
movl -0x358(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x358(%rbp)
jmp 0xcd1a
jmp 0xce85
movl -0x250(%rbp), %eax
movl %eax, -0x24c(%rbp)
jmp 0xcc66
movl -0x24c(%rbp), %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x250(%rbp)
movslq -0x250(%rbp), %rax
movq -0x30(%rbp,%rax,8), %rax
movq %rax, -0x3b8(%rbp)
cmpq $0x0, %rax
je 0xced5
movq -0x3b8(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movslq -0x24c(%rbp), %rax
movq -0x30(%rbp,%rax,8), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x3c0, %rsp            # imm = 0x3C0
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x3270
nopl (%rax,%rax)