// File: /workspace/github_c_makefile_star2_O3/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, (%rsp)
movq %rdx, %r15
movl %esi, %r12d
movq %rdi, %r14
xorl %eax, %eax
movq %rax, 0x50(%rsp)
movq %rax, 0x10(%rsp)
movl $0x10, %ebx
movq %rbx, 0x58(%rsp)
movl $0xc0, %edx
xorl %esi, %esi
callq 0x31ed
movq %rax, 0x60(%rsp)
movq %rbx, 0x18(%rsp)
movl $0x140, %edx            # imm = 0x140
movq %r14, 0x8(%rsp)
movq %r14, %rdi
xorl %esi, %esi
callq 0x31ed
xorl %esi, %esi
movq %rax, 0x20(%rsp)
testl %r12d, %r12d
jle 0x39b9
movl %r12d, %ecx
movl $0x80000000, %edi       # imm = 0x80000000
xorl %r13d, %r13d
movq %r15, 0x48(%rsp)
movq %rcx, 0x30(%rsp)
movl %r12d, 0x4(%rsp)
movslq %esi, %rbx
movl (%r15,%rbx,4), %ebp
testl %ebp, %ebp
jle 0x395a
addl %r13d, %ebp
leal 0x1(%rsi), %ebx
cmpl %r12d, %ebx
jge 0x3853
movslq %ebx, %rbx
movl (%r15,%rbx,4), %eax
testl %eax, %eax
jle 0x3853
addl %eax, %ebp
incq %rbx
cmpq %rbx, %rcx
jne 0x383f
movl %ecx, %ebx
cmpl %edi, %ebp
cmovgl %ebp, %edi
movq 0x10(%rsp), %r15
movl %r15d, %r14d
decl %r14d
js 0x38a9
movq 0x20(%rsp), %rax
movl %r14d, %ecx
leaq (%rcx,%rcx,4), %rdx
leaq (%rax,%rdx,4), %rdx
movl 0x8(%rdx), %r12d
cmpl %r13d, %r12d
jl 0x3891
movl 0x10(%rdx), %ecx
decl %r14d
testl %ecx, %ecx
cmovnsl %ecx, %r14d
testl %r14d, %r14d
jns 0x386a
jmp 0x38a9
cmpl %ebp, 0xc(%rdx)
jge 0x39af
movl (%rdx), %esi
decl %r14d
movl %r12d, %r13d
movq %rcx, %r15
jns 0x386a
jmp 0x38af
movq %r15, %rcx
movl %r13d, %r12d
movq %rsi, 0x28(%rsp)
movq %rcx, 0x10(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movl (%rsp), %ecx
callq 0x3a17
movq 0x10(%rsp), %r15
movq 0x20(%rsp), %rax
movl %r12d, %r13d
movl %ebp, %edi
cmpq 0x18(%rsp), %r15
movl 0x4(%rsp), %r12d
jne 0x3925
leaq (%r15,%r15), %rcx
testq %r15, %r15
movl $0x2, %edx
cmoveq %rdx, %rcx
movq %rcx, 0x18(%rsp)
shlq $0x2, %rcx
leaq (%rcx,%rcx,4), %rdx
movl %edi, %r12d
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x31ed
movl %r12d, %edi
movl 0x4(%rsp), %r12d
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %rdx
leaq 0x1(%r15), %rcx
movq %rcx, 0x10(%rsp)
leaq (%r15,%r15,4), %rcx
movl %edx, (%rax,%rcx,4)
movl %ebx, 0x4(%rax,%rcx,4)
movl %r13d, 0x8(%rax,%rcx,4)
movl %ebp, 0xc(%rax,%rcx,4)
movl %r14d, 0x10(%rax,%rcx,4)
movl %ebx, %esi
movq 0x48(%rsp), %r15
movq 0x30(%rsp), %rcx
jmp 0x39a1
movq 0x40(%rsp), %rax
testl %eax, %eax
jle 0x399a
addl %r13d, %eax
addl %ebp, %eax
cmpl %edi, %eax
jge 0x399a
movq 0x8(%rsp), %rdi
movq %rsi, %r14
leaq 0x50(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movl (%rsp), %ecx
callq 0x3a17
movq 0x30(%rsp), %rcx
movq %r14, %rsi
movl (%r15,%rbx,4), %ebp
movl $0x80000000, %edi       # imm = 0x80000000
xorl %r13d, %r13d
addl %r13d, %ebp
incl %esi
movl %esi, %ebx
movl %ebp, %r13d
cmpl %r12d, %ebx
jl 0x3822
jmp 0x39b9
movq %rsi, 0x28(%rsp)
jmp 0x38df
leaq 0x50(%rsp), %rbx
leaq 0x10(%rsp), %r15
movq 0x8(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl (%rsp), %ecx
callq 0x3a17
movq 0x10(%r15), %rsi
movq %r12, %rdi
callq 0x301f
movq (%rbx), %r14
movq 0x10(%rbx), %rsi
leaq (,%r14,4), %rax
leaq (%rax,%rax,2), %rdx
movq %r12, %rdi
callq 0x31ed
movq 0x38(%rsp), %rcx
movl %r14d, (%rcx)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq