// File: /workspace/github_cpp_makefile_star2_O2/OpenFirework[P]MyJPEG_Decoder/./jpeg/jpeg_decoder.cpp
// Function: readAC(unsigned char*&, unsigned char)
acCode readAC(byte *&framedata, unsigned char number) {
    unsigned char x;
    unsigned int len = 0;
    for (int count = 1; ; count++) {
        len = len << 1;
        len += (unsigned int)getBit(framedata);
        if(number == 0) {
            for(int v=0;v<YACHT.size();v++) {
                if((YACHT[v].codeLength==count) && (YACHT[v].encodeValue==len)) {
                    x = YACHT[v].realVale;
                    unsigned char zeros = x >> 4;
                    unsigned char codeLen = x & 0x0F;
                    if (x == 0) {
                        return acCode{0,0,0};
                    } else if (x == 0xF0) {
                        return acCode{0, 16, 0};
                    }
                    unsigned  char first = getBit(framedata);
                    int code = 1;
                    for (int i = 1; i < codeLen; i++) {
                        unsigned char b = getBit(framedata);
                        code = code << 1;
                        code += first ? b : !b;
                    }
                    code = first ? code : -code;
                    return acCode{codeLen, zeros, code};
                }
            }
            if (count > 16) {fprintf(stderr, "key not found\n"); count = 1; len = 0;}
        } else {
            for(int v=0;v<UVACHT.size();v++) {
                if((UVACHT[v].codeLength==count) && (UVACHT[v].encodeValue==len)) {
                    x = UVACHT[v].realVale;
                    unsigned char zeros = x >> 4;
                    unsigned char codeLen = x & 0x0F;
                    if (x == 0) {
                        return acCode{0,0,0};
                    } else if (x == 0xF0) {
                        return acCode{0, 16, 0};
                    }
                    unsigned  char first = getBit(framedata);
                    int code = 1;
                    
                    for (int i = 1; i < codeLen; i++) {
                        unsigned char b = getBit(framedata);
                        code = code << 1;
                        code += first ? b : !b;
                    }
                    code = first ? code : -code;
                    return acCode{codeLen, zeros, code};
                }
            }
            if (count > 16) {fprintf(stderr, "key not found\n"); count = 1; len = 0; }
        }
        
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %r14d
movq %rdi, %rbx
xorl %r13d, %r13d
pushq $0x1
popq %r15
pushq $0xc
popq %r12
movq %rbx, %rdi
callq 0x2af1
movzbl %al, %eax
leal (%rax,%r13,2), %r13d
testb %r14b, %r14b
je 0x2d3a
movq 0x5394(%rip), %rax      # 0x8098
movq 0x5385(%rip), %rcx      # 0x8090
subq %rcx, %rax
cqto
idivq %r12
addq $0x8, %rcx
subq $0x1, %rax
jb 0x2d2f
cmpl %r15d, -0x8(%rcx)
jne 0x2d29
cmpl %r13d, -0x4(%rcx)
je 0x2d9b
addq $0xc, %rcx
jmp 0x2d17
cmpl $0x11, %r15d
jge 0x2d76
incl %r15d
jmp 0x2ce9
movq 0x5327(%rip), %rax      # 0x8068
movq 0x5318(%rip), %rcx      # 0x8060
subq %rcx, %rax
cqto
idivq %r12
addq $0x8, %rcx
subq $0x1, %rax
jb 0x2d70
cmpl %r15d, -0x8(%rcx)
jne 0x2d6a
cmpl %r13d, -0x4(%rcx)
je 0x2dea
addq $0xc, %rcx
jmp 0x2d54
cmpl $0x10, %r15d
jle 0x2d35
movq 0x425b(%rip), %rax      # 0x6fd8
movq (%rax), %rcx
leaq 0x1556(%rip), %rdi      # 0x42dd
pushq $0xe
popq %rsi
pushq $0x1
popq %rdx
callq 0x2250
xorl %r13d, %r13d
pushq $0x1
popq %r15
jmp 0x2d35
movzbl (%rcx), %r15d
testl %r15d, %r15d
je 0x2e07
cmpl $0xf0, %r15d
je 0x2dfc
movl %r15d, %eax
shrb $0x4, %al
movb %al, -0x29(%rbp)
andl $0xf, %r15d
movq %rbx, %rdi
callq 0x2af1
movl %eax, %r14d
pushq $0x1
popq %r13
movl %r13d, %r12d
cmpl %r15d, %r12d
jae 0x2e4c
movq %rbx, %rdi
callq 0x2af1
xorb %r14b, %al
xorb $0x1, %al
movzbl %al, %eax
leal (%rax,%r13,2), %r13d
incl %r12d
jmp 0x2dcc
movzbl (%rcx), %r15d
testl %r15d, %r15d
je 0x2e07
cmpl $0xf0, %r15d
jne 0x2e0f
xorl %edx, %edx
movl $0x1000, %eax           # imm = 0x1000
xorl %ecx, %ecx
jmp 0x2e69
movl %r15d, %eax
movl %r15d, %ecx
jmp 0x2e5b
movl %r15d, %eax
shrb $0x4, %al
movb %al, -0x29(%rbp)
andl $0xf, %r15d
movq %rbx, %rdi
callq 0x2af1
movl %eax, %r14d
pushq $0x1
popq %r13
movl %r13d, %r12d
cmpl %r15d, %r12d
jae 0x2e4c
movq %rbx, %rdi
callq 0x2af1
xorb %r14b, %al
xorb $0x1, %al
movzbl %al, %eax
leal (%rax,%r13,2), %r13d
incl %r12d
jmp 0x2e2e
movl %r13d, %ecx
negl %ecx
testb %r14b, %r14b
cmovnel %r13d, %ecx
movb -0x29(%rbp), %al
shlq $0x20, %rcx
movzbl %al, %eax
shll $0x8, %eax
movzbl %r15b, %edx
addq %rcx, %rax
orq %rdx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq