// File: /workspace/github_cpp_makefile_star2_O2/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 %rbx
subq $0x458, %rsp            # imm = 0x458
movq %rdi, %rbx
leaq -0x468(%rbp), %r14
movq %r14, %rdi
callq 0x21f0
leaq 0x128b(%rip), %rsi      # 0x4360
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x2040
movq -0x468(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x448(%rbp,%rax)
jne 0x352f
movaps 0x11b0(%rip), %xmm0     # 0x42b0
movups %xmm0, -0x138(%rbp)
movaps 0x11b2(%rip), %xmm0     # 0x42c0
movups %xmm0, -0x128(%rbp)
movabsq $0x2057415273646976, %rax # imm = 0x2057415273646976
movq %rax, -0x54(%rbp)
andq $0x0, -0x4c(%rbp)
movabsq $0xc0000001000, %rax    # imm = 0xC0000001000
andl $0x0, -0x44(%rbp)
movq %rax, -0x118(%rbp)
movaps 0x118c(%rip), %xmm0     # 0x42d0
movups %xmm0, -0x40(%rbp)
movabsq $-0xfdc00000, %rax      # imm = 0xFFFFFFFF02400000
movq %rax, -0x30(%rbp)
andq $0x0, -0x28(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0x110(%rbp)
movl $0xc001000, -0x20(%rbp) # imm = 0xC001000
movabsq $0x100000000028, %rax   # imm = 0x100000000028
movq %rax, -0x100(%rbp)
movabsq $0x18000100000c00, %rax # imm = 0x18000100000C00
movq %rax, -0xf8(%rbp)
movaps 0x110b(%rip), %xmm0     # 0x42a0
movups %xmm0, -0xf0(%rbp)
andq $0x0, -0xe0(%rbp)
leaq 0x11cb(%rip), %rsi      # 0x4376
leaq -0x258(%rbp), %rdi
leaq -0xd8(%rbp), %rdx
callq 0x2812
leaq -0x268(%rbp), %r14
leaq -0x258(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl $0x24000eb, %edx        # imm = 0x24000EB
xorl %ecx, %ecx
callq 0x3020
movq %r15, %rdi
callq 0x2090
leaq -0x468(%rbp), %rsi
movq %r14, %rdi
callq 0x3052
leaq 0x117f(%rip), %rsi      # 0x437b
leaq -0x468(%rbp), %rdi
pushq $0x4
popq %rdx
callq 0x2190
leaq 0x116e(%rip), %rsi      # 0x4380
leaq -0x238(%rbp), %rdi
leaq -0xc8(%rbp), %rdx
callq 0x2812
leaq -0xd8(%rbp), %r14
leaq -0x238(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl $0xc0, %edx
xorl %ecx, %ecx
callq 0x3020
movq %r15, %rdi
callq 0x2090
leaq -0x468(%rbp), %rsi
movq %r14, %rdi
callq 0x3052
leaq 0x1122(%rip), %rsi      # 0x4385
leaq -0x468(%rbp), %rdi
pushq $0x4
popq %rdx
callq 0x2190
leaq 0x1111(%rip), %rsi      # 0x438a
leaq -0x218(%rbp), %rdi
leaq -0xb8(%rbp), %rdx
callq 0x2812
leaq -0xc8(%rbp), %r14
leaq -0x218(%rbp), %r15
pushq $0x38
popq %rdx
leaq -0x138(%rbp), %rcx
movq %r14, %rdi
movq %r15, %rsi
callq 0x3020
movq %r15, %rdi
callq 0x2090
leaq -0x468(%rbp), %rsi
movq %r14, %rdi
callq 0x3052
leaq 0x10b3(%rip), %rsi      # 0x4380
leaq -0x1f8(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0x2812
leaq -0xb8(%rbp), %r14
leaq -0x1f8(%rbp), %r15
pushq $0x74
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x3020
movq %r15, %rdi
callq 0x2090
leaq -0x468(%rbp), %rsi
movq %r14, %rdi
callq 0x3052
leaq 0x1073(%rip), %rsi      # 0x438f
leaq -0x468(%rbp), %rdi
pushq $0x4
popq %rdx
callq 0x2190
leaq 0x1062(%rip), %rsi      # 0x4394
leaq -0x1d8(%rbp), %rdi
leaq -0x98(%rbp), %rdx
callq 0x2812
leaq -0xa8(%rbp), %r14
leaq -0x1d8(%rbp), %r15
pushq $0x38
popq %rdx
leaq -0x54(%rbp), %rcx
movq %r14, %rdi
movq %r15, %rsi
callq 0x3020
movq %r15, %rdi
callq 0x2090
leaq -0x468(%rbp), %rsi
movq %r14, %rdi
callq 0x3052
leaq 0x1016(%rip), %rsi      # 0x4399
leaq -0x1b8(%rbp), %rdi
leaq -0x88(%rbp), %rdx
callq 0x2812
leaq -0x98(%rbp), %r14
leaq -0x1b8(%rbp), %r15
pushq $0x28
popq %rdx
leaq -0x100(%rbp), %rcx
movq %r14, %rdi
movq %r15, %rsi
callq 0x3020
movq %r15, %rdi
callq 0x2090
leaq -0x468(%rbp), %rsi
movq %r14, %rdi
callq 0x3052
leaq 0xfa9(%rip), %rsi       # 0x4380
leaq -0x198(%rbp), %rdi
leaq -0x78(%rbp), %rdx
callq 0x2812
leaq -0x88(%rbp), %r14
leaq -0x198(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl $0x2400018, %edx        # imm = 0x2400018
xorl %ecx, %ecx
callq 0x3020
movq %r15, %rdi
callq 0x2090
leaq -0x468(%rbp), %rsi
movq %r14, %rdi
callq 0x3052
leaq 0xf79(%rip), %rsi       # 0x439e
leaq -0x468(%rbp), %rdi
pushq $0x4
popq %rdx
callq 0x2190
leaq 0xf45(%rip), %rsi       # 0x4380
leaq -0x178(%rbp), %rdi
leaq -0x68(%rbp), %rdx
callq 0x2812
leaq -0x78(%rbp), %r14
leaq -0x178(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl $0x240000c, %edx        # imm = 0x240000C
xorl %ecx, %ecx
callq 0x3020
movq %r15, %rdi
callq 0x2090
leaq -0x468(%rbp), %rsi
movq %r14, %rdi
callq 0x3052
leaq 0xf1d(%rip), %rsi       # 0x43a3
leaq -0x468(%rbp), %rdi
pushq $0x4
popq %rdx
callq 0x2190
leaq 0xf0c(%rip), %rsi       # 0x43a8
leaq -0x158(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x2812
leaq -0x68(%rbp), %r14
leaq -0x158(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl $0x2400000, %edx        # imm = 0x2400000
xorl %ecx, %ecx
callq 0x3020
movq %r15, %rdi
callq 0x2090
leaq -0x468(%rbp), %rsi
movq %r14, %rdi
callq 0x3052
movq (%rbx), %rsi
leaq -0x468(%rbp), %rdi
movl $0x2400000, %edx        # imm = 0x2400000
callq 0x2190
leaq -0x468(%rbp), %rdi
callq 0x21b0
movq -0x468(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x448(%rbp,%rax)
jne 0x354c
leaq -0x468(%rbp), %rdi
callq 0x21c0
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x2aba(%rip), %rdi      # 0x5ff0
leaq 0xdb0(%rip), %rsi       # 0x42ed
callq 0x2130
movq %rax, %rdi
callq 0x20b0
jmp 0x3567
movq 0x2a9d(%rip), %rdi      # 0x5ff0
leaq 0xdca(%rip), %rsi       # 0x4324
callq 0x2130
movq %rax, %rdi
callq 0x20b0
pushq $0x1
popq %rdi
callq 0x2160
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
jmp 0x3593
movq %rax, %rbx
leaq -0x468(%rbp), %rdi
callq 0x21c0
movq %rbx, %rdi
callq 0x2210