// File: /workspace/github_c_makefile_star2_O0/rchiossi[P]dexterity/lib/bytestream.c
// Function: bsread
unsigned int bsread(ByteStream* bs, uint8_t* data, size_t size) {
  unsigned int* in;
  unsigned int* out;
  
  unsigned int asize;

  unsigned int isize;
  unsigned int ioffset;

  unsigned int i;

  if (bs->exhausted) return 0;

  if (bs->offset >= bs->size) return 0;

  if (bs->offset+size > bs->size) {
    size = bs->size - bs->offset;
    bs->exhausted = 1;
  }

  //align read
  asize = sizeof(unsigned int) - (bs->offset % sizeof(unsigned int)); 

  for (i=0; i<asize && i<size; i++) {
    data[i] = bs->data[bs->offset+i];
  }

  bs->offset += i;
  data += i;
  size -= i;
  asize = i;

  //optimized read
  isize = size / sizeof(unsigned int);
  ioffset = bs->offset / sizeof(unsigned int);

  in = (uint32_t*) bs->data;
  out = (uint32_t*) data;

  for (i=0; i<isize; i++) {
    out[i] = in[ioffset+i];
  }

  bs->offset += i * sizeof(unsigned int);
  data += i * sizeof(unsigned int);
  
  //trailing bytes read
  size -= isize * sizeof(unsigned int);

  for (i=0; i<size; i++) {
    data[i] = bs->data[bs->offset+i];
  }

  bs->offset += i;

  return isize * sizeof(unsigned int) + size + asize;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
je 0x4de6
movl $0x0, -0x4(%rbp)
jmp 0x4fd0
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jb 0x4e03
movl $0x0, -0x4(%rbp)
jmp 0x4fd0
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
addq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jbe 0x4e39
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %ecx
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movl $0x1, 0x1c(%rax)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, %ecx
andq $0x3, %rcx
movl $0x4, %eax
subq %rcx, %rax
movl %eax, -0x34(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %ecx
xorl %eax, %eax
cmpl -0x34(%rbp), %ecx
movb %al, -0x41(%rbp)
jae 0x4e72
movl -0x40(%rbp), %eax
cmpq -0x20(%rbp), %rax
setb %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x4e7b
jmp 0x4ea7
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %ecx
addl -0x40(%rbp), %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %dl
movq -0x18(%rbp), %rax
movl -0x40(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x4e58
movl -0x40(%rbp), %ecx
movq -0x10(%rbp), %rax
addl 0x18(%rax), %ecx
movl %ecx, 0x18(%rax)
movl -0x40(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
movl -0x40(%rbp), %eax
movl %eax, %ecx
movq -0x20(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rax
shrq $0x2, %rax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
shrq $0x2, %rax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0x4f3a
movq -0x28(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl -0x40(%rbp), %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %edx
movq -0x30(%rbp), %rax
movl -0x40(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x4f0e
movl -0x40(%rbp), %eax
movl %eax, %edx
shlq $0x2, %rdx
movq -0x10(%rbp), %rax
movl 0x18(%rax), %ecx
addq %rdx, %rcx
movl %ecx, 0x18(%rax)
movl -0x40(%rbp), %eax
shlq $0x2, %rax
addq -0x18(%rbp), %rax
movq %rax, -0x18(%rbp)
movl -0x38(%rbp), %eax
movl %eax, %ecx
shlq $0x2, %rcx
movq -0x20(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpq -0x20(%rbp), %rax
jae 0x4faf
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %ecx
addl -0x40(%rbp), %ecx
movl %ecx, %ecx
movb (%rax,%rcx), %dl
movq -0x18(%rbp), %rax
movl -0x40(%rbp), %ecx
movb %dl, (%rax,%rcx)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x4f7a
movl -0x40(%rbp), %ecx
movq -0x10(%rbp), %rax
addl 0x18(%rax), %ecx
movl %ecx, 0x18(%rax)
movl -0x38(%rbp), %eax
shlq $0x2, %rax
addq -0x20(%rbp), %rax
movl -0x34(%rbp), %ecx
addq %rcx, %rax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq
nopw %cs:(%rax,%rax)