// File: /workspace/github_c_makefile_star2_O2/ArtifexSoftware[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 $0x38, %rsp
movl %ecx, (%rsp)
movl %edx, %r13d
movq %rsi, %r14
leaq 0x14(%rsp), %r12
andl $0x0, (%r12)
movq %rdi, %r15
andl $0x0, 0x4(%rsp)
xorl %ebx, %ebx
movl %ebx, %ebp
movl %ebp, %eax
subl %r13d, %eax
jge 0x1e49d
movq %r12, %rdi
movq %r14, %rsi
callq 0x1241b
movl %eax, %eax
addq %r14, %rax
movl 0x14(%rsp), %ebx
xorl %ecx, %ecx
cmpl $0x10000, %ebx          # imm = 0x10000
setge %dl
movb %dl, %cl
movl %ebp, %edx
leal (%rdx,%rcx), %ebp
incl %ebp
movq %rax, %r14
jmp 0x1e469
movl %r13d, 0x10(%rsp)
movq %r15, 0x8(%rsp)
xorl %r13d, %r13d
movl %eax, %r15d
movq %r14, %r12
cmpl (%rsp), %r15d
jge 0x1e4ea
leaq 0x4(%rsp), %rdi
movq %r12, %rsi
callq 0x1241b
movl %eax, %eax
addq %r12, %rax
movl 0x4(%rsp), %r13d
xorl %ecx, %ecx
cmpl $0x10000, %r13d         # imm = 0x10000
setge %dl
movb %dl, %cl
movl %r15d, %edx
leal (%rdx,%rcx), %r15d
incl %r15d
movq %rax, %r12
jmp 0x1e4b0
cmpl 0x10(%rsp), %ebp
jne 0x1e50e
cmpl (%rsp), %r15d
jne 0x1e50e
movl %r12d, %edx
subl %r14d, %edx
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0xe918
jmp 0x1e61f
movq 0x8(%rsp), %rdi
callq 0xee71
movq %rax, %rdi
callq 0x3100
testl %eax, %eax
jne 0x1e62e
movq %r12, %rax
subq %r14, %rax
movq %rax, 0x30(%rsp)
movl %eax, 0x24(%rsp)
leal 0x4(%rax), %esi
movq 0x8(%rsp), %rdi
callq 0xa9e8
movq %rax, 0x28(%rsp)
movq %rax, 0x18(%rsp)
cmpl 0x10(%rsp), %ebp
jle 0x1e5a2
andl $0x3ff, %ebx            # imm = 0x3FF
orl $0xdc00, %ebx           # imm = 0xDC00
leaq 0x14(%rsp), %rsi
movl %ebx, (%rsi)
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x15185
movl %eax, %ebp
movl %eax, %eax
movl %eax, %edi
addq 0x28(%rsp), %rdi
movq %r14, %rsi
movq 0x30(%rsp), %rdx
callq 0x33d0
movl %ebp, %eax
movl 0x24(%rsp), %ecx
leal (%rcx,%rax), %edx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0xe918
cmpl (%rsp), %r15d
movq 0x8(%rsp), %r15
jle 0x1e609
movl %r13d, %edi
callq 0x15222
movl %eax, %eax
movq %r12, %rbx
subq %rax, %rbx
subq %r14, %rbx
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x33d0
addl $0xffff0000, %r13d      # imm = 0xFFFF0000
sarl $0xa, %r13d
addl $0xd800, %r13d          # imm = 0xD800
leaq 0x4(%rsp), %rsi
movl %r13d, (%rsi)
movq 0x28(%rsp), %rdi
addq %rbx, %rdi
callq 0x15185
movl %ebx, %edx
addl %eax, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0xe918
movq %r15, %rdi
callq 0xf5a7
movq (%r15), %rdi
movq 0x18(%rsp), %rsi
xorl %edx, %edx
callq *0x10(%r15)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rbx
movq (%rbx), %rdi
xorl %edx, %edx
callq *0x10(%rbx)
movq %rbx, %rdi
callq 0x7714