// File: /workspace/github_c_makefile_star2_O1/archer1357[P]Offline-Raytracer/avilib.c
// Function: avi_parse_input_file
int avi_parse_input_file(avi_t *AVI, int getIndex)
{
  long i, rate, scale, idx_type;
  off_t n;
  unsigned char *hdrl_data;
  long header_offset=0, hdrl_len=0;
  long nvi, nai[AVI_MAX_TRACKS], ioff;
  long tot[AVI_MAX_TRACKS];
  int j;
  int lasttag = 0;
  int vids_strh_seen = 0;
  int vids_strf_seen = 0;
  int auds_strh_seen = 0;
  //  int auds_strf_seen = 0;
  int num_stream = 0;
  char data[256];

  /* Read first 12 bytes and check that this is an AVI file */

   if( avi_read(AVI->fdes,data,12) != 12 ) ERR_EXIT(AVI_ERR_READ)

   if( strncasecmp(data  ,"RIFF",4) !=0 ||
       strncasecmp(data+8,"AVI ",4) !=0 ) ERR_EXIT(AVI_ERR_NO_AVI)

   /* Go through the AVI file and extract the header list,
      the start position of the 'movi' list and an optionally
      present idx1 tag */

   hdrl_data = 0;

   while(1)
   {
      if( avi_read(AVI->fdes,data,8) != 8 ) break; /* We assume it's EOF */

      n = str2ulong((unsigned char *)data+4);
      n = PAD_EVEN(n);

      if(strncasecmp(data,"LIST",4) == 0)
      {
         if( avi_read(AVI->fdes,data,4) != 4 ) ERR_EXIT(AVI_ERR_READ)
         n -= 4;
         if(strncasecmp(data,"hdrl",4) == 0)
         {
            hdrl_len = n;
            hdrl_data = (unsigned char *) malloc(n);
            if(hdrl_data==0) ERR_EXIT(AVI_ERR_NO_MEM);

	    // offset of header

	    header_offset = lseek(AVI->fdes,0,SEEK_CUR);

            if( avi_read(AVI->fdes,(char *)hdrl_data,n) != n ) ERR_EXIT(AVI_ERR_READ)
         }
         else if(strncasecmp(data,"movi",4) == 0)
         {
            AVI->movi_start = lseek(AVI->fdes,0,SEEK_CUR);
            lseek(AVI->fdes,n,SEEK_CUR);
         }
         else
            lseek(AVI->fdes,n,SEEK_CUR);
      }
      else if(strncasecmp(data,"idx1",4) == 0)
      {
         /* n must be a multiple of 16, but the reading does not
            break if this is not the case */

         AVI->n_idx = AVI->max_idx = n/16;
         AVI->idx = (unsigned  char((*)[16]) ) malloc(n);
         if(AVI->idx==0) ERR_EXIT(AVI_ERR_NO_MEM)
         if(avi_read(AVI->fdes, (char *) AVI->idx, n) != n ) ERR_EXIT(AVI_ERR_READ)
      }
      else
         lseek(AVI->fdes,n,SEEK_CUR);
   }

   if(!hdrl_data      ) ERR_EXIT(AVI_ERR_NO_HDRL)
   if(!AVI->movi_start) ERR_EXIT(AVI_ERR_NO_MOVI)

   /* Interpret the header list */

   for(i=0;i<hdrl_len;)
   {
      /* List tags are completly ignored */

      if(strncasecmp((char *)hdrl_data+i,"LIST",4)==0) { i+= 12; continue; }

      n = str2ulong(hdrl_data+i+4);
      n = PAD_EVEN(n);

      /* Interpret the tag and its args */

      if(strncasecmp((char *)hdrl_data+i,"strh",4)==0)
      {
         i += 8;
         if(strncasecmp((char *)hdrl_data+i,"vids",4) == 0 && !vids_strh_seen)
         {
            memcpy(AVI->compressor,hdrl_data+i+4,4);
            AVI->compressor[4] = 0;

	    // ThOe
	    AVI->v_codech_off = header_offset + i+4;

            scale = str2ulong(hdrl_data+i+20);
            rate  = str2ulong(hdrl_data+i+24);
            if(scale!=0) AVI->fps = (double)rate/(double)scale;
            AVI->video_frames = str2ulong(hdrl_data+i+32);
            AVI->video_strn = num_stream;
	    AVI->max_len = 0;
            vids_strh_seen = 1;
            lasttag = 1; /* vids */
         }
         else if (strncasecmp ((char *)hdrl_data+i,"auds",4) ==0 && ! auds_strh_seen)
         {

	   //inc audio tracks
	   AVI->aptr=AVI->anum;
	   ++AVI->anum;

	   if(AVI->anum > AVI_MAX_TRACKS) {
	     fprintf(stderr, "error - only %d audio tracks supported\n", AVI_MAX_TRACKS);
	     return(-1);
	   }

	   AVI->track[AVI->aptr].audio_bytes = str2ulong(hdrl_data+i+32)*avi_sampsize(AVI, 0);
	   AVI->track[AVI->aptr].audio_strn = num_stream;
	   //	   auds_strh_seen = 1;
	   lasttag = 2; /* auds */

	   // ThOe
	   AVI->track[AVI->aptr].a_codech_off = header_offset + i;

         }
         else
            lasttag = 0;
         num_stream++;
      }
      else if(strncasecmp((char *)hdrl_data+i,"strf",4)==0)
      {
         i += 8;
         if(lasttag == 1)
         {
            BITMAPINFOHEADER_avilib bih;

            memcpy(&bih, hdrl_data + i, sizeof(BITMAPINFOHEADER_avilib));
            AVI->bitmap_info_header = (BITMAPINFOHEADER_avilib *)malloc(bih.bi_size);
            if (AVI->bitmap_info_header != NULL)
              memcpy(AVI->bitmap_info_header, hdrl_data + i, bih.bi_size);

            AVI->width  = str2ulong(hdrl_data+i+4);
            AVI->height = str2ulong(hdrl_data+i+8);
                    vids_strf_seen = 1;
	    //ThOe
	    AVI->v_codecf_off = header_offset + i+16;

	    memcpy(AVI->compressor2, hdrl_data+i+16, 4);
            AVI->compressor2[4] = 0;

         }
         else if(lasttag == 2)
         {
            WAVEFORMATEX_avilib *wfe;
	    char *nwfe;
            int wfes;

            if ((hdrl_len - i) < sizeof(WAVEFORMATEX_avilib))
              wfes = hdrl_len - i;
            else
              wfes = sizeof(WAVEFORMATEX_avilib);
            wfe = (WAVEFORMATEX_avilib *)malloc(sizeof(WAVEFORMATEX_avilib));
            if (wfe != NULL) {
              memset(wfe, 0, sizeof(WAVEFORMATEX_avilib));
	      memcpy(wfe, hdrl_data + i, wfes);
	      if (wfe->cb_size != 0) {
		nwfe = (char *)realloc(wfe, sizeof(WAVEFORMATEX_avilib) +
				       wfe->cb_size);
		if (nwfe != 0) {
		  off_t lpos = lseek(AVI->fdes, 0, SEEK_CUR);
		  lseek(AVI->fdes, header_offset + i + sizeof(WAVEFORMATEX_avilib),
			SEEK_SET);
		  wfe = (WAVEFORMATEX_avilib *)nwfe;
		  nwfe = &nwfe[sizeof(WAVEFORMATEX_avilib)];
		  avi_read(AVI->fdes, nwfe, wfe->cb_size);
		  lseek(AVI->fdes, lpos, SEEK_SET);
		}
	      }
	      AVI->wave_format_ex[AVI->aptr] = wfe;
	    }

            AVI->track[AVI->aptr].a_fmt   = str2ushort(hdrl_data+i  );

	    //ThOe
	    AVI->track[AVI->aptr].a_codecf_off = header_offset + i;

            AVI->track[AVI->aptr].a_chans = str2ushort(hdrl_data+i+2);
            AVI->track[AVI->aptr].a_rate  = str2ulong (hdrl_data+i+4);
	    //ThOe: read mp3bitrate
	    AVI->track[AVI->aptr].mp3rate = 8*str2ulong(hdrl_data+i+8)/1000;
	    //:ThOe
            AVI->track[AVI->aptr].a_bits  = str2ushort(hdrl_data+i+14);
	    //            auds_strf_seen = 1;
         }
         lasttag = 0;
      }
      else
      {
         i += 8;
         lasttag = 0;
      }

      i += n;
   }

   free(hdrl_data);

   if(!vids_strh_seen || !vids_strf_seen) ERR_EXIT(AVI_ERR_NO_VIDS)

   AVI->video_tag[0] = (char)(AVI->video_strn/10) + '0';
   AVI->video_tag[1] = (char)(AVI->video_strn%10) + '0';
   AVI->video_tag[2] = 'd';
   AVI->video_tag[3] = 'b';

   /* Audio tag is set to "99wb" if no audio present */
   if(!AVI->track[0].a_chans) AVI->track[0].audio_strn = 99;

   for(j=0; j<AVI->anum; ++j) {
     AVI->track[j].audio_tag[0] = (j+1)/10 + '0';
     AVI->track[j].audio_tag[1] = (j+1)%10 + '0';
     AVI->track[j].audio_tag[2] = 'w';
     AVI->track[j].audio_tag[3] = 'b';
   }

   lseek(AVI->fdes,AVI->movi_start,SEEK_SET);

   /* get index if wanted */

   if(!getIndex) return(0);

   /* if the file has an idx1, check if this is relative
      to the start of the file or to the start of the movi list */

   idx_type = 0;

   if(AVI->idx)
   {
      off_t pos, len;

      /* Search the first videoframe in the idx1 and look where
         it is in the file */

      for(i=0;i<AVI->n_idx;i++)
         if( strncasecmp((char *)AVI->idx[i],(char *)AVI->video_tag,3)==0 ) break;
      if(i>=AVI->n_idx) ERR_EXIT(AVI_ERR_NO_VIDS)

      pos = str2ulong(AVI->idx[i]+ 8);
      len = str2ulong(AVI->idx[i]+12);

      lseek(AVI->fdes,pos,SEEK_SET);
      if(avi_read(AVI->fdes,data,8)!=8) ERR_EXIT(AVI_ERR_READ)
      if( strncasecmp(data,(char *)AVI->idx[i],4)==0 && str2ulong((unsigned char *)data+4)==len )
      {
         idx_type = 1; /* Index from start of file */
      }
      else
      {
         lseek(AVI->fdes,pos+AVI->movi_start-4,SEEK_SET);
         if(avi_read(AVI->fdes,data,8)!=8) ERR_EXIT(AVI_ERR_READ)
         if( strncasecmp(data,(char *)AVI->idx[i],4)==0 && str2ulong((unsigned char *)data+4)==len )
         {
            idx_type = 2; /* Index from start of movi list */
         }
      }
      /* idx_type remains 0 if neither of the two tests above succeeds */
   }

   if(idx_type == 0)
   {
      /* we must search through the file to get the index */

      lseek(AVI->fdes, AVI->movi_start, SEEK_SET);

      AVI->n_idx = 0;

      while(1)
      {
         if( avi_read(AVI->fdes,data,8) != 8 ) break;
         n = str2ulong((unsigned char *)data+4);

         /* The movi list may contain sub-lists, ignore them */

         if(strncasecmp(data,"LIST",4)==0)
         {
            lseek(AVI->fdes,4,SEEK_CUR);
            continue;
         }

         /* Check if we got a tag ##db, ##dc or ##wb */

         if( ( (data[2]=='d' || data[2]=='D') &&
               (data[3]=='b' || data[3]=='B' || data[3]=='c' || data[3]=='C') )
	     || ( (data[2]=='w' || data[2]=='W') &&
		  (data[3]=='b' || data[3]=='B') ) )
	   {
	   avi_add_index_entry(AVI,(unsigned char *)data,0,lseek(AVI->fdes,0,SEEK_CUR)-8,n);
         }

         lseek(AVI->fdes,PAD_EVEN(n),SEEK_CUR);
      }
      idx_type = 1;
   }

   /* Now generate the video index and audio index arrays */

   nvi = 0;
   for(j=0; j<AVI->anum; ++j) nai[j] = 0;

   for(i=0;i<AVI->n_idx;i++) {

     if(strncasecmp((char *)AVI->idx[i],AVI->video_tag,3) == 0) nvi++;

     for(j=0; j<AVI->anum; ++j) if(strncasecmp((char *)AVI->idx[i], AVI->track[j].audio_tag,4) == 0) nai[j]++;
   }

   AVI->video_frames = nvi;
   for(j=0; j<AVI->anum; ++j) AVI->track[j].audio_chunks = nai[j];

//   fprintf(stderr, "chunks = %ld %d %s\n", AVI->track[0].audio_chunks, AVI->anum, AVI->track[0].audio_tag);

   if(AVI->video_frames==0) ERR_EXIT(AVI_ERR_NO_VIDS);
   AVI->video_index = (video_index_entry *) malloc(nvi*sizeof(video_index_entry));
   if(AVI->video_index==0) ERR_EXIT(AVI_ERR_NO_MEM);

   for(j=0; j<AVI->anum; ++j) {
       if(AVI->track[j].audio_chunks) {
	   AVI->track[j].audio_index = (audio_index_entry *) malloc((nai[j]+1)*sizeof(audio_index_entry));
	   memset(AVI->track[j].audio_index, 0, (nai[j]+1)*(sizeof(audio_index_entry)));
	   if(AVI->track[j].audio_index==0) ERR_EXIT(AVI_ERR_NO_MEM);
       }
   }

   nvi = 0;
   for(j=0; j<AVI->anum; ++j) nai[j] = tot[j] = 0;

   ioff = idx_type == 1 ? 8 : AVI->movi_start+4;

   for(i=0;i<AVI->n_idx;i++) {

     //video
     if(strncasecmp((char *)AVI->idx[i],AVI->video_tag,3) == 0) {
       AVI->video_index[nvi].key = str2ulong(AVI->idx[i]+ 4);
       AVI->video_index[nvi].pos = str2ulong(AVI->idx[i]+ 8)+ioff;
       AVI->video_index[nvi].len = str2ulong(AVI->idx[i]+12);
       nvi++;
     }

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

       if(strncasecmp((char *)AVI->idx[i],AVI->track[j].audio_tag,4) == 0) {
	 AVI->track[j].audio_index[nai[j]].pos = str2ulong(AVI->idx[i]+ 8)+ioff;
	 AVI->track[j].audio_index[nai[j]].len = str2ulong(AVI->idx[i]+12);
	 AVI->track[j].audio_index[nai[j]].tot = tot[j];
	 tot[j] += AVI->track[j].audio_index[nai[j]].len;
	 nai[j]++;
       }
     }
   }


   for(j=0; j<AVI->anum; ++j) AVI->track[j].audio_bytes = tot[j];

   /* Reposition the file */

   lseek(AVI->fdes,AVI->movi_start,SEEK_SET);
   AVI->video_pos = 0;

   return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movl %esi, %r15d
