// File: /workspace/github_cpp_makefile_star2_O1/arisKoutsou[P]oopproj/./src/livings/Hero.cpp
// Function: Hero::buy(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void Hero::buy(const string& itemName) {
  Market* currentMarket = this->getTile().getMarket();

  if (currentMarket == NULL) {
    cout << "You can't buy on a tile that has no market!" << endl << endl;
    return;
  }

  Item* itemToBuy = currentMarket->getItemByName(itemName);

  if (itemToBuy != NULL) {    
    string kind = itemToBuy->kindOf();
    Item* itemToAdd;
    bool equiped = false;

    cout << endl << "Bought " << itemName
	 << " (" << kind << ")";
    if (kind == "Weapon") {
      itemToAdd = new Weapon(*static_cast<Weapon*>(itemToBuy));
      Weapon* aux = static_cast<Weapon*>(itemToAdd);
      if (aux->needsBothHands()) {
        if (this->usesLeftHand() == false && this->usesRightHand() == false) {
	  this->equipWeapon(aux);
	  equiped = true;
	}
      } else {
	if (this->usesLeftHand() == false || this->usesRightHand() == false) {
	  this->equipWeapon(aux);
	  equiped = true;
	}
      }
    } else if (kind == "Armor") {
      itemToAdd = new Armor(*static_cast<Armor*>(itemToBuy));
      if (this->shield == NULL) {
	this->shield = static_cast<Armor*>(itemToAdd);
	equiped = true;
      }
    } else {
      itemToAdd = new Potion(*static_cast<Potion*>(itemToBuy));
    }

    if(!equiped) inventory.addItem(itemToAdd);
    setMoney(getMoney() - itemToBuy->buyFor());
    return;
  }

  Spell* spellToBuy = currentMarket->getSpellByName(itemName);
  
  if (spellToBuy != NULL) {
    string kind = spellToBuy->kindOf();
    Spell* spellToAdd;
    bool equiped = false;

    cout << endl << "Bought " << itemName
	 << " (" << kind << ")";
    if (kind == "IceSpell") {
      spellToAdd = new IceSpell(*static_cast<IceSpell*>(spellToBuy));
    } else if (kind == "FireSpell") {
      spellToAdd = new FireSpell(*static_cast<FireSpell*>(spellToBuy));
    } else {
      spellToAdd = new LightningSpell(*static_cast<LightningSpell*>(spellToBuy));
    }
    if (this->spells.size() == 0) {
      this->spells.push_back(spellToAdd);
      equiped = true;
    }

    if (!equiped) inventory.addSpell(spellToAdd);
    setMoney(getMoney() - spellToBuy->getValue());
    return;
  }

  cout << endl << "There's no item/spell with this name" << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rsi, %r15
