// File: /workspace/github_cpp_makefile_star2_O2/dm4[P]huffman-homework/my_decode.cc
// Function: main
int main(int argc, const char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage:\n");
        fprintf(stderr, "    %s compressed.bin output.txt\n", argv[0]);
        exit(1);
    }

    // Read input
    printf("Open input file: %s\n", argv[1]);
    FILE *fp = fopen(argv[1], "rb");
    if (fp == NULL) {
        fprintf(stderr, "Invalid file: %s\n", argv[1]);
        exit(1);
    }

    // Read symbols
    printf("Read symbol-codeword table\n");
    char n;
    fread(&n, sizeof(char), 1, fp);
    for (int i = 0; i < n; i++) {
        Symbol *s = (Symbol *)malloc(sizeof(Symbol));
        fread(s, sizeof(Symbol), 1, fp);
        symbols.push_back(s);
    }

    // Builde LUT
    printf("Construct LUT\n");
    map<int, int> buildLUT;
    int height = 0;
    int previousBuildLUT = -1;
    for (int i = 0; i < symbols.size(); i++) {
        Symbol *p = symbols[i];
        if (p->cl > height) {
            height += 4;
        }

        // build new lookup
        int nowBuildLUT = p->codeword >> (p->cl + 4 - height);
        if (nowBuildLUT != previousBuildLUT) {
            Lookup *ptr = (Lookup *)malloc(sizeof(Lookup));
            LUT.push_back(ptr);
            buildLUT[nowBuildLUT] = LUT.size() - 1;
            previousBuildLUT = nowBuildLUT;

            // set link
            if (height >= 8) {
                int previousLUT = buildLUT[p->codeword >> (p->cl + 8 - height)];
                Lookup *previous = LUT[previousLUT];
                unsigned int index = p->codeword >> (4 + p->cl - height);
                index &= 15;
                previous->isSymbol[index] = false;
                previous->next[index] = LUT.size() - 1;
            }
        }

        // set lookup entries
        int depth = height - p->cl;
        int subNum  = 1 << depth;
        // 1111 1111 10, base is 8
        unsigned int base = p->codeword << (height - p->cl);
        base &= 15;
        for (int j = 0; j < subNum; j++) {
            LUT[LUT.size() - 1]->isSymbol[base + j] = true;
            LUT[LUT.size() - 1]->next[base + j] = i;
        }
    }

    // print
    if (DEBUG) {
        for (int i = 0; i < symbols.size(); i++) {
            printf("symbol %d\tcl %d\t", symbols[i]->symbol, symbols[i]->cl);
            p(symbols[i]->codeword, symbols[i]->cl);
        }
    }

    // print LUT
    if (DEBUG) {
        for (int i = 0; i < LUT.size(); i++) {
            fprintf(stderr, "LUT[%d]\n", i);
            for (int j = 0; j < 16; j++) {
                Lookup *ptr = LUT[i];
                if (ptr->isSymbol[j]) {
                    fprintf(stderr, "[%2d] %d ", j, symbols[ptr->next[j]]->symbol);
                    p(symbols[ptr->next[j]]->codeword, symbols[ptr->next[j]]->cl);
                }
                else {
                    fprintf(stderr, "[%2d] LUT[%d]\n", j, ptr->next[j]);
                }
            }
        }
    }

    // open output file
    printf("Open output file: %s\n", argv[2]);
    FILE *fout = fopen(argv[2], "wb");
    if (fout == NULL) {
        fprintf(stderr, "Couldn't open file: %s\n", argv[2]);
        exit(1);
    }

    unsigned char buf, buf2;
    int bufLen, buf2Len;
    int nowLUT = 0;
    int nowCL  = 0;
    fread(&buf, sizeof(char), 1, fp);
    buf2    = 0;
    bufLen  = 8;
    buf2Len = 0;
    while(!feof(fp)) {
        // read to buffer
        while (bufLen < 4) {
            if (bufLen + buf2Len <= 8) {
                buf |= (buf2 >> bufLen);
                bufLen  = bufLen + buf2Len;
                fread(&buf2, sizeof(char), 1, fp);
                buf2Len = 8;
            }
            else {
                buf |= (buf2 >> bufLen);
                buf2 <<= (8 - bufLen);
                buf2Len = bufLen + buf2Len - 8;
                bufLen  = 8;
            }
        }

        //
        char index = (buf >> 4) & 15;
        Lookup *table = LUT[nowLUT];
        if (table->isSymbol[index]) {
            Symbol *s = symbols[table->next[index]];
            if (s->symbol == 0) {
                printf("Done!\n");
                break;
            }
            fprintf(fout, "%c", s->symbol);
            int shift = s->cl - nowCL;
            buf <<= shift;
            bufLen -= shift;
            nowCL  = 0;
            nowLUT = 0;
        }
        else {
            nowLUT = table->next[index];
            nowCL += 4;
            buf <<= 4;
            bufLen -= 4;
        }
    }

    // close file
    fclose(fp);
    fclose(fout);

    // free memory
    for (int i = 0; i < symbols.size(); i++) {
        free(symbols[i]);
    }
    for (int i = 0; i < LUT.size(); i++) {
        free(LUT[i]);
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r14
cmpl $0x3, %edi
jne 0x17cf
movq 0x8(%r14), %rsi
leaq 0x1c74(%rip), %rdi      # 0x304d
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1030
movq %r14, -0x38(%rbp)
movq 0x8(%r14), %rdi
leaq 0x1c71(%rip), %rsi      # 0x3062
callq 0x1100
testq %rax, %rax
je 0x17fa
movq %rax, %r12
leaq 0x1cb9(%rip), %rdi      # 0x30c2
callq 0x1190
leaq -0x2d(%rbp), %r15
pushq $0x1
popq %r14
movq %r15, %rdi
movq %r14, %rsi
movq %r14, %rdx
movq %r12, -0x40(%rbp)
movq %r12, %rcx
callq 0x1180
movzbl (%r15), %eax
testb %al, %al
cmovgl %eax, %ebx
movzbl %bl, %ebx
pushq $0x8
popq %r15
leaq 0x3cd6(%rip), %r12      # 0x5118
leaq -0x88(%rbp), %r13
subl $0x1, %ebx
jb 0x147c
movq %r15, %rdi
callq 0x1140
movq %rax, -0x88(%rbp)
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq -0x40(%rbp), %rcx
callq 0x1180
movq %r12, %rdi
movq %r13, %rsi
callq 0x1866
jmp 0x1449
leaq 0x1c5a(%rip), %rdi      # 0x30dd
callq 0x1190
leaq -0x80(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
xorl %r12d, %r12d
pushq $-0x1
popq %r14
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0x3c6b(%rip), %rcx      # 0x5120
movq 0x3c5c(%rip), %rax      # 0x5118
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
jbe 0x15f1
movq (%rax,%r13,8), %rbx
movsbl 0x4(%rbx), %eax
leal 0x4(%r15), %ecx
cmpl %eax, %r15d
cmovll %ecx, %r15d
movl (%rbx), %edx
movl %eax, %ecx
subl %r15d, %ecx
addb $0x4, %cl
shrl %cl, %edx
movl %edx, -0x2c(%rbp)
cmpl %r14d, %edx
je 0x15a8
pushq $0x50
popq %rdi
callq 0x1140
movq %rax, -0x58(%rbp)
leaq 0x3c26(%rip), %rdi      # 0x5130
leaq -0x58(%rbp), %rsi
callq 0x188a
movq 0x3c1e(%rip), %r14      # 0x5138
movq 0x3c0f(%rip), %r12      # 0x5130
leaq -0x88(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x18ae
subq %r12, %r14
shrq $0x3, %r14
decl %r14d
movl %r14d, (%rax)
movl -0x2c(%rbp), %r14d
cmpl $0x8, %r15d
movl $0x0, %r12d
jl 0x15a4
movl (%rbx), %eax
movl 0x4(%rbx), %ecx
subl %r15d, %ecx
addb $0x8, %cl
shrl %cl, %eax
movl %eax, -0x44(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x44(%rbp), %rsi
callq 0x1900
movslq (%rax), %rax
movq 0x3bb8(%rip), %rdx      # 0x5130
movl (%rbx), %esi
movl 0x4(%rbx), %ecx
subl %r15d, %ecx
addb $0x4, %cl
shrl %cl, %esi
movq (%rdx,%rax,8), %rax
andl $0xf, %esi
movb $0x0, 0x40(%rax,%rsi)
movq 0x3ba0(%rip), %rcx      # 0x5138
subq %rdx, %rcx
shrq $0x3, %rcx
decl %ecx
movl %ecx, (%rax,%rsi,4)
movsbl 0x4(%rbx), %eax
movl %r15d, %ecx
subl %eax, %ecx
pushq $0x1
popq %rax
shll %cl, %eax
movl (%rbx), %edx
shll %cl, %edx
andl $0xf, %edx
movq 0x3b78(%rip), %rcx      # 0x5138
movq 0x3b69(%rip), %rsi      # 0x5130
subq %rsi, %rcx
testl %eax, %eax
cmovlel %r12d, %eax
subq $0x1, %rax
jb 0x15e9
movq -0x8(%rsi,%rcx), %rdi
movb $0x1, 0x40(%rdi,%rdx)
movl %r13d, (%rdi,%rdx,4)
incq %rdx
jmp 0x15d0
incq %r13
jmp 0x14ae
movq -0x38(%rbp), %rbx
movq 0x10(%rbx), %rsi
leaq 0x1a77(%rip), %rdi      # 0x3077
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x1030
movq 0x10(%rbx), %rdi
leaq 0x1a78(%rip), %rsi      # 0x308d
callq 0x1100
movq %rax, -0x50(%rbp)
testq %rax, %rax
je 0x1815
leaq -0x58(%rbp), %r14
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %rdx, %rsi
movq -0x40(%rbp), %r15
movq %r15, %rcx
callq 0x1180
leaq -0x2c(%rbp), %rax
movb $0x0, (%rax)
pushq $0x8
popq %rax
movb (%r14), %r12b
movl %eax, %r14d
xorl %ebx, %ebx
movl $0x0, -0x38(%rbp)
movq %r15, %rdi
callq 0x11a0
testl %eax, %eax
jne 0x174d
movl %ebx, %edx
cmpl $0x3, %r14d
jg 0x16c1
addl %r14d, %r13d
movzbl -0x2c(%rbp), %r15d
cmpl $0x8, %r13d
jg 0x1699
leaq -0x2c(%rbp), %rdi
pushq $0x1
popq %rdx
movq %rdx, %rsi
movq -0x40(%rbp), %rcx
callq 0x1180
movl %ebx, %edx
pushq $0x8
popq %rax
jmp 0x16b0
movb $0x8, %cl
subb %r14b, %cl
movl %r15d, %eax
shll %cl, %eax
movb %al, -0x2c(%rbp)
movl %r13d, %eax
addl $-0x8, %eax
pushq $0x8
popq %r13
movl %r14d, %ecx
shrl %cl, %r15d
orb %r15b, %r12b
movl %r13d, %r14d
movl %eax, %r13d
jmp 0x166b
movzbl %r12b, %r15d
movl %r15d, %eax
shrl $0x4, %eax
movslq %edx, %rcx
movq 0x3a5b(%rip), %rdx      # 0x5130
movq (%rdx,%rcx,8), %rcx
movl (%rcx,%rax,4), %ebx
cmpb $0x1, 0x40(%rcx,%rax)
jne 0x1728
movslq %ebx, %rax
movq 0x3a2b(%rip), %rcx      # 0x5118
movq (%rcx,%rax,8), %r12
movsbl 0x5(%r12), %edi
testl %edi, %edi
je 0x173d
movq -0x50(%rbp), %rsi
callq 0x1130
movsbl 0x4(%r12), %ecx
subl -0x38(%rbp), %ecx
shll %cl, %r15d
subl %ecx, %r14d
movl %r15d, %r12d
xorl %ebx, %ebx
movl $0x0, -0x38(%rbp)
movq -0x40(%rbp), %r15
jmp 0x1659
addl $0x4, -0x38(%rbp)
shlb $0x4, %r12b
addl $-0x4, %r14d
movq -0x40(%rbp), %r15
jmp 0x1659
leaq 0x19a7(%rip), %rdi      # 0x30eb
callq 0x1190
movq -0x40(%rbp), %r15
movq %r15, %rdi
callq 0x10c0
movq -0x50(%rbp), %rdi
callq 0x10c0
xorl %ebx, %ebx
movq 0x39b9(%rip), %rcx      # 0x5120
movq 0x39aa(%rip), %rax      # 0x5118
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbx, %rcx
jbe 0x1788
movq (%rax,%rbx,8), %rdi
callq 0x1110
incq %rbx
jmp 0x1760
xorl %ebx, %ebx
movq 0x39a7(%rip), %rcx      # 0x5138
movq 0x3998(%rip), %rax      # 0x5130
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbx, %rcx
jbe 0x17b2
movq (%rax,%rbx,8), %rdi
callq 0x1110
incq %rbx
jmp 0x178a
leaq -0x88(%rbp), %rdi
callq 0x1960
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x380a(%rip), %rbx      # 0x4fe0
movq (%rbx), %rcx
leaq 0x1843(%rip), %rdi      # 0x3023
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
callq 0x11e0
movq (%rbx), %rdi
movq (%r14), %rdx
leaq 0x1833(%rip), %rsi      # 0x302b
jmp 0x182a
movq 0x37df(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x1852(%rip), %rsi      # 0x3065
jmp 0x182a
movq 0x37c4(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
movq 0x10(%rbx), %rdx
leaq 0x1866(%rip), %rsi      # 0x3090
xorl %eax, %eax
callq 0x1160
pushq $0x1
popq %rdi
callq 0x1120
jmp 0x183b
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x1960
movq %rbx, %rdi
callq 0x11d0