// File: /workspace/github_c_makefile_star2_O1/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 %rbx
subq $0x808, %rsp            # imm = 0x808
movq %rdi, %rbx
movsd 0x20(%rdi), %xmm0
xorl %eax, %eax
movsd 0xd34c1(%rip), %xmm1    # 0x4d5008
ucomisd %xmm0, %xmm1
movl $0x0, %ecx
ja 0x401b7e
movsd 0xd34b6(%rip), %xmm1    # 0x4d5010
movapd %xmm0, %xmm2
mulsd %xmm1, %xmm2
movsd 0xd34ae(%rip), %xmm3    # 0x4d5018
addsd %xmm3, %xmm2
cvttsd2si %xmm2, %eax
divsd %xmm0, %xmm1
addsd %xmm3, %xmm1
cvttsd2si %xmm1, %ecx
movaps 0xd349b(%rip), %xmm0    # 0x4d5020
movaps %xmm0, (%rsp)
movaps 0xd34a0(%rip), %xmm0    # 0x4d5030
movaps %xmm0, 0x10(%rsp)
movb %cl, 0x20(%rsp)
movb %ch, 0x21(%rsp)
movl %ecx, %edx
shrl $0x10, %edx
movb %dl, 0x22(%rsp)
shrl $0x18, %ecx
movb %cl, 0x23(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x24(%rsp)
cmpl $0x0, 0x428(%rbx)
setne %dl
shlb $0x5, %dl
orb $0x10, %dl
movb %dl, 0x2c(%rsp)
movb $0x1, 0x2d(%rsp)
movq %rcx, 0x2e(%rsp)
movw $0x0, 0x36(%rsp)
movl 0x438(%rbx), %edx
incl %edx
movb %dl, 0x38(%rsp)
movb %dh, 0x39(%rsp)
movl %edx, %esi
shrl $0x10, %esi
movb %sil, 0x3a(%rsp)
shrl $0x18, %edx
movb %dl, 0x3b(%rsp)
movl $0x0, 0x3c(%rsp)
movq 0x10(%rbx), %rdx
movl %edx, %esi
movl %edx, 0x40(%rsp)
movq 0x18(%rbx), %rdi
movb %dil, 0x44(%rsp)
movl %edi, %r8d
shrl $0x8, %r8d
movb %r8b, 0x45(%rsp)
movl %edi, %r9d
shrl $0x10, %r9d
movb %r9b, 0x46(%rsp)
movl %edi, %r10d
shrl $0x18, %r10d
movb %r10b, 0x47(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x48(%rsp)
movaps 0xd33f8(%rip), %xmm1    # 0x4d5040
movups %xmm1, 0x58(%rsp)
movabsq $0x7364697600000038, %r11 # imm = 0x7364697600000038
movq %r11, 0x68(%rsp)
movl 0x28(%rbx), %r11d
movl %r11d, 0x70(%rsp)
movq %rcx, 0x74(%rsp)
movabsq $0xf424000000000, %r14  # imm = 0xF424000000000
movq %r14, 0x7c(%rsp)
movb %al, 0x84(%rsp)
movb %ah, 0x85(%rsp)
movl %eax, %ebp
shrl $0x10, %ebp
movb %bpl, 0x86(%rsp)
shrl $0x18, %eax
movb %al, 0x87(%rsp)
movq %rcx, 0x88(%rsp)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x90(%rsp)
movq %rcx, 0x98(%rsp)
movabsq $0x6672747300000000, %rax # imm = 0x6672747300000000
movq %rax, 0xa0(%rsp)
movabsq $0x2800000028, %rax     # imm = 0x2800000028
movq %rax, 0xa8(%rsp)
movb %dl, 0xb0(%rsp)
movb %dh, 0xb1(%rsp)
movl %edx, %eax
shrl $0x10, %eax
shrl $0x18, %esi
movb %al, 0xb2(%rsp)
movb %sil, 0xb3(%rsp)
movb %dil, 0xb4(%rsp)
movb %r8b, 0xb5(%rsp)
movb %r9b, 0xb6(%rsp)
movb %r10b, 0xb7(%rsp)
movl $0x180001, 0xb8(%rsp) # imm = 0x180001
movl %r11d, 0xbc(%rsp)
movl 0x18(%rbx), %eax
imull 0x10(%rbx), %eax
leal (%rax,%rax,2), %eax
movb %al, 0xc0(%rsp)
movb %ah, 0xc1(%rsp)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0xc2(%rsp)
shrl $0x18, %eax
movb %al, 0xc3(%rsp)
movups %xmm0, 0xc4(%rsp)
movl $0x74, 0x5c(%rsp)
movslq 0x438(%rbx), %rax
testq %rax, %rax
jle 0x40204f
imulq $0x70, %rax, %rcx
movl $0xd4, %r15d
movabsq $0xdc00000000, %rsi     # imm = 0xDC00000000
xorl %edi, %edi
movl $0x4, %r8d
movabsq $0x6400000000, %r9      # imm = 0x6400000000
movq 0x78(%rbx,%rdi), %r10
leaq 0x7(%r10), %rax
addq $0xe, %r10
testq %rax, %rax
cmovnsq %rax, %r10
shrq $0x3, %r10
imull 0x68(%rbx,%rdi), %r10d
cmpl $0x5, %r10d
cmovll %r8d, %r10d
cmpq $0x7fc, %r15            # imm = 0x7FC
ja 0x401de7
movl $0x5453494c, (%rsp,%r15) # imm = 0x5453494C
cmpq $0x7f8, %r15            # imm = 0x7F8
ja 0x401df9
movl $0x0, 0x4(%rsp,%r15)
cmpq $0x7f4, %r15            # imm = 0x7F4
ja 0x401e0b
movl $0x6c727473, 0x8(%rsp,%r15) # imm = 0x6C727473
cmpq $0x7f0, %r15            # imm = 0x7F0
ja 0x401e1d
movl $0x68727473, 0xc(%rsp,%r15) # imm = 0x68727473
cmpq $0x7ec, %r15            # imm = 0x7EC
ja 0x401e2f
movl $0x38, 0x10(%rsp,%r15)
cmpq $0x7e8, %r15            # imm = 0x7E8
ja 0x401e41
movl $0x73647561, 0x14(%rsp,%r15) # imm = 0x73647561
cmpq $0x7e4, %r15            # imm = 0x7E4
ja 0x401e53
movl $0x0, 0x18(%rsp,%r15)
cmpq $0x7e0, %r15            # imm = 0x7E0
ja 0x401e65
movl $0x0, 0x1c(%rsp,%r15)
cmpq $0x7dc, %r15            # imm = 0x7DC
ja 0x401e77
movl $0x0, 0x20(%rsp,%r15)
cmpq $0x7d8, %r15            # imm = 0x7D8
ja 0x401e89
movl $0x0, 0x24(%rsp,%r15)
cmpq $0x7d4, %r15            # imm = 0x7D4
ja 0x401ebe
movl %r10d, %eax
shrl $0x2, %eax
movb %al, 0x28(%rsp,%r15)
movl %r10d, %eax
shrl $0xa, %eax
movb %al, 0x29(%rsp,%r15)
movl %r10d, %eax
shrl $0x12, %eax
movb %al, 0x2a(%rsp,%r15)
movl %r10d, %eax
shrl $0x1a, %eax
movb %al, 0x2b(%rsp,%r15)
cmpq $0x7d0, %r15            # imm = 0x7D0
ja 0x401ed4
imull $0x7d, 0x80(%rbx,%rdi), %eax
movl %eax, 0x2c(%rsp,%r15)
cmpq $0x7cc, %r15            # imm = 0x7CC
ja 0x401ee6
movl $0x0, 0x30(%rsp,%r15)
cmpq $0x7c8, %r15            # imm = 0x7C8
ja 0x401f08
movq 0x90(%rbx,%rdi), %rax
shlq $0x2, %rax
movl %r10d, %r11d
cqto
idivq %r11
movl %eax, 0x34(%rsp,%r15)
cmpq $0x7c4, %r15            # imm = 0x7C4
ja 0x401f1a
movl $0x0, 0x38(%rsp,%r15)
cmpq $0x7c0, %r15            # imm = 0x7C0
ja 0x401f2c
movl $0xffffffff, 0x3c(%rsp,%r15) # imm = 0xFFFFFFFF
cmpq $0x7bc, %r15            # imm = 0x7BC
ja 0x401f61
movl %r10d, %eax
shrl $0x2, %eax
movb %al, 0x40(%rsp,%r15)
movl %r10d, %eax
shrl $0xa, %eax
movb %al, 0x41(%rsp,%r15)
movl %r10d, %eax
shrl $0x12, %eax
movb %al, 0x42(%rsp,%r15)
movl %r10d, %eax
shrl $0x1a, %eax
movb %al, 0x43(%rsp,%r15)
cmpq $0x7b8, %r15            # imm = 0x7B8
ja 0x401f73
movl $0x0, 0x44(%rsp,%r15)
cmpq $0x7b4, %r15            # imm = 0x7B4
ja 0x401f85
movl $0x0, 0x48(%rsp,%r15)
cmpq $0x7b0, %r15            # imm = 0x7B0
ja 0x401f97
movl $0x66727473, 0x4c(%rsp,%r15) # imm = 0x66727473
cmpq $0x7ac, %r15            # imm = 0x7AC
ja 0x401fa9
movl $0x10, 0x50(%rsp,%r15)
cmpq $0x7aa, %r15            # imm = 0x7AA
ja 0x401fbd
movzwl 0x60(%rbx,%rdi), %eax
movw %ax, 0x54(%rsp,%r15)
cmpq $0x7a8, %r15            # imm = 0x7A8
ja 0x401fd1
movzwl 0x68(%rbx,%rdi), %eax
movw %ax, 0x56(%rsp,%r15)
cmpq $0x7a4, %r15            # imm = 0x7A4
ja 0x401fe3
movl 0x70(%rbx,%rdi), %eax
movl %eax, 0x58(%rsp,%r15)
cmpq $0x7a0, %r15            # imm = 0x7A0
ja 0x401ff9
imull $0x7d, 0x80(%rbx,%rdi), %eax
movl %eax, 0x5c(%rsp,%r15)
cmpq $0x79e, %r15            # imm = 0x79E
ja 0x402016
movl %r10d, %eax
shrl $0x2, %eax
movb %al, 0x60(%rsp,%r15)
shrl $0xa, %r10d
movb %r10b, 0x61(%rsp,%r15)
cmpq $0x79c, %r15            # imm = 0x79C
ja 0x40202a
movzwl 0x78(%rbx,%rdi), %eax
movw %ax, 0x62(%rsp,%r15)
addq $0x64, %r15
movq %rsi, %rax
sarq $0x20, %rax
movl $0x5c, -0x4(%rsp,%rax)
addq $0x70, %rdi
addq %r9, %rsi
cmpq %rdi, %rcx
jne 0x401db0
jmp 0x402055
movl $0xd4, %r15d
leal -0x14(%r15), %eax
movl %eax, 0x10(%rsp)
movl $0x7ec, %eax            # imm = 0x7EC
subl %r15d, %eax
testl %eax, %eax
jle 0x402156
cmpq $0x7fc, %r15            # imm = 0x7FC
ja 0x40207e
movl $0x4b4e554a, (%rsp,%r15) # imm = 0x4B4E554A
cmpq $0x7f8, %r15            # imm = 0x7F8
ja 0x40208c
movl %eax, 0x4(%rsp,%r15)
leaq (%rsp,%r15), %rdi
addq $0x8, %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 0x4020b8
movl $0x5453494c, (%rsp,%rax) # imm = 0x5453494C
cmpq $0x7f8, %rax            # imm = 0x7F8
ja 0x4020c8
movl $0xfefff003, 0x4(%rsp,%rax) # imm = 0xFEFFF003
cmpq $0x7f4, %rax            # imm = 0x7F4
ja 0x4020d8
movl $0x69766f6d, 0x8(%rsp,%rax) # imm = 0x69766F6D
movl (%rbx), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x481860
testq %rax, %rax
js 0x402136
movl (%rbx), %ebp
xorl %r14d, %r14d
leaq (%rsp,%r14), %rsi
movl $0x800, %edx            # imm = 0x800
subq %r14, %rdx
movl %ebp, %edi
callq 0x4817c0
testq %rax, %rax
js 0x402113
addq %rax, %r14
cmpq $0x800, %r14            # imm = 0x800
jb 0x4020ed
jmp 0x402116
movq %rax, %r14
cmpq $0x800, %r14            # imm = 0x800
jne 0x402136
movl (%rbx), %edi
movq 0x3e0(%rbx), %rsi
xorl %ebx, %ebx
xorl %edx, %edx
callq 0x481860
testq %rax, %rax
jns 0x402146
movq $0x6, 0x1183ef(%rip) # 0x51a530
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x808, %rsp            # imm = 0x808
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x4011db