// File: wwdatabase[P]database/./rcdmana/rm_filescan.h
// Function: RM_FileScan::getNextRec(RM_Record&)
RC RM_FileScan::getNextRec(RM_Record &rec) {
    cout << "Get record" << endl;
  char data[this->fh->recordSize];
  bool isSelected = false;
  bool rid_error = false;
  do {
    if (this->recordScanNum >= this->fh->recordNumForAllPages) {
        cout << "File EOF" << endl;
        this->record.clean();
        break;
    }

    while (this->fh->getRec(rid, this->record) == 108) {
        cout << "Unused record, ignore it." << endl;
        incRID();
    }

    char *td = data;
    this->record.getData(td);
    char comValue[this->attrLength];
    strncpy(comValue, data+this->attrOffset, this->attrLength);
    if (attrType == ATTRINT) {
      int *intValue = (int*)(comValue);
      // int c = 40100200;
      // int *intValue = &c;
      int *cmpValue = (int*)(value);
      cout << *intValue << " : " << *cmpValue << endl;
      switch (this->compOp) {
          case EQ_OP:
              if ((*intValue) == (*cmpValue))
                  isSelected = true;
              cout << isSelected << endl;
              break;
          case LT_OP:
              if (*intValue < *cmpValue)
                  isSelected = true;
              break;
          case GT_OP:
              if (*intValue > *cmpValue)
                  isSelected = true;
              break;
          case LE_OP:
              if (*intValue <= *cmpValue)
                  isSelected = true;
              break;
          case GE_OP:
              if (*intValue >= *cmpValue)
                  isSelected = true;
              break;
          case NE_OP:
              if (*intValue != *cmpValue)
                  isSelected = true;
              break;
          case NO_OP:
              /* No comparision (when value is a null pointer) */
              isSelected = true;
              break;
          default:
              cout << "compOp Error at compare INT" << endl;
              break;
      }
    }
    else if (attrType == ATTRFLOAT) {
      float *floatValue = (float*)(comValue);
      float *cmpValue = (float*)(value);
      switch (this->compOp) {
          case EQ_OP:
              if (*floatValue == *cmpValue)
                  isSelected = true;
              break;
          case LT_OP:
              if (*floatValue < *cmpValue)
                  isSelected = true;
              break;
          case GT_OP:
              if (*floatValue > *cmpValue)
                  isSelected = true;
              break;
          case LE_OP:
              if (*floatValue <= *cmpValue)
                  isSelected = true;
              break;
          case GE_OP:
              if (*floatValue >= *cmpValue)
                  isSelected = true;
              break;
          case NE_OP:
              if (*floatValue != *cmpValue)
                  isSelected = true;
              break;
          case NO_OP:
              /* No comparision (when value is a null pointer) */
              isSelected = true;
              break;
          default:
              cout << "Compop error at compare FLOAT" << endl;
              break;
      }
    }
    else if (attrType == ATTRSTRING) {
      isSelected = true;
      char *strValue = (char*)(comValue);
      char* cmpValue = (char*)(value);
      switch (this->compOp) {
          case EQ_OP:
              if (strncmp(strValue, cmpValue, this->attrLength) == 0)
                  isSelected = true;
              break;
          case LT_OP:
              if (strncmp(strValue, cmpValue, this->attrLength) < 0)
                  isSelected = true;
              break;
          case GT_OP:
              if (strncmp(strValue, cmpValue, this->attrLength) > 0)
                  isSelected = true;
              break;
          case LE_OP:
              if (strncmp(strValue, cmpValue, this->attrLength) <= 0)
                  isSelected = true;
              break;
          case GE_OP:
              if (strncmp(strValue, cmpValue, this->attrLength) >= 0)
                  isSelected = true;
              break;
          case NE_OP:
              if (strncmp(strValue, cmpValue, this->attrLength) != 0)
                  isSelected = true;
              break;
          case NO_OP:
              /* No comparision (when value is a null pointer) */
              isSelected = true;
              break;
          default:
              cout << "Compop error at compare FLOAT" << endl;
              break;
      }
    }

    incRID();
    ++recordScanNum;
  } while ((value != NULL) && !isSelected);

  rec = this->record;
  // rec.setData(this->record.data);
  // rec.rid = this->record.rid;
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0x9f46(%rip), %rdi      # 0xdfd8
leaq 0x5231(%rip), %rsi      # 0x92ca
callq 0x21c0
movq %rax, %rdi
movq 0x9f20(%rip), %rsi      # 0xdfc8
callq 0x2200
movq -0x80(%rbp), %rax
movq (%rax), %rax
movl 0xc(%rax), %eax
movq %rsp, %rcx
movq %rcx, -0x18(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x78(%rbp)
movq %rcx, %rsp
movq %rax, -0x20(%rbp)
movb $0x0, -0x21(%rbp)
movb $0x0, -0x22(%rbp)
movq -0x80(%rbp), %rcx
movl 0x30(%rcx), %eax
movq (%rcx), %rcx
cmpl 0x68(%rcx), %eax
jl 0x4125
movq 0x9ee0(%rip), %rdi      # 0xdfd8
leaq 0x51d6(%rip), %rsi      # 0x92d5
callq 0x21c0
movq %rax, %rdi
movq 0x9eba(%rip), %rsi      # 0xdfc8
callq 0x2200
movq -0x80(%rbp), %rdi
addq $0x20, %rdi
callq 0x58d0
jmp 0x45df
jmp 0x4127
movq -0x80(%rbp), %rdx
movq (%rdx), %rdi
movq %rdx, %rsi
addq $0x34, %rsi
addq $0x20, %rdx
callq 0x2730
cmpl $0x6c, %eax
jne 0x4170
movq 0x9e8e(%rip), %rdi      # 0xdfd8
leaq 0x518d(%rip), %rsi      # 0x92de
callq 0x21c0
movq %rax, %rdi
movq 0x9e68(%rip), %rsi      # 0xdfc8
callq 0x2200
movq -0x80(%rbp), %rdi
callq 0x4600
jmp 0x4127
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rax
movq %rax, -0x30(%rbp)
addq $0x20, %rdi
movq -0x30(%rbp), %rsi
callq 0x5120
movq -0x78(%rbp), %rsi
movq -0x80(%rbp), %rax
movl 0xc(%rax), %ecx
movq %rsp, %rdx
movq %rdx, -0x38(%rbp)
movq %rcx, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rdi
subq %rdx, %rdi
movq %rdi, -0x88(%rbp)
movq %rdi, %rsp
movq %rcx, -0x40(%rbp)
movslq 0x10(%rax), %rcx
addq %rcx, %rsi
movslq 0xc(%rax), %rdx
callq 0x20e0
movq -0x80(%rbp), %rax
cmpl $0x1, 0x8(%rax)
jne 0x432a
movq -0x80(%rbp), %rax
movq -0x88(%rbp), %rcx
movq %rcx, -0x48(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movl (%rax), %esi
movq 0x9ddc(%rip), %rdi      # 0xdfd8
callq 0x2300
movq %rax, %rdi
leaq 0x4fda(%rip), %rsi      # 0x91e5
callq 0x21c0
movq %rax, %rdi
movq -0x50(%rbp), %rax
movl (%rax), %esi
callq 0x2300
movq %rax, %rdi
movq 0x9da0(%rip), %rsi      # 0xdfc8
callq 0x2200
movq -0x80(%rbp), %rax
movl 0x14(%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x90(%rbp)
subl $0x6, %eax
ja 0x4303
movq -0x90(%rbp), %rax
leaq 0x4dfa(%rip), %rcx      # 0x9050
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x48(%rbp), %rax
movl (%rax), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x4271
movb $0x1, -0x21(%rbp)
movb -0x21(%rbp), %al
movq 0x9d5d(%rip), %rdi      # 0xdfd8
andb $0x1, %al
movzbl %al, %esi
callq 0x2340
movq %rax, %rdi
movq 0x9d39(%rip), %rsi      # 0xdfc8
callq 0x2200
jmp 0x4325
movq -0x48(%rbp), %rax
movl (%rax), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x42ab
movb $0x1, -0x21(%rbp)
jmp 0x4325
movq -0x48(%rbp), %rax
movl (%rax), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
jle 0x42bf
movb $0x1, -0x21(%rbp)
jmp 0x4325
movq -0x48(%rbp), %rax
movl (%rax), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
jg 0x42d3
movb $0x1, -0x21(%rbp)
jmp 0x4325
movq -0x48(%rbp), %rax
movl (%rax), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
jl 0x42e7
movb $0x1, -0x21(%rbp)
jmp 0x4325
movq -0x48(%rbp), %rax
movl (%rax), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
je 0x42fb
movb $0x1, -0x21(%rbp)
jmp 0x4325
movb $0x1, -0x21(%rbp)
jmp 0x4325
movq 0x9cce(%rip), %rdi      # 0xdfd8
leaq 0x4fe7(%rip), %rsi      # 0x92f8
callq 0x21c0
movq %rax, %rdi
movq 0x9ca8(%rip), %rsi      # 0xdfc8
callq 0x2200
jmp 0x4596
movq -0x80(%rbp), %rax
cmpl $0x2, 0x8(%rax)
jne 0x4448
movq -0x80(%rbp), %rax
movq -0x88(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movq 0x18(%rax), %rcx
movq %rcx, -0x60(%rbp)
movl 0x14(%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x98(%rbp)
subl $0x6, %eax
ja 0x4421
movq -0x98(%rbp), %rax
leaq 0x4cc0(%rip), %rcx      # 0x9034
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x58(%rbp), %rax
movss (%rax), %xmm0
movq -0x60(%rbp), %rax
ucomiss (%rax), %xmm0
jne 0x4394
jp 0x4394
movb $0x1, -0x21(%rbp)
jmp 0x4443
movq -0x58(%rbp), %rax
movss (%rax), %xmm1
movq -0x60(%rbp), %rax
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x43b2
movb $0x1, -0x21(%rbp)
jmp 0x4443
movq -0x58(%rbp), %rax
movss (%rax), %xmm0
movq -0x60(%rbp), %rax
ucomiss (%rax), %xmm0
jbe 0x43cc
movb $0x1, -0x21(%rbp)
jmp 0x4443
movq -0x58(%rbp), %rax
movss (%rax), %xmm1
movq -0x60(%rbp), %rax
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
jb 0x43e7
movb $0x1, -0x21(%rbp)
jmp 0x4443
movq -0x58(%rbp), %rax
movss (%rax), %xmm0
movq -0x60(%rbp), %rax
ucomiss (%rax), %xmm0
jb 0x43fe
movb $0x1, -0x21(%rbp)
jmp 0x4443
movq -0x58(%rbp), %rax
movss (%rax), %xmm0
movq -0x60(%rbp), %rax
ucomiss (%rax), %xmm0
jne 0x4415
jp 0x4415
jmp 0x4419
movb $0x1, -0x21(%rbp)
jmp 0x4443
movb $0x1, -0x21(%rbp)
jmp 0x4443
movq 0x9bb0(%rip), %rdi      # 0xdfd8
leaq 0x4ee5(%rip), %rsi      # 0x9314
callq 0x21c0
movq %rax, %rdi
movq 0x9b8a(%rip), %rsi      # 0xdfc8
callq 0x2200
jmp 0x4594
movq -0x80(%rbp), %rax
cmpl $0x3, 0x8(%rax)
jne 0x4592
movq -0x80(%rbp), %rax
movq -0x88(%rbp), %rcx
movb $0x1, -0x21(%rbp)
movq %rcx, -0x68(%rbp)
movq 0x18(%rax), %rcx
movq %rcx, -0x70(%rbp)
movl 0x14(%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0xa0(%rbp)
subl $0x6, %eax
ja 0x456e
movq -0xa0(%rbp), %rax
leaq 0x4b82(%rip), %rcx      # 0x9018
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x80(%rbp), %rax
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
movslq 0xc(%rax), %rdx
callq 0x20d0
cmpl $0x0, %eax
jne 0x44bd
movb $0x1, -0x21(%rbp)
jmp 0x4590
movq -0x80(%rbp), %rax
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
movslq 0xc(%rax), %rdx
callq 0x20d0
cmpl $0x0, %eax
jge 0x44e0
movb $0x1, -0x21(%rbp)
jmp 0x4590
movq -0x80(%rbp), %rax
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
movslq 0xc(%rax), %rdx
callq 0x20d0
cmpl $0x0, %eax
jle 0x4503
movb $0x1, -0x21(%rbp)
jmp 0x4590
movq -0x80(%rbp), %rax
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
movslq 0xc(%rax), %rdx
callq 0x20d0
cmpl $0x0, %eax
jg 0x4526
movb $0x1, -0x21(%rbp)
jmp 0x4590
movq -0x80(%rbp), %rax
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
movslq 0xc(%rax), %rdx
callq 0x20d0
cmpl $0x0, %eax
jl 0x4546
movb $0x1, -0x21(%rbp)
jmp 0x4590
movq -0x80(%rbp), %rax
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
movslq 0xc(%rax), %rdx
callq 0x20d0
cmpl $0x0, %eax
je 0x4566
movb $0x1, -0x21(%rbp)
jmp 0x4590
movb $0x1, -0x21(%rbp)
jmp 0x4590
movq 0x9a63(%rip), %rdi      # 0xdfd8
leaq 0x4d98(%rip), %rsi      # 0x9314
callq 0x21c0
movq %rax, %rdi
movq 0x9a3d(%rip), %rsi      # 0xdfc8
callq 0x2200
jmp 0x4592
jmp 0x4594
jmp 0x4596
movq -0x80(%rbp), %rdi
callq 0x4600
movq -0x80(%rbp), %rax
movl 0x30(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x30(%rax)
movq -0x38(%rbp), %rax
movq %rax, %rsp
movq -0x80(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x18(%rcx)
movb %al, -0xa1(%rbp)
je 0x45d1
movb -0x21(%rbp), %al
xorb $-0x1, %al
movb %al, -0xa1(%rbp)
movb -0xa1(%rbp), %al
testb $0x1, %al
jne 0x40e2
movq -0x80(%rbp), %rsi
addq $0x20, %rsi
movq -0x10(%rbp), %rdi
callq 0x5920
movq -0x18(%rbp), %rax
movq %rax, %rsp
xorl %eax, %eax
movq %rbp, %rsp
popq %rbp
retq
nop