// File: /workspace/github_c_makefile_star2_O1/ccxvii[P]mujs/./jsstring.c
// Function: Sp_substring_imp
static void Sp_substring_imp(js_State *J, const char *s, int a, int n)
{
	Rune head_rune = 0, tail_rune = 0;
	const char *head, *tail;
	char *p;
	int i, k, head_len, tail_len;

	/* find start of substring */
	head = s;
	for (i = 0; i < a; ++i) {
		head += chartorune(&head_rune, head);
		if (head_rune >= 0x10000)
			++i;
	}

	/* find end of substring */
	tail = head;
	for (k = i - a; k < n; ++k) {
		tail += chartorune(&tail_rune, tail);
		if (tail_rune >= 0x10000)
			++k;
	}

	/* no surrogate pair splits! */
	if (i == a && k == n) {
		js_pushlstring(J, head, tail - head);
		return;
	}

	if (js_try(J)) {
		js_free(J, p);
		js_throw(J);
	}

	p = js_malloc(J, UTFmax + (tail - head));

	/* substring starts with low surrogate (head is just after character) */
	if (i > a) {
		head_rune = 0xdc00 + ((head_rune - 0x10000) & 0x3ff);
		head_len = runetochar(p, &head_rune);
		memcpy(p + head_len, head, tail - head);
		js_pushlstring(J, p, head_len + (tail - head));
	}

	/* substring ends with high surrogate (tail is just after character) */
	if (k > n) {
		tail -= runelen(tail_rune);
		memcpy(p, head, tail - head);
		tail_rune = 0xd800 + ((tail_rune - 0x10000) >> 10);
		tail_len = runetochar(p + (tail - head), &tail_rune);
		js_pushlstring(J, p, (tail - head) + tail_len);
	}

	js_endtry(J);
	js_free(J, p);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %ecx, %ebp
movl %edx, %r15d
movq %rdi, 0x28(%rsp)
xorl %ebx, %ebx
movl %ebx, 0x4(%rsp)
movl %ebx, (%rsp)
testl %edx, %edx
jle 0x29dd8
xorl %eax, %eax
leaq 0x4(%rsp), %r14
movq %rsi, %r12
movl %eax, %ebx
movq %r14, %rdi
movq %r12, %rsi
callq 0x18750
movl %eax, %eax
addq %r12, %rax
xorl %ecx, %ecx
cmpl $0x10000, 0x4(%rsp)     # imm = 0x10000
setge %dl
movb %dl, %cl
movl %ebx, %edx
leal 0x1(%rdx,%rcx), %ecx
movq %rax, %rsi
movq %rax, %r12
movl %ecx, %ebx
movl %ecx, %eax
cmpl %r15d, %ecx
jl 0x29da2
movl %ebx, %r14d
subl %r15d, %r14d
cmpl %ebp, %r14d
movq %rsi, 0x8(%rsp)
jge 0x29e24
movq %rsp, %r12
movq %rsi, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x18750
movl %eax, %eax
addq %r13, %rax
xorl %ecx, %ecx
cmpl $0x10000, (%rsp)        # imm = 0x10000
setge %dl
movb %dl, %cl
movl %r14d, %edx
leal 0x1(%rdx,%rcx), %esi
movq %rax, %rcx
movq %rax, %r13
movl %esi, %eax
movl %esi, %r14d
cmpl %ebp, %esi
jl 0x29dee
jmp 0x29e2a
movl %r14d, %eax
movq %rsi, %rcx
movq %rcx, %r13
movl %eax, %r12d
cmpl %r15d, %ebx
jne 0x29e53
cmpl %ebp, %r12d
jne 0x29e53
movq 0x8(%rsp), %rsi
movl %r13d, %edx
subl %esi, %edx
movq 0x28(%rsp), %rdi
callq 0x13802
jmp 0x29f7c
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x13fbb
movq %rax, %rdi
callq 0x3100
testl %eax, %eax
jne 0x29f8b
movq %r13, 0x30(%rsp)
subq 0x8(%rsp), %r13
movl %r13d, %eax
movl %r13d, 0x1c(%rsp)
leal 0x4(%rax), %esi
movq %r14, %rdi
callq 0xe119
movq %rax, 0x20(%rsp)
movq %rax, 0x10(%rsp)
cmpl %r15d, %ebx
jle 0x29eea
movl $0x3ff, %eax            # imm = 0x3FF
leaq 0x4(%rsp), %rsi
andl (%rsi), %eax
orl $0xdc00, %eax           # imm = 0xDC00
movl %eax, (%rsi)
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1c76f
movl %eax, %r15d
movl %eax, %eax
movl %eax, %edi
addq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r13, %rdx
callq 0x33d0
movl %r15d, %eax
movl 0x1c(%rsp), %ecx
leal (%rcx,%rax), %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x13802
cmpl %ebp, %r12d
jle 0x29f5a
movq %rsp, %r15
movl (%r15), %ebx
leaq 0x38(%rsp), %rsi
movl %ebx, (%rsi)
leaq 0x3e(%rsp), %rdi
callq 0x1c76f
movl %eax, %eax
movq 0x30(%rsp), %r13
subq %rax, %r13
movq 0x8(%rsp), %rsi
subq %rsi, %r13
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r13, %rdx
callq 0x33d0
addl $0xffff0000, %ebx       # imm = 0xFFFF0000
sarl $0xa, %ebx
addl $0xd800, %ebx           # imm = 0xD800
movl %ebx, (%r15)
movq 0x20(%rsp), %rdi
addq %r13, %rdi
movq %r15, %rsi
callq 0x1c76f
movl %r13d, %edx
addl %eax, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x13802
movl 0x8198(%r14), %eax
testl %eax, %eax
je 0x29f9c
decl %eax
movl %eax, 0x8198(%r14)
movq (%r14), %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
callq *0x10(%r14)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r14), %rdi
xorl %edx, %edx
callq *0x10(%r14)
movq %r14, %rdi
callq 0x8ec6
leaq 0x9866(%rip), %rsi      # 0x33809
xorl %eax, %eax
movq %r14, %rdi
callq 0xc939