movq %rdi, %rbx
movl (%rdi), %ebp
xorl %r14d, %r14d
leaq (%rsp,%r14), %rsi
addq $0xb0, %rsi
movl $0xc, %edx
subq %r14, %rdx
movl %ebp, %edi
callq 0x481720
testq %rax, %rax
jle 0x402f34
addq %rax, %r14
cmpq $0xb, %r14
jbe 0x402f0c
cmpq $0xc, %r14
jne 0x4031b9
leaq 0xd2157(%rip), %rsi     # 0x4d509c
leaq 0xb0(%rsp), %rdi
movl $0x4, %edx
callq 0x401070
testl %eax, %eax
jne 0x402f78
leaq 0xb8(%rsp), %rdi
leaq 0xd2137(%rip), %rsi     # 0x4d50a1
movl $0x4, %edx
callq 0x401070
testl %eax, %eax
je 0x402f90
movq %rbx, %rdi
callq 0x402633
movq $0x9, 0x1175a5(%rip) # 0x51a530
jmp 0x4031cc
movl %r15d, 0x28(%rsp)
xorl %r14d, %r14d
leaq 0xd2107(%rip), %r12     # 0x4d50a6
leaq 0xb0(%rsp), %r13
movq $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
movq %r14, 0x20(%rsp)
movl (%rbx), %ebp
xorl %r14d, %r14d
leaq (%rsp,%r14), %rsi
addq $0xb0, %rsi
movl $0x8, %edx
subq %r14, %rdx
movl %ebp, %edi
callq 0x481720
testq %rax, %rax
jle 0x402fea
addq %rax, %r14
cmpq $0x7, %r14
jbe 0x402fc2
cmpq $0x8, %r14
jne 0x4031e4
movslq 0xb4(%rsp), %r14
incq %r14
andq $-0x2, %r14
movl $0x4, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x401070
testl %eax, %eax
je 0x4030a9
movl $0x4, %edx
movq %r13, %rdi
leaq 0xd20d8(%rip), %rsi     # 0x4d5102
callq 0x401070
testl %eax, %eax
jne 0x403129
leaq 0xf(%r14), %rax
testq %r14, %r14
cmovnsq %r14, %rax
sarq $0x4, %rax
movq %rax, 0x3f0(%rbx)
movq %rax, 0x3e8(%rbx)
movq %r14, %rdi
callq 0x4449d0
movq %rax, 0x408(%rbx)
testq %rax, %rax
je 0x403cfc
movq %r12, %r13
testq %r14, %r14
je 0x40313d
movq %rax, %rbp
movl (%rbx), %r15d
xorl %r12d, %r12d
leaq (%r12,%rbp), %rsi
movq %r14, %rdx
subq %r12, %rdx
movl %r15d, %edi
callq 0x481720
testq %rax, %rax
jle 0x403140
addq %rax, %r12
cmpq %r14, %r12
jb 0x403081
jmp 0x403140
movl (%rbx), %ebp
xorl %r15d, %r15d
leaq (%rsp,%r15), %rsi
addq $0xb0, %rsi
movl $0x4, %edx
subq %r15, %rdx
movl %ebp, %edi
callq 0x481720
testq %rax, %rax
jle 0x4030d6
addq %rax, %r15
cmpq $0x3, %r15
jbe 0x4030ae
cmpq $0x4, %r15
jne 0x4031b9
addq $-0x4, %r14
movl $0x4, %edx
movq %r13, %rdi
leaq 0xd1fb8(%rip), %rsi     # 0x4d50ab
callq 0x401070
testl %eax, %eax
je 0x403156
movl $0x4, %edx
movq %r13, %rdi
leaq 0xd1fed(%rip), %rsi     # 0x4d50f8
callq 0x401070
movl (%rbx), %edi
testl %eax, %eax
jne 0x40312b
xorl %esi, %esi
movl $0x1, %edx
callq 0x481860
movq %rax, 0x430(%rbx)
movl (%rbx), %edi
movq %r14, %rsi
movl $0x1, %edx
callq 0x481860
jmp 0x402fbd
xorl %r12d, %r12d
cmpq %r14, %r12
movq %r13, %r12
leaq 0xb0(%rsp), %r13
je 0x402fbd
jmp 0x4031b9
movq %r14, %rdi
callq 0x4449d0
testq %rax, %rax
je 0x403cfc
movq %rax, (%rsp)
movl (%rbx), %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x481860
movq %rax, 0x8(%rsp)
movl $0x0, %r15d
testq %r14, %r14
je 0x4031b0
movl (%rbx), %ebp
xorl %r15d, %r15d
movq (%rsp), %rax
leaq (%rax,%r15), %rsi
movq %r14, %rdx
subq %r15, %rdx
movl %ebp, %edi
callq 0x481720
testq %rax, %rax
jle 0x4031b0
addq %rax, %r15
cmpq %r14, %r15
jb 0x40318e
cmpq %r14, %r15
je 0x402fb8
movq %rbx, %rdi
callq 0x402633
movq $0x3, 0x117364(%rip) # 0x51a530
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, (%rsp)
je 0x40362d
cmpq $0x0, 0x430(%rbx)
je 0x403645
movq 0x20(%rsp), %rax
testq %rax, %rax
jle 0x40365d
leaq 0x60(%rbx), %rcx
movq %rcx, 0x30(%rsp)
addq $-0x8, %rax
movq %rax, 0x68(%rsp)
xorl %ebp, %ebp
movl $0x0, 0x38(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x10(%rsp)
xorl %r15d, %r15d
movq %rbp, %r13
movq (%rsp), %r12
movq 0x20(%rsp), %rbp
leaq (%r12,%r13), %r14
movl $0x4, %edx
movq %r14, %rdi
leaq 0xd1e4d(%rip), %rsi     # 0x4d50a6
callq 0x401070
testl %eax, %eax
jne 0x403270
addq $0xc, %r13
cmpq %rbp, %r13
jl 0x403246
jmp 0x40366d
movslq 0x4(%r12,%r13), %rbp
movl $0x4, %edx
movq %r14, %rdi
leaq 0xd1e2c(%rip), %rsi     # 0x4d50b0
callq 0x401070
testl %eax, %eax
je 0x403457
movl $0x4, %edx
movq %r14, %rdi
leaq 0xd1e1a(%rip), %rsi     # 0x4d50ba
callq 0x401070
leaq 0x8(%r13), %r14
xorl %r12d, %r12d
testl %eax, %eax
jne 0x40359a
cmpl $0x1, %r15d
je 0x4035b7
cmpl $0x2, %r15d
jne 0x40359a
movl $0x14, %edi
callq 0x4449d0
testq %rax, %rax
je 0x4033db
movq %rax, %r15
movq 0x68(%rsp), %rdx
subq %r13, %rdx
cmpq $0x14, %rdx
movl $0x14, %eax
cmovaeq %rax, %rdx
xorpd %xmm0, %xmm0
movupd %xmm0, (%r15)
movl $0x0, 0x10(%r15)
movq (%rsp), %rax
leaq (%rax,%r13), %rsi
addq $0x8, %rsi
movq %r15, %rdi
callq 0x401058
movzwl 0x10(%r15), %esi
testq %rsi, %rsi
je 0x4033cc
addq $0x14, %rsi
movq %r15, %rdi
callq 0x445060
testq %rax, %rax
je 0x4033cc
movq %r14, 0x40(%rsp)
movq %rax, 0x50(%rsp)
movq %rax, %r14
movl (%rbx), %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x481860
movq %rax, 0x58(%rsp)
movl (%rbx), %edi
movq 0x8(%rsp), %rax
leaq (%rax,%r13), %rsi
addq $0x1c, %rsi
xorl %edx, %edx
callq 0x481860
movq %r14, 0x60(%rsp)
movzwl 0x10(%r14), %r15d
testq %r15, %r15
je 0x4033b4
addq $0x14, 0x50(%rsp)
movl (%rbx), %eax
movl %eax, 0x4c(%rsp)
xorl %r14d, %r14d
movq 0x50(%rsp), %rax
leaq (%rax,%r14), %rsi
movq %r15, %rdx
subq %r14, %rdx
movl 0x4c(%rsp), %edi
callq 0x481720
testq %rax, %rax
jle 0x4033b4
addq %rax, %r14
cmpq %r15, %r14
jb 0x40338f
movl (%rbx), %edi
movq 0x58(%rsp), %rsi
xorl %edx, %edx
callq 0x481860
movq 0x40(%rsp), %r14
movq 0x60(%rsp), %r15
movslq 0x43c(%rbx), %rax
movq %r15, 0x448(%rbx,%rax,8)
movq (%rsp), %rsi
movzwl 0x8(%rsi,%r13), %eax
movslq 0x43c(%rbx), %rcx
imulq $0x70, %rcx, %rcx
movq 0x30(%rsp), %rdi
movq %rax, (%rdi,%rcx)
movq 0x8(%rsp), %rax
addq %r13, %rax
addq $0x8, %rax
movq %rax, 0x60(%rdi,%rcx)
movzwl 0xa(%rsi,%r13), %eax
movq %rax, 0x8(%rdi,%rcx)
movslq 0xc(%rsi,%r13), %rax
movq %rax, 0x10(%rdi,%rcx)
movslq 0x10(%rsi,%r13), %rax
movabsq $0x1fffffffffffffff, %rdx # imm = 0x1FFFFFFFFFFFFFFF
andq %rdx, %rax
movabsq $0x20c49ba5e353f7cf, %rdx # imm = 0x20C49BA5E353F7CF
mulq %rdx
shrq $0x4, %rdx
movq %rdx, 0x20(%rdi,%rcx)
movzwl 0x16(%rsi,%r13), %eax
movq %rax, 0x18(%rdi,%rcx)
jmp 0x40359a
leaq (%r12,%r13), %r14
addq $0x8, %r14
movl $0x4, %edx
movq %r14, %rdi
leaq 0xd1c47(%rip), %rsi     # 0x4d50b5
callq 0x401070
orl 0x10(%rsp), %eax
movl 0x38(%rsp), %r15d
je 0x403523
movl $0x4, %edx
movq %r14, %rdi
leaq 0xd1c2e(%rip), %rsi     # 0x4d50bf
callq 0x401070
xorl %r12d, %r12d
testl %eax, %eax
jne 0x40358b
movslq 0x438(%rbx), %rax
leal 0x1(%rax), %ecx
movl %eax, 0x43c(%rbx)
movl %ecx, 0x438(%rbx)
cmpq $0x8, %rax
jge 0x403dc9
movq 0x78(%rbx), %rcx
leaq 0x7(%rcx), %rdx
addq $0xe, %rcx
testq %rdx, %rdx
cmovnsq %rdx, %rcx
shrq $0x3, %rcx
imull 0x68(%rbx), %ecx
movq (%rsp), %rdx
movslq 0x28(%rdx,%r13), %rdx
cmpl $0x5, %ecx
movl $0x4, %esi
cmovll %esi, %ecx
imulq %rdx, %rcx
imulq $0x70, %rax, %rax
movq 0x30(%rsp), %rdx
movq %rcx, 0x30(%rdx,%rax)
movslq %r15d, %rcx
movq %rcx, 0x28(%rdx,%rax)
movq 0x8(%rsp), %rcx
addq %r13, %rcx
addq $0x8, %rcx
movq %rcx, 0x58(%rdx,%rax)
movl $0x2, %r12d
jmp 0x40358b
movl 0xc(%r12,%r13), %eax
movl %eax, 0x28(%rbx)
movb $0x0, 0x2c(%rbx)
movq 0x8(%rsp), %rax
addq %r13, %rax
addq $0xc, %rax
movq %rax, 0x3f8(%rbx)
movl 0x1c(%r12,%r13), %eax
testl %eax, %eax
je 0x403565
xorps %xmm0, %xmm0
cvtsi2sdl 0x20(%r12,%r13), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rbx)
movslq 0x28(%r12,%r13), %rax
movq %rax, 0x40(%rbx)
movslq %r15d, %rax
movq %rax, 0x38(%rbx)
movq $0x0, 0x58(%rbx)
movl $0x1, %r12d
movl $0x1, 0x10(%rsp)
addq $0x8, %r13
incl %r15d
movl %r15d, 0x38(%rsp)
movq %r13, %r14
incq %rbp
andq $-0x2, %rbp
addq %r14, %rbp
movl %r12d, %r15d
cmpq 0x20(%rsp), %rbp
jl 0x40323a
jmp 0x40366d
movq %r14, 0x40(%rsp)
movq (%rsp), %r14
movl 0x8(%r14,%r13), %edi
movq %rdi, %r15
callq 0x4449d0
movq %rax, 0x440(%rbx)
testq %rax, %rax
je 0x4035ec
leaq (%r14,%r13), %rsi
addq $0x8, %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x401058
movslq 0xc(%r14,%r13), %rax
movq %rax, 0x10(%rbx)
movslq 0x10(%r14,%r13), %rax
movq %rax, 0x18(%rbx)
movq 0x8(%rsp), %rax
leaq 0x18(%rax,%r13), %rax
movq %rax, 0x400(%rbx)
movl 0x18(%r14,%r13), %eax
movl %eax, 0x30(%rbx)
movb $0x0, 0x34(%rbx)
movl $0x1, 0x18(%rsp)
movq 0x40(%rsp), %r14
jmp 0x40359a
movq %rbx, %rdi
callq 0x402633
movq $0xa, 0x116ef0(%rip) # 0x51a530
jmp 0x4031cc
movq %rbx, %rdi
callq 0x402633
movq $0xb, 0x116ed8(%rip) # 0x51a530
jmp 0x4031cc
movl $0x0, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
movq (%rsp), %rdi
callq 0x444d10
cmpl $0x0, 0x10(%rsp)
setne %al
cmpl $0x0, 0x18(%rsp)
setne %cl
testb %cl, %al
je 0x403d14
movq 0x38(%rbx), %rcx
movabsq $0x6666666666666667, %rdx # imm = 0x6666666666666667
movq %rcx, %rax
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x2, %rdx
addq %rax, %rdx
leal 0x30(%rdx), %eax
movb %al, 0x48(%rbx)
addl %edx, %edx
leal (%rdx,%rdx,4), %eax
subl %eax, %ecx
addb $0x30, %cl
movb %cl, 0x49(%rbx)
movw $0x6264, 0x4a(%rbx)     # imm = 0x6264
cmpq $0x0, 0x68(%rbx)
jne 0x4036db
movq $0x63, 0x88(%rbx)
movslq 0x438(%rbx), %rax
testq %rax, %rax
movl 0x28(%rsp), %ebp
jle 0x403740
imulq $0x70, %rax, %rax
movb $0x31, %cl
movl $0x1, %edx
xorl %esi, %esi
movl $0xcccccccd, %edi       # imm = 0xCCCCCCCD
movl %edx, %r8d
imulq %rdi, %r8
shrq $0x23, %r8
leal 0x30(%r8), %r9d
addl %r8d, %r8d
leal (%r8,%r8,4), %r8d
movl %ecx, %r10d
subb %r8b, %r10b
movb %r9b, 0xa0(%rbx,%rsi)
movb %r10b, 0xa1(%rbx,%rsi)
movw $0x6277, 0xa2(%rbx,%rsi) # imm = 0x6277
addq $0x70, %rsi
incb %cl
incl %edx
cmpq %rsi, %rax
jne 0x4036fd
movl (%rbx), %edi
movq 0x430(%rbx), %rsi
xorl %r15d, %r15d
xorl %edx, %edx
callq 0x481860
testl %ebp, %ebp
je 0x4031cf
leaq 0x48(%rbx), %rax
movq %rax, 0x20(%rsp)
movq 0x408(%rbx), %r12
testq %r12, %r12
je 0x4037d5
movq 0x3e8(%rbx), %r14
testq %r14, %r14
setg %bpl
jle 0x403874
movl $0x3, %edx
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0x401070
testl %eax, %eax
je 0x4037dc
leaq 0x10(%r12), %r13
leaq -0x1(%r14), %rbp
xorl %r15d, %r15d
cmpq %r15, %rbp
je 0x403874
movl $0x3, %edx
movq %r13, %rdi
movq 0x20(%rsp), %rsi
callq 0x401070
addq $0x10, %r13
incq %r15
testl %eax, %eax
jne 0x4037a6
cmpq %r14, %r15
setl %bpl
jmp 0x4037df
xorl %eax, %eax
jmp 0x403896
xorl %r15d, %r15d
testb %bpl, %bpl
je 0x403874
shlq $0x4, %r15
movslq 0x8(%r12,%r15), %r13
movl 0xc(%r12,%r15), %r14d
movl (%rbx), %edi
xorl %r12d, %r12d
movq %r13, %rsi
xorl %edx, %edx
callq 0x481860
movl (%rbx), %ebp
leaq (%rsp,%r12), %rsi
addq $0xb0, %rsi
movl $0x8, %edx
subq %r12, %rdx
movl %ebp, %edi
callq 0x481720
testq %rax, %rax
jle 0x40382f
addq %rax, %r12
cmpq $0x7, %r12
jbe 0x403807
cmpq $0x8, %r12
jne 0x403db1
movq 0x408(%rbx), %rsi
addq %r15, %rsi
leaq 0xb0(%rsp), %rdi
movl $0x4, %edx
callq 0x401070
testl %eax, %eax
jne 0x403d2c
cmpl %r14d, 0xb4(%rsp)
jne 0x403d2c
movl $0x1, %eax
movb $0x1, %cl
jmp 0x40388b
movq %rbx, %rdi
callq 0x402633
movq $0xc, 0x116ca9(%rip) # 0x51a530
xorl %ecx, %ecx
xorl %eax, %eax
xorl %r15d, %r15d
testb %cl, %cl
je 0x4031cf
testq %rax, %rax
jne 0x4039ad
movl (%rbx), %edi
movq 0x430(%rbx), %rsi
xorl %edx, %edx
callq 0x481860
movq $0x0, 0x3e8(%rbx)
leaq 0xd17e5(%rip), %r15     # 0x4d50a6
leaq 0xb0(%rsp), %r12
movabsq $0x300000003, %r14      # imm = 0x300000003
movl (%rbx), %ebp
xorl %r13d, %r13d
leaq (%rsp,%r13), %rsi
addq $0xb0, %rsi
movl $0x8, %edx
subq %r13, %rdx
movl %ebp, %edi
callq 0x481720
testq %rax, %rax
jle 0x403900
addq %rax, %r13
cmpq $0x7, %r13
jbe 0x4038d8
cmpq $0x8, %r13
jne 0x4039a8
movslq 0xb4(%rsp), %r13
movl $0x4, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x401070
testl %eax, %eax
je 0x403992
movzbl 0xb2(%rsp), %eax
andl $-0x21, %eax
cmpl $0x57, %eax
je 0x403955
cmpl $0x44, %eax
jne 0x403984
movzbl 0xb3(%rsp), %ecx
addl $-0x42, %ecx
cmpl $0x21, %ecx
ja 0x403951
btq %rcx, %r14
jb 0x403962
cmpb $0x57, %al
jne 0x403984
movb 0xb3(%rsp), %al
andb $-0x21, %al
cmpb $0x42, %al
jne 0x403984
movl (%rbx), %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x481860
leaq -0x8(%rax), %rcx
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
movq %r13, %r8
callq 0x402364
movl (%rbx), %edi
incq %r13
andq $-0x2, %r13
movq %r13, %rsi
jmp 0x403999
movl (%rbx), %edi
movl $0x4, %esi
movl $0x1, %edx
callq 0x481860
jmp 0x4038d3
movl $0x1, %eax
movq %rax, 0x18(%rsp)
movl 0x438(%rbx), %ebp
testl %ebp, %ebp
jle 0x4039d0
leaq (,%rbp,8), %rdx
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
callq 0x4010e0
movq 0x3e8(%rbx), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
jle 0x403a6d
movq 0x408(%rbx), %rax
movq %rax, 0x28(%rsp)
leaq 0xa0(%rbx), %rax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
xorl %r14d, %r14d
movq %r13, %r15
shlq $0x4, %r15
addq 0x28(%rsp), %r15
movl $0x3, %edx
movq %r15, %rdi
movq 0x20(%rsp), %rsi
callq 0x401070
cmpl $0x1, %eax
adcq $0x0, %r14
movq %r14, (%rsp)
testl %ebp, %ebp
jle 0x403a5d
movq 0x10(%rsp), %r12
xorl %r14d, %r14d
movl $0x4, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x401070
testl %eax, %eax
jne 0x403a51
incq 0x70(%rsp,%r14,8)
incq %r14
addq $0x70, %r12
cmpq %r14, %rbp
jne 0x403a38
incq %r13
cmpq 0x8(%rsp), %r13
movq (%rsp), %r14
jne 0x403a03
jmp 0x403a70
xorl %r14d, %r14d
movq %r14, 0x40(%rbx)
testl %ebp, %ebp
jle 0x403a95
leaq 0x98(%rbx), %rax
xorl %ecx, %ecx
movq 0x70(%rsp,%rcx,8), %rdx
movq %rdx, (%rax)
incq %rcx
addq $0x70, %rax
cmpq %rcx, %rbp
jne 0x403a81
testq %r14, %r14
je 0x403d14
shlq $0x3, %r14
leaq (%r14,%r14,2), %rdi
callq 0x4449d0
movq %rax, 0x410(%rbx)
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x403cfc
testl %ebp, %ebp
jle 0x403b25
leaq 0x70(%rsp), %r14
imulq $0x70, %rbp, %rax
movq %rax, (%rsp)
xorl %r15d, %r15d
cmpq $0x0, 0x98(%rbx,%r15)
je 0x403b17
movq (%r14), %rax
leaq (%rax,%rax,2), %rax
leaq 0x18(,%rax,8), %r12
movq %r12, %rdi
callq 0x4449d0
movq %rax, %r13
movq %rax, 0xc8(%rbx,%r15)
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x4010e0
testq %r13, %r13
je 0x403cfc
addq $0x8, %r14
addq $0x70, %r15
cmpq %r15, (%rsp)
jne 0x403ad4
testl %ebp, %ebp
jle 0x403b52
leaq (,%rbp,8), %r12
leaq 0x1b0(%rsp), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x4010e0
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x4010e0
movl $0x8, %eax
movq %rax, (%rsp)
cmpl $0x1, 0x18(%rsp)
je 0x403b71
movq 0x430(%rbx), %rax
addq $0x4, %rax
movq %rax, (%rsp)
cmpq $0x0, 0x8(%rsp)
jle 0x403cb8
movq 0x408(%rbx), %r15
leaq 0xa0(%rbx), %rax
movq %rax, 0x38(%rsp)
xorl %r14d, %r14d
movq $0x0, 0x18(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x28(%rsp)
shlq $0x4, %r14
leaq (%r15,%r14), %r12
movl $0x3, %edx
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0x401070
testl %eax, %eax
jne 0x403bfb
movslq 0x4(%r12), %rax
movq 0x18(%rsp), %rsi
leaq (%rsi,%rsi,2), %rcx
movq 0x30(%rsp), %rdx
movq %rax, (%rdx,%rcx,8)
movslq 0x8(%r15,%r14), %rax
addq (%rsp), %rax
movq %rax, 0x8(%rdx,%rcx,8)
movslq 0xc(%r15,%r14), %rax
movq %rax, 0x10(%rdx,%rcx,8)
incq %rsi
movq %rsi, 0x18(%rsp)
testl %ebp, %ebp
jle 0x403ca0
movq 0x10(%rsp), %rax
leaq (%rax,%r14), %r15
addq $0xc, %r15
movq 0x38(%rsp), %r13
xorl %r14d, %r14d
movl $0x4, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x401070
testl %eax, %eax
jne 0x403c90
movslq 0x8(%r12), %rax
addq (%rsp), %rax
movq 0x28(%r13), %rcx
movq 0x70(%rsp,%r14,8), %rdx
leaq (%rdx,%rdx,2), %rsi
movq %rax, (%rcx,%rsi,8)
movzbl (%r15), %eax
movzbl 0x1(%r15), %edi
shll $0x8, %edi
orl %eax, %edi
movzbl 0x2(%r15), %eax
shll $0x10, %eax
orl %edi, %eax
movzbl 0x3(%r15), %edi
shll $0x18, %edi
orl %eax, %edi
movslq %edi, %rax
movq %rax, 0x8(%rcx,%rsi,8)
movq 0x1b0(%rsp,%r14,8), %rdi
movq %rdi, 0x10(%rcx,%rsi,8)
addq %rax, %rdi
movq %rdi, 0x1b0(%rsp,%r14,8)
incq %rdx
movq %rdx, 0x70(%rsp,%r14,8)
incq %r14
addq $0x70, %r13
cmpq %r14, %rbp
jne 0x403c18
movq 0x28(%rsp), %r14
incq %r14
cmpq 0x8(%rsp), %r14
movq 0x10(%rsp), %r15
jne 0x403ba1
testl %ebp, %ebp
jle 0x403cdc
leaq 0x90(%rbx), %rax
xorl %ecx, %ecx
movq 0x1b0(%rsp,%rcx,8), %rdx
movq %rdx, (%rax)
incq %rcx
addq $0x70, %rax
cmpq %rcx, %rbp
jne 0x403cc5
movl (%rbx), %edi
movq 0x430(%rbx), %rsi
xorl %r15d, %r15d
xorl %edx, %edx
callq 0x481860
movq $0x0, 0x50(%rbx)
jmp 0x4031cf
movq %rbx, %rdi
callq 0x402633
movq $0x8, 0x116821(%rip) # 0x51a530
jmp 0x4031cc
movq %rbx, %rdi
callq 0x402633
movq $0xc, 0x116809(%rip) # 0x51a530
jmp 0x4031cc
movl (%rbx), %edi
movq 0x430(%rbx), %rax
leaq (%rax,%r13), %rsi
addq $-0x4, %rsi
xorl %r12d, %r12d
xorl %edx, %edx
callq 0x481860
movl (%rbx), %ebp
leaq (%rsp,%r12), %rsi
addq $0xb0, %rsi
movl $0x8, %edx
subq %r12, %rdx
movl %ebp, %edi
callq 0x481720
testq %rax, %rax
jle 0x403d71
addq %rax, %r12
cmpq $0x7, %r12
jbe 0x403d49
cmpq $0x8, %r12
jne 0x403db1
addq 0x408(%rbx), %r15
leaq 0xb0(%rsp), %rdi
movl $0x4, %edx
movq %r15, %rsi
callq 0x401070
movb $0x1, %cl
testl %eax, %eax
jne 0x403889
xorl %eax, %eax
cmpl %r14d, 0xb4(%rsp)
sete %al
addl %eax, %eax
jmp 0x40388b
movq %rbx, %rdi
callq 0x402633
movq $0x3, 0x11676c(%rip) # 0x51a530
jmp 0x403887
callq 0x401249
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x4031cf