// File: khedoros[P]khednes/util.cpp
// Function: util::debug(int, char const*, ...)
int util::debug(int orig,const char * fmt,...) {
    if(!init) {
        for(int i=0;i<1000;++i) {
            active[i]=false;
        }
        init=true;
    }

    if(active[orig]||(orig==999&&active[1])) {
        switch(orig) {
                case 1:
                        printf("CPU: ");
                        break;
                case 2:
                        printf("MAIN: ");
                        break;
                case 3:
                        printf("MEM: ");
                        break;
                case 4:
                        printf("PPU: ");
                        break;
                case 5:
                        printf("ROM: ");
                        break;
                case 6:
                        printf("UTIL: ");
                        break;
                case 7:
                        printf("WAV: ");
                        break;
                case 8:
                        printf("APU: ");
                        break;
                default:
                        break; //Don't print origin label
        }
        va_list ap;
        va_start(ap,fmt);
        return vprintf(fmt,ap);
    }
    else {
        return 0;
    }
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
testb %al, %al
je 0x20e3f
movaps %xmm0, 0x40(%rsp)
movaps %xmm1, 0x50(%rsp)
movaps %xmm2, 0x60(%rsp)
movaps %xmm3, 0x70(%rsp)
movaps %xmm4, 0x80(%rsp)
movaps %xmm5, 0x90(%rsp)
movaps %xmm6, 0xa0(%rsp)
movaps %xmm7, 0xb0(%rsp)
movq %r9, 0x38(%rsp)
movq %r8, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movl %edi, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
testb $0x1, 0x16637(%rip)     # 0x374a0
jne 0x20ed5
movl $0x0, 0x124(%rsp)
cmpl $0x3e8, 0x124(%rsp) # imm = 0x3E8
jge 0x20ece
movslq 0x124(%rsp), %rdx
leaq 0x110(%rsp), %rdi
leaq 0x1660e(%rip), %rsi     # 0x374a8
callq 0x21f90
leaq 0x110(%rsp), %rdi
xorl %esi, %esi
callq 0x21fd0
leaq 0x110(%rsp), %rdi
callq 0x22040
movl 0x124(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x124(%rsp)
jmp 0x20e76
movb $0x1, 0x165cb(%rip)     # 0x374a0
movslq 0x130(%rsp), %rdx
leaq 0x100(%rsp), %rdi
leaq 0x165bc(%rip), %rsi     # 0x374a8
callq 0x21f90
leaq 0x100(%rsp), %rdi
callq 0x22050
movb %al, %cl
movb $0x0, 0xe3(%rsp)
movb $0x1, %al
testb $0x1, %cl
movb %al, 0xf(%rsp)
jne 0x20f62
xorl %eax, %eax
cmpl $0x3e7, 0x130(%rsp) # imm = 0x3E7
movb %al, 0xe(%rsp)
jne 0x20f5a
leaq 0x1657b(%rip), %rsi     # 0x374a8
leaq 0xf0(%rsp), %rdi
movl $0x1, %edx
callq 0x21f90
jmp 0x20f41
movb $0x1, 0xe3(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x22050
movb %al, 0xe(%rsp)
movb 0xe(%rsp), %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
movb %al, 0xd(%rsp)
testb $0x1, 0xe3(%rsp)
jne 0x20f76
jmp 0x20f83
leaq 0xf0(%rsp), %rdi
callq 0x22040
leaq 0x100(%rsp), %rdi
callq 0x22040
movb 0xd(%rsp), %al
testb $0x1, %al
jne 0x20f9d
jmp 0x210b9
movl 0x130(%rsp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, (%rsp)
subl $0x7, %eax
ja 0x2106f
movq (%rsp), %rax
leaq 0xb790(%rip), %rcx      # 0x2c750
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x22040
jmp 0x210d3
leaq 0xbbfc(%rip), %rdi      # 0x2cbf2
movb $0x0, %al
callq 0x4050
jmp 0x21071
leaq 0xbbf2(%rip), %rdi      # 0x2cbf8
movb $0x0, %al
callq 0x4050
jmp 0x21071
leaq 0xbbe9(%rip), %rdi      # 0x2cbff
movb $0x0, %al
callq 0x4050
jmp 0x21071
leaq 0xbbdf(%rip), %rdi      # 0x2cc05
movb $0x0, %al
callq 0x4050
jmp 0x21071
leaq 0xbbd5(%rip), %rdi      # 0x2cc0b
movb $0x0, %al
callq 0x4050
jmp 0x21071
leaq 0xbbcb(%rip), %rdi      # 0x2cc11
movb $0x0, %al
callq 0x4050
jmp 0x21071
leaq 0xbbc2(%rip), %rdi      # 0x2cc18
movb $0x0, %al
callq 0x4050
jmp 0x21071
leaq 0xbbb8(%rip), %rdi      # 0x2cc1e
movb $0x0, %al
callq 0x4050
jmp 0x21071
jmp 0x21071
leaq 0xc0(%rsp), %rax
leaq 0x10(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x140(%rsp), %rcx
movq %rcx, 0x8(%rax)
movl $0x30, 0x4(%rax)
movl $0x10, (%rax)
movq 0x128(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x43e0
movl %eax, 0x134(%rsp)
jmp 0x210c4
movl $0x0, 0x134(%rsp)
movl 0x134(%rsp), %eax
addq $0x138, %rsp            # imm = 0x138
retq
movq 0xe8(%rsp), %rdi
callq 0x4680