// File: 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
subq $0x540, %rsp            # imm = 0x540
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x4d0(%rbp)
leaq -0x208(%rbp), %rdi
movq %rdi, -0x4c8(%rbp)
callq 0x2300
movq -0x4c8(%rbp), %rdi
leaq 0x24b5(%rip), %rsi      # 0x6320
movl $0x4, %edx
callq 0x2040
jmp 0x3e77
movq -0x208(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x208(%rbp,%rax), %rdi
callq 0x20f0
movb %al, -0x4d1(%rbp)
jmp 0x3e97
movb -0x4d1(%rbp), %al
testb $0x1, %al
jne 0x3ea3
jmp 0x3ef5
movq 0x5146(%rip), %rdi      # 0x8ff0
leaq 0x23fc(%rip), %rsi      # 0x62ad
callq 0x21e0
movq %rax, -0x4e0(%rbp)
jmp 0x3ebf
movq -0x4e0(%rbp), %rdi
movq 0x50f3(%rip), %rsi      # 0x8fc0
callq 0x2200
jmp 0x3ed4
movl $0x1, %edi
callq 0x2260
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x474e
leaq -0x24c(%rbp), %rdi
callq 0x4970
leaq -0x284(%rbp), %rdi
callq 0x4a10
leaq -0x2ac(%rbp), %rdi
callq 0x4aa0
movl $0x2400000, -0x2b0(%rbp) # imm = 0x2400000
movl -0x2b0(%rbp), %eax
addl $0xc, %eax
movl %eax, -0x2b4(%rbp)
movl -0x2b4(%rbp), %eax
addl $0xc, %eax
movl %eax, -0x2b8(%rbp)
movl $0x74, -0x2bc(%rbp)
movl -0x2bc(%rbp), %eax
addl $0x4c, %eax
movl %eax, -0x2c0(%rbp)
movl -0x2c0(%rbp), %edx
movl -0x2b8(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal 0x13(%rax,%rcx), %eax
movl %eax, -0x2c4(%rbp)
leaq -0x2f9(%rbp), %rdi
movq %rdi, -0x4e8(%rbp)
callq 0x2340
movq -0x4e8(%rbp), %rdx
leaq 0x23a1(%rip), %rsi      # 0x6336
leaq -0x2f8(%rbp), %rdi
callq 0x2d20
jmp 0x3fa3
movl -0x2c4(%rbp), %edx
xorl %eax, %eax
movl %eax, %ecx
leaq -0x2d8(%rbp), %rdi
leaq -0x2f8(%rbp), %rsi
callq 0x3d60
jmp 0x3fc2
leaq -0x2f8(%rbp), %rdi
callq 0x2150
leaq -0x2f9(%rbp), %rdi
callq 0x2220
leaq -0x2d8(%rbp), %rdi
leaq -0x208(%rbp), %rsi
callq 0x3dc0
jmp 0x3fef
leaq 0x2345(%rip), %rsi      # 0x633b
leaq -0x208(%rbp), %rdi
movl $0x4, %edx
callq 0x2290
jmp 0x4009
leaq -0x331(%rbp), %rdi
movq %rdi, -0x4f0(%rbp)
callq 0x2340
movq -0x4f0(%rbp), %rdx
leaq 0x2316(%rip), %rsi      # 0x6340
leaq -0x330(%rbp), %rdi
callq 0x2d20
jmp 0x4038
movl -0x2c0(%rbp), %edx
xorl %eax, %eax
movl %eax, %ecx
leaq -0x310(%rbp), %rdi
leaq -0x330(%rbp), %rsi
callq 0x3d60
jmp 0x4057
leaq -0x330(%rbp), %rdi
callq 0x2150
leaq -0x331(%rbp), %rdi
callq 0x2220
leaq -0x310(%rbp), %rdi
leaq -0x208(%rbp), %rsi
callq 0x3dc0
jmp 0x4084
leaq 0x22ba(%rip), %rsi      # 0x6345
leaq -0x208(%rbp), %rdi
movl $0x4, %edx
callq 0x2290
jmp 0x409e
leaq -0x369(%rbp), %rdi
movq %rdi, -0x4f8(%rbp)
callq 0x2340
movq -0x4f8(%rbp), %rdx
leaq 0x228b(%rip), %rsi      # 0x634a
leaq -0x368(%rbp), %rdi
callq 0x2d20
jmp 0x40cd
leaq -0x348(%rbp), %rdi
leaq -0x368(%rbp), %rsi
movl $0x38, %edx
leaq -0x24c(%rbp), %rcx
callq 0x3d60
jmp 0x40ee
leaq -0x368(%rbp), %rdi
callq 0x2150
leaq -0x369(%rbp), %rdi
callq 0x2220
leaq -0x348(%rbp), %rdi
leaq -0x208(%rbp), %rsi
callq 0x3dc0
jmp 0x411b
leaq -0x3a1(%rbp), %rdi
movq %rdi, -0x500(%rbp)
callq 0x2340
movq -0x500(%rbp), %rdx
leaq 0x2204(%rip), %rsi      # 0x6340
leaq -0x3a0(%rbp), %rdi
callq 0x2d20
jmp 0x414a
movl -0x2bc(%rbp), %edx
xorl %eax, %eax
movl %eax, %ecx
leaq -0x380(%rbp), %rdi
leaq -0x3a0(%rbp), %rsi
callq 0x3d60
jmp 0x4169
leaq -0x3a0(%rbp), %rdi
callq 0x2150
leaq -0x3a1(%rbp), %rdi
callq 0x2220
leaq -0x380(%rbp), %rdi
leaq -0x208(%rbp), %rsi
callq 0x3dc0
jmp 0x4196
leaq 0x21b2(%rip), %rsi      # 0x634f
leaq -0x208(%rbp), %rdi
movl $0x4, %edx
callq 0x2290
jmp 0x41b0
leaq -0x3d9(%rbp), %rdi
movq %rdi, -0x508(%rbp)
callq 0x2340
movq -0x508(%rbp), %rdx
leaq 0x2183(%rip), %rsi      # 0x6354
leaq -0x3d8(%rbp), %rdi
callq 0x2d20
jmp 0x41df
leaq -0x3b8(%rbp), %rdi
leaq -0x3d8(%rbp), %rsi
movl $0x38, %edx
leaq -0x284(%rbp), %rcx
callq 0x3d60
jmp 0x4200
leaq -0x3d8(%rbp), %rdi
callq 0x2150
leaq -0x3d9(%rbp), %rdi
callq 0x2220
leaq -0x3b8(%rbp), %rdi
leaq -0x208(%rbp), %rsi
callq 0x3dc0
jmp 0x422d
leaq -0x411(%rbp), %rdi
movq %rdi, -0x510(%rbp)
callq 0x2340
movq -0x510(%rbp), %rdx
leaq 0x210b(%rip), %rsi      # 0x6359
leaq -0x410(%rbp), %rdi
callq 0x2d20
jmp 0x425c
leaq -0x3f0(%rbp), %rdi
leaq -0x410(%rbp), %rsi
movl $0x28, %edx
leaq -0x2ac(%rbp), %rcx
callq 0x3d60
jmp 0x427d
leaq -0x410(%rbp), %rdi
callq 0x2150
leaq -0x411(%rbp), %rdi
callq 0x2220
leaq -0x3f0(%rbp), %rdi
leaq -0x208(%rbp), %rsi
callq 0x3dc0
jmp 0x42aa
leaq -0x449(%rbp), %rdi
movq %rdi, -0x518(%rbp)
callq 0x2340
movq -0x518(%rbp), %rdx
leaq 0x2075(%rip), %rsi      # 0x6340
leaq -0x448(%rbp), %rdi
callq 0x2d20
jmp 0x42d9
movl -0x2b8(%rbp), %edx
xorl %eax, %eax
movl %eax, %ecx
leaq -0x428(%rbp), %rdi
leaq -0x448(%rbp), %rsi
callq 0x3d60
jmp 0x42f8
leaq -0x448(%rbp), %rdi
callq 0x2150
leaq -0x449(%rbp), %rdi
callq 0x2220
leaq -0x428(%rbp), %rdi
leaq -0x208(%rbp), %rsi
callq 0x3dc0
jmp 0x4325
leaq 0x2032(%rip), %rsi      # 0x635e
leaq -0x208(%rbp), %rdi
movl $0x4, %edx
callq 0x2290
jmp 0x433f
leaq -0x481(%rbp), %rdi
movq %rdi, -0x520(%rbp)
callq 0x2340
movq -0x520(%rbp), %rdx
leaq 0x1fe0(%rip), %rsi      # 0x6340
leaq -0x480(%rbp), %rdi
callq 0x2d20
jmp 0x436e
movl -0x2b4(%rbp), %edx
xorl %eax, %eax
movl %eax, %ecx
leaq -0x460(%rbp), %rdi
leaq -0x480(%rbp), %rsi
callq 0x3d60
jmp 0x438d
leaq -0x480(%rbp), %rdi
callq 0x2150
leaq -0x481(%rbp), %rdi
callq 0x2220
leaq -0x460(%rbp), %rdi
leaq -0x208(%rbp), %rsi
callq 0x3dc0
jmp 0x43ba
leaq 0x1fa2(%rip), %rsi      # 0x6363
leaq -0x208(%rbp), %rdi
movl $0x4, %edx
callq 0x2290
jmp 0x43d4
leaq -0x4b9(%rbp), %rdi
movq %rdi, -0x528(%rbp)
callq 0x2340
movq -0x528(%rbp), %rdx
leaq 0x1f73(%rip), %rsi      # 0x6368
leaq -0x4b8(%rbp), %rdi
callq 0x2d20
jmp 0x4403
movl -0x2b0(%rbp), %edx
xorl %eax, %eax
movl %eax, %ecx
leaq -0x498(%rbp), %rdi
leaq -0x4b8(%rbp), %rsi
callq 0x3d60
jmp 0x4422
leaq -0x4b8(%rbp), %rdi
callq 0x2150
leaq -0x4b9(%rbp), %rdi
callq 0x2220
leaq -0x498(%rbp), %rdi
leaq -0x208(%rbp), %rsi
callq 0x3dc0
jmp 0x444f
movq -0x4d0(%rbp), %rax
movq (%rax), %rsi
leaq -0x208(%rbp), %rdi
movl $0x2400000, %edx        # imm = 0x2400000
callq 0x2290
jmp 0x446c
leaq -0x208(%rbp), %rdi
callq 0x22a0
jmp 0x447a
movq -0x208(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x208(%rbp,%rax), %rdi
callq 0x20f0
movb %al, -0x529(%rbp)
jmp 0x449a
movb -0x529(%rbp), %al
testb $0x1, %al
jne 0x44a9
jmp 0x4739
movq 0x4b40(%rip), %rdi      # 0x8ff0
leaq 0x1e2d(%rip), %rsi      # 0x62e4
callq 0x21e0
movq %rax, -0x538(%rbp)
jmp 0x44c5
movq -0x538(%rbp), %rdi
movq 0x4aed(%rip), %rsi      # 0x8fc0
callq 0x2200
jmp 0x44da
movl $0x1, %edi
callq 0x2260
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x4516
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
leaq -0x2f8(%rbp), %rdi
callq 0x2150
leaq -0x2f9(%rbp), %rdi
callq 0x2220
jmp 0x474e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x4559
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
leaq -0x330(%rbp), %rdi
callq 0x2150
leaq -0x331(%rbp), %rdi
callq 0x2220
jmp 0x474e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x459c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
leaq -0x368(%rbp), %rdi
callq 0x2150
leaq -0x369(%rbp), %rdi
callq 0x2220
jmp 0x474e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x45df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
leaq -0x3a0(%rbp), %rdi
callq 0x2150
leaq -0x3a1(%rbp), %rdi
callq 0x2220
jmp 0x474e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x4622
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
leaq -0x3d8(%rbp), %rdi
callq 0x2150
leaq -0x3d9(%rbp), %rdi
callq 0x2220
jmp 0x474e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x4665
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
leaq -0x410(%rbp), %rdi
callq 0x2150
leaq -0x411(%rbp), %rdi
callq 0x2220
jmp 0x474e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x46a8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x2150
leaq -0x449(%rbp), %rdi
callq 0x2220
jmp 0x474e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x46eb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
leaq -0x480(%rbp), %rdi
callq 0x2150
leaq -0x481(%rbp), %rdi
callq 0x2220
jmp 0x474e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x472b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
leaq -0x4b8(%rbp), %rdi
callq 0x2150
leaq -0x4b9(%rbp), %rdi
callq 0x2220
jmp 0x474e
leaq -0x208(%rbp), %rdi
callq 0x22c0
addq $0x540, %rsp            # imm = 0x540
popq %rbp
retq
leaq -0x208(%rbp), %rdi
callq 0x22c0
movq -0x210(%rbp), %rdi
callq 0x2330
nopw %cs:(%rax,%rax)