// File: /workspace/github_cpp_makefile_star2_O3/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 %r12d, %r12d
movl $0x1, %r13d
movabsq $-0x5555555555555555, %r15 # imm = 0xAAAAAAAAAAAAAAAB
movq %rbx, %rdi
callq 0x2d62
movzbl %al, %eax
leal (%rax,%r12,2), %r12d
testb %r14b, %r14b
je 0x2fe4
movq 0x60f4(%rip), %rcx      # 0x9098
movq 0x60e5(%rip), %rax      # 0x9090
subq %rax, %rcx
je 0x2fd9
sarq $0x2, %rcx
imulq %r15, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
addq $0x8, %rax
cmpl %r13d, -0x8(%rax)
jne 0x2fd0
cmpl %r12d, -0x4(%rax)
je 0x3036
addq $0xc, %rax
decq %rcx
jne 0x2fc4
cmpl $0x11, %r13d
jge 0x3026
incl %r13d
jmp 0x2f89
movq 0x607d(%rip), %rcx      # 0x9068
movq 0x606e(%rip), %rax      # 0x9060
subq %rax, %rcx
je 0x3020
sarq $0x2, %rcx
imulq %r15, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
addq $0x8, %rax
cmpl %r13d, -0x8(%rax)
jne 0x3017
cmpl %r12d, -0x4(%rax)
je 0x3096
addq $0xc, %rax
decq %rcx
jne 0x300b
cmpl $0x10, %r13d
jle 0x2fdf
callq 0x233b
xorl %r12d, %r12d
movl $0x1, %r13d
jmp 0x2fdf
movzbl (%rax), %r15d
testl %r15d, %r15d
je 0x30b3
cmpl $0xf0, %r15d
je 0x30a8
movl %r15d, %r12d
andl $0xf, %r12d
movq %rbx, %rdi
callq 0x2d62
movl %eax, %r14d
movl $0x1, %r13d
movl %r12d, -0x2c(%rbp)
cmpb $0x2, %r12b
jb 0x3103
movl -0x2c(%rbp), %r12d
decl %r12d
movl $0x1, %r13d
movq %rbx, %rdi
callq 0x2d62
xorb %r14b, %al
xorb $0x1, %al
movzbl %al, %eax
leal (%rax,%r13,2), %r13d
decl %r12d
jne 0x307b
jmp 0x3103
movzbl (%rax), %r15d
testl %r15d, %r15d
je 0x30b3
cmpl $0xf0, %r15d
jne 0x30bb
xorl %edx, %edx
movl $0x1000, %eax           # imm = 0x1000
xorl %ecx, %ecx
jmp 0x3124
movl %r15d, %edx
movl %r15d, %ecx
jmp 0x3116
movl %r15d, %r12d
andl $0xf, %r12d
movq %rbx, %rdi
callq 0x2d62
movl %eax, %r14d
movl $0x1, %r13d
movl %r12d, -0x2c(%rbp)
cmpb $0x2, %r12b
jb 0x3103
movl -0x2c(%rbp), %r12d
decl %r12d
movl $0x1, %r13d
movq %rbx, %rdi
callq 0x2d62
xorb %r14b, %al
xorb $0x1, %al
movzbl %al, %eax
leal (%rax,%r13,2), %r13d
decl %r12d
jne 0x30ea
shrb $0x4, %r15b
movl %r13d, %ecx
negl %ecx
testb %r14b, %r14b
cmovnel %r13d, %ecx
movl -0x2c(%rbp), %edx
shlq $0x20, %rcx
movzbl %r15b, %eax
shll $0x8, %eax
movzbl %dl, %edx
addq %rcx, %rax
orq %rdx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq