// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_scaf_res_t(scaf_res_t*, unsigned int, unsigned int, unsigned int, unsigned int*, unsigned int*, unsigned int*, unsigned int*, uc_block_t**)
uint32_t get_scaf_res_t(scaf_res_t *pa, uint32_t uid, uint32_t s, uint32_t e, uint32_t *in, uint32_t *rs, uint32_t *re, uint32_t *rn, uc_block_t **ra)
{
    // fprintf(stderr, "s::%u, e::%u, uid::%u, pa->n::%u\n", s, e, uid, (uint32_t)pa->n);
    if(uid >= pa->n) return 0;
    ul_vec_t *idx = &(pa->a[uid]); uint32_t k, z, a_n, os, oe, z0; uc_block_t *a; 
    (*rs) = (*re) = (uint32_t)-1; (*rn) = 0; (*ra) = NULL;
    
    for (k = (*in); k < idx->bb.n; k++) {
        a = idx->bb.a + idx->bb.a[k].ts; a_n = idx->bb.a[k].te - idx->bb.a[k].ts;
        os = MAX(s, idx->bb.a[k].qs); 
        oe = MIN(e, idx->bb.a[k].qe);
        // fprintf(stderr, "k::%u, os::%u, oe::%u, a_n::%u\n", k, os, oe, a_n);
        if(os >= oe) continue;
        for(z = 0; z < a_n && a[z].qe <= os; z++);
        if(z >= a_n) continue;
        for(z0 = z; z < a_n && a[z].qs < oe; z++);
        if(z <= z0 ) continue;
        *rs = os; *re = oe; *rn = z - z0; *ra = a + z0; (*in) = k + 1;
        return 1;
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jb 0x12804e
movl $0x0, -0x4(%rbp)
jmp 0x128283
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x14(%rbp), %ecx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq 0x10(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x30(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x18(%rbp), %rax
movl $0x0, (%rax)
movq 0x20(%rbp), %rax
movq $0x0, (%rax)
movq -0x28(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jae 0x12827c
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x30(%rcx), %rcx
movl -0x3c(%rbp), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movq -0x38(%rbp), %rcx
movq 0x30(%rcx), %rcx
movl -0x3c(%rbp), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
subl 0xc(%rcx), %eax
movl %eax, -0x44(%rbp)
movl -0x18(%rbp), %eax
movq -0x38(%rbp), %rcx
movq 0x30(%rcx), %rcx
movl -0x3c(%rbp), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jb 0x12811d
movl -0x18(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x128135
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x1c(%rbp), %eax
movq -0x38(%rbp), %rcx
movq 0x30(%rcx), %rcx
movl -0x3c(%rbp), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
ja 0x12815d
movl -0x1c(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x128175
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jb 0x128188
jmp 0x12826e
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %ecx
xorl %eax, %eax
cmpl -0x44(%rbp), %ecx
movb %al, -0x61(%rbp)
jae 0x1281b6
movq -0x58(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x48(%rbp), %eax
setbe %al
movb %al, -0x61(%rbp)
movb -0x61(%rbp), %al
testb $0x1, %al
jne 0x1281bf
jmp 0x1281cc
jmp 0x1281c1
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x12818f
movl -0x40(%rbp), %eax
cmpl -0x44(%rbp), %eax
jb 0x1281d9
jmp 0x12826e
movl -0x40(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x40(%rbp), %ecx
xorl %eax, %eax
cmpl -0x44(%rbp), %ecx
movb %al, -0x62(%rbp)
jae 0x128206
movq -0x58(%rbp), %rax
movl -0x40(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpl -0x4c(%rbp), %eax
setb %al
movb %al, -0x62(%rbp)
movb -0x62(%rbp), %al
testb $0x1, %al
jne 0x12820f
jmp 0x12821c
jmp 0x128211
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1281df
movl -0x40(%rbp), %eax
cmpl -0x50(%rbp), %eax
ja 0x128226
jmp 0x12826e
movl -0x48(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movl -0x4c(%rbp), %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
movl -0x40(%rbp), %ecx
subl -0x50(%rbp), %ecx
movq 0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x58(%rbp), %rcx
movl -0x50(%rbp), %eax
imulq $0x24, %rax, %rax
addq %rax, %rcx
movq 0x20(%rbp), %rax
movq %rcx, (%rax)
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x128283
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x128095
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq
nopl (%rax,%rax)