// File: 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
subq $0x140, %rsp            # imm = 0x140
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x1400
movq 0x7c1a(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x3c37(%rip), %rsi      # 0x5007
movb $0x0, %al
callq 0x1140
movq 0x7c02(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x3c20(%rip), %rsi      # 0x500f
movb $0x0, %al
callq 0x1140
movl $0x1, %edi
callq 0x1120
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x3c22(%rip), %rdi      # 0x5031
movb $0x0, %al
callq 0x1030
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x3c21(%rip), %rsi      # 0x5046
callq 0x1100
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x145f
movq 0x7ba4(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x3bfb(%rip), %rsi      # 0x5049
movb $0x0, %al
callq 0x1140
movl $0x1, %edi
callq 0x1120
leaq 0x3bf5(%rip), %rdi      # 0x505b
movb $0x0, %al
callq 0x1030
movq -0x18(%rbp), %rcx
leaq -0x19(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x1160
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movsbl -0x19(%rbp), %ecx
cmpl %ecx, %eax
jge 0x14d4
movl $0x8, %edi
callq 0x1130
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x1160
leaq 0x7c38(%rip), %rdi      # 0x90f8
leaq -0x28(%rbp), %rsi
callq 0x1ce0
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1489
leaq 0x3b9c(%rip), %rdi      # 0x5077
movb $0x0, %al
callq 0x1030
leaq -0x58(%rbp), %rdi
callq 0x1d50
movl $0x0, -0x5c(%rbp)
movl $0xffffffff, -0x60(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x64(%rbp)
movslq -0x64(%rbp), %rax
movq %rax, -0x100(%rbp)
leaq 0x7be6(%rip), %rdi      # 0x90f8
callq 0x1d70
movq %rax, %rcx
movq -0x100(%rbp), %rax
cmpq %rcx, %rax
jae 0x17cd
movslq -0x64(%rbp), %rsi
leaq 0x7bc3(%rip), %rdi      # 0x90f8
callq 0x1d90
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movsbl 0x4(%rax), %eax
cmpl -0x5c(%rbp), %eax
jle 0x1557
movl -0x5c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x5c(%rbp)
movq -0x70(%rbp), %rax
movl (%rax), %eax
movq -0x70(%rbp), %rcx
movsbl 0x4(%rcx), %ecx
addl $0x4, %ecx
subl -0x5c(%rbp), %ecx
shrl %cl, %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x60(%rbp), %eax
je 0x16c6
movl $0x50, %edi
callq 0x1130
movq %rax, -0x80(%rbp)
leaq 0x7b7f(%rip), %rdi      # 0x9110
leaq -0x80(%rbp), %rsi
callq 0x1db0
jmp 0x159c
leaq 0x7b6d(%rip), %rdi      # 0x9110
callq 0x1e20
decl %eax
movl %eax, -0x10c(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x74(%rbp), %rsi
callq 0x1e40
movq %rax, -0x108(%rbp)
jmp 0x15c6
movq -0x108(%rbp), %rax
movl -0x10c(%rbp), %ecx
movl %ecx, (%rax)
movl -0x74(%rbp), %eax
movl %eax, -0x60(%rbp)
cmpl $0x8, -0x5c(%rbp)
jl 0x16c4
movq -0x70(%rbp), %rcx
movl (%rcx), %eax
movl 0x4(%rcx), %ecx
movl -0x5c(%rbp), %edx
subl %edx, %ecx
addb $0x8, %cl
shrl %cl, %eax
movl %eax, -0x94(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x94(%rbp), %rsi
callq 0x1f20
movq %rax, -0x118(%rbp)
jmp 0x1617
movq -0x118(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x90(%rbp)
movslq -0x90(%rbp), %rsi
leaq 0x7adc(%rip), %rdi      # 0x9110
callq 0x2000
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x70(%rbp), %rax
movl (%rax), %eax
movq -0x70(%rbp), %rcx
movsbl 0x4(%rcx), %ecx
addl $0x4, %ecx
subl -0x5c(%rbp), %ecx
shrl %cl, %eax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
andl $0xf, %eax
movl %eax, -0xa4(%rbp)
movq -0xa0(%rbp), %rax
movl -0xa4(%rbp), %ecx
movb $0x0, 0x40(%rax,%rcx)
leaq 0x7a89(%rip), %rdi      # 0x9110
callq 0x1e20
subq $0x1, %rax
movl %eax, %edx
movq -0xa0(%rbp), %rax
movl -0xa4(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
jmp 0x16c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x2020
jmp 0x1bcb
jmp 0x16c6
movl -0x5c(%rbp), %eax
movq -0x70(%rbp), %rcx
movsbl 0x4(%rcx), %ecx
subl %ecx, %eax
movl %eax, -0xa8(%rbp)
movl -0xa8(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0xac(%rbp)
movq -0x70(%rbp), %rax
movl (%rax), %eax
movl -0x5c(%rbp), %ecx
movq -0x70(%rbp), %rdx
movsbl 0x4(%rdx), %edx
subl %edx, %ecx
shll %cl, %eax
movl %eax, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
andl $0xf, %eax
movl %eax, -0xb0(%rbp)
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0xac(%rbp), %eax
jge 0x17bd
leaq 0x79d7(%rip), %rdi      # 0x9110
callq 0x1e20
movq %rax, %rsi
subq $0x1, %rsi
leaq 0x79c4(%rip), %rdi      # 0x9110
callq 0x2000
movq (%rax), %rax
movl -0xb0(%rbp), %ecx
addl -0xb4(%rbp), %ecx
movl %ecx, %ecx
movb $0x1, 0x40(%rax,%rcx)
movl -0x64(%rbp), %eax
movl %eax, -0x11c(%rbp)
leaq 0x7999(%rip), %rdi      # 0x9110
callq 0x1e20
movq %rax, %rsi
subq $0x1, %rsi
leaq 0x7986(%rip), %rdi      # 0x9110
callq 0x2000
movl -0x11c(%rbp), %edx
movq (%rax), %rax
movl -0xb0(%rbp), %ecx
addl -0xb4(%rbp), %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x1720
jmp 0x17bf
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x1500
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
leaq 0x38aa(%rip), %rdi      # 0x5086
xorl %eax, %eax
callq 0x1030
jmp 0x17e5
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x38a8(%rip), %rsi      # 0x509c
callq 0x1100
movq %rax, -0x128(%rbp)
jmp 0x1802
movq -0x128(%rbp), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0xc0(%rbp)
jne 0x1844
movq 0x77bf(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x386c(%rip), %rsi      # 0x509f
movb $0x0, %al
callq 0x1140
movl $0x1, %edi
callq 0x1120
movl $0x0, -0xd0(%rbp)
movl $0x0, -0xd4(%rbp)
movq -0x18(%rbp), %rcx
leaq -0xc1(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x1160
jmp 0x1872
movb $0x0, -0xc2(%rbp)
movl $0x8, -0xc8(%rbp)
movl $0x0, -0xcc(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1170
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x18a7
jmp 0x1ad8
jmp 0x18a9
cmpl $0x4, -0xc8(%rbp)
jge 0x197f
movl -0xc8(%rbp), %eax
addl -0xcc(%rbp), %eax
cmpl $0x8, %eax
jg 0x1921
movzbl -0xc2(%rbp), %eax
movb -0xc8(%rbp), %cl
shrl %cl, %eax
movl %eax, %ecx
movzbl -0xc1(%rbp), %eax
orl %ecx, %eax
movb %al, -0xc1(%rbp)
movl -0xc8(%rbp), %eax
movl -0xcc(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0xc8(%rbp)
movq -0x18(%rbp), %rcx
leaq -0xc2(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x1160
jmp 0x1915
movl $0x8, -0xcc(%rbp)
jmp 0x197a
movzbl -0xc2(%rbp), %eax
movl -0xc8(%rbp), %ecx
sarl %cl, %eax
movl %eax, %ecx
movzbl -0xc1(%rbp), %eax
orl %ecx, %eax
movb %al, -0xc1(%rbp)
movl $0x8, %ecx
subl -0xc8(%rbp), %ecx
movzbl -0xc2(%rbp), %eax
shll %cl, %eax
movb %al, -0xc2(%rbp)
movl -0xc8(%rbp), %eax
addl -0xcc(%rbp), %eax
subl $0x8, %eax
movl %eax, -0xcc(%rbp)
movl $0x8, -0xc8(%rbp)
jmp 0x18a9
movzbl -0xc1(%rbp), %eax
sarl $0x4, %eax
andl $0xf, %eax
movb %al, -0xd5(%rbp)
movslq -0xd0(%rbp), %rsi
leaq 0x7770(%rip), %rdi      # 0x9110
callq 0x2000
movq (%rax), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movsbq -0xd5(%rbp), %rcx
testb $0x1, 0x40(%rax,%rcx)
je 0x1a8d
movq -0xe0(%rbp), %rax
movsbq -0xd5(%rbp), %rcx
movslq (%rax,%rcx,4), %rsi
leaq 0x7715(%rip), %rdi      # 0x90f8
callq 0x1d90
movq (%rax), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movsbl 0x5(%rax), %eax
cmpl $0x0, %eax
jne 0x1a17
leaq 0x36ae(%rip), %rdi      # 0x50b7
xorl %eax, %eax
callq 0x1030
jmp 0x1a12
jmp 0x1ad8
movq -0xc0(%rbp), %rdi
movq -0xe8(%rbp), %rax
movsbl 0x5(%rax), %edx
leaq 0x368e(%rip), %rsi      # 0x50be
movb $0x0, %al
callq 0x1140
movq -0xe8(%rbp), %rax
movsbl 0x4(%rax), %eax
subl -0xd4(%rbp), %eax
movl %eax, -0xec(%rbp)
movl -0xec(%rbp), %ecx
movzbl -0xc1(%rbp), %eax
shll %cl, %eax
movb %al, -0xc1(%rbp)
movl -0xec(%rbp), %ecx
movl -0xc8(%rbp), %eax
subl %ecx, %eax
movl %eax, -0xc8(%rbp)
movl $0x0, -0xd4(%rbp)
movl $0x0, -0xd0(%rbp)
jmp 0x1ad3
movq -0xe0(%rbp), %rax
movsbq -0xd5(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xd0(%rbp)
movl -0xd4(%rbp), %eax
addl $0x4, %eax
movl %eax, -0xd4(%rbp)
movzbl -0xc1(%rbp), %eax
shll $0x4, %eax
movb %al, -0xc1(%rbp)
movl -0xc8(%rbp), %eax
subl $0x4, %eax
movl %eax, -0xc8(%rbp)
jmp 0x188d
movq -0x18(%rbp), %rdi
callq 0x10c0
jmp 0x1ae3
movq -0xc0(%rbp), %rdi
callq 0x10c0
jmp 0x1af1
movl $0x0, -0xf0(%rbp)
movslq -0xf0(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq 0x75e8(%rip), %rdi      # 0x90f8
callq 0x1d70
movq %rax, %rcx
movq -0x130(%rbp), %rax
cmpq %rcx, %rax
jae 0x1b50
movslq -0xf0(%rbp), %rsi
leaq 0x75c6(%rip), %rdi      # 0x90f8
callq 0x1d90
movq (%rax), %rdi
callq 0x1110
movl -0xf0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf0(%rbp)
jmp 0x1afb
movl $0x0, -0xf4(%rbp)
movslq -0xf4(%rbp), %rax
movq %rax, -0x138(%rbp)
leaq 0x75a1(%rip), %rdi      # 0x9110
callq 0x1e20
movq %rax, %rcx
movq -0x138(%rbp), %rax
cmpq %rcx, %rax
jae 0x1baf
movslq -0xf4(%rbp), %rsi
leaq 0x757f(%rip), %rdi      # 0x9110
callq 0x2000
movq (%rax), %rdi
callq 0x1110
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
jmp 0x1b5a
movl $0x0, -0x4(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x2020
movl -0x4(%rbp), %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0x88(%rbp), %rdi
callq 0x11a0
nopw (%rax,%rax)