// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movl %ecx, %ebp
movl %edx, %r15d
movq %rdi, 0x28(%rsp)
xorl %r13d, %r13d
movl %r13d, 0x4(%rsp)
movl %r13d, (%rsp)
testl %edx, %edx
jle 0x299c9
xorl %eax, %eax
leaq 0x4(%rsp), %r14
movq %rsi, %r12
movl %eax, %esi
movl %esi, %ebx
movq %r14, %rdi
movq %r12, %rsi
callq 0x18239
movl %eax, %eax
addq %r12, %rax
movl 0x4(%rsp), %ecx
xorl %edx, %edx
cmpl $0x10000, %ecx          # imm = 0x10000
setge %sil
movb %sil, %dl
movl %ebx, %esi
leal (%rsi,%rdx), %ebx
incl %ebx
movq %rax, %r12
movl %ebx, %esi
cmpl %r15d, %ebx
jl 0x29982
andl $0x3ff, %ecx            # imm = 0x3FF
orl $0xdc00, %ecx           # imm = 0xDC00
movq %rax, %r14
jmp 0x299d4
movl $0xdc00, %ecx           # imm = 0xDC00
movl %r13d, %ebx
movq %rsi, %r14
movl %ecx, 0x1c(%rsp)
movl %ebx, %ecx
subl %r15d, %ecx
cmpl %ebp, %ecx
movq %r14, 0x8(%rsp)
jge 0x29a2c
movq %rsp, %r12
movq %r14, %r13
movl %ecx, %r14d
movq %r12, %rdi
movq %r13, %rsi
callq 0x18239
movl %eax, %eax
addq %r13, %rax
movl (%rsp), %edi
xorl %ecx, %ecx
cmpl $0x10000, %edi          # imm = 0x10000
setge %dl
movb %dl, %cl
movl %r14d, %edx
leal 0x1(%rdx,%rcx), %r8d
movq %rax, %rdx
movq %rax, %r13
movl %r8d, %ecx
movl %r8d, %r14d
movl %edi, %eax
cmpl %ebp, %r8d
jl 0x299ef
jmp 0x29a32
movl %r13d, %eax
movq %r14, %rdx
movq %rdx, %r13
movl %ecx, %r12d
cmpl %r15d, %ebx
jne 0x29a5b
cmpl %ebp, %r12d
jne 0x29a5b
movq 0x8(%rsp), %rsi
movl %r13d, %edx
subl %esi, %edx
movq 0x28(%rsp), %rdi
callq 0x133b3
jmp 0x29b6f
movl %eax, 0x18(%rsp)
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x13b6c
movq %rax, %rdi
callq 0x3100
testl %eax, %eax
jne 0x29b7e
movq %r13, 0x30(%rsp)
subq 0x8(%rsp), %r13
movl %r13d, %eax
movl %r13d, 0x14(%rsp)
leal 0x4(%rax), %esi
movq %r14, %rdi
callq 0xde05
movq %rax, 0x20(%rsp)
cmpl %r15d, %ebx
movq %rax, %rbx
jle 0x29ae7
leaq 0x4(%rsp), %rsi
movl 0x1c(%rsp), %eax
movl %eax, (%rsi)
movq %rbx, %rdi
callq 0x1c2e2
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 0x14(%rsp), %ecx
leal (%rcx,%rax), %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x133b3
cmpl %ebp, %r12d
jle 0x29b4f
leaq 0x38(%rsp), %rsi
movl 0x18(%rsp), %ebp
movl %ebp, (%rsi)
leaq 0x3e(%rsp), %rdi
callq 0x1c2e2
movl %eax, %eax
movq 0x30(%rsp), %r15
subq %rax, %r15
movq 0x8(%rsp), %rsi
subq %rsi, %r15
movq %rbx, %rdi
movq %r15, %rdx
callq 0x33d0
movl %ebp, %eax
addl $0xffff0000, %eax       # imm = 0xFFFF0000
sarl $0xa, %eax
addl $0xd800, %eax           # imm = 0xD800
movq %rsp, %rsi
movl %eax, (%rsi)
movq 0x20(%rsp), %rdi
addq %r15, %rdi
callq 0x1c2e2
movl %r15d, %edx
addl %eax, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x133b3
movl 0x8198(%r14), %eax
testl %eax, %eax
je 0x29b8f
decl %eax
movl %eax, 0x8198(%r14)
movq (%r14), %rdi
movq %rbx, %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 0x8e1b
leaq 0x9ca3(%rip), %rsi      # 0x33839
xorl %eax, %eax
movq %r14, %rdi
callq 0xc666