// File: /workspace/github_cpp_makefile_star2_O1/ksraj123[P]RAW12-to-bitmap-AVI/src/OutputWriter.cpp
// Function: OutputWriter::WriteAvi()
void OutputWriter::WriteAvi()
{
    std::ofstream outputAVI;
    outputAVI.open("result/AVI_output.avi", std::ios::binary);
    if(!outputAVI)
    {
        std::cerr << "Error: File cannot be opened\n\tExiting" << std::endl;
        exit(1);
    }

    MainAVIHeader mainHeader;
    AVIStreamHeader streamHeader;
    AVIStreamFormat streamFormat;

    uint32_t size_pix  = TOTAL_PIX * 3;
    uint32_t size_rec  = 3 * 4 + size_pix;
    uint32_t size_movi = 3 * 4 + size_rec;
    uint32_t size_strl = 3 * 4 + sizeof(streamHeader) + 2 * 4 + sizeof(streamFormat);
    uint32_t size_hrdl = 3 * 4 + sizeof(mainHeader) + 2 * 4 + size_strl;
    uint32_t size_riff = 3 + 2 * 4 + size_hrdl + 2 * 4 + size_movi;

    Chunk riff ("RIFF", size_riff);
    riff.WriteToFile(&outputAVI);

    outputAVI.write("AVI ", 4);

    Chunk listHdrl ("LIST", size_hrdl);
    listHdrl.WriteToFile(&outputAVI);

    outputAVI.write("hdrl", 4);

    Chunk avih ("avih", sizeof(mainHeader), &mainHeader);
    avih.WriteToFile(&outputAVI);

    Chunk listStrl ("LIST", size_strl);
    listStrl.WriteToFile(&outputAVI);

    outputAVI.write("strl", 4);

    Chunk strh ("strh", sizeof(streamHeader), &streamHeader);
    strh.WriteToFile(&outputAVI);

    Chunk strf ("strf", sizeof(streamFormat), &streamFormat);
    strf.WriteToFile(&outputAVI);

    Chunk listMovi ("LIST", size_movi);
    listMovi.WriteToFile(&outputAVI);

    outputAVI.write("movi", 4);

    Chunk listRec ("LIST", size_rec);
    listRec.WriteToFile(&outputAVI);

    outputAVI.write("rec ", 4);

    Chunk imgSubChnk ("00db", size_pix);
    imgSubChnk.WriteToFile(&outputAVI);

    // Entire Image written at once for enhanced performace
    outputAVI.write(reinterpret_cast<const char*>(bmpImage), 3*TOTAL_PIX);

    outputAVI.close();
    if (!outputAVI)
    {
        std::cerr << "Error: File cannot be Closed\n\tExiting" << std::endl;
        exit(1);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x450, %rsp            # imm = 0x450
movq %rdi, %rbx
leaq -0x468(%rbp), %r14
movq %r14, %rdi
callq 0x2220
leaq 0x1c33(%rip), %rsi      # 0x5360
movq %r14, %rdi
movl $0x4, %edx
callq 0x2050
movq -0x468(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x448(%rbp,%rax)
jne 0x3d5b
movaps 0x1b56(%rip), %xmm0     # 0x52b0
movups %xmm0, -0x268(%rbp)
movaps 0x1b58(%rip), %xmm0     # 0x52c0
movups %xmm0, -0x258(%rbp)
movabsq $0xc0000001000, %rax    # imm = 0xC0000001000
movq %rax, -0x248(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0x240(%rbp)
movabsq $0x2057415273646976, %rax # imm = 0x2057415273646976
movq %rax, -0x58(%rbp)
xorl %eax, %eax
movq %rax, -0x50(%rbp)
movl $0x0, -0x48(%rbp)
movaps 0x1b24(%rip), %xmm0     # 0x52d0
movups %xmm0, -0x44(%rbp)
movabsq $-0xfdc00000, %rcx      # imm = 0xFFFFFFFF02400000
movq %rcx, -0x34(%rbp)
movq %rax, -0x2c(%rbp)
movl $0xc001000, -0x24(%rbp) # imm = 0xC001000
movabsq $0x100000000028, %rcx   # imm = 0x100000000028
movq %rcx, -0x230(%rbp)
movabsq $0x18000100000c00, %rcx # imm = 0x18000100000C00
movq %rcx, -0x228(%rbp)
movaps 0x1aae(%rip), %xmm0     # 0x52a0
movups %xmm0, -0x220(%rbp)
movq %rax, -0x210(%rbp)
leaq -0x1e8(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x1b63(%rip), %rsi      # 0x5376
leaq 0x1b60(%rip), %rdx      # 0x537a
leaq -0x1f8(%rbp), %rdi
callq 0x2c0e
movq -0x1f8(%rbp), %r14
leaq -0x208(%rbp), %r15
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x20e0
movl $0x24000eb, 0x4(%r15)   # imm = 0x24000EB
movq $0x0, 0x8(%r15)
cmpq %r12, %r14
je 0x386b
movq -0x1e8(%rbp), %rsi
incq %rsi
movq %r14, %rdi
callq 0x2180
leaq -0x208(%rbp), %rdi
leaq -0x468(%rbp), %rsi
callq 0x36a8
leaq 0x1af6(%rip), %rsi      # 0x537b
leaq -0x468(%rbp), %rdi
movl $0x4, %edx
callq 0x21d0
leaq -0x1b8(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x1ad7(%rip), %rsi      # 0x5380
leaq 0x1ad4(%rip), %rdx      # 0x5384
leaq -0x1c8(%rbp), %rdi
callq 0x2c0e
movq -0x1c8(%rbp), %r14
leaq -0x1d8(%rbp), %r15
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x20e0
movl $0xc0, 0x4(%r15)
movq $0x0, 0x8(%r15)
cmpq %r12, %r14
je 0x3901
movq -0x1b8(%rbp), %rsi
incq %rsi
movq %r14, %rdi
callq 0x2180
leaq -0x1d8(%rbp), %rdi
leaq -0x468(%rbp), %rsi
callq 0x36a8
leaq 0x1a6a(%rip), %rsi      # 0x5385
leaq -0x468(%rbp), %rdi
movl $0x4, %edx
callq 0x21d0
leaq -0x188(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x1a4b(%rip), %rsi      # 0x538a
leaq 0x1a48(%rip), %rdx      # 0x538e
leaq -0x198(%rbp), %rdi
callq 0x2c0e
movq -0x198(%rbp), %r14
leaq -0x1a8(%rbp), %r15
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x20e0
movl $0x38, 0x4(%r15)
leaq -0x268(%rbp), %rax
movq %rax, 0x8(%r15)
cmpq %r12, %r14
je 0x399a
movq -0x188(%rbp), %rsi
incq %rsi
movq %r14, %rdi
callq 0x2180
leaq -0x1a8(%rbp), %rdi
leaq -0x468(%rbp), %rsi
callq 0x36a8
leaq -0x158(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x19c0(%rip), %rsi      # 0x5380
leaq 0x19bd(%rip), %rdx      # 0x5384
leaq -0x168(%rbp), %rdi
callq 0x2c0e
movq -0x168(%rbp), %r14
leaq -0x178(%rbp), %r15
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x20e0
movl $0x74, 0x4(%r15)
movq $0x0, 0x8(%r15)
cmpq %r12, %r14
je 0x3a18
movq -0x158(%rbp), %rsi
incq %rsi
movq %r14, %rdi
callq 0x2180
leaq -0x178(%rbp), %rdi
leaq -0x468(%rbp), %rsi
callq 0x36a8
leaq 0x195d(%rip), %rsi      # 0x538f
leaq -0x468(%rbp), %rdi
movl $0x4, %edx
callq 0x21d0
leaq -0x128(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x193e(%rip), %rsi      # 0x5394
leaq 0x193b(%rip), %rdx      # 0x5398
leaq -0x138(%rbp), %rdi
callq 0x2c0e
movq -0x138(%rbp), %r14
leaq -0x148(%rbp), %r15
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x20e0
movl $0x38, 0x4(%r15)
leaq -0x58(%rbp), %rax
movq %rax, 0x8(%r15)
cmpq %r12, %r14
je 0x3aae
movq -0x128(%rbp), %rsi
incq %rsi
movq %r14, %rdi
callq 0x2180
leaq -0x148(%rbp), %rdi
leaq -0x468(%rbp), %rsi
callq 0x36a8
leaq -0xf8(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x18c5(%rip), %rsi      # 0x5399
leaq 0x18c2(%rip), %rdx      # 0x539d
leaq -0x108(%rbp), %rdi
callq 0x2c0e
movq -0x108(%rbp), %r14
leaq -0x118(%rbp), %r15
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x20e0
movl $0x28, 0x4(%r15)
leaq -0x230(%rbp), %rax
movq %rax, 0x8(%r15)
cmpq %r12, %r14
je 0x3b2f
movq -0xf8(%rbp), %rsi
incq %rsi
movq %r14, %rdi
callq 0x2180
leaq -0x118(%rbp), %rdi
leaq -0x468(%rbp), %rsi
callq 0x36a8
leaq -0xc8(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x182b(%rip), %rsi      # 0x5380
leaq 0x1828(%rip), %rdx      # 0x5384
leaq -0xd8(%rbp), %rdi
callq 0x2c0e
movq -0xd8(%rbp), %r14
leaq -0xe8(%rbp), %r15
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x20e0
movl $0x2400018, 0x4(%r15)   # imm = 0x2400018
movq $0x0, 0x8(%r15)
cmpq %r12, %r14
je 0x3bad
movq -0xc8(%rbp), %rsi
incq %rsi
movq %r14, %rdi
callq 0x2180
leaq -0xe8(%rbp), %rdi
leaq -0x468(%rbp), %rsi
callq 0x36a8
leaq 0x17d7(%rip), %rsi      # 0x539e
leaq -0x468(%rbp), %rdi
movl $0x4, %edx
callq 0x21d0
leaq -0x98(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x1795(%rip), %rsi      # 0x5380
leaq 0x1792(%rip), %rdx      # 0x5384
leaq -0xa8(%rbp), %rdi
callq 0x2c0e
movq -0xa8(%rbp), %r14
leaq -0xb8(%rbp), %r15
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x20e0
movl $0x240000c, 0x4(%r15)   # imm = 0x240000C
movq $0x0, 0x8(%r15)
cmpq %r12, %r14
je 0x3c43
movq -0x98(%rbp), %rsi
incq %rsi
movq %r14, %rdi
callq 0x2180
leaq -0xb8(%rbp), %rdi
leaq -0x468(%rbp), %rsi
callq 0x36a8
leaq 0x1746(%rip), %rsi      # 0x53a3
leaq -0x468(%rbp), %rdi
movl $0x4, %edx
callq 0x21d0
leaq -0x68(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x172a(%rip), %rsi      # 0x53a8
leaq 0x1727(%rip), %rdx      # 0x53ac
leaq -0x78(%rbp), %rdi
callq 0x2c0e
movq -0x78(%rbp), %r14
leaq -0x88(%rbp), %r15
movl $0x4, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x20e0
movl $0x2400000, 0x4(%r15)   # imm = 0x2400000
movq $0x0, 0x8(%r15)
cmpq %r12, %r14
je 0x3ccd
movq -0x68(%rbp), %rsi
incq %rsi
movq %r14, %rdi
callq 0x2180
leaq -0x88(%rbp), %rdi
leaq -0x468(%rbp), %rsi
callq 0x36a8
movq (%rbx), %rsi
leaq -0x468(%rbp), %rdi
movl $0x2400000, %edx        # imm = 0x2400000
callq 0x21d0
leaq -0x468(%rbp), %rdi
callq 0x21e0
movq -0x468(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x448(%rbp,%rax)
jne 0x3d81
movq 0x32ac(%rip), %rax      # 0x6fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x460(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x468(%rbp,%rcx)
callq 0x2130
leaq -0x370(%rbp), %rdi
callq 0x20a0
addq $0x450, %rsp            # imm = 0x450
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x328e(%rip), %rdi      # 0x6ff0
leaq 0x1584(%rip), %rsi      # 0x52ed
movl $0x25, %edx
callq 0x21a0
movq 0x3276(%rip), %rdi      # 0x6ff0
callq 0x20d0
jmp 0x3da5
movq 0x3268(%rip), %rdi      # 0x6ff0
leaq 0x1595(%rip), %rsi      # 0x5324
movl $0x25, %edx
callq 0x21a0
movq 0x3250(%rip), %rdi      # 0x6ff0
callq 0x20d0
movl $0x1, %edi
callq 0x21b0
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
jmp 0x3dd3
movq %rax, %rbx
leaq -0x468(%rbp), %rdi
callq 0x21f0
movq %rbx, %rdi
callq 0x2240