// File: /workspace/github_c_makefile_star2_O2/nh13[P]ksw/src/parasail/src/pssw.c
// Function: parasail_ssw_profile
parasail_result_ssw_t* parasail_ssw_profile(
        const parasail_profile_t * const restrict profile,
        const char * const restrict s2, const int s2Len_,
        const int open, const int gap)
{
    const char *s1 = profile->s1;
    const parasail_matrix_t *matrix = profile->matrix;
    parasail_result_t *result_forward = NULL;
    parasail_result_t *result_reverse = NULL;
    parasail_result_t *result_final = NULL;
    parasail_result_ssw_t *result_ssw = NULL;
    int has8 = 0;
    int has16 = 0;
    char *s1_reverse = NULL;
    char *s2_reverse = NULL;
    int s1Len = 0;
    int s2Len = 0;
    int s1Off = 0;
    int s2Off = 0;
    parasail_cigar_t *cigar = NULL;

    /* find the end loc first with the faster implementation */
    has8 = (NULL != profile->profile8.score);
    has16 = (NULL != profile->profile16.score);

    /* find the end loc first with the faster implementation */
    if (has8) {
        result_forward = parasail_sw_striped_profile_8(profile, s2, s2Len_, open, gap);
        if (parasail_result_is_saturated(result_forward)) {
            has8 = 0;
            parasail_result_free(result_forward);
            result_forward = NULL;
        }
    }
    if (NULL == result_forward && has16) {
        result_forward = parasail_sw_striped_profile_16(profile, s2, s2Len_, open, gap);
        if (parasail_result_is_saturated(result_forward)) {
            parasail_result_free(result_forward);
            result_forward = NULL;
        }
    }
    /* 8- and/or 16-bit options could fail */
    if (NULL == result_forward) {
        return NULL;
    }

    /* find beginning loc by going in reverse */
    s1Len = result_forward->end_query+1;
    s2Len = result_forward->end_ref+1;
    s1_reverse = parasail_reverse(s1, s1Len);
    s2_reverse = parasail_reverse(s2, s2Len);
    if (has8) {
        result_reverse = parasail_sw_striped_8(
                s1_reverse, s1Len,
                s2_reverse, s2Len,
                open, gap, matrix);
        if (parasail_result_is_saturated(result_reverse)) {
            has8 = 0;
            parasail_result_free(result_reverse);
            result_reverse = NULL;
        }
    }
    if (NULL == result_reverse && has16) {
        result_reverse = parasail_sw_striped_16(
                s1_reverse, s1Len,
                s2_reverse, s2Len,
                open, gap, matrix);
        if (parasail_result_is_saturated(result_reverse)) {
            parasail_result_free(result_reverse);
            result_reverse = NULL;
        }
    }
    free(s2_reverse);
    free(s1_reverse);

    /* 8- and/or 16-bit options could fail */
    if (NULL == result_reverse) {
        parasail_result_free(result_forward); /* free the forward result */
        return NULL;
    }

    /* run trace version of sw on just the aligned portion */
    s1Off = result_forward->end_query - result_reverse->end_query;
    s2Off = result_forward->end_ref - result_reverse->end_ref;
    s1Len = result_reverse->end_query+1;
    s2Len = result_reverse->end_ref+1;
    if (has8) {
        result_final = parasail_sw_trace_striped_8(
                &s1[s1Off], s1Len,
                &s2[s2Off], s2Len,
                open, gap, matrix);
        if (parasail_result_is_saturated(result_final)) {
            has8 = 0;
            parasail_result_free(result_final);
            result_final = NULL;
        }
    }
    if (NULL == result_final && has16) {
        result_final = parasail_sw_trace_striped_16(
                &s1[s1Off], s1Len,
                &s2[s2Off], s2Len,
                open, gap, matrix);
        if (parasail_result_is_saturated(result_final)) {
            parasail_result_free(result_final);
            result_final = NULL;
        }
    }

    /* 8- and/or 16-bit options could fail */
    if (NULL == result_final) {
        parasail_result_free(result_reverse); /* free reverse result */
        parasail_result_free(result_forward); /* free forward result */
        return NULL;
    }

    /* get cigar from traceback */
    cigar = parasail_result_get_cigar(result_final,
            &s1[s1Off], s1Len,
            &s2[s2Off], s2Len,
            matrix);

    result_ssw = (parasail_result_ssw_t*)malloc(sizeof(parasail_result_ssw_t));
    result_ssw->score1 = result_forward->score;
    result_ssw->ref_begin1 = s2Off;
    result_ssw->ref_end1 = result_forward->end_ref;
    result_ssw->read_begin1 = s1Off;
    result_ssw->read_end1 = result_forward->end_query;
    result_ssw->cigar = cigar->seq;
    result_ssw->cigarLen = cigar->len;

    free(cigar);
    parasail_result_free(result_final);
    parasail_result_free(result_reverse);
    parasail_result_free(result_forward);

    return result_ssw;;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %r14
movq (%rdi), %rax
movq 0x10(%rdi), %r13
cmpq $0x0, 0x18(%rdi)
movq 0x30(%rdi), %rdx
movq %rdx, 0x20(%rsp)
movq %rax, 0x38(%rsp)
movl %r8d, 0x18(%rsp)
movl %ecx, 0x14(%rsp)
je 0x1f767b
movq %r14, %rdi
movq %r12, %rsi
movl %ebp, %edx
callq 0x205d93
movq %rax, %rbx
movq %rax, %rdi
callq 0x1f5e3e
testl %eax, %eax
je 0x1f7682
movq %rbx, %rdi
callq 0x1f4da9
xorl %ebx, %ebx
xorl %r15d, %r15d
jmp 0x1f7685
movb $0x1, %r15b
cmpq $0x0, 0x20(%rsp)
setne %al
testq %rbx, %rbx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1f76c4
movq %r14, %rdi
movq %r12, %rsi
movl %ebp, %edx
movl 0x14(%rsp), %ecx
movl 0x18(%rsp), %r8d
callq 0x205d8a
movq %rax, %rbx
movq %rax, %rdi
callq 0x1f5e3e
testl %eax, %eax
jne 0x1f7962
testq %rbx, %rbx
je 0x1f796a
movq %r13, 0x30(%rsp)
movq %r12, %rbp
movslq 0x4(%rbx), %r13
incq %r13
movslq 0x8(%rbx), %r14
incq %r14
movq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x1f5cb1
movq %rax, %r12
movq %rbp, 0x40(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x1f5cb1
movq %rax, %rbp
movb $0x1, %al
movl %eax, 0x2c(%rsp)
testb %r15b, %r15b
je 0x1f7750
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movl %r13d, %esi
movq %rbp, %rdx
movl %r14d, %ecx
movl 0x14(%rsp), %r8d
movl 0x18(%rsp), %r9d
callq 0x205a9f
movq %rax, %r15
movq %rax, %rdi
callq 0x1f5e3e
testl %eax, %eax
je 0x1f78b4
movq %r15, %rdi
callq 0x1f4da9
xorl %r15d, %r15d
cmpq $0x0, 0x20(%rsp)
setne %al
testq %r15, %r15
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1f77a7
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movl %r13d, %esi
movq %rbp, %rdx
movl %r14d, %ecx
movl 0x14(%rsp), %r8d
movl 0x18(%rsp), %r9d
callq 0x205a96
movq %rax, %r15
movq %rax, %rdi
callq 0x1f5e3e
testl %eax, %eax
je 0x1f77a7
movq %r15, %rdi
callq 0x1f4da9
xorl %r15d, %r15d
movq %rbp, %rdi
callq 0x1f01c0
movq %r12, %rdi
callq 0x1f01c0
testq %r15, %r15
je 0x1f7962
movslq 0x4(%rbx), %r14
movslq 0x4(%r15), %rax
subq %rax, %r14
movslq 0x8(%rbx), %r13
movslq 0x8(%r15), %rcx
subq %rcx, %r13
leal 0x1(%rax), %r8d
leal 0x1(%rcx), %r9d
cmpb $0x0, 0x2c(%rsp)
movl %r8d, 0x1c(%rsp)
je 0x1f77ef
xorl %r12d, %r12d
jmp 0x1f7842
movq %r14, %rdi
addq 0x38(%rsp), %rdi
movq %r13, %rdx
addq 0x40(%rsp), %rdx
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
movl %r8d, %esi
movl %r9d, %ebp
movl %r9d, %ecx
movl 0x14(%rsp), %r8d
movl 0x18(%rsp), %r9d
callq 0x205d27
movq %rax, %r12
movq %rax, %rdi
callq 0x1f5e3e
testl %eax, %eax
je 0x1f783a
movq %r12, %rdi
callq 0x1f4da9
xorl %r12d, %r12d
movl 0x1c(%rsp), %r8d
movl %ebp, %r9d
cmpq $0x0, 0x20(%rsp)
setne %al
testq %r12, %r12
sete %cl
andb %al, %cl
movslq %r14d, %rdx
movslq %r13d, %rbp
cmpb $0x1, %cl
movq 0x40(%rsp), %r14
movq 0x30(%rsp), %r13
movq %rdx, 0x20(%rsp)
jne 0x1f78d0
movq 0x38(%rsp), %rax
leaq (%rax,%rdx), %rdi
leaq (%r14,%rbp), %rdx
movq %r13, (%rsp)
movl %r8d, %esi
movl %r9d, %ecx
movl 0x14(%rsp), %r8d
movl %r9d, 0x14(%rsp)
movl 0x18(%rsp), %r9d
callq 0x205d1e
movq %rax, %r12
movq %rax, %rdi
callq 0x1f5e3e
testl %eax, %eax
je 0x1f78c1
movq %r12, %rdi
callq 0x1f4da9
jmp 0x1f795a
movl $0x0, 0x2c(%rsp)
jmp 0x1f7753
movl 0x1c(%rsp), %r8d
movl 0x14(%rsp), %r9d
movq 0x20(%rsp), %rdx
testq %r12, %r12
je 0x1f795a
movq 0x38(%rsp), %rsi
addq %rdx, %rsi
addq %rbp, %r14
movq %r12, %rdi
movl %r8d, %edx
movq %r14, %rcx
movl %r9d, %r8d
movq %r13, %r9
callq 0x5b58e7
movq %rax, %r13
pushq $0x28
popq %rdi
callq 0x1f01f0
movq %rax, %r14
movzwl (%rbx), %eax
movw %ax, (%r14)
movl %ebp, 0x4(%r14)
movl 0x8(%rbx), %eax
movl %eax, 0x8(%r14)
movq 0x20(%rsp), %rax
movl %eax, 0xc(%r14)
movl 0x4(%rbx), %eax
movl %eax, 0x10(%r14)
movq (%r13), %rax
movq %rax, 0x18(%r14)
movl 0x8(%r13), %eax
movl %eax, 0x20(%r14)
movq %r13, %rdi
callq 0x1f01c0
movq %r12, %rdi
callq 0x1f4da9
movq %r15, %rdi
callq 0x1f4da9
movq %rbx, %rdi
callq 0x1f4da9
jmp 0x1f796d
movq %r15, %rdi
callq 0x1f4da9
movq %rbx, %rdi
callq 0x1f4da9
xorl %r14d, %r14d
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq