// File: /workspace/github_c_makefile_star2_O2/archer1357[P]Offline-Raytracer/avilib.c
// Function: avi_update_header
int avi_update_header(avi_t *AVI)
{
   int njunk, sampsize, hasIndex, ms_per_frame, frate, flag;
   int movi_len, hdrl_start, strl_start, j;
   unsigned char AVI_header[HEADERBYTES];
   long nhb;

   //assume max size
   movi_len = AVI_MAX_LEN - HEADERBYTES + 4;

   //assume index will be written
   hasIndex=1;

   if(AVI->fps < 0.001) {
     frate=0;
     ms_per_frame=0;
   } else {
     frate = (int) (FRAME_RATE_SCALE*AVI->fps + 0.5);
     ms_per_frame=(int) (1000000/AVI->fps + 0.5);
   }

   /* Prepare the file header */

   nhb = 0;

   /* The RIFF header */

   OUT4CC ("RIFF");
   OUTLONG(movi_len);    // assume max size
   OUT4CC ("AVI ");

   /* Start the header list */

   OUT4CC ("LIST");
   OUTLONG(0);        /* Length of list in bytes, don't know yet */
   hdrl_start = nhb;  /* Store start position */
   OUT4CC ("hdrl");

   /* The main AVI header */

   /* The Flags in AVI File header */

#define AVIF_HASINDEX           0x00000010      /* Index at end of file */
#define AVIF_MUSTUSEINDEX       0x00000020
#define AVIF_ISINTERLEAVED      0x00000100
#define AVIF_TRUSTCKTYPE        0x00000800      /* Use CKType to find key frames */
#define AVIF_WASCAPTUREFILE     0x00010000
#define AVIF_COPYRIGHTED        0x00020000

   OUT4CC ("avih");
   OUTLONG(56);                 /* # of bytes to follow */
   OUTLONG(ms_per_frame);       /* Microseconds per frame */
   //ThOe ->0
   //   OUTLONG(10000000);           /* MaxBytesPerSec, I hope this will never be used */
   OUTLONG(0);
   OUTLONG(0);                  /* PaddingGranularity (whatever that might be) */
                                /* Other sources call it 'reserved' */
   flag = AVIF_ISINTERLEAVED;
   if(hasIndex) flag |= AVIF_HASINDEX;
   if(hasIndex && AVI->must_use_index) flag |= AVIF_MUSTUSEINDEX;
   OUTLONG(flag);               /* Flags */
   OUTLONG(0);                  // no frames yet
   OUTLONG(0);                  /* InitialFrames */

   OUTLONG(AVI->anum+1);

   OUTLONG(0);                  /* SuggestedBufferSize */
   OUTLONG(AVI->width);         /* Width */
   OUTLONG(AVI->height);        /* Height */
                                /* MS calls the following 'reserved': */
   OUTLONG(0);                  /* TimeScale:  Unit used to measure time */
   OUTLONG(0);                  /* DataRate:   Data rate of playback     */
   OUTLONG(0);                  /* StartTime:  Starting time of AVI data */
   OUTLONG(0);                  /* DataLength: Size of AVI data chunk    */


   /* Start the video stream list ---------------------------------- */

   OUT4CC ("LIST");
   OUTLONG(0);        /* Length of list in bytes, don't know yet */
   strl_start = nhb;  /* Store start position */
   OUT4CC ("strl");

   /* The video stream header */

   OUT4CC ("strh");
   OUTLONG(56);                 /* # of bytes to follow */
   OUT4CC ("vids");             /* Type */
   OUT4CC (AVI->compressor);    /* Handler */
   OUTLONG(0);                  /* Flags */
   OUTLONG(0);                  /* Reserved, MS says: wPriority, wLanguage */
   OUTLONG(0);                  /* InitialFrames */
   OUTLONG(FRAME_RATE_SCALE);              /* Scale */
   OUTLONG(frate);              /* Rate: Rate/Scale == samples/second */
   OUTLONG(0);                  /* Start */
   OUTLONG(0);                  // no frames yet
   OUTLONG(0);                  /* SuggestedBufferSize */
   OUTLONG(-1);                 /* Quality */
   OUTLONG(0);                  /* SampleSize */
   OUTLONG(0);                  /* Frame */
   OUTLONG(0);                  /* Frame */
   //   OUTLONG(0);                  /* Frame */
   //OUTLONG(0);                  /* Frame */

   /* The video stream format */

   OUT4CC ("strf");
   OUTLONG(40);                 /* # of bytes to follow */
   OUTLONG(40);                 /* Size */
   OUTLONG(AVI->width);         /* Width */
   OUTLONG(AVI->height);        /* Height */
   OUTSHRT(1); OUTSHRT(24);     /* Planes, Count */
   OUT4CC (AVI->compressor);    /* Compression */
   // ThOe (*3)
   OUTLONG(AVI->width*AVI->height*3);  /* SizeImage (in bytes?) */
   OUTLONG(0);                  /* XPelsPerMeter */
   OUTLONG(0);                  /* YPelsPerMeter */
   OUTLONG(0);                  /* ClrUsed: Number of colors used */
   OUTLONG(0);                  /* ClrImportant: Number of colors important */

   /* Finish stream list, i.e. put number of bytes in the list to proper pos */

   long2str(AVI_header+strl_start-4,nhb-strl_start);


   /* Start the audio stream list ---------------------------------- */

   for(j=0; j<AVI->anum; ++j) {

       sampsize = avi_sampsize(AVI, j);

       OUT4CC ("LIST");
       OUTLONG(0);        /* Length of list in bytes, don't know yet */
       strl_start = nhb;  /* Store start position */
       OUT4CC ("strl");

       /* The audio stream header */

       OUT4CC ("strh");
       OUTLONG(56);            /* # of bytes to follow */
       OUT4CC ("auds");

       // -----------
       // ThOe
       OUTLONG(0);             /* Format (Optionally) */
       // -----------

       OUTLONG(0);             /* Flags */
       OUTLONG(0);             /* Reserved, MS says: wPriority, wLanguage */
       OUTLONG(0);             /* InitialFrames */

       // ThOe /4
       OUTLONG(sampsize/4);      /* Scale */
       OUTLONG(1000*AVI->track[j].mp3rate/8);
       OUTLONG(0);             /* Start */
       OUTLONG(4*AVI->track[j].audio_bytes/sampsize);   /* Length */
       OUTLONG(0);             /* SuggestedBufferSize */
       OUTLONG(-1);            /* Quality */

       // ThOe /4
       OUTLONG(sampsize/4);    /* SampleSize */

       OUTLONG(0);             /* Frame */
       OUTLONG(0);             /* Frame */
       //       OUTLONG(0);             /* Frame */
       //OUTLONG(0);             /* Frame */

       /* The audio stream format */

       OUT4CC ("strf");
       OUTLONG(16);                   /* # of bytes to follow */
       OUTSHRT(AVI->track[j].a_fmt);           /* Format */
       OUTSHRT(AVI->track[j].a_chans);         /* Number of channels */
       OUTLONG(AVI->track[j].a_rate);          /* SamplesPerSec */
       // ThOe
       OUTLONG(1000*AVI->track[j].mp3rate/8);
       //ThOe (/4)

       OUTSHRT(sampsize/4);           /* BlockAlign */


       OUTSHRT(AVI->track[j].a_bits);          /* BitsPerSample */

       /* Finish stream list, i.e. put number of bytes in the list to proper pos */

       long2str(AVI_header+strl_start-4,nhb-strl_start);
   }

   /* Finish header list */

   long2str(AVI_header+hdrl_start-4,nhb-hdrl_start);


   /* Calculate the needed amount of junk bytes, output junk */

   njunk = HEADERBYTES - nhb - 8 - 12;

   /* Safety first: if njunk <= 0, somebody has played with
      HEADERBYTES without knowing what (s)he did.
      This is a fatal error */

   if(njunk<=0)
     {
       fprintf(stderr,"AVI_close_output_file: # of header bytes too small\n");
       exit(1);
     }

   OUT4CC ("JUNK");
   OUTLONG(njunk);
   memset(AVI_header+nhb,0,njunk);

   //2001-11-14 added id string

   if((size_t)njunk > strlen(id_str)+8) {
     //sprintf(id_str, "%s-%s", PACKAGE, VERSION);
     //memcpy(AVI_header+nhb, id_str, strlen(id_str));
   }

   nhb += njunk;

   /* Start the movi list */

   OUT4CC ("LIST");
   OUTLONG(movi_len); /* Length of list in bytes */
   OUT4CC ("movi");

   /* Output the header, truncate the file to the number of bytes
      actually written, report an error if someting goes wrong */

   if ( lseek(AVI->fdes,0,SEEK_SET)<0 ||
        avi_write(AVI->fdes,(char *)AVI_header,HEADERBYTES)!=HEADERBYTES ||
	lseek(AVI->fdes,AVI->pos,SEEK_SET)<0)
     {
       AVI_errno = AVI_ERR_CLOSE;
       return -1;
     }

   return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x828, %rsp            # imm = 0x828
movq %rdi, %r15
movsd 0x20(%rdi), %xmm0
xorl %eax, %eax
movsd 0xd05a5(%rip), %xmm1    # 0x4d2008
ucomisd %xmm0, %xmm1
movl $0x0, %ecx
ja 0x401a9a
movsd 0xd059a(%rip), %xmm1    # 0x4d2010
movapd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movsd 0xd0592(%rip), %xmm3    # 0x4d2018
addsd %xmm3, %xmm2
cvttsd2si %xmm2, %eax
divsd %xmm0, %xmm1
addsd %xmm3, %xmm1
cvttsd2si %xmm1, %ecx
movabsq $-0x1000ffcb9b9b6ae, %rdx # imm = 0xFEFFF00346464952
movq %rdx, 0x20(%rsp)
movaps 0xd0570(%rip), %xmm0    # 0x4d2020
movups %xmm0, 0x28(%rsp)
movabsq $0x3868697661, %rdx     # imm = 0x3868697661
movq %rdx, 0x38(%rsp)
movb %cl, 0x40(%rsp)
movb %ch, 0x41(%rsp)
movl %ecx, %edx
shrl $0x10, %edx
movb %dl, 0x42(%rsp)
shrl $0x18, %ecx
movb %cl, 0x43(%rsp)
andq $0x0, 0x44(%rsp)
cmpl $0x0, 0x428(%r15)
setne %cl
shlb $0x5, %cl
orb $0x10, %cl
movb %cl, 0x4c(%rsp)
movb $0x1, 0x4d(%rsp)
andq $0x0, 0x4e(%rsp)
andw $0x0, 0x56(%rsp)
movl 0x438(%r15), %ecx
incl %ecx
movb %cl, 0x58(%rsp)
movb %ch, 0x59(%rsp)
movl %ecx, %edx
shrl $0x10, %edx
movb %dl, 0x5a(%rsp)
shrl $0x18, %ecx
movb %cl, 0x5b(%rsp)
andl $0x0, 0x5c(%rsp)
movq 0x10(%r15), %rcx
movl %ecx, %edx
movl %ecx, 0x60(%rsp)
movq 0x18(%r15), %rsi
movb %sil, 0x64(%rsp)
movl %esi, %edi
shrl $0x8, %edi
movb %dil, 0x65(%rsp)
movl %esi, %r8d
shrl $0x10, %r8d
movb %r8b, 0x66(%rsp)
movl %esi, %r9d
shrl $0x18, %r9d
movb %r9b, 0x67(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x68(%rsp)
movl $0x5453494c, 0x78(%rsp) # imm = 0x5453494C
movaps 0xd04b6(%rip), %xmm1    # 0x4d2030
movaps %xmm1, 0x80(%rsp)
movl 0x28(%r15), %r10d
movl %r10d, 0x90(%rsp)
movabsq $0xf424000000000, %r11  # imm = 0xF424000000000
movq %r11, 0x9c(%rsp)
andq $0x0, 0x94(%rsp)
movb %al, 0xa4(%rsp)
movb %ah, 0xa5(%rsp)
movl %eax, %r11d
shrl $0x10, %r11d
movb %r11b, 0xa6(%rsp)
shrl $0x18, %eax
movb %al, 0xa7(%rsp)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0xb0(%rsp)
andq $0x0, 0xa8(%rsp)
movabsq $0x6672747300000000, %rax # imm = 0x6672747300000000
movq %rax, 0xc0(%rsp)
andq $0x0, 0xb8(%rsp)
movabsq $0x2800000028, %rax     # imm = 0x2800000028
movq %rax, 0xc8(%rsp)
movb %cl, 0xd0(%rsp)
movb %ch, 0xd1(%rsp)
movl %ecx, %eax
shrl $0x10, %eax
shrl $0x18, %edx
movb %al, 0xd2(%rsp)
movb %dl, 0xd3(%rsp)
movb %sil, 0xd4(%rsp)
movb %dil, 0xd5(%rsp)
movb %r8b, 0xd6(%rsp)
movb %r9b, 0xd7(%rsp)
movl $0x180001, 0xd8(%rsp) # imm = 0x180001
movl %r10d, 0xdc(%rsp)
movl 0x18(%r15), %eax
imull 0x10(%r15), %eax
leal (%rax,%rax,2), %eax
movb %al, 0xe0(%rsp)
movb %ah, 0xe1(%rsp)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0xe2(%rsp)
shrl $0x18, %eax
movb %al, 0xe3(%rsp)
movups %xmm0, 0xe4(%rsp)
movl $0x74, 0x7c(%rsp)
leaq 0x60(%r15), %rcx
movslq 0x438(%r15), %rsi
movl $0xd4, %edi
movl $0x718, %r14d           # imm = 0x718
xorl %ebp, %ebp
pushq $0x8
popq %r8
pushq $0x4
popq %r9
xorl %ebx, %ebx
leaq 0xd4(%rbp), %r11
cmpq %rsi, %rbx
jge 0x402030
movq 0x8(%rcx), %r13
movq 0x18(%rcx), %r12
leaq 0x7(%r12), %rax
cqto
idivq %r8
movl %r13d, %edx
imull %eax, %edx
cmpl $0x5, %edx
jge 0x401d01
movl %r9d, %edx
cmpq $0x7fc, %r11            # imm = 0x7FC
ja 0x402006
movl $0x5453494c, 0xf4(%rsp,%rbp) # imm = 0x5453494C
cmpq $0x7f9, %r11            # imm = 0x7F9
jae 0x402006
leaq 0x8(%rdi), %r10
andl $0x0, 0xf8(%rsp,%rbp)
cmpq $0x7f4, %r11            # imm = 0x7F4
ja 0x40200d
movl $0x6c727473, 0xfc(%rsp,%rbp) # imm = 0x6C727473
cmpq $0x7f0, %r11            # imm = 0x7F0
ja 0x40200d
movl $0x68727473, 0x100(%rsp,%rbp) # imm = 0x68727473
cmpq $0x7ec, %r11            # imm = 0x7EC
ja 0x40200d
movl $0x38, 0x104(%rsp,%rbp)
cmpq $0x7e8, %r11            # imm = 0x7E8
ja 0x40200d
movl $0x73647561, 0x108(%rsp,%rbp) # imm = 0x73647561
cmpq $0x7e4, %r11            # imm = 0x7E4
ja 0x40200d
andl $0x0, 0x10c(%rsp,%rbp)
cmpq $0x7e0, %r11            # imm = 0x7E0
ja 0x40200d
andl $0x0, 0x110(%rsp,%rbp)
cmpq $0x7dc, %r11            # imm = 0x7DC
ja 0x40200d
andl $0x0, 0x114(%rsp,%rbp)
cmpq $0x7d8, %r11            # imm = 0x7D8
ja 0x40200d
andl $0x0, 0x118(%rsp,%rbp)
cmpq $0x7d4, %r11            # imm = 0x7D4
ja 0x40200d
movl %edx, %eax
shrl $0x2, %eax
movl %eax, 0xc(%rsp)
movb %al, 0x11c(%rsp,%rbp)
movl %edx, %eax
shrl $0xa, %eax
movl %eax, 0x8(%rsp)
movb %al, 0x11d(%rsp,%rbp)
movl %edx, %eax
shrl $0x12, %eax
movl %eax, 0x4(%rsp)
movb %al, 0x11e(%rsp,%rbp)
movl %edx, %eax
shrl $0x1a, %eax
movl %eax, 0x14(%rsp)
movb %al, 0x11f(%rsp,%rbp)
cmpq $0x7d0, %r11            # imm = 0x7D0
ja 0x40200d
imull $0x7d, 0x20(%rcx), %eax
movl %eax, 0x120(%rsp,%rbp)
cmpq $0x7cc, %r11            # imm = 0x7CC
ja 0x40200d
andl $0x0, 0x124(%rsp,%rbp)
cmpq $0x7c8, %r11            # imm = 0x7C8
ja 0x40200d
movl %eax, 0x10(%rsp)
movq 0x30(%rcx), %rax
shlq $0x2, %rax
movl %edx, %edx
movq %rdx, 0x18(%rsp)
cqto
idivq 0x18(%rsp)
movl %eax, 0x128(%rsp,%rbp)
cmpq $0x7c4, %r11            # imm = 0x7C4
ja 0x40200d
andl $0x0, 0x12c(%rsp,%rbp)
cmpq $0x7c0, %r11            # imm = 0x7C0
ja 0x40200d
orl $-0x1, 0x130(%rsp,%rbp)
cmpq $0x7bc, %r11            # imm = 0x7BC
ja 0x40200d
movl 0xc(%rsp), %eax
movb %al, 0x134(%rsp,%rbp)
movl 0x8(%rsp), %eax
movb %al, 0x135(%rsp,%rbp)
movl 0x4(%rsp), %eax
movb %al, 0x136(%rsp,%rbp)
movl 0x14(%rsp), %eax
movb %al, 0x137(%rsp,%rbp)
cmpq $0x7b8, %r11            # imm = 0x7B8
ja 0x40200d
andl $0x0, 0x138(%rsp,%rbp)
cmpq $0x7b4, %r11            # imm = 0x7B4
ja 0x40200d
andl $0x0, 0x13c(%rsp,%rbp)
cmpq $0x7b0, %r11            # imm = 0x7B0
ja 0x40200d
movl $0x66727473, 0x140(%rsp,%rbp) # imm = 0x66727473
cmpq $0x7ac, %r11            # imm = 0x7AC
ja 0x40200d
movl $0x10, 0x144(%rsp,%rbp)
cmpq $0x7aa, %r11            # imm = 0x7AA
ja 0x40200d
movzwl (%rcx), %eax
movw %ax, 0x148(%rsp,%rbp)
cmpq $0x7a8, %r11            # imm = 0x7A8
ja 0x40200d
movw %r13w, 0x14a(%rsp,%rbp)
cmpq $0x7a4, %r11            # imm = 0x7A4
ja 0x40200d
movl 0x10(%rcx), %eax
movl %eax, 0x14c(%rsp,%rbp)
cmpq $0x7a0, %r11            # imm = 0x7A0
ja 0x40200d
movl 0x10(%rsp), %eax
movl %eax, %edx
shrl $0x8, %edx
movl %edx, 0x4(%rsp)
movl %eax, %edx
shrl $0x10, %edx
movl %eax, %r13d
shrl $0x18, %r13d
movb %al, 0x150(%rsp,%rbp)
movl 0x4(%rsp), %eax
movb %al, 0x151(%rsp,%rbp)
movb %dl, 0x152(%rsp,%rbp)
movb %r13b, 0x153(%rsp,%rbp)
cmpq $0x79e, %r11            # imm = 0x79E
ja 0x40200d
movl 0xc(%rsp), %eax
movb %al, 0x154(%rsp,%rbp)
movl 0x8(%rsp), %eax
movb %al, 0x155(%rsp,%rbp)
cmpq $0x79c, %r11            # imm = 0x79C
ja 0x40200d
movw %r12w, 0x156(%rsp,%rbp)
jmp 0x40200d
leaq 0xdc(%rbp), %r10
addq $0x64, %rdi
movslq %r10d, %rax
movl $0x5c, 0x1c(%rsp,%rax)
incq %rbx
addq $0x70, %rcx
addq $0x64, %rbp
addl $-0x64, %r14d
jmp 0x401cd1
leal 0xc0(%rbp), %eax
movl %eax, 0x30(%rsp)
movl $0x718, %eax            # imm = 0x718
subq %rbp, %rax
testl %eax, %eax
jle 0x40216d
cmpq $0x7fc, %r11            # imm = 0x7FC
ja 0x402092
movl $0x4b4e554a, 0xf4(%rsp,%rbp) # imm = 0x4B4E554A
cmpq $0x7f8, %r11            # imm = 0x7F8
ja 0x402092
movb $0x18, %cl
subb %bpl, %cl
movb %cl, 0xf8(%rsp,%rbp)
movb %ah, 0xf9(%rsp,%rbp)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0xfa(%rsp,%rbp)
movl %eax, %ecx
shrl $0x18, %ecx
movb %cl, 0xfb(%rsp,%rbp)
leaq (%rsp,%rbp), %rdi
addq $0x20, %rdi
movl $0xdc, %ebx
addq %rbx, %rdi
movl %eax, %edx
xorl %esi, %esi
callq 0x4010e8
movl %r14d, %eax
leaq (%rax,%rbp), %rcx
addq %rbx, %rcx
cmpq $0x7fc, %rcx            # imm = 0x7FC
ja 0x402108
leaq (%rsp,%rax), %rdx
addq $0x20, %rdx
movl $0x5453494c, 0xdc(%rbp,%rdx) # imm = 0x5453494C
cmpq $0x7f8, %rcx            # imm = 0x7F8
ja 0x402108
leaq (%rsp,%rax), %rdx
addq $0x20, %rdx
movl $0xfefff003, 0xe0(%rbp,%rdx) # imm = 0xFEFFF003
cmpq $0x7f4, %rcx            # imm = 0x7F4
ja 0x402108
addq %rsp, %rax
addq $0x20, %rax
movl $0x69766f6d, 0xe4(%rbp,%rax) # imm = 0x69766F6D
movl (%r15), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x47efe0
testq %rax, %rax
js 0x40214b
movl (%r15), %edi
leaq 0x20(%rsp), %rsi
movl $0x800, %edx            # imm = 0x800
callq 0x4019c5
cmpq $0x800, %rax            # imm = 0x800
jne 0x40214b
movl (%r15), %edi
movq 0x3e0(%r15), %rsi
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x47efe0
testq %rax, %rax
jns 0x402159
movq $0x6, 0x1163da(%rip) # 0x518530
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x828, %rsp            # imm = 0x828
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x516868, %rax         # imm = 0x516868
movq (%rax), %rcx
leaq 0xcff12(%rip), %rdi     # 0x4d2090
pushq $0x33
popq %rsi
pushq $0x1
popq %rdx
callq 0x42be90
pushq $0x1
popq %rdi
callq 0x4239b0