// File: /workspace/github_c_makefile_star2_O2/Oric4ever[P]lcc65/6502/build/../src/stmt.c
// Function: swcode
static int swcode(swp, b, lb, ub, n) struct swtch *swp; int lb, ub, n, b[]; {
	int hilab, k, l, lolab, median, u, *v;

	v = swp->values;
	median = ((float)v[b[lb]] + (float)v[b[ub+1]-1] + 1)/2;
	for (l = lb, u = ub; l <= u; ) {
		k = (l + u)/2;
		if (v[b[k]] > median)
			u = k - 1;
		else if (v[b[k+1]-1] < median)
			l = k + 1;
		else
			break;
	}
	if (l > u) {	/* which of two buckets is closest to the median? */
		l = b[k];
		u = b[k+1] - 1;
		if (median < v[l] && k > lb && v[l] - median > median - v[l-1])
			k--;
		else if (median > v[u] && k < ub && median - v[u] > v[u+1] - median)
			k++;
	}
	l = b[k];
	u = b[k+1] - 1;
	lolab = hilab = swp->deflab->u.l.label;
	if (k > lb) {
		lolab = 0;
		if (k < ub)
			hilab = genlabel(1);
	} else if (k < ub)
		hilab = 0;
	if (n == 0)
		lolab = hilab = 0;
	if (u - l + 1 <= 3) {
		int i;
		for (i = l; i <= u; i++)
			cmp(EQ, swp->sym, swp->values[i], swp->labels[i]->u.l.label);
		if (lolab && lolab == hilab)
			branch(lolab);
		else {
			if (lolab)
				cmp(LT, swp->sym, swp->values[l], lolab);
			if (hilab)
				cmp(GT, swp->sym, swp->values[u], hilab);
			walk(0, 0, 0);
		}
	} else {
		int lab = genlabel(1);
		Symbol tab = genident(STATIC, array(voidptype, u - l + 1, 0), GLOBAL);
		cmp(LT, swp->sym, swp->values[l], lolab ? lolab : lab);
		cmp(GT, swp->sym, swp->values[u], hilab ? hilab : lab);
		walk(tree(JUMP, voidtype, rvalue((*opnode['+'])(ADD, pointer(idnode(tab)),
			(*opnode['-'])(SUB, cast(idnode(swp->sym), inttype),
				constnode(swp->values[l], inttype)))), 0), 0, 0);
		code(Switch);
		codelist->u.swtch.sym = swp->sym;
		codelist->u.swtch.deflab = swp->deflab;
		codelist->u.swtch.table = tab;
		codelist->u.swtch.size = u - l + 1;
		codelist->u.swtch.values = &swp->values[l];
		codelist->u.swtch.labels = &swp->labels[l];
		branch(0);
		definelab(lab);
		if (swp->values[u] - swp->values[l] + 1 >= 10000)
			warning("switch generates a huge table\n");
	}
	if (k > lb)
		n = swcode(swp, b, lb, k - 1, n - 1);
	if (k < ub) {
		if (hilab)
			definelab(hilab);
		n = swcode(swp, b, k + 1, ub, n - 1);
	}
	return n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r8d, 0x8(%rsp)
movl %ecx, %r13d
movl %edx, %r9d
movq %rsi, %r14
movq %rdi, %r15
movslq %ecx, %rax
movq %rax, 0x60(%rsp)
pushq $0x2
popq %rbx
movl %ecx, 0x10(%rsp)
movq 0x20(%r15), %rcx
movslq %r9d, %rax
movslq (%r14,%rax,4), %rax
cvtsi2ssl (%rcx,%rax,4), %xmm0
movq 0x60(%rsp), %rax
movslq 0x4(%r14,%rax,4), %rax
cvtsi2ssl -0x4(%rcx,%rax,4), %xmm1
addss %xmm0, %xmm1
addss 0x92ad(%rip), %xmm1     # 0x1e524
mulss 0x92a9(%rip), %xmm1     # 0x1e528
cvttss2si %xmm1, %esi
movl %r9d, %r8d
movl %r13d, %edi
cmpl %edi, %r8d
jg 0x152bb
leal (%rdi,%r8), %eax
cltd
idivl %ebx
movl %eax, %r12d
cltq
movslq (%r14,%rax,4), %rdx
cmpl %esi, (%rcx,%rdx,4)
jle 0x152aa
movl %r12d, %edi
decl %edi
jmp 0x15289
leal 0x1(%rax), %r8d
movslq 0x4(%r14,%rax,4), %rax
cmpl %esi, -0x4(%rcx,%rax,4)
jl 0x15289
jmp 0x15305
movslq %r12d, %rdx
movslq 0x4(%r14,%rdx,4), %rax
cmpl %r9d, %r12d
jle 0x152e5
movslq (%r14,%rdx,4), %rdx
movl (%rcx,%rdx,4), %edi
subl %esi, %edi
jle 0x152e5
movl %esi, %r8d
subl -0x4(%rcx,%rdx,4), %r8d
cmpl %r8d, %edi
jle 0x152e5
decl %r12d
jmp 0x15305
cmpl %r13d, %r12d
jge 0x15305
movl %esi, %edx
subl -0x4(%rcx,%rax,4), %edx
jle 0x15305
leal 0x1(%r12), %edi
movl (%rcx,%rax,4), %eax
subl %esi, %eax
cmpl %eax, %edx
cmovlel %r12d, %edi
movl %edi, %r12d
movslq %r12d, %rax
movslq (%r14,%rax,4), %rdx
movslq 0x4(%r14,%rax,4), %rbp
movq 0x10(%r15), %rax
movl 0x48(%rax), %eax
cmpl %r9d, %r12d
movl %r9d, 0x2c(%rsp)
movq %rdx, %rbx
jle 0x1533a
cmpl %r13d, %r12d
jge 0x15335
pushq $0x1
popq %rdi
callq 0x1616e
movq %rbx, %rdx
xorl %r13d, %r13d
jmp 0x15348
cmpl %r13d, %r12d
movl %eax, %r13d
movl $0x0, %ecx
cmovll %ecx, %eax
movl 0x8(%rsp), %ecx
testl %ecx, %ecx
cmovel %ecx, %r13d
cmovel %ecx, %eax
leal -0x1(%rbp), %esi
movl %esi, %ecx
subl %edx, %ecx
cmpl $0x2, %ecx
movl %eax, 0xc(%rsp)
jg 0x1539e
movl %r13d, 0x18(%rsp)
movq %rdx, %r13
cmpq %rbp, %r13
jge 0x15589
movq (%r15), %rsi
movq 0x20(%r15), %rax
movl (%rax,%r13,4), %edx
movq 0x28(%r15), %rax
movq (%rax,%r13,8), %rax
movl 0x48(%rax), %ecx
movl $0x1e0, %edi            # imm = 0x1E0
callq 0x15646
movq %rbx, %rdx
incq %r13
jmp 0x1536d
incl %ecx
pushq $0x1
popq %rdi
movq %rdx, %rbx
movl %ecx, %ebp
movl %ecx, 0x28(%rsp)
movl %esi, 0x20(%rsp)
callq 0x1616e
movl %eax, 0x14(%rsp)
leaq 0x1cc30(%rip), %rax     # 0x31ff0
movq (%rax), %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x17382
pushq $0x51
popq %rdi
movq %rax, %rsi
pushq $0x3
popq %rdx
callq 0x16090
movq %rax, 0x18(%rsp)
movq (%r15), %rsi
movq 0x20(%r15), %rax
movq %rbx, 0x48(%rsp)
leaq (,%rbx,4), %rcx
movq %rcx, 0x58(%rsp)
movl (%rax,%rbx,4), %edx
testl %r13d, %r13d
movl 0x14(%rsp), %ebx
cmovel %ebx, %r13d
movl $0x220, %edi            # imm = 0x220
movl %r13d, %ecx
callq 0x15646
movq (%r15), %rsi
movq 0x20(%r15), %rax
movslq 0x20(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movl (%rax,%rcx,4), %edx
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
cmovel %ebx, %ecx
movl $0x200, %edi            # imm = 0x200
callq 0x15646
leaq 0x1cb9f(%rip), %rax     # 0x31fe0
movq (%rax), %rax
movq %rax, 0x50(%rsp)
leaq 0x12d10(%rip), %rbx     # 0x28160
movq 0x158(%rbx), %rax
movq %rax, 0x40(%rsp)
movq 0x18(%rsp), %rdi
callq 0x93b4
movq %rax, %rdi
callq 0xa22f
movq %rax, 0x38(%rsp)
movq 0x168(%rbx), %rax
movq %rax, 0x30(%rsp)
movq (%r15), %rdi
callq 0x93b4
leaq 0x1cb0a(%rip), %rbp     # 0x31f98
movq (%rbp), %rsi
movq %rax, %rdi
callq 0x9912
movq %rax, %rbx
movq 0x20(%r15), %rax
movq 0x48(%rsp), %r13
movl (%rax,%r13,4), %edi
movq (%rbp), %rsi
callq 0x8726
movl $0x140, %edi            # imm = 0x140
movq %rbx, %rsi
movq %rax, %rdx
callq *0x30(%rsp)
movl $0x130, %edi            # imm = 0x130
movq 0x38(%rsp), %rsi
movq %rax, %rdx
callq *0x40(%rsp)
movq %rax, %rdi
callq 0xa0b9
movl $0x240, %edi            # imm = 0x240
movq 0x50(%rsp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x165eb
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x376d
pushq $0xa
popq %rdi
callq 0x32d6
movq (%r15), %rax
leaq 0x12c2b(%rip), %rcx     # 0x28138
movq (%rcx), %rcx
movq %rax, 0x18(%rcx)
movq 0x10(%r15), %rax
movq %rax, 0x28(%rcx)
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rcx)
movl 0x28(%rsp), %eax
movl %eax, 0x30(%rcx)
movq 0x58(%rsp), %rax
addq 0x20(%r15), %rax
movq %rax, 0x38(%rcx)
leaq (,%r13,8), %rax
addq 0x28(%r15), %rax
movq %rax, 0x40(%rcx)
xorl %edi, %edi
callq 0x1413f
movl 0x14(%rsp), %edi
callq 0x13b80
movq 0x20(%r15), %rax
movq 0x20(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
subl (%rax,%r13,4), %ecx
cmpl $0x270f, %ecx           # imm = 0x270F
movl 0x10(%rsp), %r13d
pushq $0x2
popq %rbx
jl 0x155ed
leaq 0x9243(%rip), %rdi      # 0x1e7c3
xorl %eax, %eax
callq 0x9239
jmp 0x155ed
movl 0x18(%rsp), %ecx
testl %ecx, %ecx
je 0x155a5
cmpl 0xc(%rsp), %ecx
jne 0x155a5
movl %ecx, %edi
callq 0x1413f
movl 0x10(%rsp), %r13d
jmp 0x155ea
testl %ecx, %ecx
movl 0x10(%rsp), %r13d
je 0x155c2
movq (%r15), %rsi
movq 0x20(%r15), %rax
movl (%rax,%rdx,4), %edx
movl $0x220, %edi            # imm = 0x220
callq 0x15646
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
je 0x155df
movq (%r15), %rsi
movq 0x20(%r15), %rax
movl -0x4(%rax,%rbp,4), %edx
movl $0x200, %edi            # imm = 0x200
callq 0x15646
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x376d
pushq $0x2
popq %rbx
movl 0x2c(%rsp), %edx
cmpl %edx, %r12d
jle 0x15612
leal -0x1(%r12), %ecx
movl 0x8(%rsp), %r8d
decl %r8d
movq %r15, %rdi
movq %r14, %rsi
callq 0x1521d
movl %eax, 0x8(%rsp)
movl 0xc(%rsp), %edi
cmpl %r13d, %r12d
jge 0x15633
testl %edi, %edi
je 0x15624
callq 0x13b80
incl %r12d
decl 0x8(%rsp)
movl %r12d, %r9d
jmp 0x1524b
movl 0x8(%rsp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq