// File: lorenatoscano[P]musify/src/linkedList.cpp
// Function: LinkedList::insertPosition(unsigned long, Song)
void LinkedList::insertPosition(size_t pos, Song value) {
  node* pre = nullptr;
  node* cur = nullptr;

  if (pos == 1) {
    if (size == 0) {
      insertEnd(value);
    } else {
      insertStart(value);
    } 
  } else if (pos <= size) {
    // Evita a adição de músicas repetidas
    if (search(value) != nullptr) {
      cout << "A música já existe no sistema." << endl;
    } else {

      node* temp = new node;
      
      cur = head;
    
      for (size_t i = 1; i < pos; ++i) {
        pre = cur;
        cur = cur->next;
      }

      temp->data = value;
      pre->next = temp;
      temp->next = cur;

      ++size;
    }
  } else {
    insertEnd(value);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdx, -0x158(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x150(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
cmpq $0x1, -0x10(%rbp)
jne 0x3192
movq -0x150(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x3133
movq -0x158(%rbp), %rsi
leaq -0x68(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x3bb0
movq -0x150(%rbp), %rdi
movq -0x160(%rbp), %rsi
callq 0x25f0
jmp 0x310e
leaq -0x68(%rbp), %rdi
callq 0xa9a0
jmp 0x318d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x68(%rbp), %rdi
callq 0xa9a0
jmp 0x3379
movq -0x158(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0x3bb0
movq -0x150(%rbp), %rdi
movq -0x168(%rbp), %rsi
callq 0x2b20
jmp 0x3162
leaq -0xb8(%rbp), %rdi
callq 0xa9a0
jmp 0x318d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0xa9a0
jmp 0x3379
jmp 0x3370
movq -0x150(%rbp), %rcx
movq -0x10(%rbp), %rax
cmpq 0x10(%rcx), %rax
ja 0x3317
movq -0x158(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x3bb0
movq -0x150(%rbp), %rdi
movq -0x178(%rbp), %rsi
callq 0x2c40
movq %rax, -0x170(%rbp)
jmp 0x31dd
leaq -0xf8(%rbp), %rdi
callq 0xa9a0
movq -0x170(%rbp), %rax
cmpq $0x0, %rax
je 0x323a
movq 0xbdd3(%rip), %rdi      # 0xefd0
leaq 0x7e00(%rip), %rsi      # 0xb004
callq 0x2120
movq %rax, %rdi
movq 0xbdad(%rip), %rsi      # 0xefc0
callq 0x2140
jmp 0x3315
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0xa9a0
jmp 0x3379
movl $0x48, %edi
callq 0x2130
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x180(%rbp)
callq 0x3c10
jmp 0x3258
movq -0x150(%rbp), %rax
movq -0x180(%rbp), %rcx
movq %rcx, -0x100(%rbp)
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x1, -0x108(%rbp)
movq -0x108(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x32d1
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x108(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x108(%rbp)
jmp 0x327f
movq -0x180(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
callq 0x2110
jmp 0x3379
movq -0x158(%rbp), %rsi
movq -0x100(%rbp), %rdi
callq 0x3c30
movq -0x150(%rbp), %rax
movq -0x100(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq %rdx, 0x40(%rcx)
movq -0x28(%rbp), %rdx
movq -0x100(%rbp), %rcx
movq %rdx, 0x40(%rcx)
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
jmp 0x336e
movq -0x158(%rbp), %rsi
leaq -0x148(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x3bb0
movq -0x150(%rbp), %rdi
movq -0x188(%rbp), %rsi
callq 0x25f0
jmp 0x3346
leaq -0x148(%rbp), %rdi
callq 0xa9a0
jmp 0x336e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x148(%rbp), %rdi
callq 0xa9a0
jmp 0x3379
jmp 0x3370
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x21e0
nopw %cs:(%rax,%rax)