// File: /workspace/github_cpp_makefile_star2_O1/MichaelXanth[P]RPG_Game/Market/Market.cpp
// Function: Market::Sell(std::vector<Hero*, std::allocator<Hero*>>&)
void Market::Sell(vector<Hero *>& heroes)
{
    unsigned int i;

    int action;
    string testAction;
    bool infoState     = false;
    Hero* selectedHero = NULL;

    if (heroes.size() == 1 && isAbleToSell(heroes[0]) == -1) {
        usleep(2500000);
        DisplayMarket();
        return;
    
    } else {
        for (i = 0; i < heroes.size() && isAbleToSell(heroes[i],false) == -1; i++)
            ;

        if (i == heroes.size()) {
            cerr << endl << "\033[31mError:\033[0m No one of the heroes have any Items or Spells for sale.";
            usleep(2500000);
            DisplayMarket();
            return;
        }
    }

    while(true) {
        int sellState;

        if (heroes.size() == 1) {
            selectedHero = heroes[0];
        
        } else {
            for (i = 0; i < heroes.size() && isAbleToSell(heroes[i],false) == -1; i++)
                ;

            if (i == heroes.size()) {
                cerr << endl << "-------------------------------" << endl << endl;
                cerr << "\033[35mWarning:\033[0m No one of the heroes have any Items or Spells left for sale.";
                usleep(2500000);
                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 ((sellState = isAbleToSell(selectedHero)) != -1) {
            bool errFlag = false;

            selectedHero->DisplayProdsForSale(infoState,sellState);

            cout << endl;
            /*if (sellState == 0 || sellState == 1)
            {
                cout << "\033[44C-------------------------------" << endl << endl;
                cout << "\033[44CChoose an action: ";
            
            } else {*/
                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:
                    Sale(*selectedHero,sellState);
                    break;
                case 2:
                    infoState = !infoState;
                    //selectedHero->DisplayProdsForSale(infoState,sellState);
                    break;
                case 3:
                    DisplayMarket();
                    return;
            }
        
        } else if (heroes.size() == 1) {
            usleep(2500000);
            DisplayMarket();
            return;
        }

        if (sellState == -1) {
            DisplayMarket();
            return;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movq %rdi, -0x40(%rbp)
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 0x21dd5
movq (%rax), %rsi
leaq -0x34(%rbp), %rdx
movb $0x1, (%rdx)
callq 0x20fd8
cmpl $-0x1, %eax
je 0x22511
movq (%rbx), %rax
movq %rbx, -0x58(%rbp)
cmpq %rax, 0x8(%rbx)
je 0x21e23
movl $0x1, %r15d
xorl %r14d, %r14d
leaq -0x34(%rbp), %rbx
movq (%rax,%r14,8), %rsi
movb $0x0, -0x34(%rbp)
movq %rbx, %rdx
callq 0x20fd8
cmpl $-0x1, %eax
jne 0x21e26
movl %r15d, %r14d
movq -0x58(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
incl %r15d
cmpq %r14, %rcx
ja 0x21def
jmp 0x21e26
xorl %r14d, %r14d
movq -0x58(%rbp), %rbx
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x3, %rax
cmpq %r14, %rax
jne 0x21ea3
movq 0xd1af(%rip), %rdi      # 0x2eff0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq 0xd194(%rip), %rdi      # 0x2eff0
callq 0x3040
movq %rax, %rdi
callq 0x3100
leaq 0x5b04(%rip), %rsi      # 0x27974
movl $0x47, %edx
movq %rax, %rdi
callq 0x31e0
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x3260
leaq -0x34(%rbp), %rsi
movb $0x0, (%rsi)
movq -0x40(%rbp), %rdi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b940
jmp 0x22532
movq $0x0, -0x48(%rbp)
movq 0xd116(%rip), %r12      # 0x2efc8
leaq 0x29c5(%rip), %r13      # 0x2487e
leaq 0x48e3(%rip), %r15      # 0x267a3
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
cmpq $0x8, %rcx
jne 0x2205c
movq (%rax), %rsi
movb $0x1, -0x2c(%rbp)
movq %rsi, -0x48(%rbp)
leaq -0x2c(%rbp), %rdx
callq 0x20fd8
movl %eax, -0x34(%rbp)
cmpl $-0x1, %eax
je 0x220e4
movq -0x48(%rbp), %rdi
leaq -0x2d(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x11858
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movl $0x1f, %edx
movq %r12, %rdi
leaq 0x52e6(%rip), %rsi      # 0x27222
callq 0x31e0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r12, %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 %r12, %rdi
leaq 0x5841(%rip), %rsi      # 0x277e3
callq 0x31e0
movl $0x5, %edx
movq %r12, %rdi
leaq 0x5442(%rip), %rsi      # 0x273f8
callq 0x31e0
movl $0x9, %edx
movq %r12, %rdi
leaq 0x582c(%rip), %rsi      # 0x277f6
callq 0x31e0
movl $0x3, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x31e0
movl $0x3, %edx
movq %r12, %rdi
leaq 0x28ba(%rip), %rsi      # 0x248a8
callq 0x31e0
movl $0x3, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x31e0
movl $0x3, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x31e0
movq 0xcfbe(%rip), %rdi      # 0x2efd8
leaq -0x80(%rbp), %rsi
callq 0x3230
movq -0x80(%rbp), %rdi
movl $0x1, -0x2c(%rbp)
movl $0x3, -0x5c(%rbp)
leaq -0x4c(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
leaq -0x5c(%rbp), %rcx
movl $0x1, %r8d
callq 0x103de
testb %al, %al
jne 0x220a9
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x3260
jmp 0x21fdf
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x2211c
movl $0x1, %ebx
xorl %r14d, %r14d
movq (%rax,%r14,8), %rsi
movb $0x0, -0x2c(%rbp)
leaq -0x2c(%rbp), %rdx
callq 0x20fd8
cmpl $-0x1, %eax
jne 0x2211f
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 0x22071
jmp 0x2211f
movl -0x4c(%rbp), %eax
cmpl $0x1, %eax
je 0x224d8
cmpl $0x2, %eax
je 0x224d2
cmpl $0x3, %eax
jne 0x224e5
movb $0x0, -0x2c(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b940
jmp 0x22505
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cmpq $0x8, %rax
jne 0x224e5
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x3260
movb $0x0, -0x2c(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b940
jmp 0x22505
xorl %r14d, %r14d
movq -0x58(%rbp), %rbx
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x3, %rax
cmpq %r14, %rax
jne 0x22210
movq 0xceb2(%rip), %rcx      # 0x2eff0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq 0xce94(%rip), %rdi      # 0x2eff0
callq 0x3040
movq %rax, %rdi
callq 0x3100
movq %rax, %r14
movl $0x1f, %edx
movq %rax, %rdi
leaq 0x50a7(%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 $0x4e, %edx
movq 0xce13(%rip), %rdi      # 0x2eff0
leaq 0x57d8(%rip), %rsi      # 0x279bc
callq 0x31e0
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x3260
movb $0x0, -0x2c(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b940
jmp 0x22505
movq -0x48(%rbp), %rsi
testq %rsi, %rsi
jne 0x21ed7
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movl $0x1f, %edx
movq %r12, %rdi
leaq 0x4fce(%rip), %rsi      # 0x27222
callq 0x31e0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r12, %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 %r12, %rdi
leaq 0x50b3(%rip), %rsi      # 0x2736d
callq 0x31e0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3100
movl $0x13, %edx
movq %r12, %rdi
leaq 0x5494(%rip), %rsi      # 0x2778a
callq 0x31e0
movq 0x8(%rbx), %rsi
subq (%rbx), %rsi
sarq $0x3, %rsi
movq %r12, %rdi
callq 0x30f0
movl $0x3, %edx
movq %rax, %rdi
leaq 0x50d4(%rip), %rsi      # 0x273f1
callq 0x31e0
movl $0x5, %edx
movq %r12, %rdi
leaq 0x50c7(%rip), %rsi      # 0x273f8
callq 0x31e0
movl $0x9, %edx
movq %r12, %rdi
leaq 0x5459(%rip), %rsi      # 0x2779e
callq 0x31e0
movq $0x0, -0x48(%rbp)
movl $0x3, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x31e0
movl $0x3, %edx
movq %r12, %rdi
leaq 0x2537(%rip), %rsi      # 0x248a8
callq 0x31e0
movl $0x3, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x31e0
movl $0x3, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x31e0
movq 0xcc3b(%rip), %rdi      # 0x2efd8
leaq -0x80(%rbp), %rsi
callq 0x3230
movq -0x80(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
movl $0x1, %edx
callq 0x102dc
testb %al, %al
jne 0x223c8
movl $0x1b7740, %edi         # imm = 0x1B7740
callq 0x3260
jmp 0x22362
movslq -0x4c(%rbp), %rax
testq %rax, %rax
js 0x223e8
movq (%rbx), %rcx
movq 0x8(%rbx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rax, %rdx
jae 0x2248c
movq 0xcc01(%rip), %rcx      # 0x2eff0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3190
movsbl %al, %esi
movq 0xcbe3(%rip), %rdi      # 0x2eff0
callq 0x3040
movq %rax, %rdi
callq 0x3100
movq %rax, %r14
movl $0x3a, %edx
movq %rax, %rdi
leaq 0x537c(%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 %r12, %rdi
leaq 0x2437(%rip), %rsi      # 0x248a8
callq 0x31e0
movl $0x3, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x31e0
xorl %ecx, %ecx
movb $0x1, %al
jmp 0x224bf
testl %eax, %eax
je 0x224a0
movq -0x8(%rcx,%rax,8), %rax
movq %rax, -0x48(%rbp)
movl $0xd, %ecx
jmp 0x224bd
movb $0x0, -0x2c(%rbp)
movq -0x40(%rbp), %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 0x22352
cmpl $0xd, %ecx
je 0x22210
jmp 0x22507
xorb $0x1, -0x2d(%rbp)
jmp 0x224e5
movq -0x48(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x21134
movb $0x1, %al
cmpl $-0x1, -0x34(%rbp)
jne 0x22507
movb $0x0, -0x2c(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b940
xorl %eax, %eax
testb %al, %al
jne 0x21ec0
jmp 0x22532
movl $0x2625a0, %edi         # imm = 0x2625A0
callq 0x3260
leaq -0x34(%rbp), %rsi
movb $0x0, (%rsi)
movq -0x40(%rbp), %rdi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x1b940
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x22544
callq 0x3170
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
jmp 0x22577
movq %rax, %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x2258c
callq 0x3170
movq %rbx, %rdi
callq 0x32f0