// File: /workspace/github_cpp_makefile_star2_O1/nicoeps[P]NicoGB/src/ppu.cpp
// Function: PPU::drawSprites()
void PPU::drawSprites() {
    if (!(lcdc & 0x2)) {
        return;
    }
    int size = ((lcdc & 0x4) >> 2) ? 16 : 8;
    const uint16_t OAM = 0xFE00;

    typedef std::tuple<int, int, int> SpriteTuple;
    std::vector<SpriteTuple> sprites = std::vector<SpriteTuple>();
    sprites.reserve(10);
    for (int byte = 0; byte < 0x9F; byte += 4) {
        int Y = memory.read(OAM+byte) - 16;
        int X = memory.read(OAM+byte + 1) - 8;

        if (Y <= ly && Y > ly - size) {
            sprites.push_back(std::make_tuple(byte, Y, X));
            if (sprites.size() >= 10) {
                break;
            }
        }
    }

    std::sort(sprites.begin(), sprites.end(),
        [](const SpriteTuple& a, const SpriteTuple& b) {
            return (std::get<2>(a) >= std::get<2>(b));
        }
    );

    for (const auto &sprite : sprites) {
        int byte = std::get<0>(sprite);
        int Y = std::get<1>(sprite);
        int X = std::get<2>(sprite);

        if (X == -8 || X >= 160) {
            continue;
        }

        uint8_t tileNumber = memory.read(OAM+byte+2);
        if (size == 16) {
            tileNumber &= ~1;
        }

        uint8_t attributes = memory.read(OAM+byte+3);
        std::vector<uint32_t> col = getPalette((attributes & 0x10) ? obp1 : obp0);
        bool xFlip = attributes & 0x20;
        bool yFlip = attributes & 0x40;
        bool priority = attributes & 0x80;

        int i = ly - Y;
        uint8_t byte1 = memory.read(0x8000 + (tileNumber*16) + i * 2);
        uint8_t byte2 = memory.read(0x8000 + (tileNumber*16) + i * 2 + 1);
        if (yFlip) {
            byte1 = memory.read(0x8000 + (tileNumber*16) + (size - 1 - i) * 2);
            byte2 = memory.read(0x8000 + (tileNumber*16) + (size - 1 - i) * 2 + 1);
        }

        for (int j = 0; j < 8; ++j) {
            int XX = X + j;
            if (ly < 144 && XX < 160 && XX >= 0) {
                if (priority && line[XX] > 0) {
                    continue;
                }
                uint8_t bit1 = (byte1 >> (7 - j)) & 0x1;
                uint8_t bit2 = (byte2 >> (7 - j)) & 0x1;
                if (xFlip) {
                    bit1 = (byte1 >> j) & 0x1;
                    bit2 = (byte2 >> j) & 0x1;
                }
                uint8_t color = (bit2 << 1) | bit1;
                uint32_t pixel = col[color];

                if (color != 0) {
                    writebuffer[ly * 160 + XX] = pixel;
                }
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq 0x28(%rdi), %rax
movzbl (%rax), %eax
movl %eax, 0x8(%rsp)
testb $0x2, %al
je 0x87f2
movq %rdi, %rbx
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl $0xa, %esi
callq 0x8cc4
movl 0x8(%rsp), %eax
andl $0x4, %eax
leal 0x8(,%rax,2), %eax
movl %eax, 0xc(%rsp)
xorl %r13d, %r13d
leaq 0x10(%rsp), %r14
leaq 0x30(%rsp), %r15
movabsq $-0x5555555555555555, %r12 # imm = 0xAAAAAAAAAAAAAAAB
movq (%rbx), %rdi
leal 0xfe00(%r13), %eax
movzwl %ax, %esi
callq 0x70d8
movl %eax, %ebp
movq (%rbx), %rdi
leal 0xfe01(%r13), %eax
movzwl %ax, %esi
callq 0x70d8
movzbl %bpl, %ecx
addl $-0x10, %ecx
movq 0x48(%rbx), %rdx
movzbl (%rdx), %edx
cmpl %edx, %ecx
jg 0x856b
subl 0xc(%rsp), %edx
cmpl %edx, %ecx
jle 0x856b
movzbl %al, %eax
addl $-0x8, %eax
movl %eax, 0x30(%rsp)
movl %ecx, 0x34(%rsp)
movl %r13d, 0x38(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x8e8e
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x2, %rax
imulq %r12, %rax
cmpq $0x9, %rax
ja 0x857d
cmpl $0x9b, %r13d
jb 0x8574
jmp 0x857d
cmpl $0x9a, %r13d
ja 0x857d
addl $0x4, %r13d
jmp 0x84ea
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r14
cmpq %r14, %r15
movq %rbx, (%rsp)
je 0x85ff
movq %r14, %rbx
subq %r15, %rbx
movq %rbx, %rax
sarq $0x2, %rax
imulq %r12, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x884d
cmpq $0xc1, %rbx
jl 0x85f0
leaq 0xc0(%r15), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x8b8f
cmpq %r14, %r12
movq (%rsp), %rbx
je 0x85ff
movq %r12, %rdi
callq 0x8c48
addq $0xc, %r12
cmpq %r14, %r12
jne 0x85dd
jmp 0x85ff
movq %r15, %rdi
movq %r14, %rsi
callq 0x8b8f
movq (%rsp), %rbx
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rsi
cmpq %rsi, %rdx
je 0x87db
movq %rsi, 0x48(%rsp)
movslq (%rdx), %r13
cmpq $0xa0, %r13
setge %al
cmpq $-0x8, %r13
sete %cl
orb %al, %cl
jne 0x87ce
movl 0x4(%rdx), %r12d
movq %rdx, 0x50(%rsp)
movl 0x8(%rdx), %r14d
movq (%rbx), %rdi
leal -0x1fe(%r14), %eax
movzwl %ax, %esi
callq 0x70d8
movq %rbx, %r15
movzbl %al, %ecx
andb $-0x2, %al
testb $0x4, 0x8(%rsp)
movzbl %al, %ebx
cmovel %ecx, %ebx
movq (%r15), %rdi
addl $0xfffffe03, %r14d      # imm = 0xFFFFFE03
movzwl %r14w, %esi
callq 0x70d8
movl %eax, %ebp
movzbl %al, %eax
andl $0x10, %eax
shrl %eax
movq 0x60(%r15,%rax), %rax
movzbl (%rax), %edx
leaq 0x30(%rsp), %rdi
callq 0x7f0a
movq (%r15), %rdi
movq 0x48(%r15), %rax
movzbl (%rax), %r14d
subl %r12d, %r14d
movzbl %bl, %ebx
shll $0x4, %ebx
leal (%rbx,%r14,2), %r12d
addl $0x8000, %r12d          # imm = 0x8000
movzwl %r12w, %esi
callq 0x70d8
movl %eax, %r15d
movq (%rsp), %rax
movq (%rax), %rdi
orl $0x1, %r12d
movzwl %r12w, %esi
callq 0x70d8
testb $0x40, %bpl
je 0x870f
orl $0x8000, %ebx           # imm = 0x8000
movq (%rsp), %rax
movq (%rax), %rdi
notl %r14d
addl 0xc(%rsp), %r14d
leal (%rbx,%r14,2), %ebx
movzwl %bx, %esi
callq 0x70d8
movl %eax, %r15d
movq (%rsp), %rax
movq (%rax), %rdi
orl $0x1, %ebx
movzwl %bx, %esi
callq 0x70d8
movq (%rsp), %rcx
movq 0x48(%rcx), %rdx
movq 0x88(%rcx), %rsi
movq 0xa0(%rcx), %rdi
movzbl %r15b, %r8d
movzbl %al, %eax
movq 0x30(%rsp), %r9
movl $0x7, %r10d
xorl %r11d, %r11d
movzbl (%rdx), %ebx
cmpq $0x8f, %rbx
ja 0x879a
cmpq $0x9f, %r13
ja 0x879a
testb %bpl, %bpl
js 0x8793
testb $0x20, %bpl
movl %r11d, %ecx
cmovel %r10d, %ecx
movl %eax, %r14d
shrl %cl, %r14d
xorl %r15d, %r15d
btl %ecx, %r8d
setb %r15b
addl %r14d, %r14d
andl $0x2, %r14d
orl %r15d, %r14d
je 0x879a
movl %r14d, %ecx
movl (%r9,%rcx,4), %ecx
leaq (%rbx,%rbx,4), %rbx
shll $0x7, %ebx
addq %rsi, %rbx
movl %ecx, (%rbx,%r13,4)
jmp 0x879a
cmpb $0x0, (%rdi,%r13)
je 0x8754
incq %r11
decl %r10d
incq %r13
cmpq $0x8, %r11
jne 0x873a
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x87c0
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq (%rsp), %rbx
movq 0x50(%rsp), %rdx
movq 0x48(%rsp), %rsi
addq $0xc, %rdx
cmpq %rsi, %rdx
jne 0x8617
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x87f2
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x882b
jmp 0x882b
jmp 0x882b
jmp 0x882b
jmp 0x880d
jmp 0x882b
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x882e
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
jmp 0x882e
jmp 0x882b
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x8845
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x2270