// File: cpottamus[P]btree_lab/btree.cc
// Function: BTreeIndex::Insert(Block const&, Block const&)
ERROR_T BTreeIndex::Insert(const KEY_T &key, const VALUE_T &value)
{
  //This is really a B+ tree (with optional sequentially linked leaf nodes).
  //This means that on every key you split, you need to include that key AGAIN in it's >= diskblock, so that is eventually included in a leaf node with it's key/value pair.
  //This is also what makes deleting a key a nightmare, since you have to get rid of ALL instances of the ky (including the leaf version with the value), and then rebalance.
  // page 636 in the book has good diagrams of this
  //cout << "Started insert" << endl;

  //======ALGORITHM======
  VALUE_T val;

  //Lookup and attempt to update key
  ERROR_T retCode = LookupOrUpdateInternal(superblock.info.rootnode, BTREE_OP_LOOKUP, key, val);

  //cout << "Finished LookupOrUpdateInternal" << endl;

  switch(retCode) {
    //If there is no error in the update call, end function, declare update successful.
    case ERROR_NOERROR:
    //std::cout << "Key already existed, value updated."<<std::endl;
    return ERROR_INSERT;
    //If the key doesn't exist (as expected), begin insert functionality
    case ERROR_NONEXISTENT:
      //traverse to find the leaf
      //Use a stack of pointers to track the path down to the node where the key would go.
    BTreeNode leafNode;
    BTreeNode rootNode;
    BTreeNode rightLeafNode;
    ERROR_T rc;
    SIZE_T leafPtr;
    SIZE_T rightLeafPtr;

    SIZE_T rootPtr = superblock.info.rootnode;
    rootNode.Unserialize(buffercache, rootPtr);
    initBlock = false;
    if (rootNode.info.numkeys != 0) {
      initBlock = true;
    }
    
    rootNode.Serialize(buffercache, rootPtr);

    //If no keys  existent yet...
    if(!initBlock){
      initBlock=true;
      
        //Allocate a new block, and set the values to the first key spot.
      AllocateNode(leafPtr);
      leafNode = BTreeNode(BTREE_LEAF_NODE, superblock.info.keysize, superblock.info.valuesize, superblock.info.blocksize);
      leafNode.Serialize(buffercache, leafPtr);
      rc = leafNode.Unserialize(buffercache, leafPtr);
      if(rc){ return rc;}

      leafNode.info.numkeys++;
      leafNode.SetKey(0, key);
      leafNode.SetVal(0, value);
      //Re-serialize after the access and write. 
      leafNode.Serialize(buffercache, leafPtr);

      //Connect it to root
      rc = rootNode.Unserialize(buffercache, superblock.info.rootnode);
      if(rc){ return rc;}
      rootNode.info.numkeys++;
      rootNode.SetPtr(0, leafPtr);
      rootNode.SetKey(0, key);

      //Built right node and connect
      AllocateNode(rightLeafPtr);
      rightLeafNode = BTreeNode(BTREE_LEAF_NODE, superblock.info.keysize, superblock.info.valuesize, superblock.info.blocksize);
      rc = rightLeafNode.Serialize(buffercache, rightLeafPtr);
      if(rc){ return rc;}
      rootNode.SetPtr(1, rightLeafPtr);
      rc = rootNode.Serialize(buffercache, superblock.info.rootnode);
      if(rc){ return rc;}
    } else{
      std::vector<SIZE_T> pointerPath;
      pointerPath.push_back(superblock.info.rootnode);
      //cout << "Got to LookupLeaf" << endl;
      LookupLeaf(superblock.info.rootnode, key, pointerPath);
      //cout << "Finished LookupLeaf" << endl;
    //Get the node from the last pointer (which points to the leaf node that the key belongs on)
      leafPtr = pointerPath.back();
      // std::cout<<"WE BUILT THIS CITY ON ROCK AND ROLL SHOEFLY Dont bother me ::: "<<leafPtr<<std::endl;
      // std::cout<<"ALSO THIS ::: "<<pointerPath.size()<<std::endl;
      // for(int i =pointerPath.size()-1; i>=0; i--) {
      //   std::cout<<"Little sumpin"<<pointerPath.at(i)<<std::endl;
      // }
      pointerPath.pop_back();
      //cout << "LeafPtr:" << leafPtr << endl;
      KEY_T testkey;
      KEY_T keySpot;
      VALUE_T valSpot;
      rc = leafNode.Unserialize(buffercache, leafPtr);
      //cout << "Unserialized LeafPtr" << endl;
      //Walk the leaf node
      //Increment the key count for the given node.
      leafNode.info.numkeys++;
        //cout << leafNode.info.numkeys << endl;
      if (leafNode.info.numkeys == 1) {
        rc = leafNode.SetKey(0, key);
        if (rc) { return rc;}
        rc = leafNode.SetVal(0, value);
        if (rc) { return rc;}
      } else {
        bool inserted = false;
        for(SIZE_T offset =0; offset<(int)leafNode.info.numkeys-1; offset++){
          rc = leafNode.GetKey(offset, testkey);
          if (rc) { return rc;}
          if(key < testkey) {
        //Once you've found the spot the key needs to go, move all other keys over by 1
            for(int offset2 = (int)leafNode.info.numkeys-2; offset2 >= (int)offset; offset2--){
          //Grab the old key and value
              rc = leafNode.GetKey((SIZE_T)offset2, keySpot);
              if (rc) { return rc;}
              rc = leafNode.GetVal((SIZE_T)offset2, valSpot);
              if (rc) { return rc;}
         //move it up by 1
              rc = leafNode.SetKey((SIZE_T)offset2+1, keySpot);
              if (rc) { return rc;}
              rc = leafNode.SetVal((SIZE_T)offset2+1, valSpot);
              if (rc) { return rc;}
            }

        //assign the new key to offset
            inserted = true;
            rc = leafNode.SetKey(offset, key);
            if (rc) { return rc;}
            rc = leafNode.SetVal(offset, value);
            if (rc) { return rc;}

            break;
          }
        }
        if (!inserted) {
          rc = leafNode.SetKey(leafNode.info.numkeys - 1, key);
          if (rc) { return rc;}
          rc = leafNode.SetVal(leafNode.info.numkeys - 1, value);
          if (rc) { return rc;}
        }
      }
     //Re-serialize after the access and write. 
      leafNode.Serialize(buffercache, leafPtr); 
    //check if the node length is over 2/3, and call rebalance if necessary
      if((int)leafNode.info.numkeys > (int)(2*maxNumKeys/3)) {
          SIZE_T parentPtr = pointerPath.back();
          pointerPath.pop_back();
        rc = Rebalance(parentPtr, pointerPath);
      }
    }

    /*
    default:
    std::cout << "Unexpected Error on look up. Please perform sanityCheck and diagnose issue."<<std::endl;
    return ERROR_INSANE;
    */ 
  }

  //If it exists, call update on it with the new value


  return ERROR_NOERROR;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x280, %rsp            # imm = 0x280
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x208(%rbp)
leaq -0x48(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x43e0
movq -0x208(%rbp), %rdi
movq -0x200(%rbp), %r8
movq %rdi, %rsi
addq $0x28, %rsi
movq -0x18(%rbp), %rcx
movl $0x3, %edx
callq 0xa490
movl %eax, -0x1f4(%rbp)
jmp 0xa96d
movl -0x1f4(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x20c(%rbp)
subl $-0x9, %eax
je 0xa9b9
jmp 0xa986
movl -0x20c(%rbp), %eax
testl %eax, %eax
je 0xa9a6
jmp 0xb401
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0xb44c
movl $0xfffffff0, -0x4(%rbp) # imm = 0xFFFFFFF0
movl $0x1, -0x60(%rbp)
jmp 0xb437
leaq -0x88(%rbp), %rdi
callq 0xee80
jmp 0xa9c7
leaq -0xb0(%rbp), %rdi
callq 0xee80
jmp 0xa9d5
leaq -0xd8(%rbp), %rdi
callq 0xee80
jmp 0xa9e3
movq -0x208(%rbp), %rax
movl 0x28(%rax), %ecx
movl %ecx, -0xe8(%rbp)
movq 0x8(%rax), %rsi
movl -0xe8(%rbp), %edx
leaq -0xb0(%rbp), %rdi
callq 0xf1a0
jmp 0xaa0b
movq -0x208(%rbp), %rax
movb $0x0, 0x44(%rax)
cmpl $0x0, -0x98(%rbp)
je 0xaa5f
movq -0x208(%rbp), %rax
movb $0x1, 0x44(%rax)
jmp 0xaa5f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0xb429
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0xb41d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0xb411
movq -0x208(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0xe8(%rbp), %edx
leaq -0xb0(%rbp), %rdi
callq 0xf080
jmp 0xaa7e
movq -0x208(%rbp), %rax
testb $0x1, 0x44(%rax)
jne 0xad7d
movq -0x208(%rbp), %rdi
movb $0x1, 0x44(%rdi)
leaq -0xe0(%rbp), %rsi
callq 0x9e90
jmp 0xaaa8
movq -0x208(%rbp), %rax
movl 0x1c(%rax), %edx
movl 0x20(%rax), %ecx
movl 0x24(%rax), %r8d
leaq -0x110(%rbp), %rdi
movl $0x4, %esi
callq 0xeef0
jmp 0xaacc
leaq -0x88(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0xf050
jmp 0xaae1
leaq -0x110(%rbp), %rdi
callq 0xeea0
movq -0x208(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0xe0(%rbp), %edx
leaq -0x88(%rbp), %rdi
callq 0xf080
jmp 0xab0c
movq -0x208(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0xe0(%rbp), %edx
leaq -0x88(%rbp), %rdi
callq 0xf1a0
movl %eax, -0x210(%rbp)
jmp 0xab31
movl -0x210(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xab78
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb3d2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x110(%rbp), %rdi
callq 0xeea0
jmp 0xb411
movl -0x70(%rbp), %eax
incl %eax
movl %eax, -0x70(%rbp)
movq -0x18(%rbp), %rdx
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
callq 0xf780
jmp 0xab94
movq -0x20(%rbp), %rdx
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
callq 0xf840
jmp 0xaba8
movq -0x208(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0xe0(%rbp), %edx
leaq -0x88(%rbp), %rdi
callq 0xf080
jmp 0xabc7
movq -0x208(%rbp), %rax
movq 0x8(%rax), %rsi
movl 0x28(%rax), %edx
leaq -0xb0(%rbp), %rdi
callq 0xf1a0
movl %eax, -0x214(%rbp)
jmp 0xabe9
movl -0x214(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xac13
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb3d2
movl -0x98(%rbp), %eax
incl %eax
movl %eax, -0x98(%rbp)
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
leaq -0xe0(%rbp), %rdx
callq 0xf7f0
jmp 0xac38
movq -0x18(%rbp), %rdx
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
callq 0xf780
jmp 0xac4c
movq -0x208(%rbp), %rdi
leaq -0xe4(%rbp), %rsi
callq 0x9e90
jmp 0xac61
movq -0x208(%rbp), %rax
movl 0x1c(%rax), %edx
movl 0x20(%rax), %ecx
movl 0x24(%rax), %r8d
leaq -0x138(%rbp), %rdi
movl $0x4, %esi
callq 0xeef0
jmp 0xac85
leaq -0xd8(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0xf050
jmp 0xac9a
leaq -0x138(%rbp), %rdi
callq 0xeea0
movq -0x208(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0xe4(%rbp), %edx
leaq -0xd8(%rbp), %rdi
callq 0xf080
movl %eax, -0x218(%rbp)
jmp 0xaccb
movl -0x218(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xad12
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb3d2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x138(%rbp), %rdi
callq 0xeea0
jmp 0xb411
leaq -0xb0(%rbp), %rdi
movl $0x1, %esi
leaq -0xe4(%rbp), %rdx
callq 0xf7f0
jmp 0xad2c
movq -0x208(%rbp), %rax
movq 0x8(%rax), %rsi
movl 0x28(%rax), %edx
leaq -0xb0(%rbp), %rdi
callq 0xf080
movl %eax, -0x21c(%rbp)
jmp 0xad4e
movl -0x21c(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xad78
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb3d2
jmp 0xb3cb
leaq -0x150(%rbp), %rdi
movq %rdi, -0x228(%rbp)
callq 0xdb70
movq -0x208(%rbp), %rsi
movq -0x228(%rbp), %rdi
addq $0x28, %rsi
callq 0xdb90
jmp 0xada9
movq -0x208(%rbp), %rdi
movq %rdi, %rsi
addq $0x28, %rsi
movq -0x18(%rbp), %rdx
leaq -0x150(%rbp), %rcx
callq 0xb460
jmp 0xadc9
leaq -0x150(%rbp), %rdi
movq %rdi, -0x230(%rbp)
callq 0xdc00
movq -0x230(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0xe0(%rbp)
callq 0xdc40
leaq -0x178(%rbp), %rdi
callq 0x43e0
jmp 0xadfe
leaq -0x1a0(%rbp), %rdi
callq 0x43e0
jmp 0xae0c
leaq -0x1c8(%rbp), %rdi
callq 0x43e0
jmp 0xae1a
movq -0x208(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0xe0(%rbp), %edx
leaq -0x88(%rbp), %rdi
callq 0xf1a0
movl %eax, -0x234(%rbp)
jmp 0xae3f
movl -0x234(%rbp), %eax
movl %eax, -0xdc(%rbp)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
cmpl $0x1, -0x70(%rbp)
jne 0xaf2f
movq -0x18(%rbp), %rdx
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
callq 0xf780
movl %eax, -0x238(%rbp)
jmp 0xae78
movl -0x238(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xaee6
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb35e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0xb3bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0xb3b1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0xb3a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0xb399
movq -0x20(%rbp), %rdx
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
callq 0xf840
movl %eax, -0x23c(%rbp)
jmp 0xaf00
movl -0x23c(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xaf2a
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb35e
jmp 0xb278
movb $0x0, -0x1c9(%rbp)
movl $0x0, -0x1d0(%rbp)
movl -0x1d0(%rbp), %eax
movl -0x70(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0xb1d9
movl -0x1d0(%rbp), %esi
leaq -0x88(%rbp), %rdi
leaq -0x178(%rbp), %rdx
callq 0xf5d0
movl %eax, -0x240(%rbp)
jmp 0xaf75
movl -0x240(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xaf9f
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb35e
movq -0x18(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x47c0
movb %al, -0x241(%rbp)
jmp 0xafb7
movb -0x241(%rbp), %al
testb $0x1, %al
jne 0xafc6
jmp 0xb1c3
movl -0x70(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x1d4(%rbp)
movl -0x1d4(%rbp), %eax
cmpl -0x1d0(%rbp), %eax
jl 0xb12a
movl -0x1d4(%rbp), %esi
leaq -0x88(%rbp), %rdi
leaq -0x1a0(%rbp), %rdx
callq 0xf5d0
movl %eax, -0x248(%rbp)
jmp 0xb005
movl -0x248(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xb02f
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb35e
movl -0x1d4(%rbp), %esi
leaq -0x88(%rbp), %rdi
leaq -0x1c8(%rbp), %rdx
callq 0xf6a0
movl %eax, -0x24c(%rbp)
jmp 0xb050
movl -0x24c(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xb07a
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb35e
movl -0x1d4(%rbp), %esi
incl %esi
leaq -0x88(%rbp), %rdi
leaq -0x1a0(%rbp), %rdx
callq 0xf780
movl %eax, -0x250(%rbp)
jmp 0xb09d
movl -0x250(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xb0c7
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb35e
movl -0x1d4(%rbp), %esi
incl %esi
leaq -0x88(%rbp), %rdi
leaq -0x1c8(%rbp), %rdx
callq 0xf840
movl %eax, -0x254(%rbp)
jmp 0xb0ea
movl -0x254(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xb114
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb35e
jmp 0xb116
movl -0x1d4(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1d4(%rbp)
jmp 0xafd2
movb $0x1, -0x1c9(%rbp)
movl -0x1d0(%rbp), %esi
movq -0x18(%rbp), %rdx
leaq -0x88(%rbp), %rdi
callq 0xf780
movl %eax, -0x258(%rbp)
jmp 0xb14f
movl -0x258(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xb179
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb35e
movl -0x1d0(%rbp), %esi
movq -0x20(%rbp), %rdx
leaq -0x88(%rbp), %rdi
callq 0xf840
movl %eax, -0x25c(%rbp)
jmp 0xb197
movl -0x25c(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xb1c1
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb35e
jmp 0xb1d9
jmp 0xb1c5
movl -0x1d0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d0(%rbp)
jmp 0xaf40
testb $0x1, -0x1c9(%rbp)
jne 0xb276
movl -0x70(%rbp), %esi
decl %esi
movq -0x18(%rbp), %rdx
leaq -0x88(%rbp), %rdi
callq 0xf780
movl %eax, -0x260(%rbp)
jmp 0xb203
movl -0x260(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xb22d
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb35e
movl -0x70(%rbp), %esi
decl %esi
movq -0x20(%rbp), %rdx
leaq -0x88(%rbp), %rdi
callq 0xf840
movl %eax, -0x264(%rbp)
jmp 0xb24a
movl -0x264(%rbp), %eax
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
je 0xb274
movl -0xdc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb35e
jmp 0xb276
jmp 0xb278
movq -0x208(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0xe0(%rbp), %edx
leaq -0x88(%rbp), %rdi
callq 0xf080
jmp 0xb297
movq -0x208(%rbp), %rax
movl -0x70(%rbp), %ecx
movl %ecx, -0x268(%rbp)
movl 0x40(%rax), %eax
shll %eax
movl $0x3, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
movl -0x268(%rbp), %eax
cmpl %ecx, %eax
jle 0xb357
leaq -0x150(%rbp), %rdi
movq %rdi, -0x270(%rbp)
callq 0xdc00
movq -0x270(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x1d8(%rbp)
callq 0xdc40
movq -0x270(%rbp), %rsi
leaq -0x1f0(%rbp), %rdi
callq 0xdc70
jmp 0xb301
movq -0x208(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
leaq -0x1f0(%rbp), %rdx
callq 0xb760
movl %eax, -0x274(%rbp)
jmp 0xb323
movl -0x274(%rbp), %eax
movl %eax, -0xdc(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0xdd60
jmp 0xb357
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0xdd60
jmp 0xb399
movl $0x0, -0x60(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0x46e0
leaq -0x1a0(%rbp), %rdi
callq 0x46e0
leaq -0x178(%rbp), %rdi
callq 0x46e0
leaq -0x150(%rbp), %rdi
callq 0xdd60
movl -0x60(%rbp), %eax
testl %eax, %eax
jne 0xb3d2
jmp 0xb397
jmp 0xb3cb
leaq -0x1c8(%rbp), %rdi
callq 0x46e0
leaq -0x1a0(%rbp), %rdi
callq 0x46e0
leaq -0x178(%rbp), %rdi
callq 0x46e0
leaq -0x150(%rbp), %rdi
callq 0xdd60
jmp 0xb411
movl $0x0, -0x60(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0xeea0
leaq -0xb0(%rbp), %rdi
callq 0xeea0
leaq -0x88(%rbp), %rdi
callq 0xeea0
movl -0x60(%rbp), %eax
testl %eax, %eax
jne 0xb437
jmp 0xb3ff
jmp 0xb401
movl $0x0, -0x4(%rbp)
movl $0x1, -0x60(%rbp)
jmp 0xb437
leaq -0xd8(%rbp), %rdi
callq 0xeea0
leaq -0xb0(%rbp), %rdi
callq 0xeea0
leaq -0x88(%rbp), %rdi
callq 0xeea0
jmp 0xb44c
leaq -0x48(%rbp), %rdi
callq 0x46e0
movl -0x4(%rbp), %eax
addq $0x280, %rsp            # imm = 0x280
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x46e0
movq -0x58(%rbp), %rdi
callq 0x3400
nop