// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20c8, %rsp           # imm = 0x20C8
movq %r9, %r13
movq %r8, %r15
movq %rcx, %r14
movq %rdx, -0x48(%rbp)
movq %rsi, %rbx
movq %rdi, %r12
movq (%rdi), %rdi
callq 0x469c
movq 0x540a(%rip), %rdi      # 0x7fd8
leaq 0x24db(%rip), %rsi      # 0x50b0
callq 0x2170
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2170
leaq 0x264e(%rip), %rsi      # 0x523a
movq %rax, %rdi
callq 0x2170
movq %rax, %rdi
callq 0x20b0
movq %r12, -0x40(%rbp)
movq (%r12), %rdi
movq %rbx, %r12
leaq -0x2c(%rbp), %rdx
movq %rbx, %rsi
callq 0x3e32
testb %al, %al
je 0x2c80
movl 0x10(%rbp), %esi
leaq -0x20f0(%rbp), %rcx
movq -0x48(%rbp), %rbx
movl %ebx, (%rcx)
movq (%r14), %rax
movq 0x8(%r14), %rdx
subq %rax, %rdx
shrq $0x2, %rdx
movl %edx, 0x4(%rcx)
movl %esi, 0x8(%rcx)
xorl %edi, %edi
testl %edx, %edx
cmovlel %edi, %edx
leaq (%rdx,%rdx,2), %rdx
xorl %r14d, %r14d
cmpq %r14, %rdx
je 0x2cad
movl (%rax,%rdi), %esi
movl %esi, -0x20e4(%rbp,%r14,4)
movq (%r15), %rsi
movl (%rsi,%rdi), %esi
movl %esi, -0x20e0(%rbp,%r14,4)
movq (%r13), %rsi
movl (%rsi,%rdi), %esi
movl %esi, -0x20dc(%rbp,%r14,4)
addq $0x3, %r14
addq $0x4, %rdi
jmp 0x2c49
movq 0x5351(%rip), %rdi      # 0x7fd8
leaq 0x2433(%rip), %rsi      # 0x50c1
callq 0x2170
movq %rax, %rdi
movq %r12, %rsi
callq 0x2170
movq %rax, %rdi
callq 0x20b0
pushq $0x67
jmp 0x2d9f
cvtsi2sd %ebx, %xmm0
addsd 0x234f(%rip), %xmm0     # 0x5008
movsd 0x234f(%rip), %xmm1     # 0x5010
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
testl %edi, %edi
je 0x2d82
movq %r12, -0x38(%rbp)
leaq 0x3(%r14), %r15
leaq 0x4(%r14), %r13
movl %r15d, %eax
movl %edi, -0x20f0(%rbp,%rax,4)
pushq $0x8
popq %rsi
movl %edi, %eax
cltd
idivl %esi
imull %edi, %ebx
leal (%rax,%rbx), %edx
incl %edx
movl %eax, %r12d
incl %r12d
cmpl $0x1ffe, %edx           # imm = 0x1FFE
jl 0x2d27
movq 0x52cc(%rip), %rdi      # 0x7fd8
leaq 0x23da(%rip), %rsi      # 0x50ed
callq 0x2170
movq %rax, %rdi
callq 0x20b0
leaq -0x20f0(%rbp), %rcx
leal 0x5(%r14), %eax
movl %r13d, %edx
movl %r12d, -0x20f0(%rbp,%rdx,4)
andl $0x0, -0x20f0(%rbp,%rax,4)
leal 0x6(%r14), %eax
addl $0x7, %r14d
movl $0x4, -0x20f0(%rbp,%rax,4)
andl $0x0, -0x20f0(%rbp,%r14,4)
cmpq $0x7fb, %r15            # imm = 0x7FB
jb 0x2db2
movq 0x526e(%rip), %rdi      # 0x7fd8
leaq 0x2397(%rip), %rsi      # 0x5108
callq 0x2170
movq %rax, %rdi
callq 0x20b0
pushq $0x6e
jmp 0x2d9f
movq 0x524f(%rip), %rdi      # 0x7fd8
leaq 0x2344(%rip), %rsi      # 0x50d4
callq 0x2170
movq %rax, %rdi
callq 0x20b0
pushq $0x69
popq %rax
addq $0x20c8, %rsp           # imm = 0x20C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x40(%rbp), %r15
movq (%r15), %rdi
movl -0x2c(%rbp), %esi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x3e7e
movq (%r15), %rsi
movq 0x8(%r15), %rdx
leaq -0xe8(%rbp), %r14
movq %r14, %rdi
callq 0x2472
movq %r15, %rdi
movq -0x38(%rbp), %rsi
movq %r14, %rdx
callq 0x2e22
leaq -0xe8(%rbp), %rsi
movq %r15, %rdi
callq 0x3148
leaq -0xe8(%rbp), %rdi
callq 0x2516
xorl %eax, %eax
jmp 0x2da0
movq %rax, %rbx
leaq -0xe8(%rbp), %rdi
callq 0x2516
movq %rbx, %rdi
callq 0x2270
nop