// File: /workspace/github_cpp_makefile_star2_O1/MichaelXanth[P]RPG_Game/Market/Market.cpp
// Function: Market::Buy(std::vector<Hero*, std::allocator<Hero*>>&)
void Market::Buy(vector<Hero *>& heroes)
{
    unsigned int i;

    int action;
    string testAction;
    bool infoState     = false;
    Hero* selectedHero = NULL;

    if (heroes.size() == 1 && isAbleToBuy(heroes[0]) == -1) {
        usleep(2500000);
        DisplayMarket();
        return;
    
    } else {
        for (i = 0; i < heroes.size() && isAbleToBuy(heroes[i],false) == -1; i++)
            ;

        if (i == heroes.size()) {
            cerr << endl << "\033[31mError:\033[0m No one of the heroes are able to buy any Items or Spells from the Market.";
            usleep(2500000);
            DisplayMarket();
            return;
        }
    }

    while(true) {
        
        int buyState;

        if (heroes.size() == 1) {
            selectedHero = heroes[0];
        
        } else {
        
            for (i = 0; i < heroes.size() && isAbleToBuy(heroes[i],false) == -1; i++)
                ;

            if (i == heroes.size()) {
                cerr << endl << "-------------------------------" << endl << endl;
                cerr << "\033[35mWarning:\033[0m There are no other heroes able to buy any Items or Spells from the Market.";
                usleep(3000000);
                DisplayMarket();
                return;
            }

            while(selectedHero == NULL) {
                //bool errorFlag = false;
                cout << endl;
                cout << "-------------------------------" << endl << endl;
                cout << "(Type 0 to cancel)" << endl;
                cout << "Choose a Hero (1 - " << heroes.size() << "): ";

                cout << "\n\n\n\n\n";
                cout << "\033[5A\033[23C";

                while(true) {

                    bool errFlag = false;
                    cout << "\033[s";

                    do {
                        if (errFlag)
                            usleep(1800000);

                        cout << "\033[u";
                        cout << "\033[J";
                        cout << "\033[s";

                        cin >> testAction;

                        errFlag = true;
                    } while(!isNumeric(testAction.c_str(),action));

                    if (action < 0 || action > heroes.size()) {
                        cerr << endl << "\033[31mError:\033[0m Please choose a number in the given range." << endl;

                        usleep(1800000);

                        cout << "\033[u";
                        cout << "\033[J";

                        //cout << "\033[K";
                        //cout << "\033[1A\033[K";
                        //cout << "\033[1A\033[K";

                        //if (errorFlag)
                        //    cout << "\033[1A\033[K";

                        //errorFlag = true;
                    } else {
                        if (!action) {
                            DisplayMarket();
                            return;
                        }
                        selectedHero = heroes[action - 1];
                        break;
                    }
                }
            }
        }

        if ((buyState = isAbleToBuy(selectedHero)) != -1) {
            bool errFlag = false;

            DisplayMarket(infoState,selectedHero,buyState);

            cout << endl;
            cout << "-------------------------------" << endl << endl;
            cout << "Choose an action: ";

            cout << "\n\n\n\n\n";
            cout << "\033[5A\033[18C";
            cout << "\033[s";

            do {
                if (errFlag)
                    usleep(1800000);

                cout << "\033[u";
                cout << "\033[J";
                cout << "\033[s";

                cin >> testAction;

                errFlag = true;
            } while(!isValid(testAction.c_str(),action,1,3));

            switch(action)
            {
                case 1:
                    Purchase(*selectedHero,buyState);
                    break;
                case 2:
                    infoState = !infoState;
                    // DisplayMarket(infoState,selectedHero,buyState);
                    break;
                case 3:
                    DisplayMarket();
                    return;
            }
        
        } else if (heroes.size() == 1) {
            usleep(2500000);
            DisplayMarket();
            return;
        }

        if (buyState == -1) {
            DisplayMarket();
            return;
        }
    //        selectedHero = NULL;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r12
movq %rdi, %rbx
leaq -0x70(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq $0x0, -0x8(%rcx)
xorl %eax, %eax
movb %al, (%rcx)
movb %al, -0x2d(%rbp)
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
subq %rax, %rcx
cmpq $0x8, %rcx
jne 0x207e9
movq (%rax), %rsi
leaq -0x34(%rbp), %rdx
movb $0x1, (%rdx)
movq %rbx, %rdi
callq 0x1ad96
cmpl $-0x1, %eax
je 0x20f55
movq %rbx, -0x50(%rbp)
movq (%r12), %rax
cmpq %rax, 0x8(%r12)
je 0x2083b
movl $0x1, %r15d
xorl %r14d, %r14d
leaq -0x34(%rbp), %rbx
movq (%rax,%r14,8), %rsi
movb $0x0, -0x34(%rbp)
movq -0x50(%rbp), %rdi
movq %rbx, %rdx
callq 0x1ad96
cmpl $-0x1, %eax
jne 0x2083e
movl %r15d, %r14d
movq (%r12), %rax
movq 0x8(%r12), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
incl %r15d
cmpq %r14, %rcx
ja 0x20805
jmp 0x2083e
xorl %r14d, %r14d
movq 0x8(%r12), %rax
subq (%r12), %rax
sarq $0x3, %rax
cmpq %r14, %rax
jne 0x208bc
movq 0xe799(%rip), %rdi      # 0x2eff0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x3190
movq -0x50(%rbp), %rbx
movsbl %al, %esi
movq 0xe77a(%rip), %rdi      # 0x2eff0
callq 0x3040
movq %rax, %rdi
callq 0x3100
leaq 0x6e49(%rip), %rsi      # 0x276d3
movl $0x59, %edx
movq %rax, %rdi
callq 0x31e0
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x3260
leaq -0x34(%rbp), %rsi
movb $0x0, (%rsi)
movq %rbx, %rdi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b940
jmp 0x20f75
movq %r12, -0x58(%rbp)
movq $0x0, -0x40(%rbp)
movq 0xe6f9(%rip), %r13      # 0x2efc8
leaq 0x3fa8(%rip), %r12      # 0x2487e
leaq 0x5ec6(%rip), %r15      # 0x267a3
movq -0x50(%rbp), %rbx
movq -0x58(%rbp), %rdx
movq (%rdx), %rax
movq 0x8(%rdx), %rcx
subq %rax, %rcx
cmpq $0x8, %rcx
jne 0x208fd
movq (%rax), %rsi
jmp 0x20a4e
movq (%rdx), %rax
cmpq %rax, 0x8(%rdx)
je 0x20946
movl $0x1, %ebx
xorl %r14d, %r14d
movq (%rax,%r14,8), %rsi
movb $0x0, -0x2c(%rbp)
movq -0x50(%rbp), %rdi
leaq -0x2c(%rbp), %rdx
callq 0x1ad96
cmpl $-0x1, %eax
jne 0x20949
movl %ebx, %r14d
movq -0x58(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
incl %ebx
cmpq %r14, %rcx
ja 0x2090e
jmp 0x20949
xorl %r14d, %r14d
movq -0x58(%rbp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
sarq $0x3, %rax
cmpq %r14, %rax
jne 0x20a3d
movq 0xe688(%rip), %rcx      # 0x2eff0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3190
movq -0x50(%rbp), %rbx
movsbl %al, %esi
movq 0xe666(%rip), %rdi      # 0x2eff0
callq 0x3040
movq %rax, %rdi
callq 0x3100
movq %rax, %r14
movl $0x1f, %edx
movq %rax, %rdi
leaq 0x6879(%rip), %rsi      # 0x27222
callq 0x31e0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movl $0x5c, %edx
movq 0xe5e5(%rip), %rdi      # 0x2eff0
leaq 0x6d1b(%rip), %rsi      # 0x2772d
callq 0x31e0
movl $0x2dc6c0, %edi         # imm = 0x2DC6C0
callq 0x3260
movb $0x0, -0x2c(%rbp)
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b940
jmp 0x20f49
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
movq -0x50(%rbp), %rbx
je 0x20c4c
movb $0x1, -0x2c(%rbp)
movq %rbx, %rdi
movq %rsi, -0x40(%rbp)
leaq -0x2c(%rbp), %rdx
callq 0x1ad96
movl %eax, -0x34(%rbp)
cmpl $-0x1, %eax
je 0x20c11
movq %rbx, %rdi
leaq -0x2d(%rbp), %rsi
movq -0x40(%rbp), %rdx
movl %eax, %ecx
callq 0x1b940
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movl $0x1f, %edx
movq %r13, %rdi
leaq 0x676b(%rip), %rsi      # 0x27222
callq 0x31e0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movl $0x12, %edx
movq %r13, %rdi
leaq 0x6cc6(%rip), %rsi      # 0x277e3
callq 0x31e0
movl $0x5, %edx
movq %r13, %rdi
leaq 0x68c7(%rip), %rsi      # 0x273f8
callq 0x31e0
movl $0x9, %edx
movq %r13, %rdi
leaq 0x6cb1(%rip), %rsi      # 0x277f6
callq 0x31e0
movl $0x3, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x31e0
movl $0x3, %edx
movq %r13, %rdi
leaq 0x3d3f(%rip), %rsi      # 0x248a8
callq 0x31e0
movl $0x3, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x31e0
movl $0x3, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x31e0
movq 0xe443(%rip), %rdi      # 0x2efd8
leaq -0x80(%rbp), %rsi
callq 0x3230
movq -0x80(%rbp), %rdi
movl $0x1, -0x2c(%rbp)
movl $0x3, -0x5c(%rbp)
leaq -0x44(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
leaq -0x5c(%rbp), %rcx
movl $0x1, %r8d
callq 0x103de
testb %al, %al
jne 0x20bd7
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x3260
jmp 0x20b5a
movl -0x44(%rbp), %eax
cmpl $0x1, %eax
je 0x20f1a
cmpl $0x2, %eax
je 0x20f14
cmpl $0x3, %eax
jne 0x20f2a
movb $0x0, -0x2c(%rbp)
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b940
jmp 0x20f49
movq -0x58(%rbp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cmpq $0x8, %rax
jne 0x20f2a
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x3260
movb $0x0, -0x2c(%rbp)
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b940
jmp 0x20f49
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movl $0x1f, %edx
movq %r13, %rdi
leaq 0x659f(%rip), %rsi      # 0x27222
callq 0x31e0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movl $0x12, %edx
movq %r13, %rdi
leaq 0x6684(%rip), %rsi      # 0x2736d
callq 0x31e0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movl $0x13, %edx
movq %r13, %rdi
leaq 0x6a65(%rip), %rsi      # 0x2778a
callq 0x31e0
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rsi
subq (%rax), %rsi
sarq $0x3, %rsi
movq %r13, %rdi
callq 0x30f0
movl $0x3, %edx
movq %rax, %rdi
leaq 0x66a1(%rip), %rsi      # 0x273f1
callq 0x31e0
movl $0x5, %edx
movq %r13, %rdi
leaq 0x6694(%rip), %rsi      # 0x273f8
callq 0x31e0
movl $0x9, %edx
movq %r13, %rdi
leaq 0x6a26(%rip), %rsi      # 0x2779e
callq 0x31e0
movq $0x0, -0x40(%rbp)
movl $0x3, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x31e0
movl $0x3, %edx
movq %r13, %rdi
leaq 0x3b04(%rip), %rsi      # 0x248a8
callq 0x31e0
movl $0x3, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x31e0
movl $0x3, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x31e0
movq 0xe208(%rip), %rdi      # 0x2efd8
leaq -0x80(%rbp), %rsi
callq 0x3230
movq -0x80(%rbp), %rdi
leaq -0x44(%rbp), %rsi
movl $0x1, %edx
callq 0x102dc
testb %al, %al
jne 0x20dfb
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x3260
jmp 0x20d95
movslq -0x44(%rbp), %rax
testq %rax, %rax
js 0x20e1f
movq -0x58(%rbp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rax, %rdx
jae 0x20ec3
movq 0xe1ca(%rip), %rcx      # 0x2eff0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq 0xe1ac(%rip), %rdi      # 0x2eff0
callq 0x3040
movq %rax, %rdi
callq 0x3100
movq %rax, %r14
movl $0x3a, %edx
movq %rax, %rdi
leaq 0x6945(%rip), %rsi      # 0x277a8
callq 0x31e0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x3260
movl $0x3, %edx
movq %r13, %rdi
leaq 0x3a00(%rip), %rsi      # 0x248a8
callq 0x31e0
movl $0x3, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x31e0
xorl %ecx, %ecx
movb $0x1, %al
jmp 0x20ef5
testl %eax, %eax
je 0x20ed7
movq -0x8(%rcx,%rax,8), %rax
movq %rax, -0x40(%rbp)
movl $0xd, %ecx
jmp 0x20ef3
movb $0x0, -0x2c(%rbp)
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b940
movl $0x1, %ecx
xorl %eax, %eax
testl %ecx, %ecx
je 0x20d85
cmpl $0xd, %ecx
jne 0x20f4b
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
je 0x20c4c
jmp 0x20a4e
xorb $0x1, -0x2d(%rbp)
jmp 0x20f2a
movq %rbx, %rdi
movq -0x40(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x1faf4
movb $0x1, %al
cmpl $-0x1, -0x34(%rbp)
jne 0x20f4b
movb $0x0, -0x2c(%rbp)
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b940
xorl %eax, %eax
testb %al, %al
jne 0x208e1
jmp 0x20f75
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x3260
leaq -0x34(%rbp), %rsi
movb $0x0, (%rsi)
movq %rbx, %rdi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b940
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x20f87
callq 0x3170
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
jmp 0x20fba
movq %rax, %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x20fcf
callq 0x3170
movq %rbx, %rdi
callq 0x32f0
nop