// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rdi, %r12
leaq -0x70(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq $0x0, -0x8(%rcx)
xorl %eax, %eax
movb %al, (%rcx)
movb %al, -0x29(%rbp)
movq (%rsi), %rax
movq %rsi, -0x48(%rbp)
movq 0x8(%rsi), %rcx
movq %rcx, %rdx
subq %rax, %rdx
cmpq $0x8, %rdx
jne 0x20872
movq (%rax), %rsi
leaq -0x34(%rbp), %rdx
movb $0x1, (%rdx)
movq %r12, %rdi
callq 0x1ada0
cmpl $-0x1, %eax
je 0x20e86
movq -0x48(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
cmpq %rax, %rcx
je 0x208be
movl $0x1, %r15d
xorl %r14d, %r14d
leaq -0x34(%rbp), %rbx
movq (%rax,%r14,8), %rsi
movb $0x0, -0x34(%rbp)
movq %r12, %rdi
movq %rbx, %rdx
callq 0x1ada0
cmpl $-0x1, %eax
jne 0x208c6
movl %r15d, %r14d
movq -0x48(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
incl %r15d
cmpq %r14, %rdx
ja 0x20884
jmp 0x208d1
xorl %r14d, %r14d
movq %rax, %rcx
jmp 0x208d1
movq -0x48(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jne 0x20948
movq 0xe709(%rip), %rdi      # 0x2eff0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq 0xe6ee(%rip), %rdi      # 0x2eff0
callq 0x3040
movq %rax, %rdi
callq 0x3100
leaq 0x6ded(%rip), %rsi      # 0x27703
movl $0x59, %edx
movq %rax, %rdi
callq 0x31e0
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x3260
leaq -0x34(%rbp), %rsi
movb $0x0, (%rsi)
movq %r12, %rdi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b982
jmp 0x20fd3
movq %r12, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq 0xe66d(%rip), %r13      # 0x2efc8
leaq 0x3f1c(%rip), %r12      # 0x2487e
leaq 0x3f3f(%rip), %rbx      # 0x248a8
leaq 0x5e33(%rip), %r15      # 0x267a3
movq %rcx, %rdx
subq %rax, %rdx
cmpq $0x8, %rdx
jne 0x20b29
movq (%rax), %rsi
movb $0x1, -0x30(%rbp)
movq -0x40(%rbp), %rdi
movq %rsi, -0x58(%rbp)
leaq -0x30(%rbp), %rdx
callq 0x1ada0
movl %eax, -0x34(%rbp)
cmpl $-0x1, %eax
je 0x20b79
movq -0x40(%rbp), %rdi
leaq -0x29(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl %eax, %ecx
callq 0x1b982
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 0x6864(%rip), %rsi      # 0x27252
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 0x6dbf(%rip), %rsi      # 0x27813
callq 0x31e0
movl $0x5, %edx
movq %r13, %rdi
leaq 0x69c0(%rip), %rsi      # 0x27428
callq 0x31e0
movl $0x9, %edx
movq %r13, %rdi
leaq 0x6daa(%rip), %rsi      # 0x27826
callq 0x31e0
movl $0x3, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x31e0
movl $0x3, %edx
movq %r13, %rdi
movq %rbx, %rsi
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 0xe510(%rip), %rdi      # 0x2efd8
leaq -0x80(%rbp), %rsi
callq 0x3230
movq -0x80(%rbp), %rdi
movl $0x1, -0x30(%rbp)
movl $0x3, -0x5c(%rbp)
leaq -0x4c(%rbp), %rsi
leaq -0x30(%rbp), %rdx
leaq -0x5c(%rbp), %rcx
movl $0x1, %r8d
callq 0x10336
testb %al, %al
jne 0x20b0a
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x3260
jmp 0x20a91
movl -0x4c(%rbp), %eax
cmpl $0x1, %eax
je 0x20b9c
cmpl $0x2, %eax
je 0x20b96
cmpl $0x3, %eax
jne 0x20bad
jmp 0x20fbc
cmpq %rax, %rcx
je 0x20b8f
movl $0x1, %r14d
xorl %esi, %esi
movq %rsi, -0x88(%rbp)
movq (%rax,%rsi,8), %rsi
movb $0x0, -0x30(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x1ada0
cmpl $-0x1, %eax
jne 0x20bc7
movl %r14d, %esi
movq -0x48(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
incl %r14d
cmpq %rsi, %rdx
ja 0x20b36
jmp 0x20bd9
movq -0x48(%rbp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cmpq $0x8, %rax
jne 0x20bad
jmp 0x20f99
movq %rax, %rcx
xorl %esi, %esi
jmp 0x20bd9
xorb $0x1, -0x29(%rbp)
jmp 0x20bad
movq -0x40(%rbp), %rdi
movq -0x58(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x1fb64
cmpl $-0x1, -0x34(%rbp)
je 0x20eab
movq -0x48(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
jmp 0x20970
movq -0x48(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq -0x88(%rbp), %rsi
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rsi, %rcx
je 0x20ec7
movq -0x58(%rbp), %rsi
testq %rsi, %rsi
jne 0x20983
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 0x6625(%rip), %rsi      # 0x27252
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 0x670a(%rip), %rsi      # 0x2739d
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 0x6aeb(%rip), %rsi      # 0x277ba
callq 0x31e0
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rsi
subq (%rax), %rsi
sarq $0x3, %rsi
movq %r13, %rdi
callq 0x30f0
movl $0x3, %edx
movq %rax, %rdi
leaq 0x6727(%rip), %rsi      # 0x27421
callq 0x31e0
movl $0x5, %edx
movq %r13, %rdi
leaq 0x671a(%rip), %rsi      # 0x27428
callq 0x31e0
movl $0x9, %edx
movq %r13, %rdi
leaq 0x6aac(%rip), %rsi      # 0x277ce
callq 0x31e0
movl $0x3, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x31e0
movl $0x3, %edx
movq %r13, %rdi
movq %rbx, %rsi
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 0xe26a(%rip), %rdi      # 0x2efd8
leaq -0x80(%rbp), %rsi
callq 0x3230
movq -0x80(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
movl $0x1, %edx
callq 0x1023c
testb %al, %al
jne 0x20d99
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x3260
jmp 0x20d37
movslq -0x4c(%rbp), %rax
testq %rax, %rax
js 0x20dbd
movq -0x48(%rbp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rax, %rdx
jae 0x20e5c
movq 0xe22c(%rip), %rcx      # 0x2eff0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq 0xe20e(%rip), %rdi      # 0x2eff0
callq 0x3040
movq %rax, %rdi
callq 0x3100
movq %rax, %r14
movl $0x3a, %edx
movq %rax, %rdi
leaq 0x69d7(%rip), %rsi      # 0x277d8
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
movq %rbx, %rsi
callq 0x31e0
movl $0x3, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x31e0
jmp 0x20d27
testl %eax, %eax
je 0x20e6a
movq -0x8(%rcx,%rax,8), %rsi
jmp 0x20bed
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
movq -0x40(%rbp), %rdi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b982
jmp 0x20fd3
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x3260
leaq -0x34(%rbp), %rsi
movb $0x0, (%rsi)
movq %r12, %rdi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b982
jmp 0x20fd3
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
movq -0x40(%rbp), %rdi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b982
jmp 0x20fd3
movq 0xe122(%rip), %rdi      # 0x2eff0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq 0xe107(%rip), %rdi      # 0x2eff0
callq 0x3040
movq %rax, %rdi
callq 0x3100
movq %rax, %rbx
leaq 0x6352(%rip), %rsi      # 0x27252
movl $0x1f, %edx
movq %rax, %rdi
callq 0x31e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movq 0xe08b(%rip), %rdi      # 0x2eff0
leaq 0x67f1(%rip), %rsi      # 0x2775d
movl $0x5c, %edx
callq 0x31e0
movl $0x2dc6c0, %edi         # imm = 0x2DC6C0
callq 0x3260
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
movq -0x40(%rbp), %rdi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b982
jmp 0x20fd3
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x3260
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
movq -0x40(%rbp), %rdi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b982
jmp 0x20fd3
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
movq -0x40(%rbp), %rdi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b982
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x20fe5
callq 0x3170
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
jmp 0x21018
movq %rax, %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x2102d
callq 0x3170
movq %rbx, %rdi
callq 0x32f0
nop