// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movq %r8, %r14
movl %ecx, %r12d
movq %rdx, -0x88(%rbp)
movq %rsi, %r15
movl %edi, %r13d
movslq %edi, %rax
movq %rax, -0x38(%rbp)
imulq $0x38, %rax, %rdi
callq 0x8090
testq %rax, %rax
je 0xab63
movl %r12d, -0x4c(%rbp)
movq %r14, -0x60(%rbp)
movq %rbx, -0x58(%rbp)
movq %rax, %rbx
xorl %r12d, %r12d
testl %r13d, %r13d
movl $0x0, %r14d
movl %r13d, -0x48(%rbp)
cmovgl %r13d, %r14d
movq %rax, -0x40(%rbp)
cmpq %r12, %r14
je 0xab7c
movq (%r15,%r12,8), %rdi
movq %rbx, %rsi
callq 0xa81b
incq %r12
addq $0x38, %rbx
testl %eax, %eax
je 0xab3b
xorl %r12d, %r12d
pushq $-0x2
popq %r13
jmp 0xabff
pushq $-0x1
popq %r13
movl %r13d, %eax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x40(%rbp), %r11
movzwl (%r11), %r15d
movzwl 0x2(%r11), %ebx
movzwl 0x6(%r11), %eax
movzwl 0x8(%r11), %ecx
movzwl 0x14(%r11), %r12d
leaq 0x38(%r11), %rsi
xorl %edx, %edx
pushq $-0x3
popq %r13
movq -0x38(%rbp), %r9
incq %rdx
cmpq %r9, %rdx
jge 0xabd6
cmpw (%rsi), %r15w
jne 0xabfc
cmpw 0x2(%rsi), %bx
jne 0xabfc
cmpw 0x6(%rsi), %ax
jne 0xabfc
cmpw 0x8(%rsi), %cx
jne 0xabfc
leaq 0x38(%rsi), %rdi
cmpw 0x14(%rsi), %r12w
movq %rdi, %rsi
je 0xaba6
jmp 0xabfc
cmpw $0x10, %r12w
setae %dl
testb $0x1, %r12b
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0xabf8
cmpw %cx, %ax
jbe 0xac2a
pushq $-0x5
jmp 0xabfa
pushq $-0x4
popq %r13
xorl %r12d, %r12d
movq -0x40(%rbp), %rdi
callq 0x8030
movzwl %r12w, %ebx
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0xab67
movq -0x150(%rbp,%r14,8), %rdi
callq 0x8030
incq %r14
jmp 0xac0f
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
imulq $0x38, %r14, %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0xac4c
movzwl 0x4(%r11,%rdx), %esi
incb -0x70(%rbp,%rsi)
addq $0x38, %rdx
jmp 0xac37
xorl %ecx, %ecx
xorl %edx, %edx
movl -0x48(%rbp), %esi
cmpq $0x10, %rcx
je 0xac67
cmpb $0x1, -0x70(%rbp,%rcx)
sbbw $-0x1, %dx
incq %rcx
jmp 0xac53
cmpw %ax, %dx
jae 0xac70
pushq $-0x6
jmp 0xabfa
movl %esi, %eax
imulq $0x38, %rax, %rcx
xorl %edx, %edx
pushq $-0x7
popq %r13
xorl %esi, %esi
cmpq %r14, %rsi
je 0xacc6
imulq $0x38, %rsi, %r8
leaq (%r11,%r8), %rdi
movzwl 0x4(%r11,%r8), %r8d
xorl %r9d, %r9d
cmpq %r9, %rcx
je 0xacbd
cmpq %r9, %rdx
je 0xacb7
cmpw 0x4(%r11,%r9), %r8w
jne 0xacb7
movzwl 0xc(%rdi), %r10d
cmpw 0xc(%r11,%r9), %r10w
jne 0xabfc
addq $0x38, %r9
jmp 0xac94
incq %rsi
addq $0x38, %rdx
jmp 0xac7e
movq %r12, -0x30(%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, %rax, %r9
xorl %r10d, %r10d
leaq -0xb0(%rbp), %rdx
xorl %r12d, %r12d
cmpq $0x10, %r10
je 0xae55
cmpb $0x0, -0x70(%rbp,%r10)
je 0xae4d
xorl %eax, %eax
xorl %ecx, %ecx
xorl %r13d, %r13d
movq $0x0, -0x38(%rbp)
cmpq %r14, %rcx
je 0xadd2
imulq $0x38, %rcx, %r11
movq -0x40(%rbp), %rdi
movzwl 0x4(%rdi,%r11), %esi
cmpq %rsi, %r10
jne 0xadc6
addq %rdi, %r11
xorl %esi, %esi
pushq $-0x8
popq %r13
cmpq %rsi, %r9
je 0xad9a
cmpq %rsi, %rax
je 0xad94
movq -0x40(%rbp), %rdi
movzwl 0x4(%rdi,%rsi), %edi
cmpq %rdi, %r10
jne 0xad94
movzwl 0xa(%r11), %edi
movq -0x40(%rbp), %r8
cmpw 0xa(%r8,%rsi), %di
je 0xabff
addq $0x38, %rsi
jmp 0xad67
movzwl 0xc(%r11), %r13d
leaq 0x16(%r11), %rsi
movq -0x38(%rbp), %r8
movzwl %r8w, %edi
movq %rsi, -0x1d0(%rbp,%rdi,8)
movb 0xa(%r11), %sil
movb %sil, -0xb0(%rbp,%rdi)
incl %r8d
movq %r8, -0x38(%rbp)
incq %rcx
addq $0x38, %rax
jmp 0xad42
movq %r10, -0x80(%rbp)
movq %r9, -0x48(%rbp)
movzbl %r13b, %edi
movq -0x30(%rbp), %rsi
leaq -0x1d0(%rbp), %rcx
leaq -0xd0(%rbp), %r8
callq 0xaf01
testl %eax, %eax
jne 0xaea1
pushq $0x20
popq %rdi
callq 0x8090
movzwl %r12w, %ecx
movq %rcx, -0x78(%rbp)
movq %rax, -0x150(%rbp,%rcx,8)
movq %rax, %rdi
leaq -0xd0(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x8080
movq -0x80(%rbp), %r10
movq -0x78(%rbp), %rax
movb %r10b, -0xa0(%rbp,%rax)
incl %r12d
cmpw %r13w, -0x38(%rbp)
movq -0x48(%rbp), %r9
leaq -0xb0(%rbp), %rdx
jb 0xaea8
incq %r10
jmp 0xad1d
pushq $-0xb
popq %r13
cmpq $0x0, -0x60(%rbp)
je 0xabff
movq -0x58(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpl %ecx, (%rax)
jl 0xabff
movzbl %r12b, %edi
leaq -0xa0(%rbp), %rdx
leaq -0x150(%rbp), %rcx
leaq -0xd0(%rbp), %r8
movq -0x30(%rbp), %rsi
callq 0xaf01
testl %eax, %eax
je 0xaeaf
pushq $-0xc
jmp 0xab5c
pushq $-0x9
jmp 0xab5c
pushq $-0xa
jmp 0xab5c
movzwl %r15w, %eax
movzbl %bl, %r10d
leaq -0xd0(%rbp), %rbx
movq %rbx, %rdi
movq -0x30(%rbp), %r14
movl %r14d, %esi
movq %rbx, %rdx
movl %r14d, %ecx
movq -0x88(%rbp), %r8
movl -0x4c(%rbp), %r9d
pushq %rax
pushq %r10
callq 0xaffb
popq %rax
popq %rcx
movq -0x60(%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x8080
movq -0x58(%rbp), %rax
movl %r14d, (%rax)
xorl %r13d, %r13d
jmp 0xabff