// File: /workspace/github_cpp_makefile_star2_O3/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 0x1933
movq 0x8(%r14), %rsi
leaq 0x1bb6(%rip), %rdi      # 0x304d
xorl %eax, %eax
callq 0x1030
movq 0x8(%r14), %rdi
leaq 0x1bb9(%rip), %rsi      # 0x3062
callq 0x1100
testq %rax, %rax
je 0x193b
movq %rax, %rbx
movq %r14, -0x40(%rbp)
leaq 0x1bfd(%rip), %rdi      # 0x30c2
callq 0x1190
leaq -0x31(%rbp), %r15
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x1180
movsbl (%r15), %r14d
testl %r14d, %r14d
jle 0x1554
leaq 0x3c25(%rip), %r15      # 0x5118
leaq -0x90(%rbp), %r12
movl $0x8, %edi
callq 0x1140
movq %rax, -0x90(%rbp)
movl $0x8, %esi
movl $0x1, %edx
movq %rax, %rdi
movq %rbx, %rcx
callq 0x1180
movq 0x3bf9(%rip), %rsi      # 0x5120
cmpq 0x3bfa(%rip), %rsi      # 0x5128
je 0x1544
movq -0x90(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x3bde(%rip)      # 0x5120
jmp 0x154f
movq %r15, %rdi
movq %r12, %rdx
callq 0x1ae4
decl %r14d
jne 0x14fa
movq %rbx, -0x48(%rbp)
leaq 0x1b7e(%rip), %rdi      # 0x30dd
callq 0x1190
leaq -0x88(%rbp), %rax
movl $0x0, (%rax)
xorl %r12d, %r12d
movq %r12, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %r12, 0x20(%rax)
movq 0x3b8d(%rip), %rax      # 0x5118
cmpq %rax, 0x3b8e(%rip)      # 0x5120
je 0x170e
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movl %r12d, %ecx
movq (%rax,%r14,8), %r13
movsbl 0x4(%r13), %eax
leal 0x4(%rcx), %r12d
cmpl %eax, %ecx
cmovgel %ecx, %r12d
movl (%r13), %edx
movl %eax, %ecx
subl %r12d, %ecx
addb $0x4, %cl
shrl %cl, %edx
movl %edx, -0x2c(%rbp)
cmpl %r15d, %edx
je 0x16aa
movl $0x50, %edi
callq 0x1140
movq %rax, -0x60(%rbp)
movq 0x3b52(%rip), %rsi      # 0x5138
cmpq 0x3b53(%rip), %rsi      # 0x5140
je 0x1606
movq %rax, (%rsi)
movq 0x3b3f(%rip), %rbx      # 0x5138
addq $0x8, %rbx
movq %rbx, 0x3b34(%rip)      # 0x5138
jmp 0x161d
leaq 0x3b23(%rip), %rdi      # 0x5130
leaq -0x60(%rbp), %rdx
callq 0x1c24
movq 0x3b1b(%rip), %rbx      # 0x5138
movq 0x3b0c(%rip), %r15      # 0x5130
leaq -0x90(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x19a8
subq %r15, %rbx
shrq $0x3, %rbx
decl %ebx
movl %ebx, (%rax)
movl -0x2c(%rbp), %r15d
cmpl $0x8, %r12d
jl 0x16a5
movl (%r13), %eax
movl 0x4(%r13), %ecx
subl %r12d, %ecx
addb $0x8, %cl
shrl %cl, %eax
movl %eax, -0x4c(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
callq 0x1a10
movslq (%rax), %rax
movq 0x3aba(%rip), %rdx      # 0x5130
movl (%r13), %esi
movl 0x4(%r13), %ecx
subl %r12d, %ecx
addb $0x4, %cl
shrl %cl, %esi
movq (%rdx,%rax,8), %rax
andl $0xf, %esi
movb $0x0, 0x40(%rax,%rsi)
movq 0x3a9f(%rip), %rcx      # 0x5138
subq %rdx, %rcx
shrq $0x3, %rcx
decl %ecx
movl %ecx, (%rax,%rsi,4)
movsbl 0x4(%r13), %eax
movl %r12d, %ecx
subl %eax, %ecx
cmpl $0x1f, %ecx
je 0x16ed
movl (%r13), %edx
shll %cl, %edx
andl $0xf, %edx
movl $0x1, %eax
shll %cl, %eax
movq 0x3a6d(%rip), %rcx      # 0x5138
movq -0x8(%rcx), %rsi
leaq (%rsi,%rdx), %rcx
addq $0x40, %rcx
leaq (%rsi,%rdx,4), %rdx
xorl %esi, %esi
movb $0x1, (%rcx,%rsi)
movl %r14d, (%rdx,%rsi,4)
incq %rsi
cmpq %rsi, %rax
jne 0x16dd
incq %r14
movq 0x3a29(%rip), %rcx      # 0x5120
movq 0x3a1a(%rip), %rax      # 0x5118
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
ja 0x15a1
movq -0x40(%rbp), %rbx
movq 0x10(%rbx), %rsi
leaq 0x195a(%rip), %rdi      # 0x3077
xorl %eax, %eax
callq 0x1030
movq 0x10(%rbx), %rdi
leaq 0x195e(%rip), %rsi      # 0x308d
callq 0x1100
movq %rax, -0x58(%rbp)
testq %rax, %rax
je 0x1947
leaq -0x60(%rbp), %r14
movl $0x1, %esi
movl $0x1, %edx
movq %r14, %rdi
movq -0x48(%rbp), %rbx
movq %rbx, %rcx
callq 0x1180
movb $0x0, -0x2c(%rbp)
movb (%r14), %r15b
movq %rbx, %rdi
callq 0x11a0
testl %eax, %eax
jne 0x1895
movl $0x8, %r12d
movl $0x0, -0x30(%rbp)
movl $0x0, -0x40(%rbp)
xorl %r14d, %r14d
cmpl $0x3, %r12d
jg 0x17d2
movl %r14d, %r13d
addl %r12d, %r13d
movzbl -0x2c(%rbp), %ebx
cmpl $0x9, %r13d
jge 0x17d7
movl $0x1, %esi
movl $0x1, %edx
leaq -0x2c(%rbp), %rdi
movq -0x48(%rbp), %rcx
callq 0x1180
movl %r12d, %ecx
shrl %cl, %ebx
orb %bl, %r15b
movl $0x8, %r14d
movl %r13d, %r12d
cmpl $0x4, %r13d
jl 0x1792
jmp 0x17f8
movl %r12d, %r13d
jmp 0x17f8
movb $0x8, %cl
subb %r12b, %cl
movl %ebx, %eax
shll %cl, %eax
movb %al, -0x2c(%rbp)
movl %r12d, %ecx
shrl %cl, %ebx
movl %r13d, %r14d
addl $-0x8, %r14d
orb %bl, %r15b
movl $0x8, %r13d
movzbl %r15b, %ebx
movl %ebx, %eax
shrl $0x4, %eax
movslq -0x40(%rbp), %rcx
movq 0x3924(%rip), %rdx      # 0x5130
movq (%rdx,%rcx,8), %rcx
movl (%rcx,%rax,4), %edx
cmpb $0x1, 0x40(%rcx,%rax)
jne 0x185d
movslq %edx, %rax
movq 0x38f4(%rip), %rcx      # 0x5118
movq (%rcx,%rax,8), %r15
movsbl 0x5(%r15), %edi
testl %edi, %edi
je 0x1885
movq -0x58(%rbp), %rsi
callq 0x1130
movsbl 0x4(%r15), %ecx
subl -0x30(%rbp), %ecx
shll %cl, %ebx
subl %ecx, %r13d
movl $0x0, -0x40(%rbp)
movl %ebx, %r15d
movl %r13d, %r12d
movl $0x0, -0x30(%rbp)
jmp 0x186f
movl %edx, -0x40(%rbp)
addl $0x4, -0x30(%rbp)
shlb $0x4, %r15b
addl $-0x4, %r13d
movl %r13d, %r12d
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
callq 0x11a0
testl %eax, %eax
je 0x178c
jmp 0x1895
leaq 0x185f(%rip), %rdi      # 0x30eb
callq 0x1190
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
callq 0x10c0
movq -0x58(%rbp), %rdi
callq 0x10c0
movq 0x386b(%rip), %rax      # 0x5118
cmpq %rax, 0x386c(%rip)      # 0x5120
je 0x18de
xorl %ebx, %ebx
movq (%rax,%rbx,8), %rdi
callq 0x1110
incq %rbx
movq 0x3855(%rip), %rcx      # 0x5120
movq 0x3846(%rip), %rax      # 0x5118
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbx, %rcx
ja 0x18b8
movq 0x384b(%rip), %rax      # 0x5130
cmpq %rax, 0x384c(%rip)      # 0x5138
je 0x1916
xorl %ebx, %ebx
movq (%rax,%rbx,8), %rdi
callq 0x1110
incq %rbx
movq 0x3835(%rip), %rcx      # 0x5138
movq 0x3826(%rip), %rax      # 0x5130
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbx, %rcx
ja 0x18f0
leaq -0x90(%rbp), %rdi
callq 0x1a86
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x1200
addq $0x8, %r14
movq %r14, %rdi
callq 0x126c
addq $0x10, %rbx
movq %rbx, %rdi
callq 0x1242
jmp 0x1955
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x1a86
movq %rbx, %rdi
callq 0x11d0