// File: /workspace/github_c_makefile_star2_O3/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 $0x818, %rsp            # imm = 0x818
movq %rdi, %rbx
movsd 0x20(%rdi), %xmm0
xorl %eax, %eax
movsd 0xd44e5(%rip), %xmm1    # 0x4d6008
ucomisd %xmm0, %xmm1
movl $0x0, %ecx
ja 0x401b5a
movsd 0xd44da(%rip), %xmm1    # 0x4d6010
movapd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movsd 0xd44d2(%rip), %xmm3    # 0x4d6018
addsd %xmm3, %xmm2
cvttsd2si %xmm2, %eax
divsd %xmm0, %xmm1
addsd %xmm3, %xmm1
cvttsd2si %xmm1, %ecx
movabsq $-0x1000ffcb9b9b6ae, %rdx # imm = 0xFEFFF00346464952
movq %rdx, 0x10(%rsp)
movaps 0xd44b0(%rip), %xmm0    # 0x4d6020
movups %xmm0, 0x18(%rsp)
movabsq $0x3868697661, %rdx     # imm = 0x3868697661
movq %rdx, 0x28(%rsp)
movb %cl, 0x30(%rsp)
movb %ch, 0x31(%rsp)
movl %ecx, %edx
shrl $0x10, %edx
movb %dl, 0x32(%rsp)
shrl $0x18, %ecx
movb %cl, 0x33(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x34(%rsp)
cmpl $0x0, 0x428(%rbx)
setne %dl
shlb $0x5, %dl
orb $0x10, %dl
movb %dl, 0x3c(%rsp)
movb $0x1, 0x3d(%rsp)
movq %rcx, 0x3e(%rsp)
movw $0x0, 0x46(%rsp)
movl 0x438(%rbx), %edx
incl %edx
movb %dl, 0x48(%rsp)
movb %dh, 0x49(%rsp)
movl %edx, %esi
shrl $0x10, %esi
movb %sil, 0x4a(%rsp)
shrl $0x18, %edx
movb %dl, 0x4b(%rsp)
movl $0x0, 0x4c(%rsp)
movq 0x10(%rbx), %rdx
movl %edx, %esi
movl %edx, 0x50(%rsp)
movq 0x18(%rbx), %rdi
movb %dil, 0x54(%rsp)
movl %edi, %r8d
shrl $0x8, %r8d
movb %r8b, 0x55(%rsp)
movl %edi, %r9d
shrl $0x10, %r9d
movb %r9b, 0x56(%rsp)
movl %edi, %r10d
shrl $0x18, %r10d
movb %r10b, 0x57(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x58(%rsp)
movl $0x5453494c, 0x68(%rsp) # imm = 0x5453494C
movaps 0xd43f1(%rip), %xmm1    # 0x4d6030
movaps %xmm1, 0x70(%rsp)
movl 0x28(%rbx), %r11d
movl %r11d, 0x80(%rsp)
movq %rcx, 0x84(%rsp)
movabsq $0xf424000000000, %r14  # imm = 0xF424000000000
movq %r14, 0x8c(%rsp)
movb %al, 0x94(%rsp)
movb %ah, 0x95(%rsp)
movl %eax, %ebp
shrl $0x10, %ebp
movb %bpl, 0x96(%rsp)
shrl $0x18, %eax
movb %al, 0x97(%rsp)
movq %rcx, 0x98(%rsp)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0xa0(%rsp)
movq %rcx, 0xa8(%rsp)
movabsq $0x6672747300000000, %rax # imm = 0x6672747300000000
movq %rax, 0xb0(%rsp)
movabsq $0x2800000028, %rax     # imm = 0x2800000028
movq %rax, 0xb8(%rsp)
movb %dl, 0xc0(%rsp)
movb %dh, 0xc1(%rsp)
movl %edx, %eax
shrl $0x10, %eax
shrl $0x18, %esi
movb %al, 0xc2(%rsp)
movb %sil, 0xc3(%rsp)
movb %dil, 0xc4(%rsp)
movb %r8b, 0xc5(%rsp)
movb %r9b, 0xc6(%rsp)
movb %r10b, 0xc7(%rsp)
movl $0x180001, 0xc8(%rsp) # imm = 0x180001
movl %r11d, 0xcc(%rsp)
movl 0x18(%rbx), %eax
imull 0x10(%rbx), %eax
leal (%rax,%rax,2), %eax
movb %al, 0xd0(%rsp)
movb %ah, 0xd1(%rsp)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0xd2(%rsp)
shrl $0x18, %eax
movb %al, 0xd3(%rsp)
movups %xmm0, 0xd4(%rsp)
movl $0x74, 0x6c(%rsp)
movslq 0x438(%rbx), %rcx
testq %rcx, %rcx
jle 0x402085
leaq 0x90(%rbx), %rsi
movl $0xd4, %r15d
xorl %edi, %edi
movl $0x4, %r8d
movq -0x18(%rsi), %r10
leaq 0x7(%r10), %rax
leaq 0xe(%r10), %r9
testq %rax, %rax
cmovnsq %rax, %r9
movq -0x28(%rsi), %r11
shrq $0x3, %r9
movl %r11d, %edx
imull %r9d, %edx
cmpl $0x5, %edx
cmovll %r8d, %edx
cmpq $0x7fc, %r15            # imm = 0x7FC
ja 0x402060
movl $0x5453494c, 0x10(%rsp,%r15) # imm = 0x5453494C
cmpq $0x7f9, %r15            # imm = 0x7F9
jae 0x402060
leaq 0x8(%r15), %r9
movl $0x0, 0x14(%rsp,%r15)
cmpq $0x7f4, %r15            # imm = 0x7F4
ja 0x402064
movl $0x6c727473, 0x18(%rsp,%r15) # imm = 0x6C727473
cmpq $0x7f0, %r15            # imm = 0x7F0
ja 0x402064
movl $0x68727473, 0x1c(%rsp,%r15) # imm = 0x68727473
cmpq $0x7ec, %r15            # imm = 0x7EC
ja 0x402064
movl $0x38, 0x20(%rsp,%r15)
cmpq $0x7e8, %r15            # imm = 0x7E8
ja 0x402064
movl $0x73647561, 0x24(%rsp,%r15) # imm = 0x73647561
cmpq $0x7e4, %r15            # imm = 0x7E4
ja 0x402064
movl $0x0, 0x28(%rsp,%r15)
cmpq $0x7e0, %r15            # imm = 0x7E0
ja 0x402064
movl $0x0, 0x2c(%rsp,%r15)
cmpq $0x7dc, %r15            # imm = 0x7DC
ja 0x402064
movl $0x0, 0x30(%rsp,%r15)
cmpq $0x7d8, %r15            # imm = 0x7D8
ja 0x402064
movl $0x0, 0x34(%rsp,%r15)
cmpq $0x7d4, %r15            # imm = 0x7D4
ja 0x402064
movl %edx, %ebp
shrl $0x2, %ebp
movb %bpl, 0x38(%rsp,%r15)
movl %edx, %r14d
shrl $0xa, %r14d
movb %r14b, 0x39(%rsp,%r15)
movl %edx, %r13d
shrl $0x12, %r13d
movb %r13b, 0x3a(%rsp,%r15)
movl %edx, %r12d
shrl $0x1a, %r12d
movb %r12b, 0x3b(%rsp,%r15)
cmpq $0x7d0, %r15            # imm = 0x7D0
ja 0x402064
imull $0x7d, -0x10(%rsi), %eax
movl %eax, 0x3c(%rsp,%r15)
cmpq $0x7cc, %r15            # imm = 0x7CC
ja 0x402064
movl $0x0, 0x40(%rsp,%r15)
cmpq $0x7c8, %r15            # imm = 0x7C8
ja 0x402064
movl %eax, 0x4(%rsp)
movq (%rsi), %rax
shlq $0x2, %rax
movl %edx, %edx
movq %rdx, 0x8(%rsp)
cqto
idivq 0x8(%rsp)
movl %eax, 0x44(%rsp,%r15)
cmpq $0x7c4, %r15            # imm = 0x7C4
ja 0x402064
movl $0x0, 0x48(%rsp,%r15)
cmpq $0x7c0, %r15            # imm = 0x7C0
ja 0x402064
movl $0xffffffff, 0x4c(%rsp,%r15) # imm = 0xFFFFFFFF
cmpq $0x7bc, %r15            # imm = 0x7BC
ja 0x402064
movb %bpl, 0x50(%rsp,%r15)
movb %r14b, 0x51(%rsp,%r15)
movb %r13b, 0x52(%rsp,%r15)
movb %r12b, 0x53(%rsp,%r15)
cmpq $0x7b8, %r15            # imm = 0x7B8
ja 0x402064
movl $0x0, 0x54(%rsp,%r15)
cmpq $0x7b4, %r15            # imm = 0x7B4
ja 0x402064
movl $0x0, 0x58(%rsp,%r15)
cmpq $0x7b0, %r15            # imm = 0x7B0
ja 0x402064
movl $0x66727473, 0x5c(%rsp,%r15) # imm = 0x66727473
cmpq $0x7ac, %r15            # imm = 0x7AC
ja 0x402064
movl $0x10, 0x60(%rsp,%r15)
cmpq $0x7aa, %r15            # imm = 0x7AA
ja 0x402064
movzwl -0x30(%rsi), %eax
movw %ax, 0x64(%rsp,%r15)
cmpq $0x7a8, %r15            # imm = 0x7A8
ja 0x402064
movw %r11w, 0x66(%rsp,%r15)
cmpq $0x7a4, %r15            # imm = 0x7A4
ja 0x402064
movl -0x20(%rsi), %eax
movl %eax, 0x68(%rsp,%r15)
cmpq $0x7a0, %r15            # imm = 0x7A0
ja 0x402064
movl 0x4(%rsp), %r12d
movl %r12d, %eax
shrl $0x8, %eax
movl %r12d, %edx
shrl $0x10, %edx
movl %r12d, %r11d
shrl $0x18, %r11d
movb %r12b, 0x6c(%rsp,%r15)
movb %al, 0x6d(%rsp,%r15)
movb %dl, 0x6e(%rsp,%r15)
movb %r11b, 0x6f(%rsp,%r15)
cmpq $0x79e, %r15            # imm = 0x79E
ja 0x402064
movb %bpl, 0x70(%rsp,%r15)
movb %r14b, 0x71(%rsp,%r15)
cmpq $0x79c, %r15            # imm = 0x79C
ja 0x402064
movw %r10w, 0x72(%rsp,%r15)
jmp 0x402064
leaq 0x8(%r15), %r9
addq $0x64, %r15
movslq %r9d, %rax
movl $0x5c, 0xc(%rsp,%rax)
incq %rdi
addq $0x70, %rsi
cmpq %rcx, %rdi
jb 0x401d90
jmp 0x40208b
movl $0xd4, %r15d
leal -0x14(%r15), %eax
movl %eax, 0x20(%rsp)
movl $0x7ec, %eax            # imm = 0x7EC
subl %r15d, %eax
testl %eax, %eax
jle 0x40218a
cmpq $0x7fc, %r15            # imm = 0x7FC
ja 0x4020c3
movl $0x4b4e554a, 0x10(%rsp,%r15) # imm = 0x4B4E554A
cmpq $0x7f8, %r15            # imm = 0x7F8
ja 0x4020c3
movl %eax, 0x14(%rsp,%r15)
leaq (%rsp,%r15), %rdi
addq $0x18, %rdi
movl %eax, %r14d
xorl %esi, %esi
movq %r14, %rdx
callq 0x4010e0
leaq (%r15,%r14), %rax
addq $0x8, %rax
cmpq $0x7fc, %rax            # imm = 0x7FC
ja 0x402110
movl $0x5453494c, 0x10(%rsp,%rax) # imm = 0x5453494C
cmpq $0x7f8, %rax            # imm = 0x7F8
ja 0x402110
movl $0xfefff003, 0x14(%rsp,%rax) # imm = 0xFEFFF003
cmpq $0x7f4, %rax            # imm = 0x7F4
ja 0x402110
movl $0x69766f6d, 0x18(%rsp,%rax) # imm = 0x69766F6D
movl (%rbx), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x482720
testq %rax, %rax
js 0x402166
movl (%rbx), %ebp
xorl %r14d, %r14d
leaq (%rsp,%r14), %rsi
addq $0x10, %rsi
movl $0x800, %edx            # imm = 0x800
subq %r14, %rdx
movl %ebp, %edi
callq 0x482680
testq %rax, %rax
js 0x402166
addq %rax, %r14
cmpq $0x800, %r14            # imm = 0x800
jb 0x402125
jne 0x402166
movl (%rbx), %edi
movq 0x3e0(%rbx), %rsi
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x482720
testq %rax, %rax
jns 0x402176
movq $0x6, 0x11939f(%rip) # 0x51b510
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x818, %rsp            # imm = 0x818
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4011db