// File: /workspace/github_c_makefile_star2_O3/mloar[P]charset/confuse.c
// Function: main
int main(int argc, char **argv)
{
    wchar_t *chars;
    struct enc { char string[MAXENCLEN]; int len; } *encodings;
    int nchars;
    int i, j, k, cs;
    const char *sep;

    setlocale(LC_ALL, "");

    chars = malloc(argc * sizeof(wchar_t));
    if (!chars) {
        fprintf(stderr, "out of memory\n");
        return 1;
    }

    nchars = 0;

    while (--argc) {
        char *p = *++argv;
        char *orig = p;
        char *end;
        int base = 16, semi_ok = 0;
        wchar_t ch;

        if ((p[0] == 'U' || p[0] == 'u') &&
            (p[1] == '-' || p[1] == '+')) {
            p += 2;
        } else if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
            p += 2;
        } else if (p[0] == '&' && p[1] == '#') {
            p += 2;
            if (p[0] == 'x' || p[0] == 'X')
                p++;
            else
                base = 10;
            semi_ok = 1;
        } else if (mbtowc(&ch, p, strlen(p)) == strlen(p)) {
            chars[nchars++] = ch;
            continue;
        }

        chars[nchars++] = strtoul(p, &end, base);
        if (!*end || (semi_ok && !strcmp(end, ";")))
            continue;
        else {
            fprintf(stderr, "unable to parse '%s' as a Unicode code point\n",
                    orig);
            return 1;
        }
    }

    encodings = malloc(nchars * CS_LIMIT * sizeof(struct enc));
    for (cs = 0; cs < CS_LIMIT; cs++) {
        for (i = 0; i < nchars; i++) {
            wchar_t inbuf[1];
            const wchar_t *inptr;
            int inlen, error, ret;

            if (!charset_exists(cs)) {
                encodings[i*CS_LIMIT+cs].len = 0;
                continue;
            }

            inbuf[0] = chars[i];
            inptr = inbuf;
            inlen = 1;
            error = 0;
            ret = charset_from_unicode(&inptr, &inlen,
                                       encodings[i*CS_LIMIT+cs].string,
                                       MAXENCLEN, cs, NULL, &error);
            if (error || inlen > 0)
                encodings[i*CS_LIMIT+cs].len = 0;
            else
                encodings[i*CS_LIMIT+cs].len = ret;
        }
    }

    /*
     * Really simple and slow approach to finding each distinct string
     * and outputting it.
     */
    for (i = 0; i < nchars*CS_LIMIT; i++) {
        const char *thisstr = encodings[i].string;
        int thislen = encodings[i].len;

        if (thislen == 0)
            continue;
        for (j = 0; j < i; j++)
            if (encodings[j].len == thislen &&
                !memcmp(encodings[j].string, thisstr, thislen))
                break;
        if (j < i)
            continue;        /* not the first instance of this encoding */

        /*
         * See if every character is encoded like this somewhere.
         */
        for (j = 0; j < nchars; j++) {
            for (cs = 0; cs < CS_LIMIT; cs++) {
                if (encodings[j*CS_LIMIT+cs].len == thislen &&
                    !memcmp(encodings[j*CS_LIMIT+cs].string, thisstr, thislen))
                    break;
            }
            if (cs == CS_LIMIT)
                break;                 /* this char not in any cs */
        }
        if (j < nchars)
            continue;                  /* some char not in any cs */

        /*
         * Match! Print the encoding, then all charsets.
         */
        for (j = 0; j < nchars; j++) {
            for (k = 0; k < thislen; k++)
                printf("%s%02X", k>0?" ":"", (unsigned)(thisstr[k] & 0xFF));
            printf(" = ");
            if (chars[j] >= 0x10000)
                printf("U-%08X", (unsigned)chars[j]);
            else
                printf("U+%04X", (unsigned)chars[j]);
            printf(" in:");
            sep = " ";
            for (cs = 0; cs < CS_LIMIT; cs++)
                if (encodings[j*CS_LIMIT+cs].len == thislen &&
                    !memcmp(encodings[j*CS_LIMIT+cs].string, thisstr, thislen))
                {
                    printf("%s%s", sep, charset_to_localenc(cs));
                    sep = ", ";
                }
            printf("\n");
        }
        printf("\n");
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r15
movl %edi, %ebx
leaq 0x1da20(%rip), %rsi     # 0x23c46
movl $0x6, %edi
callq 0x60b0
movslq %ebx, %rdi
shlq $0x2, %rdi
callq 0x6090
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x6686
decl %ebx
je 0x635e
movl %ebx, %r13d
xorl %ebx, %ebx
leaq 0x38(%rsp), %r12
movq 0x8(%r15,%rbx,8), %rbp
movzbl (%rbp), %eax
cmpl $0x54, %eax
jg 0x6282
cmpl $0x26, %eax
je 0x62a8
cmpl $0x30, %eax
jne 0x62cc
movzbl 0x1(%rbp), %eax
orl $0x20, %eax
cmpl $0x78, %eax
je 0x629a
jmp 0x62cc
cmpl $0x75, %eax
je 0x628c
cmpl $0x55, %eax
jne 0x62cc
movzbl 0x1(%rbp), %eax
cmpl $0x2d, %eax
je 0x629a
cmpl $0x2b, %eax
jne 0x62cc
leaq 0x2(%rbp), %rdi
movl $0x10, %edx
xorl %r14d, %r14d
jmp 0x630d
cmpb $0x23, 0x1(%rbp)
jne 0x62cc
movzbl 0x2(%rbp), %eax
movb $0x1, %r14b
orl $0x20, %eax
cmpl $0x78, %eax
jne 0x6353
leaq 0x3(%rbp), %rdi
movl $0x10, %edx
jmp 0x630d
movq %rbp, %rdi
callq 0x6040
leaq 0xc(%rsp), %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x6080
movslq %eax, %r14
movq %rbp, %rdi
callq 0x6040
cmpq %r14, %rax
jne 0x6302
movl 0xc(%rsp), %eax
movq 0x28(%rsp), %rcx
movl %eax, (%rcx,%rbx,4)
jmp 0x6345
movl $0x10, %edx
xorl %r14d, %r14d
movq %rbp, %rdi
movq %r12, %rsi
callq 0x60c0
movq 0x28(%rsp), %rcx
movl %eax, (%rcx,%rbx,4)
movq 0x38(%rsp), %rax
movb (%rax), %cl
testb %cl, %cl
je 0x6345
cmpb $0x3b, %cl
setne %cl
xorb $0x1, %r14b
orb %cl, %r14b
jne 0x668d
cmpb $0x0, 0x1(%rax)
jne 0x668d
incq %rbx
cmpl %ebx, %r13d
jne 0x625c
jmp 0x6360
leaq 0x2(%rbp), %rdi
movl $0xa, %edx
jmp 0x630d
xorl %ebx, %ebx
leal (%rbx,%rbx,8), %eax
leal (%rax,%rax,8), %eax
movq %rax, 0x20(%rsp)
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rdi
callq 0x6090
movq %rax, 0x10(%rsp)
movl %ebx, %r15d
xorl %r12d, %r12d
leaq 0x38(%rsp), %r13
leaq 0x1c(%rsp), %rbp
testq %r15, %r15
je 0x6437
leaq (%r12,%r12,2), %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax,8), %rbx
xorl %r14d, %r14d
movl %r12d, %edi
callq 0x6a19
testl %eax, %eax
je 0x6412
movq 0x28(%rsp), %rax
movl (%rax,%r14,4), %eax
movl %eax, 0xc(%rsp)
leaq 0xc(%rsp), %rax
movq %rax, 0x38(%rsp)
movl $0x1, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movl $0x14, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
callq 0x66b0
cmpl $0x0, 0x18(%rsp)
jne 0x6412
cmpl $0x0, 0x1c(%rsp)
jg 0x6412
movl %eax, 0x14(%rbx)
jmp 0x6419
movl $0x0, 0x14(%rbx)
incq %r14
addq $0x798, %rbx            # imm = 0x798
cmpq %r14, %r15
jne 0x63aa
incq %r12
cmpq $0x51, %r12
jne 0x6391
jmp 0x6449
incq %r12
cmpq $0x51, %r12
jne 0x6391
jmp 0x6675
testq %r15, %r15
je 0x6675
movl 0x20(%rsp), %eax
movq %rax, 0x70(%rsp)
movq 0x10(%rsp), %rax
xorl %ecx, %ecx
movq %rax, 0x48(%rsp)
movq %rcx, 0x50(%rsp)
leaq (%rcx,%rcx,2), %rax
movq 0x10(%rsp), %rcx
movslq 0x14(%rcx,%rax,8), %rbp
testq %rbp, %rbp
je 0x6659
movq 0x10(%rsp), %rbx
leaq (%rbx,%rax,8), %r13
movq 0x50(%rsp), %rax
movq %rax, %r14
testq %rax, %rax
je 0x64bd
cmpl %ebp, 0x14(%rbx)
jne 0x64b4
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x60a0
testl %eax, %eax
je 0x6659
addq $0x18, %rbx
decq %r14
jne 0x6499
movq 0x10(%rsp), %rbx
xorl %r14d, %r14d
xorl %r12d, %r12d
cmpl %ebp, 0x14(%rbx,%r12)
jne 0x64e2
leaq (%rbx,%r12), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x60a0
testl %eax, %eax
je 0x64f4
addq $0x18, %r12
cmpq $0x798, %r12            # imm = 0x798
jne 0x64c8
jmp 0x6659
incq %r14
addq $0x798, %rbx            # imm = 0x798
cmpq %r15, %r14
jne 0x64c5
movl %ebp, %eax
movq %rax, 0x68(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x30(%rsp)
movq %r13, 0x60(%rsp)
movq %r15, 0x58(%rsp)
testl %ebp, %ebp
leaq 0x1d716(%rip), %r13     # 0x23c46
leaq 0x4b0a(%rip), %r14      # 0xb041
movq 0x48(%rsp), %rcx
movq 0x68(%rsp), %r15
jle 0x656f
xorl %ebx, %ebx
testq %rbx, %rbx
leaq 0x4b16(%rip), %rsi      # 0xb065
cmoveq %r13, %rsi
movzbl (%rcx,%rbx), %edx
movq %r14, %rdi
xorl %eax, %eax
movq %rcx, %r12
callq 0x6050
movq %r12, %rcx
incq %rbx
cmpq %rbx, %r15
jne 0x6545
leaq 0x4ad2(%rip), %rdi      # 0xb048
xorl %eax, %eax
callq 0x6050
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rcx
movl (%rax,%rcx,4), %esi
cmpl $0x10000, %esi          # imm = 0x10000
leaq 0x4abc(%rip), %rdi      # 0xb053
leaq 0x4aae(%rip), %rax      # 0xb04c
cmovgeq %rax, %rdi
xorl %eax, %eax
callq 0x6050
leaq 0x4aaa(%rip), %rdi      # 0xb05a
xorl %eax, %eax
callq 0x6050
movq 0x40(%rsp), %rbx
xorl %r12d, %r12d
leaq 0x4a9f(%rip), %rax      # 0xb065
movq %rax, 0x20(%rsp)
leaq 0x4a8d(%rip), %r14      # 0xb05f
movq 0x60(%rsp), %r13
movq 0x58(%rsp), %r15
cmpl %ebp, 0x14(%rbx)
jne 0x6619
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x60a0
testl %eax, %eax
jne 0x6619
movl %r12d, %edi
callq 0x67e4
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x6050
leaq 0x4a50(%rip), %rax      # 0xb064
movq %rax, 0x20(%rsp)
incq %r12
addq $0x18, %rbx
cmpq $0x51, %r12
jne 0x65dc
movl $0xa, %edi
callq 0x6030
movq 0x30(%rsp), %rcx
incq %rcx
addq $0x798, 0x40(%rsp)      # imm = 0x798
movq %rcx, 0x30(%rsp)
cmpq %r15, %rcx
jne 0x6527
movl $0xa, %edi
callq 0x6030
movq 0x50(%rsp), %rcx
incq %rcx
movq 0x48(%rsp), %rax
addq $0x18, %rax
cmpq 0x70(%rsp), %rcx
jne 0x6462
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x6100
jmp 0x66a8
movq 0xa2964(%rip), %rax     # 0xa8ff8
movq (%rax), %rdi
leaq 0x4975(%rip), %rsi      # 0xb013
movq %rbp, %rdx
xorl %eax, %eax
callq 0x6070
movl $0x1, %eax
jmp 0x6677
nop