movq %rdi, %rbx
callq 0x134d6
movq %rax, %rdi
callq 0x6682
testq %rax, %rax
je 0x155a4
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
callq 0x11e7e
testq %rax, %rax
je 0x1561c
movq %rax, %r14
movq (%rax), %rax
movq %rsp, %rdi
movq %r14, %rsi
callq *0x18(%rax)
movq 0xfb47(%rip), %rdi      # 0x24fc0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x4270
movsbl %al, %esi
movq 0xfb2c(%rip), %rdi      # 0x24fc0
callq 0x4040
movq %rax, %rdi
callq 0x41b0
movq %rax, %r12
leaq 0x6bd8(%rip), %rsi      # 0x1c083
movl $0x7, %edx
movq %rax, %rdi
callq 0x42f0
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %r12, %rdi
callq 0x42f0
movq %rax, %r15
leaq 0x6bba(%rip), %rsi      # 0x1c08b
movl $0x2, %edx
movq %rax, %rdi
callq 0x42f0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r15, %rdi
callq 0x42f0
leaq 0x60d5(%rip), %rsi      # 0x1b5cb
movl $0x1, %edx
movq %rax, %rdi
callq 0x42f0
leaq 0x66a8(%rip), %rsi      # 0x1bbb2
movq %rsp, %rdi
callq 0x40d0
testl %eax, %eax
je 0x15762
leaq 0x6698(%rip), %rsi      # 0x1bbb9
movq %rsp, %rdi
callq 0x40d0
testl %eax, %eax
je 0x157f2
movl $0x60, %edi
callq 0x4290
movq %rax, %r15
leaq 0xf7f3(%rip), %rax      # 0x24d38
addq $0x10, %rax
movq %rax, (%r15)
leaq 0x8(%r15), %rdi
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x8(%r15)
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
addq %rsi, %rdx
callq 0x4cf0
movq 0x38(%r14), %rax
movq %rax, 0x38(%r15)
movups 0x28(%r14), %xmm0
movups %xmm0, 0x28(%r15)
leaq 0xf764(%rip), %rax      # 0x24ce8
addq $0x10, %rax
movq %rax, (%r15)
movups 0x40(%r14), %xmm0
movups 0x4c(%r14), %xmm1
movups %xmm0, 0x40(%r15)
movups %xmm1, 0x4c(%r15)
jmp 0x158c5
movq 0xfa15(%rip), %rbx      # 0x24fc0
leaq 0x6aa5(%rip), %rsi      # 0x1c057
movl $0x2b, %edx
movq %rbx, %rdi
callq 0x42f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41b0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x41b0
movq %r12, %rdi
movq %r15, %rsi
callq 0x11f14
testq %rax, %rax
je 0x15868
movq %rax, %r14
movq (%rax), %rax
movq %rsp, %rdi
movq %r14, %rsi
callq *0x18(%rax)
movq 0xf97a(%rip), %rdi      # 0x24fc0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x4270
movsbl %al, %esi
movq 0xf95f(%rip), %rdi      # 0x24fc0
callq 0x4040
movq %rax, %rdi
callq 0x41b0
movq %rax, %r12
leaq 0x6a0b(%rip), %rsi      # 0x1c083
movl $0x7, %edx
movq %rax, %rdi
callq 0x42f0
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %r12, %rdi
callq 0x42f0
movq %rax, %r15
leaq 0x69ed(%rip), %rsi      # 0x1c08b
movl $0x2, %edx
movq %rax, %rdi
callq 0x42f0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r15, %rdi
callq 0x42f0
leaq 0x5f08(%rip), %rsi      # 0x1b5cb
movl $0x1, %edx
movq %rax, %rdi
callq 0x42f0
leaq 0x656b(%rip), %rsi      # 0x1bc42
movq %rsp, %rdi
callq 0x40d0
testl %eax, %eax
je 0x158fe
leaq 0x655d(%rip), %rsi      # 0x1bc4b
movq %rsp, %rdi
callq 0x40d0
testl %eax, %eax
je 0x1595f
movl $0x58, %edi
callq 0x4290
movq %rax, %r15
leaq 0xf176(%rip), %rax      # 0x24888
addq $0x10, %rax
movq %rax, (%r15)
movsd 0x8(%r14), %xmm0
movsd %xmm0, 0x8(%r15)
leaq 0x10(%r15), %rdi
movq %r15, %rax
addq $0x20, %rax
movq %rax, 0x10(%r15)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
addq %rsi, %rdx
callq 0x4cf0
movl 0x40(%r14), %eax
movl %eax, 0x40(%r15)
movups 0x30(%r14), %xmm0
movups %xmm0, 0x30(%r15)
leaq 0xf08b(%rip), %rax      # 0x247e8
jmp 0x159be
movl $0x48, %edi
callq 0x4290
movq %rax, %r15
leaq 0xf5c2(%rip), %rax      # 0x24d38
addq $0x10, %rax
movq %rax, (%r15)
leaq 0x8(%r15), %rdi
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x8(%r15)
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
addq %rsi, %rdx
callq 0x4cf0
movq 0x38(%r14), %rax
movq %rax, 0x38(%r15)
movups 0x28(%r14), %xmm0
movups %xmm0, 0x28(%r15)
leaq 0xf493(%rip), %rax      # 0x24c48
addq $0x10, %rax
movq %rax, (%r15)
movl 0x40(%r14), %eax
movl %eax, 0x40(%r15)
movb 0x44(%r14), %al
movb %al, 0x44(%r15)
movq %r15, %rdi
callq 0x19d34
movq 0x48(%rbx), %rcx
movq 0x50(%rbx), %rdx
testb %al, %al
je 0x158b5
orq %rdx, %rcx
jne 0x158c5
jmp 0x158c9
movl $0x48, %edi
callq 0x4290
movq %rax, %r15
leaq 0xf532(%rip), %rax      # 0x24d38
addq $0x10, %rax
movq %rax, (%r15)
leaq 0x8(%r15), %rdi
movq %r15, %rax
addq $0x18, %rax
movq %rax, 0x8(%r15)
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
addq %rsi, %rdx
callq 0x4cf0
movq 0x38(%r14), %rax
movq %rax, 0x38(%r15)
movups 0x28(%r14), %xmm0
movups %xmm0, 0x28(%r15)
leaq 0xf453(%rip), %rax      # 0x24c98
addq $0x10, %rax
movq %rax, (%r15)
movsd 0x40(%r14), %xmm0
movsd %xmm0, 0x40(%r15)
cmpq $0x0, 0x58(%rbx)
jne 0x158c5
movq %r15, 0x58(%rbx)
movb $0x1, %bpl
jmp 0x158d7
movq 0xf751(%rip), %rbx      # 0x24fc0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41b0
movq %rax, %rbx
leaq 0x67ee(%rip), %rsi      # 0x1c08e
movl $0x24, %edx
movq %rax, %rdi
callq 0x42f0
movq (%rbx), %rax
jmp 0x155ec
testq %rdx, %rdx
setne %al
testq %rcx, %rcx
setne %cl
testb %al, %cl
je 0x158c9
xorl %ebp, %ebp
jmp 0x158d7
movb $0x1, %bpl
movq %rbx, %rdi
movq %r15, %rsi
callq 0x14a34
testb %bpl, %bpl
jne 0x158eb
leaq 0xc0(%rbx), %rdi
movq %r15, %rsi
callq 0xea48
movl 0xac(%rbx), %ebp
movq %r14, %rdi
callq 0x1a73c
jmp 0x15a19
movl $0x58, %edi
callq 0x4290
movq %rax, %r15
leaq 0xef76(%rip), %rax      # 0x24888
addq $0x10, %rax
movq %rax, (%r15)
movsd 0x8(%r14), %xmm0
movsd %xmm0, 0x8(%r15)
leaq 0x10(%r15), %rdi
movq %r15, %rax
addq $0x20, %rax
movq %rax, 0x10(%r15)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
addq %rsi, %rdx
callq 0x4cf0
movl 0x40(%r14), %eax
movl %eax, 0x40(%r15)
movups 0x30(%r14), %xmm0
movups %xmm0, 0x30(%r15)
leaq 0xee3b(%rip), %rax      # 0x24798
jmp 0x159be
movl $0x58, %edi
callq 0x4290
movq %rax, %r15
leaq 0xef15(%rip), %rax      # 0x24888
addq $0x10, %rax
movq %rax, (%r15)
movsd 0x8(%r14), %xmm0
movsd %xmm0, 0x8(%r15)
leaq 0x10(%r15), %rdi
movq %r15, %rax
addq $0x20, %rax
movq %rax, 0x10(%r15)
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
addq %rsi, %rdx
callq 0x4cf0
movl 0x40(%r14), %eax
movl %eax, 0x40(%r15)
movups 0x30(%r14), %xmm0
movups %xmm0, 0x30(%r15)
leaq 0xee7a(%rip), %rax      # 0x24838
addq $0x10, %rax
movq %rax, (%r15)
movl 0x50(%r14), %eax
movl %eax, 0x50(%r15)
movq 0x48(%r14), %rax
movq %rax, 0x48(%r15)
cmpq $0x0, 0x70(%rbx)
je 0x159ed
leaq 0xc0(%rbx), %rdi
movq %r15, %rsi
callq 0xeacc
jmp 0x15a0b
movl $0x18, %edi
callq 0x4290
leaq 0x60(%rbx), %rsi
movq %r15, 0x10(%rax)
movq %rax, %rdi
callq 0x4050
incq 0x70(%rbx)
movl 0xac(%rbx), %ebp
movq %r14, %rdi
callq 0x130ba
subl %eax, %ebp
movl %ebp, 0xac(%rbx)
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x15a3c
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42a0
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x15a4d
jmp 0x15a4d
movq %rax, %rbx
movl $0x58, %esi
jmp 0x15a6b
jmp 0x15a59
movq %rax, %rbx
movl $0x48, %esi
jmp 0x15a6b
movq %rax, %rbx
movl $0x60, %esi
movq %r15, %rdi
callq 0x42a0
jmp 0x15a7c
jmp 0x15a79
jmp 0x15a79
movq %rax, %rbx
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x15a97
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42a0
movq %rbx, %rdi
callq 0x4430
nop