// File: /workspace/github_c_makefile_star2_O0/rchiossi[P]dexterity/lib/bytestream.c
// Function: bswrite
int bswrite(ByteStream* bs, uint8_t* data, unsigned int 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 write
  asize = sizeof(unsigned int) - (bs->offset % sizeof(unsigned int)); 

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

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

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

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

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

  bs->offset += i * sizeof(unsigned int);
  data += i * sizeof(unsigned int);

  //write finishing bytes
  size -= i * sizeof(unsigned int);

  for (i=0; i<size; i++) {
    bs->data[bs->offset+i] = data[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)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
je 0x5075
movl $0x0, -0x4(%rbp)
jmp 0x5257
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jb 0x5092
movl $0x0, -0x4(%rbp)
jmp 0x5257
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
addl -0x1c(%rbp), %eax
movl %eax, %eax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jbe 0x50c8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %ecx
subq %rcx, %rax
movl %eax, -0x1c(%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 0x5100
movl -0x40(%rbp), %eax
cmpl -0x1c(%rbp), %eax
setb %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x5109
jmp 0x5135
movq -0x18(%rbp), %rax
movl -0x40(%rbp), %ecx
movb (%rax,%rcx), %dl
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %ecx
addl -0x40(%rbp), %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x50e7
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), %ecx
movl -0x1c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x1c(%rbp), %eax
shrq $0x2, %rax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
shrq $0x2, %rax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0x51c2
movq -0x28(%rbp), %rax
movl -0x40(%rbp), %ecx
movl (%rax,%rcx,4), %edx
movq -0x30(%rbp), %rax
movl -0x3c(%rbp), %ecx
addl -0x40(%rbp), %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x5196
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 -0x40(%rbp), %eax
movl %eax, %ecx
shlq $0x2, %rcx
movl -0x1c(%rbp), %eax
subq %rcx, %rax
movl %eax, -0x1c(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x5234
movq -0x18(%rbp), %rax
movl -0x40(%rbp), %ecx
movb (%rax,%rcx), %dl
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %ecx
addl -0x40(%rbp), %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x5200
movl -0x40(%rbp), %ecx
movq -0x10(%rbp), %rax
addl 0x18(%rax), %ecx
movl %ecx, 0x18(%rax)
movl -0x38(%rbp), %eax
shlq $0x2, %rax
movl -0x1c(%rbp), %ecx
addq %rcx, %rax
movl -0x34(%rbp), %ecx
addq %rcx, %rax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq
nopl (%rax)