// File: /workspace/github_cpp_makefile_star2_O3/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 0x8786
movq %rdi, %rbx
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl $0xa, %esi
callq 0x8c30
movl 0x8(%rsp), %eax
andl $0x4, %eax
leal 0x8(,%rax,2), %eax
movl %eax, 0xc(%rsp)
xorl %r14d, %r14d
leaq 0x30(%rsp), %r15
movq (%rbx), %rdi
leal 0xfe00(%r14), %eax
movzwl %ax, %esi
callq 0x7022
movl %eax, %ebp
movq (%rbx), %rdi
leal 0xfe01(%r14), %eax
movzwl %ax, %esi
callq 0x7022
movzbl %bpl, %ecx
addl $-0x10, %ecx
movq 0x48(%rbx), %rdx
movzbl (%rdx), %edx
cmpl %edx, %ecx
jg 0x84e8
subl 0xc(%rsp), %edx
cmpl %edx, %ecx
jle 0x84e8
movzbl %al, %eax
addl $-0x8, %eax
movl %eax, 0x30(%rsp)
movl %ecx, 0x34(%rsp)
movl %r14d, 0x38(%rsp)
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x8db6
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %r12
movq %r12, %rax
subq %r13, %rax
sarq $0x2, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
cmpq $0x9, %rax
ja 0x8504
cmpl $0x9b, %r14d
jb 0x84f1
jmp 0x8504
cmpl $0x9b, %r14d
jae 0x84fa
addl $0x4, %r14d
jmp 0x8455
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %r12
cmpq %r12, %r13
movq %rbx, (%rsp)
je 0x8586
movq %r12, %rbx
subq %r13, %rbx
movq %rbx, %rax
sarq $0x2, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x87e1
cmpq $0xc1, %rbx
jl 0x8577
leaq 0xc0(%r13), %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0x8af7
cmpq %r12, %r14
movq (%rsp), %rbx
je 0x8586
movq %r14, %rdi
callq 0x8bb0
addq $0xc, %r14
cmpq %r12, %r14
jne 0x8564
jmp 0x8586
movq %r13, %rdi
movq %r12, %rsi
callq 0x8af7
movq (%rsp), %rbx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
cmpq %rdx, %rdi
je 0x8774
movq %rdx, 0x48(%rsp)
movslq (%rdi), %r15
cmpq $0xa0, %r15
setge %al
cmpq $-0x8, %r15
sete %cl
orb %al, %cl
jne 0x8762
movl 0x4(%rdi), %r14d
movq %rdi, 0x50(%rsp)
movl 0x8(%rdi), %r13d
movq (%rbx), %rdi
leal -0x1fe(%r13), %eax
movzwl %ax, %esi
callq 0x7022
movq %rbx, %r12
movzbl %al, %ecx
andb $-0x2, %al
testb $0x4, 0x8(%rsp)
movzbl %al, %ebx
cmovel %ecx, %ebx
movq (%r12), %rdi
addl $0xfffffe03, %r13d      # imm = 0xFFFFFE03
movzwl %r13w, %esi
callq 0x7022
movl %eax, %ebp
movzbl %al, %eax
andl $0x10, %eax
shrl %eax
movq 0x60(%r12,%rax), %rax
movzbl (%rax), %edx
leaq 0x30(%rsp), %rdi
callq 0x7e84
movq (%r12), %rdi
movq 0x48(%r12), %rax
movzbl (%rax), %r13d
subl %r14d, %r13d
movzbl %bl, %ebx
shll $0x4, %ebx
leal (%rbx,%r13,2), %r14d
addl $0x8000, %r14d          # imm = 0x8000
movzwl %r14w, %esi
callq 0x7022
movl %eax, %r12d
movq (%rsp), %rax
movq (%rax), %rdi
orl $0x1, %r14d
movzwl %r14w, %esi
callq 0x7022
testb $0x40, %bpl
je 0x8699
orl $0x8000, %ebx           # imm = 0x8000
movq (%rsp), %rax
movq (%rax), %rdi
notl %r13d
addl 0xc(%rsp), %r13d
leal (%rbx,%r13,2), %ebx
movzwl %bx, %esi
callq 0x7022
movl %eax, %r12d
movq (%rsp), %rax
movq (%rax), %rdi
orl $0x1, %ebx
movzwl %bx, %esi
callq 0x7022
movq (%rsp), %rcx
movq 0x48(%rcx), %rdx
movzbl %r12b, %esi
movzbl %al, %eax
movq 0x30(%rsp), %rdi
leaq (,%r15,4), %r8
addq 0x88(%rcx), %r8
movq 0xa0(%rcx), %r9
addq %r15, %r9
movl $0x7, %r10d
xorl %r11d, %r11d
movzbl (%rdx), %ebx
cmpq $0x8f, %rbx
ja 0x8731
leaq (%r15,%r11), %rcx
cmpq $0x9f, %rcx
ja 0x8731
testb %bpl, %bpl
js 0x872a
testb $0x20, %bpl
movl %r11d, %ecx
cmovel %r10d, %ecx
movl %eax, %r14d
shrl %cl, %r14d
xorl %r12d, %r12d
btl %ecx, %esi
setb %r12b
addl %r14d, %r14d
andl $0x2, %r14d
orl %r12d, %r14d
je 0x8731
movl %r14d, %ecx
movl (%rdi,%rcx,4), %ecx
leaq (%rbx,%rbx,4), %rbx
shll $0x7, %ebx
addq %r8, %rbx
movl %ecx, (%rbx,%r11,4)
jmp 0x8731
cmpb $0x0, (%r9,%r11)
je 0x86ed
incq %r11
decl %r10d
cmpq $0x8, %r11
jne 0x86cf
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x8754
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq (%rsp), %rbx
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rdx
addq $0xc, %rdi
cmpq %rdx, %rdi
jne 0x859e
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x8786
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 0x87bf
jmp 0x87bf
jmp 0x87bf
jmp 0x87bf
jmp 0x87a1
jmp 0x87bf
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x87c2
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
jmp 0x87c2
jmp 0x87bf
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x87d9
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x2270