// File: sidvishnoi[P]filesystem-assignment/day3/findEmptySectors.cpp
// Function: FileSystem::findEmptySectors(int)
vector<int> FileSystem::findEmptySectors(int requiredNumber) {
    /*
    Objective: To get a list of sectors that are available to be written on
    Input Parameters:
        requiredNumber: Number of empty sectors required
    Return Value:
        list of available sectors
        if not enough available, return empty list
    */
    TypeCastInt cast;
    vector<int> availableSectors;
    int found = 0, sector = 0;
    int div = sectorSize_k/4;   // each sector contains status of this much sectors
    char buffer[sectorSize_k];
    while (found != requiredNumber && sector < reservedSectors_k) {
        readSector(sector, buffer);
        for (int s = 0; s < div; ++s) {
            for (int j = 0; j < 4; ++j) {
                cast.str[j] = buffer[4*s + j];
            }
            if (cast.num == FREE) {
                availableSectors.push_back(s + reservedSectors_k + sector*div);
                ++found;
            }
            if (found == requiredNumber) break;
        }
    ++sector;
    }

    // if still, found != requiredNumber, now try looking in DELETED sectors (DAY 4)
    // %% look in sectors marked deleted here

    if (found != requiredNumber) {
        // empty list if enough sectors are not available (failure)
        availableSectors.empty();
    }
    return availableSectors; // success
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x70(%rbp)
movq %rdi, %rax
movq %rax, -0x68(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
movb $0x0, -0x19(%rbp)
callq 0x7aa0
movq -0x60(%rbp), %rax
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl 0x20(%rax), %ecx
movl %ecx, %edx
addl $0x3, %edx
testl %ecx, %ecx
cmovsl %edx, %ecx
sarl $0x2, %ecx
movl %ecx, -0x28(%rbp)
movl 0x20(%rax), %eax
movq %rsp, %rcx
movq %rcx, -0x30(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
movl -0x20(%rbp), %ecx
xorl %eax, %eax
cmpl -0x14(%rbp), %ecx
movb %al, -0x71(%rbp)
je 0x868a
movq -0x60(%rbp), %rcx
movl -0x24(%rbp), %eax
cmpl 0x28(%rcx), %eax
setl %al
movb %al, -0x71(%rbp)
movb -0x71(%rbp), %al
testb $0x1, %al
jne 0x8696
jmp 0x8768
movq -0x58(%rbp), %rdx
movq -0x60(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x8290
jmp 0x86a8
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x875a
movl $0x0, -0x4c(%rbp)
cmpl $0x4, -0x4c(%rbp)
jge 0x8708
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
shll $0x2, %ecx
addl -0x4c(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %cl
movslq -0x4c(%rbp), %rax
movb %cl, -0x18(%rbp,%rax)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x86c2
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x3db0
jmp 0x879c
cmpl $0x0, -0x18(%rbp)
jne 0x8740
movq -0x70(%rbp), %rdi
movq -0x60(%rbp), %rcx
movl -0x48(%rbp), %eax
movl 0x28(%rcx), %ecx
addl %ecx, %eax
movl -0x24(%rbp), %ecx
movl -0x28(%rbp), %edx
imull %edx, %ecx
addl %ecx, %eax
movl %eax, -0x50(%rbp)
leaq -0x50(%rbp), %rsi
callq 0x87b0
jmp 0x8737
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x874a
jmp 0x875a
jmp 0x874c
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x86af
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x866d
movl -0x20(%rbp), %eax
cmpl -0x14(%rbp), %eax
je 0x8779
movq -0x70(%rbp), %rdi
callq 0x87e0
movb $0x1, -0x19(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rsp
testb $0x1, -0x19(%rbp)
jne 0x8793
movq -0x70(%rbp), %rdi
callq 0x3db0
movq -0x68(%rbp), %rax
movq %rbp, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x3450
nopw %cs:(%rax,%rax)
nop