// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
cmpl $0x3, %edi
jne 0x1939
movq 0x8(%rbx), %rsi
leaq 0x1bb6(%rip), %rdi      # 0x304d
xorl %eax, %eax
callq 0x1030
movq 0x8(%rbx), %rdi
leaq 0x1bb9(%rip), %rsi      # 0x3062
callq 0x1100
testq %rax, %rax
je 0x1941
movq %rax, %r14
movq %rbx, -0x40(%rbp)
leaq 0x1bfd(%rip), %rdi      # 0x30c2
callq 0x1190
leaq -0x35(%rbp), %r15
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x1180
movsbl (%r15), %ebx
testl %ebx, %ebx
jle 0x1552
leaq 0x3c26(%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 %r14, %rcx
callq 0x1180
movq 0x3bfa(%rip), %rsi      # 0x5120
cmpq 0x3bfb(%rip), %rsi      # 0x5128
je 0x1543
movq -0x90(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, 0x3bdf(%rip)      # 0x5120
jmp 0x154e
movq %r15, %rdi
movq %r12, %rdx
callq 0x1aea
decl %ebx
jne 0x14f9
movq %r14, -0x48(%rbp)
leaq 0x1b80(%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 0x3b8f(%rip), %rax      # 0x5118
cmpq %rax, 0x3b90(%rip)      # 0x5120
je 0x170b
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movl %r12d, %edx
movq (%rax,%r14,8), %r13
movsbl 0x4(%r13), %ecx
leal 0x4(%rdx), %r12d
cmpl %ecx, %edx
cmovgel %edx, %r12d
movl (%r13), %eax
subl %r12d, %ecx
addb $0x4, %cl
shrl %cl, %eax
movl %eax, -0x2c(%rbp)
cmpl %r15d, %eax
je 0x1697
movl $0x50, %edi
callq 0x1140
movq %rax, -0x50(%rbp)
movq 0x3b56(%rip), %rsi      # 0x5138
cmpq 0x3b57(%rip), %rsi      # 0x5140
je 0x15f8
movq %rax, (%rsi)
addq $0x8, 0x3b42(%rip)      # 0x5138
jmp 0x1608
leaq 0x3b31(%rip), %rdi      # 0x5130
leaq -0x50(%rbp), %rdx
callq 0x1c2a
movq 0x3b29(%rip), %rbx      # 0x5138
movq 0x3b1a(%rip), %r15      # 0x5130
leaq -0x90(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x19ae
subq %r15, %rbx
shrq $0x3, %rbx
decl %ebx
movl %ebx, (%rax)
movl -0x2c(%rbp), %r15d
cmpl $0x8, %r12d
jl 0x1697
movl (%r13), %eax
movl 0x4(%r13), %ecx
subl %r12d, %ecx
addb $0x8, %cl
shrl %cl, %eax
movl %eax, -0x54(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x54(%rbp), %rsi
callq 0x1a16
movslq (%rax), %rax
movq 0x3ac8(%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 0x3aad(%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 0x16ea
movl (%r13), %edx
shll %cl, %edx
andl $0xf, %edx
movl $0x1, %eax
shll %cl, %eax
movq 0x3a7b(%rip), %rcx      # 0x5138
cmpl $0x2, %eax
movl $0x1, %esi
cmovll %esi, %eax
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 0x16da
incq %r14
movq 0x3a2c(%rip), %rcx      # 0x5120
movq 0x3a1d(%rip), %rax      # 0x5118
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
ja 0x159f
movq -0x40(%rbp), %rbx
movq 0x10(%rbx), %rsi
leaq 0x195d(%rip), %rdi      # 0x3077
movl $0x0, -0x34(%rbp)
xorl %eax, %eax
callq 0x1030
movq 0x10(%rbx), %rdi
leaq 0x195a(%rip), %rsi      # 0x308d
callq 0x1100
movq %rax, -0x60(%rbp)
testq %rax, %rax
je 0x194d
leaq -0x50(%rbp), %r12
movl $0x1, %esi
movl $0x1, %edx
movq %r12, %rdi
movq -0x48(%rbp), %rbx
movq %rbx, %rcx
callq 0x1180
leaq -0x2c(%rbp), %rax
movb $0x0, (%rax)
movl $0x8, %r13d
movb (%r12), %r12b
movl $0x0, -0x40(%rbp)
movl $0x0, -0x30(%rbp)
movq %rbx, %rdi
callq 0x11a0
testl %eax, %eax
jne 0x1897
cmpl $0x3, %r13d
jg 0x17f9
movl -0x34(%rbp), %r14d
addl %r13d, %r14d
movzbl -0x2c(%rbp), %r15d
cmpl $0x8, %r14d
jg 0x17c7
movl $0x1, %esi
movl $0x1, %edx
leaq -0x2c(%rbp), %rdi
movq -0x48(%rbp), %rcx
callq 0x1180
movl $0x8, %esi
jmp 0x17e0
movb $0x8, %cl
subb %r13b, %cl
movl %r15d, %eax
shll %cl, %eax
movb %al, -0x2c(%rbp)
movl %r14d, %esi
addl $-0x8, %esi
movl $0x8, %r14d
movl %r14d, %ebx
movl %r13d, %ecx
shrl %cl, %r15d
orb %r15b, %r12b
movl %esi, %r14d
movl %ebx, %r13d
cmpl $0x4, %ebx
jl 0x179b
jmp 0x17ff
movl %r13d, %ebx
movl -0x34(%rbp), %esi
movzbl %r12b, %r15d
movl %r15d, %ecx
shrl $0x4, %ecx
movslq -0x40(%rbp), %rax
movq 0x391c(%rip), %rdx      # 0x5130
movq (%rdx,%rax,8), %rdx
movl (%rdx,%rcx,4), %eax
cmpb $0x1, 0x40(%rdx,%rcx)
movl %esi, -0x34(%rbp)
jne 0x1868
cltq
movq 0x38ea(%rip), %rcx      # 0x5118
movq (%rcx,%rax,8), %r13
movsbl 0x5(%r13), %edi
testl %edi, %edi
setne %r14b
je 0x187b
movq -0x60(%rbp), %rsi
callq 0x1130
movsbl 0x4(%r13), %ecx
subl -0x30(%rbp), %ecx
shll %cl, %r15d
subl %ecx, %ebx
movl $0x0, -0x40(%rbp)
movl %r15d, %r12d
movl $0x0, -0x30(%rbp)
jmp 0x1887
addl $0x4, -0x30(%rbp)
shlb $0x4, %r12b
addl $-0x4, %ebx
movb $0x1, %r14b
movl %eax, -0x40(%rbp)
jmp 0x1887
leaq 0x1869(%rip), %rdi      # 0x30eb
callq 0x1190
movl %ebx, %r13d
testb %r14b, %r14b
movq -0x48(%rbp), %rbx
jne 0x1781
movb %r12b, -0x50(%rbp)
movq %rbx, %rdi
callq 0x10c0
movq -0x60(%rbp), %rdi
callq 0x10c0
movq 0x3865(%rip), %rax      # 0x5118
cmpq %rax, 0x3866(%rip)      # 0x5120
je 0x18e4
xorl %ebx, %ebx
movq (%rax,%rbx,8), %rdi
callq 0x1110
incq %rbx
movq 0x384f(%rip), %rcx      # 0x5120
movq 0x3840(%rip), %rax      # 0x5118
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbx, %rcx
ja 0x18be
movq 0x3845(%rip), %rax      # 0x5130
cmpq %rax, 0x3846(%rip)      # 0x5138
je 0x191c
xorl %ebx, %ebx
movq (%rax,%rbx,8), %rdi
callq 0x1110
incq %rbx
movq 0x382f(%rip), %rcx      # 0x5138
movq 0x3820(%rip), %rax      # 0x5130
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbx, %rcx
ja 0x18f6
leaq -0x90(%rbp), %rdi
callq 0x1a8c
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x1200
addq $0x8, %rbx
movq %rbx, %rdi
callq 0x126c
addq $0x10, %rbx
movq %rbx, %rdi
callq 0x1242
jmp 0x195b
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x1a8c
movq %rbx, %rdi
callq 0x11d0