// File: /workspace/github_c_makefile_star2_O1/Seclabs-htc[P]slip39/slip39.c
// Function: combine_mnemonics
int combine_mnemonics(int gn, char *ml[], uint8_t *pp, int ppl, uint8_t *ms, int *msl)
{
    int i, j, k, r, ret = 0;
    group_shares *gs;
    uint16_t id, ie, gt, gc, gi, sl;
    uint8_t gm[16]; // group index member
    uint16_t gmc, gmt, mic, gmic = 0;
    uint8_t *m_share[16];
    uint8_t m_share_index[16];
    uint8_t *g_share[16];
    uint8_t g_share_index[16];
    uint8_t gsv[MAX_SAHRE_VALUE_LEN];

    gs = malloc(sizeof(group_shares) * gn);
    if (gs == NULL)
        return -1;

    for (i = 0; i < gn; i++)
    {
        r = decode_mnemonic(ml[i], &gs[i]);
        if (r != 0)
        {
            ret = -2;
            goto exit;
        }
    }

    id = gs[0].id;
    ie = gs[0].ie;
    gt = gs[0].gt;
    gc = gs[0].gc;
    sl = gs[0].sl;
    for (i = 1; i < gn; i++)
    {
        if ((id != gs[i].id) || (ie != gs[i].ie) ||
            (gt != gs[i].gt) || (gc != gs[i].gc) ||
            (sl != gs[i].sl))
        {
            ret = -3;
            goto exit;
        }
    }

    if ((sl < 16) || (sl & 1))
    {
        ret = -4;
        goto exit;
    }
    if (gt > gc)
    {
        ret = -5;
        goto exit;
    }

    memset(gm, 0, sizeof(gm));
    for (i = 0; i < gn; i++)
        gm[gs[i].gi]++;

    gmc = 0;
    for (i = 0; i < 16; i++)
        if (gm[i] != 0)
            gmc++;
    if (gmc < gt)
    {
        ret = -6;
        goto exit;
    }

    for (i = 0; i < gn; i++)
    {
        gi = gs[i].gi;
        for (j = 0; j < gn; j++)
        {
            if ((i != j) && (gi == gs[j].gi))
            {
                if (gs[i].mt != gs[j].mt)
                {
                    ret = -7;
                    goto exit;
                }
            }
        }
    }

    memset(g_share, 0, sizeof(g_share));
    memset(g_share_index, 0, sizeof(g_share_index));

    for (i = 0; i < 16; i++)
    {
        if (gm[i] != 0)
        {
            mic = 0; gmt = 0;
            for (j = 0; j < gn; j++)
            {
                if (i == gs[j].gi)
                {
                    for (k = 0; k < gn; k++)
                    {
                        if ((j != k) && (i == gs[k].gi) && (gs[j].mi == gs[k].mi))
                        {
                            ret = -8;
                            goto exit;
                        }
                    }
                    gmt = gs[j].mt;
                    m_share[mic] = gs[j].s;
                    m_share_index[mic] = gs[j].mi;
                    mic++;
                }
            }
            r = _recover_secret(gmt, sl, m_share_index, (const uint8_t **)m_share, gsv);
            if (r == 0)
            {
                g_share[gmic] = (uint8_t *)malloc(MAX_SAHRE_VALUE_LEN);
                memcpy(g_share[gmic], gsv, sl);

                g_share_index[gmic] = i;
                gmic++;
            }
            else
            {
                ret = -9;
                goto exit;
            }
            if (mic < gmt)
            {
                ret = -10;
                goto exit;
            }
        }
    }

    if ((ms == NULL) || (*msl < sl))
    {
        ret = -11;
        goto exit;
    }

    r = _recover_secret(gmic, sl, g_share_index, (const uint8_t **)g_share, gsv);
    if (r == 0)
    {
        _decrypt(gsv, sl, gsv, sl, pp, ppl, ie, id);

        memcpy(ms, gsv, sl);
        *msl = sl;
    }
    else
    {
        ret = -12;
    }

exit:

    if (gs != NULL)
        free(gs);

    for (i = 0; i < gmic; i++)
        if (g_share[i] != NULL)
            free(g_share[i]);

    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq %r9, %r15
movq %r8, %r12
movl %ecx, %r13d
movq %rdx, -0x88(%rbp)
movq %rsi, %r14
movl %edi, %ebx
movslq %edi, %rax
imulq $0x38, %rax, %rdi
callq 0x8090
testq %rax, %rax
je 0xaf05
movl %r13d, -0x44(%rbp)
movq %r12, -0x58(%rbp)
movq %r15, -0x50(%rbp)
movl %ebx, -0x2c(%rbp)
testl %ebx, %ebx
movq %rax, %rbx
jle 0xae7f
movl -0x2c(%rbp), %r13d
xorl %r15d, %r15d
movq %rbx, %r12
movq (%r14,%r15,8), %rdi
movq %r12, %rsi
callq 0xaadc
testl %eax, %eax
jne 0xaf0d
incq %r15
addq $0x38, %r12
cmpq %r15, %r13
jne 0xae5f
movzwl (%rbx), %r14d
movzwl 0x2(%rbx), %r11d
movzwl 0x6(%rbx), %eax
movzwl 0x8(%rbx), %ecx
movzwl 0x14(%rbx), %r15d
movl -0x2c(%rbp), %r10d
cmpl $0x2, %r10d
jl 0xaedf
movl %r10d, %edx
imulq $0x38, %rdx, %rdx
movl $0x38, %esi
movl $0xfffffffd, %r12d      # imm = 0xFFFFFFFD
cmpw (%rbx,%rsi), %r14w
jne 0xaf1b
cmpw 0x2(%rbx,%rsi), %r11w
jne 0xaf1b
cmpw 0x6(%rbx,%rsi), %ax
jne 0xaf1b
cmpw 0x8(%rbx,%rsi), %cx
jne 0xaf1b
cmpw 0x14(%rbx,%rsi), %r15w
jne 0xaf1b
addq $0x38, %rsi
cmpq %rsi, %rdx
jne 0xaeb1
cmpw $0x10, %r15w
setae %dl
testb $0x1, %r15b
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0xaf15
cmpw %cx, %ax
jbe 0xaf62
movl $0xfffffffb, %r12d      # imm = 0xFFFFFFFB
jmp 0xaf1b
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0xaf4d
movl $0xfffffffe, %r12d      # imm = 0xFFFFFFFE
jmp 0xaf1b
movl $0xfffffffc, %r12d      # imm = 0xFFFFFFFC
xorl %r13d, %r13d
movq %rbx, %rdi
callq 0x8030
testw %r13w, %r13w
je 0xaf4d
movzwl %r13w, %ebx
xorl %r14d, %r14d
movq -0x150(%rbp,%r14,8), %rdi
testq %rdi, %rdi
je 0xaf45
callq 0x8030
incq %r14
cmpq %r14, %rbx
jne 0xaf33
movl %r12d, %eax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movl %r10d, %r8d
testl %r10d, %r10d
jle 0xaf89
imulq $0x38, %r8, %rcx
xorl %edx, %edx
movzwl 0x4(%rbx,%rdx), %esi
incb -0x70(%rbp,%rsi)
addq $0x38, %rdx
cmpq %rdx, %rcx
jne 0xaf77
xorl %ecx, %ecx
xorl %edx, %edx
cmpb $0x1, -0x70(%rbp,%rcx)
sbbw $-0x1, %dx
incq %rcx
cmpq $0x10, %rcx
jne 0xaf8d
cmpw %ax, %dx
jae 0xafaf
movl $0xfffffffa, %r12d      # imm = 0xFFFFFFFA
jmp 0xaf1b
testl %r10d, %r10d
jle 0xb005
imulq $0x38, %r8, %rax
xorl %ecx, %ecx
xorl %edx, %edx
movq %r8, %r12
imulq $0x38, %rdx, %rdi
leaq (%rbx,%rdi), %rsi
movzwl 0x4(%rbx,%rdi), %edi
xorl %r8d, %r8d
cmpq %r8, %rcx
je 0xafed
cmpw 0x4(%rbx,%r8), %di
jne 0xafed
movzwl 0xc(%rsi), %r9d
cmpw 0xc(%rbx,%r8), %r9w
jne 0xb232
addq $0x38, %r8
cmpq %r8, %rax
jne 0xafcf
incq %rdx
addq $0x38, %rcx
movq %r12, %r8
cmpq %r12, %rdx
jne 0xafbc
movw %r14w, -0x2e(%rbp)
movl %r11d, -0x3c(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0x100(%rbp)
movaps %xmm0, -0x110(%rbp)
movaps %xmm0, -0x120(%rbp)
movaps %xmm0, -0x130(%rbp)
movaps %xmm0, -0x140(%rbp)
movaps %xmm0, -0x150(%rbp)
movaps %xmm0, -0xa0(%rbp)
imulq $0x38, %r8, %r12
xorl %r14d, %r14d
leaq -0xb0(%rbp), %rdx
xorl %r13d, %r13d
cmpb $0x0, -0x70(%rbp,%r14)
je 0xb184
testl %r10d, %r10d
jle 0xb0fb
xorl %eax, %eax
xorl %r10d, %r10d
movq $0x0, -0x38(%rbp)
xorl %ecx, %ecx
imulq $0x38, %r10, %r9
movzwl 0x4(%rbx,%r9), %esi
cmpq %rsi, %r14
jne 0xb0ed
addq %rbx, %r9
xorl %esi, %esi
cmpq %rsi, %rax
je 0xb0b8
movzwl 0x4(%rbx,%rsi), %edi
cmpq %rdi, %r14
jne 0xb0b8
movzwl 0xa(%r9), %edi
cmpw 0xa(%rbx,%rsi), %di
je 0xb23d
addq $0x38, %rsi
cmpq %rsi, %r12
jne 0xb099
movzwl 0xc(%r9), %ecx
leaq 0x16(%r9), %rsi
movq -0x38(%rbp), %r11
movzwl %r11w, %edi
movq %rsi, -0x1d0(%rbp,%rdi,8)
movb 0xa(%r9), %sil
movb %sil, -0xb0(%rbp,%rdi)
incl %r11d
movq %r11, -0x38(%rbp)
incq %r10
addq $0x38, %rax
cmpq %r8, %r10
jne 0xb085
jmp 0xb105
xorl %ecx, %ecx
movq $0x0, -0x38(%rbp)
movl %ecx, -0x40(%rbp)
movq %r8, -0x80(%rbp)
movzbl %cl, %edi
movl %r15d, %esi
leaq -0x1d0(%rbp), %rcx
leaq -0xd0(%rbp), %r8
callq 0xb25e
testl %eax, %eax
jne 0xb248
movl $0x20, %edi
callq 0x8090
movq %r15, %rdx
movzwl %r13w, %ecx
movq %rcx, -0x78(%rbp)
movq %rax, -0x150(%rbp,%rcx,8)
movq %rax, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x8080
movq -0x78(%rbp), %rax
movb %r14b, -0xa0(%rbp,%rax)
incl %r13d
movl -0x40(%rbp), %eax
cmpw %ax, -0x38(%rbp)
jb 0xb253
movl -0x2c(%rbp), %r10d
movq -0x80(%rbp), %r8
leaq -0xb0(%rbp), %rdx
incq %r14
cmpq $0x10, %r14
jne 0xb061
movl $0xfffffff5, %r12d      # imm = 0xFFFFFFF5
cmpq $0x0, -0x58(%rbp)
je 0xaf1e
movq -0x50(%rbp), %rax
cmpl %r15d, (%rax)
jl 0xaf1e
movzbl %r13b, %edi
leaq -0xa0(%rbp), %rdx
leaq -0x150(%rbp), %rcx
leaq -0xd0(%rbp), %r8
movl %r15d, %esi
callq 0xb25e
movl $0xfffffff4, %r12d      # imm = 0xFFFFFFF4
testl %eax, %eax
jne 0xaf1e
movzwl -0x2e(%rbp), %eax
movzbl -0x3c(%rbp), %r10d
movq %r15, %r14
leaq -0xd0(%rbp), %r15
movq %r15, %rdi
movl %r14d, %esi
movq %r15, %rdx
movl %r14d, %ecx
movq -0x88(%rbp), %r8
movl -0x44(%rbp), %r9d
pushq %rax
pushq %r10
callq 0xb360
addq $0x10, %rsp
movq -0x58(%rbp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x8080
movq -0x50(%rbp), %rax
movl %r14d, (%rax)
xorl %r12d, %r12d
jmp 0xaf1e
movl $0xfffffff9, %r12d      # imm = 0xFFFFFFF9
jmp 0xaf1b
movl $0xfffffff8, %r12d      # imm = 0xFFFFFFF8
jmp 0xaf1e
movl $0xfffffff7, %r12d      # imm = 0xFFFFFFF7
jmp 0xaf1e
movl $0xfffffff6, %r12d      # imm = 0xFFFFFFF6
jmp 0xaf1e