// File: /workspace/github_c_makefile_star2_O2/lh3[P]minigraph/algo.c
// Function: mg_mss_all
mg_msseg_t *mg_mss_all(void *km, int32_t n, const MG_MSS_TYPE *S, MG_MSS_TYPE min_sc, MG_MSS_TYPE xdrop, int32_t *n_seg)
{
    int32_t i, j;
    MG_MSS_TYPE L, max;
    msseg_v ret = {0,0,0};
    msseg_aux_v seg = {0,0,0};
    msseg_aux_t t;

	kv_resize(mg_msseg_t, km, ret, 16);
	kv_resize(msseg_aux_t, km, seg, 16);
    for (i = 0, L = 0, max = MSS_NEG_INF; i < n;) {
        if (S[i] > 0) {
            int32_t k;
            MG_MSS_TYPE R = L + S[i];
            for (k = i + 1; k < n && S[k] > 0; ++k)
                R += S[k];
			if (R > max) max = R;
            t.st = i, t.en = k, t.L = L, t.R = R;
            while (1) {
                msseg_aux_t *p;
                for (j = seg.n - 1; j >= 0;) {
                    p = &seg.a[j];
                    if (p->L < t.L) break;
                    j = p->pre >= 0? p->pre : j - 1;
                }
                if (j >= 0 && seg.a[j].R < t.R) {
                    p = &seg.a[j];
                    t.st = p->st, t.L = p->L, t.pre = p->pre;
                    seg.n = j;
                } else {
                    if (j < 0) {
						move_segs(km, &ret, &seg, min_sc);
						max = R;
					}
                    t.pre = j;
                    kv_push(msseg_aux_t, km, seg, t);
                    break;
                }
            }
            L = R, i = k;
        } else {
			if (xdrop > 0 && L + S[i] + xdrop < max) { // reset
				move_segs(km, &ret, &seg, min_sc);
				L = 0, max = MSS_NEG_INF;
			}
			L += S[i++];
		}
    }
    move_segs(km, &ret, &seg, min_sc);
    kfree(km, seg.a);
	KREALLOC(km, ret.a, ret.n);
    *n_seg = ret.n;
    return ret.a;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x38(%rsp)
movq %r8, 0x40(%rsp)
movl %ecx, 0x4(%rsp)
movq %rdx, %r13
leaq 0x50(%rsp), %r12
andq $0x0, (%r12)
leaq 0x18(%rsp), %rbp
andq $0x0, (%rbp)
movl %esi, %ebx
movq %rdi, %r15
pushq $0x10
popq %rax
movq %rax, 0x8(%r12)
xorl %r14d, %r14d
movl $0xc0, %edx
xorl %esi, %esi
callq 0x2774
movq %rax, 0x10(%r12)
pushq $0x10
popq %rax
movq %rax, 0x8(%rbp)
movl $0x140, %edx            # imm = 0x140
movq %r15, 0x10(%rsp)
movq %r15, %rdi
xorl %esi, %esi
callq 0x2774
movq %rax, 0x10(%rbp)
movl %ebx, 0xc(%rsp)
movslq %ebx, %r15
movl $0x80000000, %r8d       # imm = 0x80000000
xorl %edi, %edi
movq %r13, 0x48(%rsp)
cmpl 0xc(%rsp), %edi
jge 0x2f49
movslq %edi, %r12
movl (%r13,%r12,4), %eax
testl %eax, %eax
jle 0x2e0b
addl %r14d, %eax
incq %r12
movl %edi, %ebp
movl %eax, %ebx
cmpq %r15, %r12
jge 0x2e4f
movl (%r13,%r12,4), %ecx
leal (%rcx,%rbx), %eax
incl %ebp
incq %r12
testl %ecx, %ecx
jg 0x2df1
jmp 0x2e53
movq 0x40(%rsp), %rcx
testl %ecx, %ecx
jle 0x2e48
addl %r14d, %ecx
addl %eax, %ecx
cmpl %r8d, %ecx
jge 0x2e48
movl %edi, %ebx
movq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movl 0x4(%rsp), %ecx
callq 0x2fa0
movl %ebx, %edi
movl (%r13,%r12,4), %eax
movl $0x80000000, %r8d       # imm = 0x80000000
xorl %r14d, %r14d
addl %eax, %r14d
incl %edi
jmp 0x2dd3
movl 0xc(%rsp), %ebp
cmpl %r8d, %ebx
cmovgl %ebx, %r8d
movq 0x18(%rsp), %r13
movq 0x28(%rsp), %rax
leal -0x1(%r13), %r12d
testl %r12d, %r12d
js 0x2ea2
movl %r12d, %ecx
imulq $0x14, %rcx, %rsi
leaq (%rax,%rsi), %rdx
movl 0x8(%rax,%rsi), %esi
cmpl %r14d, %esi
jl 0x2e8f
movl 0x10(%rdx), %ecx
decl %r12d
testl %ecx, %ecx
cmovnsl %ecx, %r12d
jmp 0x2e68
cmpl %ebx, 0xc(%rdx)
jge 0x2f43
movl (%rdx), %edi
movq %rcx, %r13
movl %esi, %r14d
jmp 0x2e64
movl %edi, 0x8(%rsp)
movq %r13, 0x18(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movl 0x4(%rsp), %ecx
callq 0x2fa0
movq 0x18(%rsp), %r13
movq 0x28(%rsp), %rax
movl %ebx, %r8d
cmpq 0x20(%rsp), %r13
jne 0x2f0e
leaq (,%r13,2), %rcx
testq %r13, %r13
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x20(%rsp)
imulq $0x14, %rcx, %rdx
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movl %r8d, 0x34(%rsp)
callq 0x2774
movl 0x34(%rsp), %r8d
movq %rax, 0x28(%rsp)
movl 0x8(%rsp), %edx
leaq 0x1(%r13), %rcx
movq %rcx, 0x18(%rsp)
imulq $0x14, %r13, %rcx
movl %edx, (%rax,%rcx)
movl %ebp, 0x4(%rax,%rcx)
movl %r14d, 0x8(%rax,%rcx)
movl %ebx, 0xc(%rax,%rcx)
movl %r12d, 0x10(%rax,%rcx)
movl %ebx, %r14d
movl %ebp, %edi
movq 0x48(%rsp), %r13
jmp 0x2dd3
movl %edi, 0x8(%rsp)
jmp 0x2ed0
leaq 0x50(%rsp), %rbx
leaq 0x18(%rsp), %r15
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl 0x4(%rsp), %ecx
callq 0x2fa0
movq 0x10(%r15), %rsi
movq %r12, %rdi
callq 0x2577
movq (%rbx), %r14
movq 0x10(%rbx), %rsi
imulq $0xc, %r14, %rdx
movq %r12, %rdi
callq 0x2774
movq 0x38(%rsp), %rcx
movl %r14d, (%rcx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq