// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x304a7
movq -0x28(%rbp), %rsi
leaq -0x1c(%rbp), %rdi
callq 0x19d20
movl %eax, %ecx
movq -0x28(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
cmpl $0x10000, -0x1c(%rbp)   # imm = 0x10000
jl 0x3049a
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x3049c
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x30463
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x3c(%rbp), %eax
subl -0x14(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x304fc
movq -0x30(%rbp), %rsi
leaq -0x20(%rbp), %rdi
callq 0x19d20
movl %eax, %ecx
movq -0x30(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
cmpl $0x10000, -0x20(%rbp)   # imm = 0x10000
jl 0x304ef
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x304f1
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x304b8
movl -0x3c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jne 0x3052b
movl -0x40(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x3052b
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
subq %rcx, %rax
movl %eax, %edx
callq 0x13a70
jmp 0x3068b
movq -0x8(%rbp), %rdi
callq 0x14270
movq %rax, %rdi
callq 0x30e0
cmpl $0x0, %eax
je 0x30557
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0xd710
movq -0x8(%rbp), %rdi
callq 0x8cb0
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
subq %rcx, %rax
addq $0x4, %rax
movl %eax, %esi
callq 0xd9d0
movq %rax, -0x38(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jle 0x305e2
movl -0x1c(%rbp), %eax
subl $0x10000, %eax          # imm = 0x10000
andl $0x3ff, %eax            # imm = 0x3FF
addl $0xdc00, %eax           # imm = 0xDC00
movl %eax, -0x1c(%rbp)
movq -0x38(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x1d910
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rdi
movslq -0x44(%rbp), %rax
addq %rax, %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x28(%rbp), %rax
subq %rax, %rdx
callq 0x33a0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movslq -0x44(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rdx
subq %rdx, %rcx
addq %rcx, %rax
movl %eax, %edx
callq 0x13a70
movl -0x40(%rbp), %eax
cmpl -0x18(%rbp), %eax
jle 0x30675
movl -0x20(%rbp), %edi
callq 0x1da60
movl %eax, %ecx
movq -0x30(%rbp), %rax
movslq %ecx, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x28(%rbp), %rax
subq %rax, %rdx
callq 0x33a0
movl -0x20(%rbp), %eax
subl $0x10000, %eax          # imm = 0x10000
sarl $0xa, %eax
addl $0xd800, %eax           # imm = 0xD800
movl %eax, -0x20(%rbp)
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
subq %rcx, %rax
addq %rax, %rdi
leaq -0x20(%rbp), %rsi
callq 0x1d910
movl %eax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
subq %rcx, %rax
movslq -0x48(%rbp), %rcx
addq %rcx, %rax
movl %eax, %edx
callq 0x13a70
movq -0x8(%rbp), %rdi
callq 0x14a10
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0xd710
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)