// File: /workspace/github_c_makefile_star2_O3/cyb70289[P]tt/src/apn/decimal/basic.c
// Function: shift_digs
static int shift_digs(uint *dst, uint dst_sz, const uint *src, int msb, int adj)
{
	int uints = (msb + 8) / 9;	/* Valid uints in src */
	int msb_r = msb + adj;		/* Result length for debugging */

	/* Check shift direction */
	int append0;
	if (adj < 0) {
		adj = -adj;
		if (adj >= msb) {
			/* All digits truncated, just fill with 0 */
			if (dst == src)
				memset(dst, 0, uints * 4);
			return 1;
		}
		append0 = 0;	/* Truncate */
	} else if (adj > 0) {
		append0 = 1;	/* Append */
	} else {
		if (dst != src)
			memcpy(dst, src, uints * 4);
		return msb;	/* No shift */
	}

	/* Shift */
	int adj9 = adj / 9;	/* Align to uint */
	adj %= 9;
	int i;
	const uint *cur = src;	/* Current digit buffer */
	if (append0) { /* Left shift */
		/* Align-9 */
		if (adj9) {
			for (i = uints-1; i >= 0; i--)
				dst[i+adj9] = src[i];
			memset(dst, 0, adj9 * 4);

			msb += adj9 * 9;
			uints += adj9;

			cur = dst;
		}

		/* Unaligned */
		if (adj) {	/* adj = 1~8 */
			/* Check digits in first uint */
			int u1_dig = (msb - 1) % 9 + 1;
			if ((u1_dig + adj) > 9)
				uints++;
			msb += adj;

			/* Shift left */
			for (i = uints-1; i > 0; i--) {
				dst[i] = lshift_dig_1_8(cur[i], adj);
				/* Append msb of lower uint */
				dst[i] += cur[i-1] / one_tbl[9-adj];
			}
			dst[0] = lshift_dig_1_8(cur[0], adj);
		}
	} else { /* Right shift */
		/* Align-9 */
		if (adj9) {
			for (i = adj9; i < uints; i++)
				dst[i-adj9] = src[i];
			int remains = dst_sz - (uints - adj9) * 4;
			if (remains > 0)
				memset(dst + uints - adj9, 0, remains);

			msb -= adj9 * 9;
			uints -= adj9;

			cur = dst;
		}

		/* Unaligned */
		if (adj) {	/* adj = 1~8 */
			/* Shift right */
			for (i = 0; i < uints - 1; i++) {
				dst[i] = rshift_dig_1_8(cur[i], adj);
				/* Prepend lsb of higher uint */
				dst[i] += (cur[i+1] % one_tbl[adj]) *
					one_tbl[9-adj];
			}
			dst[uints-1] = rshift_dig_1_8(cur[uints-1], adj);

			/* Check digits in first uint */
			int u1_dig = (msb - 1) % 9 + 1;
			if (u1_dig <= adj)
				uints--;
			msb -= adj;
		}
	}

	tt_assert_fa(msb_r == msb);
	return msb;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r8d, %ebp
movl %ecx, %ebx
movq %rdx, %rcx
movq %rdi, %r14
leal 0x8(%rbx), %eax
cltq
imulq $0x38e38e39, %rax, %r15 # imm = 0x38E38E39
movq %r15, %rax
shrq $0x3f, %rax
sarq $0x21, %r15
addl %eax, %r15d
testl %r8d, %r8d
js 0x3b24
je 0x3b5c
movl %ebp, %eax
imulq $0x38e38e39, %rax, %r13 # imm = 0x38E38E39
shrq $0x21, %r13
leal (%r13,%r13,8), %eax
movl %ebp, %r12d
subl %eax, %r12d
cmpl $0x9, %ebp
jae 0x3b7c
movl %ebx, %edi
jmp 0x3bc3
movl %ebp, %eax
negl %eax
cmpl %ebx, %eax
jge 0x3cb7
movl %eax, %edx
imulq $0x38e38e39, %rdx, %r12 # imm = 0x38E38E39
shrq $0x21, %r12
leal (%r12,%r12,8), %edx
movl %eax, %edi
subl %edx, %edi
movl %edi, 0x4(%rsp)
cmpl $0x9, %eax
jae 0x3cdb
movl %r15d, %r13d
movl %ebx, %edi
jmp 0x3d39
cmpq %rcx, %r14
je 0x3db4
shll $0x2, %r15d
movslq %r15d, %rdx
movq %r14, %rdi
movq %rcx, %rsi
callq 0x1120
jmp 0x3db4
testl %ebx, %ebx
jle 0x3ba4
leal 0x1(%r15), %eax
leal -0x1(%r15), %edx
leaq (%rcx,%rdx,4), %rcx
movl (%rcx), %edx
leal (%rax,%r13), %esi
addl $-0x2, %esi
movl %edx, (%r14,%rsi,4)
decl %eax
addq $-0x4, %rcx
cmpl $0x1, %eax
jg 0x3b8c
leal (,%r13,4), %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x10e0
leal (%r13,%r13,8), %edi
addl %ebx, %edi
addl %r13d, %r15d
movq %r14, %rcx
testl %r12d, %r12d
je 0x3dae
movl %ebp, 0x4(%rsp)
leal -0x1(%rdi), %eax
cltq
imulq $0x38e38e39, %rax, %rax # imm = 0x38E38E39
movq %rax, %rdx
shrq $0x3f, %rdx
sarq $0x21, %rax
addl %edx, %eax
leal (%rax,%rax,8), %eax
negl %eax
addl %edi, %eax
decl %eax
leal (%r12,%rax), %edx
incl %edx
xorl %eax, %eax
cmpl $0xa, %edx
setge %al
leal (%r15,%rax), %edx
movl %r12d, %esi
leaq 0x7582(%rip), %r9       # 0xb190
movl (%r9,%rsi,4), %esi
movabsq $0x44b82fa09b5a53, %r8  # imm = 0x44B82FA09B5A53
cmpl $0x2, %edx
jl 0x3c8c
movl $0x9, %r10d
subl %r12d, %r10d
movl (%r9,%r10,4), %r9d
movl %edx, %r10d
movl $0xfffffffe, %r11d      # imm = 0xFFFFFFFE
addq %r10, %r11
addl %eax, %r15d
incl %r15d
decq %r10
movl %r10d, %r13d
shlq $0x2, %r13
movl (%rcx,%r13), %ebp
imulq %rsi, %rbp
movq %rbp, %rax
shrq $0x9, %rax
mulq %r8
shrq $0xb, %rdx
imull $0x3b9aca00, %edx, %eax # imm = 0x3B9ACA00
subl %eax, %ebp
movl %ebp, (%r14,%r13)
movl %r11d, %eax
movl (%rcx,%rax,4), %eax
xorl %edx, %edx
divl %r9d
addl %ebp, %eax
movl %eax, (%r14,%r13)
decq %r11
decl %r15d
decq %r10
cmpl $0x2, %r15d
jg 0x3c43
addl %r12d, %edi
movl (%rcx), %eax
imulq %rax, %rsi
movq %rsi, %rax
shrq $0x9, %rax
mulq %r8
shrq $0xb, %rdx
imull $0x3b9aca00, %edx, %eax # imm = 0x3B9ACA00
subl %eax, %esi
movl %esi, (%r14)
movl 0x4(%rsp), %ebp
jmp 0x3dae
movl $0x1, %ebx
cmpq %rcx, %r14
jne 0x3db4
shll $0x2, %r15d
movslq %r15d, %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x10e0
jmp 0x3db4
movl %r15d, %r13d
subl %r12d, %r13d
jle 0x3d05
movl %r12d, %edx
leal (%r15,%r12), %eax
subl %r12d, %eax
leaq (%rcx,%rdx,4), %rcx
subq %rdx, %rax
xorl %edx, %edx
movl (%rcx,%rdx,4), %edi
movl %edi, (%r14,%rdx,4)
incq %rdx
cmpq %rdx, %rax
jne 0x3cf6
leal (,%r13,4), %eax
subl %eax, %esi
testl %esi, %esi
jle 0x3d2e
movslq %r15d, %rax
leaq (%r14,%rax,4), %rdi
leal (,%r12,4), %eax
subq %rax, %rdi
movl %esi, %edx
xorl %esi, %esi
callq 0x10e0
leal (%r12,%r12,8), %eax
movl %ebx, %edi
subl %eax, %edi
movq %r14, %rcx
movl 0x4(%rsp), %r12d
testl %r12d, %r12d
je 0x3dae
leal -0x1(%r13), %r8d
movl %r12d, %edx
leaq 0x743f(%rip), %rax      # 0xb190
movl (%rax,%rdx,4), %r9d
cmpl $0x2, %r13d
jl 0x3d9c
movl $0x9, %edx
subl %r12d, %edx
movl (%rax,%rdx,4), %r10d
movl %r8d, %r11d
xorl %r15d, %r15d
movl (%rcx,%r15,4), %eax
xorl %edx, %edx
divl %r9d
movl %eax, %esi
movl %eax, (%r14,%r15,4)
movl 0x4(%rcx,%r15,4), %eax
xorl %edx, %edx
divl %r9d
imull %r10d, %edx
addl %esi, %edx
movl %edx, (%r14,%r15,4)
leaq 0x1(%r15), %rax
movq %rax, %r15
cmpq %rax, %r11
jne 0x3d6d
movslq %r8d, %rsi
movl (%rcx,%rsi,4), %eax
xorl %edx, %edx
divl %r9d
movl %eax, (%r14,%rsi,4)
subl %r12d, %edi
addl %ebp, %ebx
cmpl %edi, %ebx
jne 0x3dc5
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1412