// File: jstaursky[P]hutch/src/Sleigh/memstate.cc
// Function: MemoryBank::setValue(unsigned long, int, unsigned long)
void MemoryBank::setValue(uintb offset,int4 size,uintb val)

{
  uintb alignmask = (uintb)(wordsize-1);
  uintb ind = offset & (~alignmask);
  int4 skip = offset & alignmask;
  int4 size1 = wordsize-skip;
  int4 size2;
  int4 gap;
  uintb val1,val2;

  if (size > size1) {		// We have spill over
    size2 = size - size1;
    val1 = find(ind);
    val2 = find(ind+wordsize);
    gap = wordsize - size2;
  }
  else {
    if (size == wordsize) {
      insert(ind,val);
      return;
    }
    val1 = find(ind);
    val2 = 0;
    gap = size1-size;
    size1 = size;
    size2 = 0;
  }

  skip = skip * 8;		// Convert from byte skip to bit skip
  gap = gap * 8;		// Convert from byte to bits
  if (space->isBigEndian()) {
    if (size2 == 0) {
      val1 &= ~(calc_mask(size1)<<gap);
      val1 |= val << gap;
      insert(ind,val1);
    }
    else {
      val1 &= (~((uintb)0)) << 8*size1;
      val1 |= val >> 8*size2;
      insert(ind,val1);
      val2 &= (~((uintb)0)) >> 8*size2;
      val2 |= val << gap;
      insert(ind+wordsize,val2);
    }
  }
  else {
    if (size2 == 0) {
      val1 &= ~(calc_mask(size1)<<skip);
      val1 |= val << skip;
      insert(ind,val1);
    }
    else {
      val1 &= (~((uintb)0)) >> 8*size1;
      val1 |= val << skip;
      insert(ind,val1);
      val2 &= (~((uintb)0)) << 8*size2;
      val2 |= val >> 8*size1;
      insert(ind+wordsize,val2);
    }
  }
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movl %edx, 0x44(%rsp)
movq %rcx, 0x38(%rsp)
movq 0x50(%rsp), %rax
movq %rax, (%rsp)
movl 0x8(%rax), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq %rcx, 0x30(%rsp)
movq 0x48(%rsp), %rcx
movq 0x30(%rsp), %rdx
xorq $-0x1, %rdx
andq %rdx, %rcx
movq %rcx, 0x28(%rsp)
movq 0x48(%rsp), %rcx
andq 0x30(%rsp), %rcx
movl %ecx, 0x24(%rsp)
movl 0x8(%rax), %eax
subl 0x24(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x20(%rsp), %eax
jle 0x164f06
movq (%rsp), %rdi
movl 0x44(%rsp), %eax
subl 0x20(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x28(%rsp), %rsi
movq (%rdi), %rax
callq *0x8(%rax)
movq (%rsp), %rdi
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rsi
movslq 0x8(%rdi), %rax
addq %rax, %rsi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x8(%rsp)
movl 0x8(%rax), %eax
subl 0x1c(%rsp), %eax
movl %eax, 0x18(%rsp)
jmp 0x164f64
movq (%rsp), %rcx
movl 0x44(%rsp), %eax
cmpl 0x8(%rcx), %eax
jne 0x164f2b
movq (%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq (%rdi), %rax
callq *(%rax)
jmp 0x165175
movq (%rsp), %rdi
movq 0x28(%rsp), %rsi
movq (%rdi), %rax
callq *0x8(%rax)
movq %rax, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
movl 0x20(%rsp), %eax
subl 0x44(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movq (%rsp), %rax
movl 0x24(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, 0x24(%rsp)
movl 0x18(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, 0x18(%rsp)
movq 0x10(%rax), %rdi
callq 0xddec0
testb $0x1, %al
jne 0x164f90
jmp 0x165084
cmpl $0x0, 0x1c(%rsp)
jne 0x164fe3
movl 0x20(%rsp), %edi
callq 0xcfa60
movq (%rsp), %rdi
movl 0x18(%rsp), %ecx
shlq %cl, %rax
xorq $-0x1, %rax
andq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x38(%rsp), %rax
movl 0x18(%rsp), %ecx
shlq %cl, %rax
orq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq (%rdi), %rax
callq *(%rax)
jmp 0x16507f
movq (%rsp), %rdi
movl 0x20(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %ecx
movq $-0x1, %rax
shlq %cl, %rax
andq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x38(%rsp), %rax
movl 0x1c(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
shrq %cl, %rax
orq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq (%rdi), %rax
callq *(%rax)
movq (%rsp), %rdi
movl 0x1c(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %ecx
movq $-0x1, %rax
shrq %cl, %rax
andq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x38(%rsp), %rax
movl 0x18(%rsp), %ecx
shlq %cl, %rax
orq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %rsi
movslq 0x8(%rdi), %rax
addq %rax, %rsi
movq 0x8(%rsp), %rdx
movq (%rdi), %rax
callq *(%rax)
jmp 0x165175
cmpl $0x0, 0x1c(%rsp)
jne 0x1650d7
movl 0x20(%rsp), %edi
callq 0xcfa60
movq (%rsp), %rdi
movl 0x24(%rsp), %ecx
shlq %cl, %rax
xorq $-0x1, %rax
andq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x38(%rsp), %rax
movl 0x24(%rsp), %ecx
shlq %cl, %rax
orq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq (%rdi), %rax
callq *(%rax)
jmp 0x165173
movq (%rsp), %rdi
movl 0x20(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %ecx
movq $-0x1, %rax
shrq %cl, %rax
andq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x38(%rsp), %rax
movl 0x24(%rsp), %ecx
shlq %cl, %rax
orq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq (%rdi), %rax
callq *(%rax)
movq (%rsp), %rdi
movl 0x1c(%rsp), %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %ecx
movq $-0x1, %rax
shlq %cl, %rax
andq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x38(%rsp), %rax
movl 0x20(%rsp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
shrq %cl, %rax
orq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x28(%rsp), %rsi
movslq 0x8(%rdi), %rax
addq %rax, %rsi
movq 0x8(%rsp), %rdx
movq (%rdi), %rax
callq *(%rax)
jmp 0x165175
addq $0x58, %rsp
retq
nopw (%rax,%rax)