// File: wwdatabase[P]database/./rcdmana/rm.h
// Function: RecordManager::createFile(char const*, int, std::vector<int, std::allocator<int>>, std::vector<int, std::allocator<int>>, std::vector<int, std::allocator<int>>, int)
RC RecordManager::createFile(const char *fileName,
                            int recordSize,
                            vector<int> attrType,
                            vector<int> attrLength,
                            vector<int> isNull,
                            int priKeyNum) {
    if (!pfm->createFile(fileName)){
        cout << "create file error!" << endl;
    }
    cout << ">>> create file " << fileName << " sucessfully." << endl;

    /* init */
    int fileID;
    if (!pfm->openFile(fileName, fileID)){
        cout << "error open file : " << fileName << endl;
        return 103;
    }
    uint b[2048];

    /* set record size. */
    int offset = 0;
    b[offset ++] = recordSize;

    /* set attr num. */
    int attrNum = attrType.size();
    b[offset ++] = attrNum;

    /* set primary key num. */
    b[offset ++] = priKeyNum;

    for (int i = 0; i < attrNum; ++i) {
        b[offset ++] = attrType[i];
        b[offset ++] = attrLength[i];
        b[offset ++] = isNull[i];
    }

    /* set record log number for each page. */
    double par = recordSize + 1.0 / 8; 
    int rcdTop = (PAGE_SIZE - 1) / par;
    if (rcdTop == 0){
        cout << "record size is too large" << endl;
        return 105;
    }
    else
        b[offset++] = rcdTop;

    /* set record tag size with bit map in each page. */
    int tagSize = rcdTop / 8 + 1;
    if (rcdTop * recordSize + tagSize + 3 > PAGE_SIZE) {
        cout << "error attribute tags map !" << endl;
    }
    b[offset++] = tagSize;

    /* set record log number for all pages. */
    int rcdNum = 0;
    b[offset++] = rcdNum;

    /* set bitmap size (devided by 8 by default). */
    b[offset++] = 4;

    /* set page bit map in file, init with 32 pages' map. */
    unsigned int &bitmap = b[offset++];
    bitmap = 0;
    /*for (int i = 0; i < 32; ++i)
        bitmap &= ~(1 << i); // set i bit zero
     */

    if (offset > 2047) {
        cout << "Data Bomb!!!" << endl;
        return 110;
    }

    pfm->writePage(fileID, 0, b, 0);

    /* output file init info */
    RM_FileHandle fileHandle(this->pfm, this->bpm);
    openFile(fileName, fileHandle);
    closeFile(fileHandle);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2150, %rsp           # imm = 0x2150
movq %r9, -0x2140(%rbp)
movq %r8, -0x2138(%rbp)
movq %rcx, -0x2130(%rbp)
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x2128(%rbp)
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x5440
testb $0x1, %al
jne 0x32b7
movq 0xad3c(%rip), %rdi      # 0xdfd8
leaq 0x5e29(%rip), %rsi      # 0x90cc
callq 0x21c0
movq %rax, %rdi
movq 0xad16(%rip), %rsi      # 0xdfc8
callq 0x2200
movq 0xad1a(%rip), %rdi      # 0xdfd8
leaq 0x5e1a(%rip), %rsi      # 0x90df
callq 0x21c0
movq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x21c0
movq %rax, %rdi
leaq 0x5f89(%rip), %rsi      # 0x9269
callq 0x21c0
movq %rax, %rdi
movq 0xacd9(%rip), %rsi      # 0xdfc8
callq 0x2200
movq -0x2128(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
leaq -0x3c(%rbp), %rdx
callq 0x5470
testb $0x1, %al
jne 0x3349
movq 0xacc2(%rip), %rdi      # 0xdfd8
leaq 0x5dd3(%rip), %rsi      # 0x90f0
callq 0x21c0
movq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x21c0
movq %rax, %rdi
movq 0xac90(%rip), %rsi      # 0xdfc8
callq 0x2200
movl $0x67, -0x4(%rbp)
jmp 0x36d4
movq -0x2130(%rbp), %rdi
movl $0x0, -0x2044(%rbp)
movl -0x1c(%rbp), %ecx
movl -0x2044(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2044(%rbp)
cltq
movl %ecx, -0x2040(%rbp,%rax,4)
callq 0x54e0
movl %eax, -0x2048(%rbp)
movl -0x2048(%rbp), %ecx
movl -0x2044(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2044(%rbp)
cltq
movl %ecx, -0x2040(%rbp,%rax,4)
movl 0x10(%rbp), %ecx
movl -0x2044(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2044(%rbp)
cltq
movl %ecx, -0x2040(%rbp,%rax,4)
movl $0x0, -0x204c(%rbp)
movl -0x204c(%rbp), %eax
cmpl -0x2048(%rbp), %eax
jge 0x347c
movq -0x2130(%rbp), %rdi
movslq -0x204c(%rbp), %rsi
callq 0x5500
movq -0x2138(%rbp), %rdi
movl (%rax), %ecx
movl -0x2044(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2044(%rbp)
cltq
movl %ecx, -0x2040(%rbp,%rax,4)
movslq -0x204c(%rbp), %rsi
callq 0x5500
movq -0x2140(%rbp), %rdi
movl (%rax), %ecx
movl -0x2044(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2044(%rbp)
cltq
movl %ecx, -0x2040(%rbp,%rax,4)
movslq -0x204c(%rbp), %rsi
callq 0x5500
movl (%rax), %ecx
movl -0x2044(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2044(%rbp)
cltq
movl %ecx, -0x2040(%rbp,%rax,4)
movl -0x204c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x204c(%rbp)
jmp 0x33c9
cvtsi2sdl -0x1c(%rbp), %xmm0
movsd 0x5b87(%rip), %xmm1     # 0x9010
addsd %xmm1, %xmm0
movsd %xmm0, -0x2058(%rbp)
movsd 0x5b6b(%rip), %xmm0     # 0x9008
divsd -0x2058(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x205c(%rbp)
cmpl $0x0, -0x205c(%rbp)
jne 0x34e6
movq 0xab19(%rip), %rdi      # 0xdfd8
leaq 0x5c3d(%rip), %rsi      # 0x9103
callq 0x21c0
movq %rax, %rdi
movq 0xaaf3(%rip), %rsi      # 0xdfc8
callq 0x2200
movl $0x69, -0x4(%rbp)
jmp 0x36d4
movl -0x205c(%rbp), %ecx
movl -0x2044(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2044(%rbp)
cltq
movl %ecx, -0x2040(%rbp,%rax,4)
movl -0x205c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
addl $0x1, %eax
movl %eax, -0x2060(%rbp)
movl -0x205c(%rbp), %eax
imull -0x1c(%rbp), %eax
addl -0x2060(%rbp), %eax
addl $0x3, %eax
cmpl $0x2000, %eax           # imm = 0x2000
jle 0x3559
movq 0xaa9a(%rip), %rdi      # 0xdfd8
leaq 0x5bd7(%rip), %rsi      # 0x911c
callq 0x21c0
movq %rax, %rdi
movq 0xaa74(%rip), %rsi      # 0xdfc8
callq 0x2200
movl -0x2060(%rbp), %ecx
movl -0x2044(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2044(%rbp)
cltq
movl %ecx, -0x2040(%rbp,%rax,4)
movl $0x0, -0x2064(%rbp)
movl -0x2064(%rbp), %ecx
movl -0x2044(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x2044(%rbp)
cltq
movl %ecx, -0x2040(%rbp,%rax,4)
movl -0x2044(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2044(%rbp)
cltq
movl $0x4, -0x2040(%rbp,%rax,4)
movl -0x2044(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2044(%rbp)
movslq %eax, %rcx
leaq -0x2040(%rbp), %rax
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x2070(%rbp)
movq -0x2070(%rbp), %rax
movl $0x0, (%rax)
cmpl $0x7ff, -0x2044(%rbp)   # imm = 0x7FF
jle 0x3631
movq 0xa9ce(%rip), %rdi      # 0xdfd8
leaq 0x5b26(%rip), %rsi      # 0x9137
callq 0x21c0
movq %rax, %rdi
movq 0xa9a8(%rip), %rsi      # 0xdfc8
callq 0x2200
movl $0x6e, -0x4(%rbp)
jmp 0x36d4
movq -0x2128(%rbp), %rax
movq (%rax), %rdi
movl -0x3c(%rbp), %esi
leaq -0x2040(%rbp), %rcx
xorl %r8d, %r8d
movl %r8d, %edx
callq 0x5520
movq -0x2128(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
leaq -0x2110(%rbp), %rdi
movq %rdi, -0x2148(%rbp)
callq 0x2550
movq -0x2128(%rbp), %rdi
movq -0x2148(%rbp), %rdx
movq -0x18(%rbp), %rsi
callq 0x36f0
jmp 0x368a
movq -0x2128(%rbp), %rdi
leaq -0x2110(%rbp), %rsi
callq 0x3b90
jmp 0x369f
movl $0x0, -0x4(%rbp)
leaq -0x2110(%rbp), %rdi
callq 0x26b0
jmp 0x36d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2118(%rbp)
movl %eax, -0x211c(%rbp)
leaq -0x2110(%rbp), %rdi
callq 0x26b0
jmp 0x36e0
movl -0x4(%rbp), %eax
addq $0x2150, %rsp           # imm = 0x2150
popq %rbp
retq
movq -0x2118(%rbp), %rdi
callq 0x2310
nopl (%rax)