// File: /workspace/github_c_makefile_star2_O1/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 0x4045d8
movq %rdi, %r13
movq %rdx, 0x30(%rsp)
movq %r9, 0x8(%rsp)
movq %r8, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
leaq 0x48(%rdi), %r12
leaq 0x60(%rdi), %rbp
leaq 0x10(%rsp), %rbx
movl (%r13), %r14d
xorl %r15d, %r15d
leaq (%rsp,%r15), %rsi
addq $0x10, %rsi
movl $0x8, %edx
subq %r15, %rdx
movl %r14d, %edi
callq 0x481720
testq %rax, %rax
jle 0x40453b
addq %rax, %r15
cmpq $0x7, %r15
jbe 0x404515
xorl %r14d, %r14d
cmpq $0x8, %r15
jne 0x4045db
movl $0x4, %edx
movq %rbx, %rdi
leaq 0xd0b4f(%rip), %rsi     # 0x4d50a6
callq 0x401070
testl %eax, %eax
je 0x4045c0
movslq 0x14(%rsp), %r15
incq %r15
andq $-0x2, %r15
movl $0x3, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x401070
testl %eax, %eax
je 0x4045ed
movslq 0x43c(%r13), %rax
imulq $0x70, %rax, %rax
leaq (%rax,%rbp), %rsi
addq $0x40, %rsi
movl $0x4, %edx
movq %rbx, %rdi
callq 0x401070
testl %eax, %eax
je 0x40461b
movl (%r13), %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0x481860
testq %rax, %rax
jns 0x40450e
jmp 0x4045db
movl (%r13), %edi
movl $0x4, %esi
movl $0x1, %edx
callq 0x481860
jmp 0x40450e
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
movq %r15, (%rax)
incq 0x50(%r13)
movl (%r13), %ebx
cmpq 0x30(%rsp), %r15
jle 0x404645
movl %ebx, %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0x481860
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x4045db
movq 0x8(%rsp), %rax
movq %r15, (%rax)
movl (%r13), %ebx
cmpq 0x28(%rsp), %r15
jle 0x40467f
movl %ebx, %edi
movq %r15, %rsi
movl $0x1, %edx
callq 0x481860
movl $0xfffffffe, %r14d      # imm = 0xFFFFFFFE
jmp 0x4045db
xorl %r12d, %r12d
testq %r15, %r15
je 0x404670
movq 0x20(%rsp), %r14
leaq (%r14,%r12), %rsi
movq %r15, %rdx
subq %r12, %rdx
movl %ebx, %edi
callq 0x481720
testq %rax, %rax
jle 0x404670
addq %rax, %r12
cmpq %r15, %r12
jb 0x404652
xorl %r14d, %r14d
cmpq %r15, %r12
sete %r14b
jmp 0x4045db
xorl %r12d, %r12d
testq %r15, %r15
je 0x4046aa
movq 0x18(%rsp), %r14
leaq (%r14,%r12), %rsi
movq %r15, %rdx
subq %r12, %rdx
movl %ebx, %edi
callq 0x481720
testq %rax, %rax
jle 0x4046aa
addq %rax, %r12
cmpq %r15, %r12
jb 0x40468c
xorl %r14d, %r14d
cmpq %r15, %r12
sete %r14b
addl %r14d, %r14d
jmp 0x4045db