// File: MITRECND[P]snugglefish/src/indexSet.cpp
// Function: snugglefish::indexSet::create(unsigned int)
void indexSet::create(ngram_t_numfiles nFiles){
    if (indexFile || nGramFile){//Already opened?
        throw runtime_error("Error opening index or ngram file");
    }

    //Create the Files
    //First create the filenames
    string indexFileName, nGramFileName;
    indexFileName = nGramFileName = this->fileBase;
    char number_string[FILE_NUM_BUFFER_SIZE];
    snprintf(number_string, FILE_NUM_BUFFER_SIZE, FILE_NUM_SPRINTF_STRING, this->count);

    indexFileName.append(INDEX_FILE_EXTENSION).append(number_string);
    nGramFileName.append(NGRAM_FILE_EXTENSION).append(number_string);

    this->indexFile = new file(indexFileName.c_str());
    this->nGramFile = new file(nGramFileName.c_str());

    if(indexFile->exists() || nGramFile->exists()){ //Already exist?
       throw runtime_error("index file or ngram file already exists"); 
    }

    indexFile->create();
    nGramFile->create();

    
    this->endian_check = ENDIAN_CHECK;
    this->version = VERSION;

    //Write standard header
    indexFile->write((uint8_t*) (&(this->endian_check)), ENDIAN_CHECK_FIELD);
    indexFile->write((uint8_t*) (&(this->version)), VERSION_FIELD);
    indexFile->write((uint8_t*) (&(this->ngramLength)), NGRAM_SIZE_FIELD);
    indexFile->write((uint8_t*) (&nFiles), INDEX_HEADER_NUM_FILES_FIELD);

    offset = 0; //Offset in the ngramfile

    writable = true;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, (%rax)
jne 0x188e1
movq -0x88(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1893d
movl $0x10, %edi
callq 0xe170
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x90(%rbp)
leaq 0x1f85(%rip), %rsi      # 0x1a884
callq 0xe100
jmp 0x18906
movq -0x90(%rbp), %rdi
movq 0x86dc(%rip), %rsi      # 0x20ff0
movq 0x868d(%rip), %rdx      # 0x20fa8
callq 0xe5b0
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0xe230
jmp 0x18c62
leaq -0x40(%rbp), %rdi
callq 0xe470
leaq -0x60(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
callq 0xe470
movq -0x88(%rbp), %rsi
movq -0xa0(%rbp), %rdi
addq $0x28, %rsi
callq 0xe040
movq %rax, -0x98(%rbp)
jmp 0x18976
movq -0x98(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0xe040
jmp 0x18988
movq -0x88(%rbp), %rax
movl 0x58(%rax), %ecx
leaq 0x1f0d(%rip), %rdx      # 0x1a8a6
xorl %eax, %eax
leaq -0x80(%rbp), %rdi
movl $0x1e, %esi
callq 0xe520
leaq 0x1d43(%rip), %rsi      # 0x1a6f3
leaq -0x40(%rbp), %rdi
callq 0xe640
movq %rax, -0xa8(%rbp)
jmp 0x189c2
movq -0xa8(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0xe640
jmp 0x189d4
leaq 0x1d11(%rip), %rsi      # 0x1a6ec
leaq -0x60(%rbp), %rdi
callq 0xe640
movq %rax, -0xb0(%rbp)
jmp 0x189ed
movq -0xb0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0xe640
jmp 0x189ff
movl $0x40, %edi
callq 0xe300
movq %rax, -0xb8(%rbp)
jmp 0x18a12
leaq -0x40(%rbp), %rdi
callq 0xe120
movq -0xb8(%rbp), %rdi
movq %rax, %rsi
movl $0x10000000, %edx       # imm = 0x10000000
callq 0x17aa0
jmp 0x18a31
movq -0x88(%rbp), %rax
movq -0xb8(%rbp), %rcx
movq %rcx, (%rax)
movl $0x40, %edi
callq 0xe300
movq %rax, -0xc0(%rbp)
jmp 0x18a55
leaq -0x60(%rbp), %rdi
callq 0xe120
movq -0xc0(%rbp), %rdi
movq %rax, %rsi
movl $0x10000000, %edx       # imm = 0x10000000
callq 0x17aa0
jmp 0x18a74
movq -0x88(%rbp), %rax
movq -0xc0(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq (%rax), %rdi
callq 0x18650
movb %al, -0xc1(%rbp)
jmp 0x18a96
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0x18ac9
jmp 0x18aa2
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x18650
movb %al, -0xc2(%rbp)
jmp 0x18aba
movb -0xc2(%rbp), %al
testb $0x1, %al
jne 0x18ac9
jmp 0x18b7f
movl $0x10, %edi
callq 0xe170
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xd0(%rbp)
leaq 0x1dc4(%rip), %rsi      # 0x1a8ab
callq 0xe100
jmp 0x18aee
movq -0xd0(%rbp), %rdi
movq 0x84f4(%rip), %rsi      # 0x20ff0
movq 0x84a5(%rip), %rdx      # 0x20fa8
callq 0xe5b0
jmp 0x18c6b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x18c50
movq -0xb8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x40, %esi
callq 0xe310
jmp 0x18c50
movq -0xc0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x40, %esi
callq 0xe310
jmp 0x18c50
movq -0xd0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0xe230
jmp 0x18c50
movq -0x88(%rbp), %rax
movq (%rax), %rdi
movl $0x1ed, %esi            # imm = 0x1ED
callq 0x17d50
jmp 0x18b95
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x1ed, %esi            # imm = 0x1ED
callq 0x17d50
jmp 0x18bac
movq -0x88(%rbp), %rax
movq %rax, %rsi
addq $0x4c, %rsi
movl $0x1234567, 0x4c(%rax)  # imm = 0x1234567
movb $0x1, 0x50(%rax)
movq (%rax), %rdi
movl $0x4, %edx
callq 0x183e0
jmp 0x18bd4
movq -0x88(%rbp), %rsi
movq (%rsi), %rdi
addq $0x50, %rsi
movl $0x1, %edx
callq 0x183e0
jmp 0x18bee
movq -0x88(%rbp), %rsi
movq (%rsi), %rdi
addq $0x51, %rsi
movl $0x1, %edx
callq 0x183e0
jmp 0x18c08
movq -0x88(%rbp), %rax
movq (%rax), %rdi
leaq -0xc(%rbp), %rsi
movl $0x4, %edx
callq 0x183e0
jmp 0x18c22
movq -0x88(%rbp), %rax
movq $0x0, 0x60(%rax)
movb $0x1, 0x48(%rax)
leaq -0x60(%rbp), %rdi
callq 0xe1d0
leaq -0x40(%rbp), %rdi
callq 0xe1d0
addq $0xd0, %rsp
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0xe1d0
leaq -0x40(%rbp), %rdi
callq 0xe1d0
movq -0x18(%rbp), %rdi
callq 0xe5d0
nopl (%rax,%rax)