// File: /workspace/github_c_makefile_star2_O3/archer1357[P]Offline-Raytracer/avilib.c
// Function: AVI_read_data
int AVI_read_data(avi_t *AVI, char *vidbuf, long max_vidbuf,
                              char *audbuf, long max_audbuf,
                              long *len)
{

/*
 * Return codes:
 *
 *    1 = video data read
 *    2 = audio data read
 *    0 = reached EOF
 *   -1 = video buffer too small
 *   -2 = audio buffer too small
 */

   off_t n;
   char data[8];

   if(AVI->mode==AVI_MODE_WRITE) return 0;

   while(1)
   {
      /* Read tag and length */

      if( avi_read(AVI->fdes,data,8) != 8 ) return 0;

      /* if we got a list tag, ignore it */

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

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

      if(strncasecmp(data,AVI->video_tag,3) == 0)
      {
         *len = n;
         AVI->video_pos++;
         if(n>max_vidbuf)
         {
            lseek(AVI->fdes,n,SEEK_CUR);
            return -1;
         }
         if(avi_read(AVI->fdes,vidbuf,n) != n ) return 0;
         return 1;
      }
      else if(strncasecmp(data,AVI->track[AVI->aptr].audio_tag,4) == 0)
      {
         *len = n;
         if(n>max_audbuf)
         {
            lseek(AVI->fdes,n,SEEK_CUR);
            return -2;
         }
         if(avi_read(AVI->fdes,audbuf,n) != n ) return 0;
         return 2;
         break;
      }
      else
         if(lseek(AVI->fdes,n,SEEK_CUR)<0)  return 0;
   }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
cmpq $0x0, 0x8(%rdi)
je 0x4045bc
movq %rdi, %r13
movq %rdx, 0x20(%rsp)
movq %r9, (%rsp)
movq %r8, 0x18(%rsp)
movq %rsi, 0x28(%rsp)
movq %rcx, 0x10(%rsp)
leaq 0x48(%rdi), %r14
leaq 0x60(%rdi), %rax
movq %rax, 0x30(%rsp)
leaq 0x8(%rsp), %rbx
xorl %r12d, %r12d
movl (%r13), %r15d
xorl %ebp, %ebp
leaq (%rsp,%rbp), %rsi
addq $0x8, %rsi
movl $0x8, %edx
subq %rbp, %rdx
movl %r15d, %edi
callq 0x4825e0
testq %rax, %rax
jle 0x4045bf
addq %rax, %rbp
cmpq $0x7, %rbp
jbe 0x4044f3
cmpq $0x8, %rbp
jne 0x4045bf
movl $0x4, %edx
movq %rbx, %rdi
leaq 0xd1b3c(%rip), %rsi     # 0x4d6072
callq 0x401070
testl %eax, %eax
je 0x4045a4
movslq 0xc(%rsp), %r15
incq %r15
andq $-0x2, %r15
movl $0x3, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x401070
testl %eax, %eax
je 0x4045d1
movslq 0x43c(%r13), %rax
imulq $0x70, %rax, %rax
movq 0x30(%rsp), %rcx
leaq (%rcx,%rax), %rsi
addq $0x40, %rsi
movl $0x4, %edx
movq %rbx, %rdi
callq 0x401070
testl %eax, %eax
je 0x404603
movl (%r13), %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0x482720
testq %rax, %rax
jns 0x4044ed
jmp 0x4045bf
movl (%r13), %edi
movl $0x4, %esi
movl $0x1, %edx
callq 0x482720
jmp 0x4044ed
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rax
movq %r15, (%rax)
incq 0x50(%r13)
movl (%r13), %ebx
cmpq 0x20(%rsp), %r15
movq 0x28(%rsp), %r12
jle 0x40462c
movl %ebx, %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0x482720
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x4045bf
movq (%rsp), %rax
movq %r15, (%rax)
movl (%r13), %ebx
cmpq 0x18(%rsp), %r15
jle 0x404661
movl %ebx, %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0x482720
movl $0xfffffffe, %r12d      # imm = 0xFFFFFFFE
jmp 0x4045bf
xorl %r14d, %r14d
testq %r15, %r15
je 0x404652
leaq (%r12,%r14), %rsi
movq %r15, %rdx
subq %r14, %rdx
movl %ebx, %edi
callq 0x4825e0
testq %rax, %rax
jle 0x404652
addq %rax, %r14
cmpq %r15, %r14
jb 0x404634
xorl %r12d, %r12d
cmpq %r15, %r14
sete %r12b
jmp 0x4045bf
xorl %r14d, %r14d
testq %r15, %r15
je 0x40468c
movq 0x10(%rsp), %r12
leaq (%r12,%r14), %rsi
movq %r15, %rdx
subq %r14, %rdx
movl %ebx, %edi
callq 0x4825e0
testq %rax, %rax
jle 0x40468c
addq %rax, %r14
cmpq %r15, %r14
jb 0x40466e
xorl %r12d, %r12d
cmpq %r15, %r14
sete %r12b
addl %r12d, %r12d
jmp 0x4045bf