// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %r9, 0x60(%rsp)
movl %r8d, %ebx
movl %ecx, 0x14(%rsp)
movq %rdx, %r14
movl %esi, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movq %rax, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
cmpq $0xf, 0x48(%rsp)
ja 0x38e1
movq $0x10, 0x48(%rsp)
movq 0x50(%rsp), %rsi
movl $0xc0, %edx
movq 0x18(%rsp), %rdi
callq 0x32c4
movq %rax, 0x50(%rsp)
movq %r14, 0x38(%rsp)
movq %rbx, 0x70(%rsp)
cmpq $0xf, 0x28(%rsp)
ja 0x3915
movq $0x10, 0x28(%rsp)
movq 0x30(%rsp), %rsi
movl $0x140, %edx            # imm = 0x140
movq 0x18(%rsp), %rdi
callq 0x32c4
movq %rax, 0x30(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0x3aba
movl 0x10(%rsp), %eax
movq %rax, 0x68(%rsp)
movl $0x80000000, 0xc(%rsp)  # imm = 0x80000000
xorl %r12d, %r12d
xorl %ebx, %ebx
movslq %r12d, %rbp
movq 0x38(%rsp), %rax
movl (%rax,%rbp,4), %r15d
testl %r15d, %r15d
jle 0x3a69
addl %ebx, %r15d
leal 0x1(%r12), %r13d
cmpl 0x10(%rsp), %r13d
jge 0x397d
movslq %r13d, %r13
movq 0x38(%rsp), %rcx
movq 0x68(%rsp), %rdx
movl (%rcx,%r13,4), %eax
testl %eax, %eax
jle 0x397d
addl %eax, %r15d
incq %r13
cmpq %r13, %rdx
jne 0x3967
movl %edx, %r13d
movl 0xc(%rsp), %eax
cmpl %eax, %r15d
cmovgl %r15d, %eax
movl %eax, 0xc(%rsp)
movl 0x20(%rsp), %ebp
decl %ebp
js 0x39d1
movq 0x30(%rsp), %rax
movl %ebp, %ecx
leaq (%rcx,%rcx,4), %rdx
leaq (%rax,%rdx,4), %rsi
movl 0x8(%rsi), %edx
cmpl %ebx, %edx
jl 0x39ba
movl 0x10(%rsi), %ecx
decl %ebp
testl %ecx, %ecx
cmovnsl %ecx, %ebp
testl %ebp, %ebp
jns 0x3999
jmp 0x39d1
cmpl %r15d, 0xc(%rsi)
jge 0x39d1
movl (%rsi), %r12d
movq %rcx, 0x20(%rsp)
movb $0x1, %al
movl %edx, %ebx
jmp 0x3a5c
testl %ebp, %ebp
jns 0x39f2
movq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movl 0x14(%rsp), %ecx
callq 0x3b1d
movl %r15d, 0xc(%rsp)
movq 0x20(%rsp), %r14
movq 0x28(%rsp), %rax
cmpq %rax, %r14
jne 0x3a32
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x28(%rsp)
movq 0x30(%rsp), %rsi
shlq $0x2, %rcx
leaq (%rcx,%rcx,4), %rdx
movq 0x18(%rsp), %rdi
callq 0x32c4
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
leaq 0x1(%r14), %rcx
movq %rcx, 0x20(%rsp)
leaq (%r14,%r14,4), %rcx
movl %r12d, (%rax,%rcx,4)
movl %r13d, 0x4(%rax,%rcx,4)
movl %ebx, 0x8(%rax,%rcx,4)
movl %r15d, 0xc(%rax,%rcx,4)
movl %ebp, 0x10(%rax,%rcx,4)
xorl %eax, %eax
testb %al, %al
jne 0x398c
movl %r15d, %ebx
jmp 0x3aac
movq 0x70(%rsp), %rax
testl %eax, %eax
jle 0x3aa0
addl %ebx, %eax
addl %eax, %r15d
cmpl 0xc(%rsp), %r15d
jge 0x3aa0
movq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movl 0x14(%rsp), %ecx
callq 0x3b1d
movl $0x80000000, 0xc(%rsp)  # imm = 0x80000000
xorl %ebx, %ebx
leal 0x1(%rbp), %r13d
movq 0x38(%rsp), %rax
addl (%rax,%rbp,4), %ebx
movl %r13d, %r12d
cmpl 0x10(%rsp), %r13d
jl 0x3936
leaq 0x40(%rsp), %rbx
leaq 0x20(%rsp), %r15
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl 0x14(%rsp), %ecx
callq 0x3b1d
movq 0x10(%r15), %rsi
movq %r12, %rdi
callq 0x30f6
movq (%rbx), %r14
movq 0x10(%rbx), %rsi
leaq (,%r14,4), %rax
leaq (%rax,%rax,2), %rdx
movq %r12, %rdi
callq 0x32c4
movq %rax, 0x10(%rbx)
movq 0x60(%rsp), %rcx
movl %r14d, (%rcx)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq