// File: Lut99[P]RayTracer/src/lib/Frames.cpp
// Function: RayTracer::Frames::Frames(unsigned int, unsigned int, unsigned int, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool)
Frames::Frames(unsigned int width, unsigned int height, unsigned int num_of_frames, unsigned int framerate, std::string temp_dir, bool dynamic_write)
    : frame_index(0),
    temp_dir(temp_dir),
    dynamic_writing(dynamic_write),
    width(width),
    height(height),
    n_frames(num_of_frames),
    fps(framerate)
{
    // Touch a file in the temp dir to make sure that the director exists
    ofstream temp_check(temp_dir + "/out1.png");
    if (!temp_check.is_open()) {
        cerr << "temp_dir existance check failed: " << string_error(errno) << endl;
        exit(-1);
    }
    temp_check.close();

    // Delete all .png files starting with out and ending in .png in the folder
    string command = "rm -r "+ temp_dir + "/out*.png";
    int res = system(command.c_str());
    if (res != 0) {
        cerr << "WARNING: Could not remove .png files in the temporary output folder." << endl;
    }

    // Initialize the first frame
    this->current_frame = new Image(this->width, this->height);

    // If everything should be kept in memory, allocate and sync the frames list as well
    if (!this->dynamic_writing) {
        this->frames = new Image*[this->n_frames];
        this->frames[this->frame_index] = this->current_frame;
    } 
}

// Assembly:
subq $0x318, %rsp            # imm = 0x318
movq %r9, 0x50(%rsp)
movl %esi, %eax
movq 0x50(%rsp), %rsi
movl %eax, 0x3c(%rsp)
movq %rdi, %r9
movl 0x3c(%rsp), %edi
movb 0x320(%rsp), %al
movq %r9, 0x310(%rsp)
movl %edi, 0x30c(%rsp)
movl %edx, 0x308(%rsp)
movl %ecx, 0x304(%rsp)
movl %r8d, 0x300(%rsp)
movq %rsi, 0x2f8(%rsp)
andb $0x1, %al
movb %al, 0x2f7(%rsp)
movq 0x310(%rsp), %rdi
movq %rdi, 0x48(%rsp)
movq $0x0, 0x10(%rdi)
addq $0x18, %rdi
movq %rdi, 0x40(%rsp)
callq 0xd0250
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rsi
movb 0x2f7(%rsp), %cl
andb $0x1, %cl
movb %cl, 0x38(%rax)
movl 0x30c(%rsp), %ecx
movl %ecx, 0x3c(%rax)
movl 0x308(%rsp), %ecx
movl %ecx, 0x40(%rax)
movl 0x304(%rsp), %ecx
movl %ecx, 0x44(%rax)
movl 0x300(%rsp), %ecx
movl %ecx, 0x48(%rax)
leaq 0x3c1bc(%rip), %rdx     # 0x175f40
leaq 0xd0(%rsp), %rdi
callq 0x13ae30
jmp 0x139d93
leaq 0xf0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
movl $0x10, %edx
callq 0xd0ad0
jmp 0x139daf
leaq 0xd0(%rsp), %rdi
callq 0xd0b38
leaq 0xf0(%rsp), %rdi
callq 0xd0460
movb %al, 0x3b(%rsp)
jmp 0x139dcf
movb 0x3b(%rsp), %al
testb $0x1, %al
jne 0x139ece
jmp 0x139ddd
movq 0x721fc(%rip), %rdi     # 0x1abfe0
leaq 0x3c15f(%rip), %rsi     # 0x175f4a
callq 0xd05b0
movq %rax, 0x30(%rsp)
jmp 0x139df7
callq 0xd0090
movl (%rax), %esi
leaq 0xa0(%rsp), %rdi
callq 0x16ab30
jmp 0x139e0d
movq 0x30(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0xd0540
movq %rax, 0x28(%rsp)
jmp 0x139e26
movq 0x28(%rsp), %rdi
movq 0x72106(%rip), %rsi     # 0x1abf38
callq 0xd0610
jmp 0x139e39
leaq 0xa0(%rsp), %rdi
callq 0xd0b38
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0xd0700
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
jmp 0x13a084
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0xd0b38
jmp 0x13a084
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
jmp 0x13a077
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xd0b38
jmp 0x13a077
leaq 0xf0(%rsp), %rdi
callq 0xd0830
jmp 0x139edd
movq 0x50(%rsp), %rdx
leaq 0x3c083(%rip), %rsi     # 0x175f6c
leaq 0x60(%rsp), %rdi
callq 0xe3ed0
jmp 0x139ef5
leaq 0x3c077(%rip), %rdx     # 0x175f73
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xe6bb0
jmp 0x139f10
leaq 0x60(%rsp), %rdi
callq 0xd0b38
leaq 0x80(%rsp), %rdi
callq 0xd0220
movq %rax, %rdi
callq 0xd04f0
movl %eax, 0x24(%rsp)
jmp 0x139f35
movl 0x24(%rsp), %eax
movl %eax, 0x5c(%rsp)
cmpl $0x0, 0x5c(%rsp)
je 0x139faf
movq 0x72095(%rip), %rdi     # 0x1abfe0
leaq 0x3c02b(%rip), %rsi     # 0x175f7d
callq 0xd05b0
movq %rax, 0x18(%rsp)
jmp 0x139f5e
movq 0x18(%rsp), %rdi
movq 0x71fce(%rip), %rsi     # 0x1abf38
callq 0xd0610
jmp 0x139f71
jmp 0x139faf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0x60(%rsp), %rdi
callq 0xd0b38
jmp 0x13a077
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
jmp 0x13a06a
movl $0x10, %edi
callq 0xd05d0
movq %rax, 0x10(%rsp)
jmp 0x139fc0
movq 0x10(%rsp), %rdi
movq 0x48(%rsp), %rax
movl 0x3c(%rax), %esi
movl 0x40(%rax), %edx
callq 0x13b350
jmp 0x139fd7
movq 0x48(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
testb $0x1, 0x38(%rax)
jne 0x13a048
movq 0x48(%rsp), %rax
movl 0x44(%rax), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0xd0050
movq %rax, 0x8(%rsp)
jmp 0x13a004
movq 0x48(%rsp), %rcx
movq 0x8(%rsp), %rax
movq %rax, 0x8(%rcx)
movq (%rcx), %rdx
movq 0x8(%rcx), %rax
movq 0x10(%rcx), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x13a048
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
movl $0x10, %esi
callq 0xd05e0
jmp 0x13a06a
leaq 0x80(%rsp), %rdi
callq 0xd0b38
leaq 0xf0(%rsp), %rdi
callq 0xd0860
addq $0x318, %rsp            # imm = 0x318
retq
leaq 0x80(%rsp), %rdi
callq 0xd0b38
leaq 0xf0(%rsp), %rdi
callq 0xd0860
movq 0x40(%rsp), %rdi
callq 0xd0b38
movq 0xc8(%rsp), %rdi
callq 0xd0a10
nopl (%rax,%rax)