// File: /workspace/github_cpp_makefile_star2_O3/jstaursky[P]hutch/src/Sleigh/address.cc
// Function: power2Divide(int, unsigned long, unsigned long&, unsigned long&)
int4 power2Divide(int4 n,uint8 divisor,uint8 &q,uint8 &r)

{
  if (divisor == 0) return 2;
  uint8 power = 1;
  if (n < 64) {
    power <<= n;
    q = power / divisor;
    r = power % divisor;
    return 0;
  }
  // Divide numerand and divisor by 2^(n-63) to get approximation of result
  uint8 y = divisor >> (n-64);	// Most of the way on divisor
  if (y == 0) return 1;		// Check if result will be too big
  y >>= 1;			// Divide divisor by final bit
  power <<= 63;
  uint8 max;
  if (y == 0) {
    max = 0;
    max -= 1;			// Could be maximal
    // Check if divisor is a power of 2
    if ((((uint8)1) << (n-64)) == divisor)
      return 1;
  }
  else
    max = power / y + 1;
  uint8 min = power / (y+1);
  if (min != 0)
    min -= 1;
  uint8 fullpower[2];
  fullpower[1] = ((uint8)1)<<(n-64);
  fullpower[0] = 0;
  uint8 mult[2];
  mult[0] = 0;
  mult[1] = 0;
  uint8 tmpq = 0;
  while(max > min+1) {
    tmpq = max + min;
    if (tmpq < min) {
      tmpq = (tmpq>>1) + 0x8000000000000000L;
    }
    else
      tmpq >>= 1;
    mult64to128(mult,divisor,tmpq);
    if (unsignedCompare128(fullpower,mult) < 0)
      max = tmpq-1;
    else
      min = tmpq;
  }
  // min is now our putative quotient
  if (tmpq != min)
    mult64to128(mult,divisor,min);
  unsignedSubtract128(fullpower,mult); // Calculate remainder
  // min might be 1 too small
  if (fullpower[1] != 0 || fullpower[0] >= divisor) {
    q = min + 1;
    r = fullpower[0] - divisor;
  }
  else {
    q = min;
    r = fullpower[0];
  }
  return 0;
}

// Assembly:
testq %rsi, %rsi
je 0x440a8
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rcx, %r12
movq %rdx, %r9
movl %edi, %ecx
cmpl $0x3f, %edi
jg 0x440ae
movl $0x1, %eax
shlq %cl, %rax
xorl %edx, %edx
divq %rsi
jmp 0x44266
movl $0x2, %eax
retq
movq %rsi, %r15
shrq %cl, %r15
movl $0x1, %eax
testq %r15, %r15
je 0x4426f
addl $-0x40, %ecx
movabsq $0x7fffffffffffffff, %r14 # imm = 0x7FFFFFFFFFFFFFFF
cmpq $0x1, %r15
jne 0x440f2
movl $0x1, %r11d
shlq %cl, %r11
movq $-0x1, %rdi
movq %r14, %rcx
cmpq %rsi, %r11
jne 0x44136
jmp 0x4426f
shrq %r15
xorl %ebx, %ebx
movabsq $-0x8000000000000000, %r10 # imm = 0x8000000000000000
movq %r10, %rax
xorl %edx, %edx
divq %r15
movq %rax, %rdi
movl $0x1, %r11d
shlq %cl, %r11
incq %r15
movq %r10, %rax
xorl %edx, %edx
divq %r15
leaq -0x1(%rax), %rcx
cmpq %rax, %rdi
jae 0x44133
xorl %edx, %edx
xorl %r10d, %r10d
jmp 0x441dd
incq %rdi
movq %r12, -0x8(%rsp)
movl %esi, %r15d
movq %rsi, %r12
shrq $0x20, %r12
leaq (%rcx,%rdi), %rax
shrq %rax
movq %rcx, %rdx
addq %rdi, %rdx
leaq 0x1(%r14,%rax), %r10
cmovaeq %rax, %r10
movl %r10d, %ebx
movq %r10, %rax
shrq $0x20, %rax
movq %rbx, %rbp
imulq %r15, %rbp
movq %rax, %r13
imulq %r15, %r13
imulq %r12, %rbx
imulq %r12, %rax
movl %ebp, %edx
shrq $0x20, %rbp
movl %r13d, %r8d
addq %rbp, %r8
movl %ebx, %ebp
addq %r8, %rbp
movq %rbp, %r8
shrq $0x20, %r8
shrq $0x20, %r13
addq %rax, %r13
shrq $0x20, %rbx
addq %r13, %rbx
addq %r8, %rbx
shlq $0x20, %rbp
xorl %eax, %eax
orq %rbp, %rdx
setne %al
xorl %r8d, %r8d
cmpq %rbx, %r11
setb %r8b
cmovnel %r8d, %eax
testb %al, %al
jne 0x441c7
movq %r10, %rcx
jmp 0x441cb
leaq -0x1(%r10), %rdi
leaq 0x1(%rcx), %rax
cmpq %rax, %rdi
ja 0x44145
movq -0x8(%rsp), %r12
cmpq %rcx, %r10
je 0x44239
movl %esi, %edi
movq %rsi, %rdx
shrq $0x20, %rdx
movl %ecx, %ebx
movq %rcx, %r10
shrq $0x20, %r10
movq %rbx, %r14
imulq %rdi, %r14
imulq %r10, %rdi
imulq %rdx, %rbx
imulq %rdx, %r10
movl %r14d, %edx
shrq $0x20, %r14
movl %edi, %r8d
addq %r14, %r8
movl %ebx, %r14d
addq %r8, %r14
movq %r14, %r8
shrq $0x20, %r8
shrq $0x20, %rdi
addq %r10, %rdi
shrq $0x20, %rbx
addq %rdi, %rbx
addq %r8, %rbx
shlq $0x20, %r14
orq %r14, %rdx
xorl %edi, %edi
negq %rdx
movl $0x0, %r8d
sbbq %r8, %r8
subq %rbx, %r8
addq %r11, %r8
sete %r8b
cmpq %rsi, %rdx
setb %r10b
testb %r8b, %r10b
cmovneq %rcx, %rax
cmovneq %rdi, %rsi
subq %rsi, %rdx
movq %rax, (%r9)
movq %rdx, (%r12)
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq