// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq 0x28(%rdi), %rax
movzbl (%rax), %eax
movl %eax, 0x14(%rsp)
testb $0x2, %al
je 0x787a
movq %rdi, 0x8(%rsp)
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
pushq $0xa
popq %rsi
callq 0x7cac
movl 0x14(%rsp), %eax
andl $0x4, %eax
leal 0x8(,%rax,2), %eax
movl %eax, 0x18(%rsp)
xorl %r14d, %r14d
leaq 0x40(%rsp), %r15
movq 0x8(%rsp), %rbx
cmpl $0x9f, %r14d
jae 0x761d
movq (%rbx), %rdi
leal 0xfe00(%r14), %eax
movzwl %ax, %esi
callq 0x64d2
movl %eax, %ebp
movq (%rbx), %rdi
leal 0xfe01(%r14), %eax
movzwl %ax, %esi
callq 0x64d2
movzbl %bpl, %ecx
addl $-0x10, %ecx
movq 0x48(%rbx), %rdx
movzbl (%rdx), %edx
cmpl %edx, %ecx
jg 0x7614
subl 0x18(%rsp), %edx
cmpl %edx, %ecx
jle 0x7614
movzbl %al, %eax
addl $-0x8, %eax
movl %eax, 0x40(%rsp)
movl %ecx, 0x44(%rsp)
movl %r14d, 0x48(%rsp)
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x7fce
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %r12
movq %r12, %rax
subq %r13, %rax
cqto
pushq $0xc
popq %rcx
idivq %rcx
cmpq $0x9, %rax
ja 0x7627
addl $0x4, %r14d
jmp 0x7589
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %r12
cmpq %r12, %r13
je 0x768e
movq %r12, %r14
subq %r13, %r14
movq %r14, %rax
cqto
pushq $0xc
popq %rcx
idivq %rcx
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x78bb
cmpq $0xc1, %r14
jl 0x7683
leaq 0xc0(%r13), %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0x7b51
cmpq %r12, %r14
je 0x768e
movq %r14, %rdi
callq 0x7ba8
addq $0xc, %r14
jmp 0x7670
movq %r13, %rdi
movq %r12, %rsi
callq 0x7b51
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rsi
movq %rsi, 0x58(%rsp)
cmpq %rsi, %rdx
je 0x7870
movslq (%rdx), %r14
cmpq $0xa0, %r14
setge %al
cmpq $-0x8, %r14
sete %cl
orb %al, %cl
jne 0x7867
movl 0x4(%rdx), %r15d
movq %rdx, 0x60(%rsp)
movl 0x8(%rdx), %r13d
movq 0x8(%rsp), %r12
movq (%r12), %rdi
leal -0x1fe(%r13), %eax
movzwl %ax, %esi
callq 0x64d2
movzbl %al, %ecx
andb $-0x2, %al
testb $0x4, 0x14(%rsp)
movzbl %al, %ebx
cmovel %ecx, %ebx
movq (%r12), %rdi
addl $0xfffffe03, %r13d      # imm = 0xFFFFFE03
movzwl %r13w, %esi
callq 0x64d2
movl %eax, %ebp
movl %r15d, 0x1c(%rsp)
movzbl %al, %eax
andl $0x10, %eax
shrl %eax
movq 0x60(%r12,%rax), %rax
movzbl (%rax), %edx
leaq 0x40(%rsp), %rdi
callq 0x701e
movq (%r12), %rdi
movq 0x48(%r12), %rax
movzbl (%rax), %r13d
subl 0x1c(%rsp), %r13d
movzbl %bl, %ebx
shll $0x4, %ebx
leal (%rbx,%r13,2), %r12d
addl $0x8000, %r12d          # imm = 0x8000
movzwl %r12w, %esi
callq 0x64d2
movl %eax, %r15d
movq 0x8(%rsp), %rax
movq (%rax), %rdi
orl $0x1, %r12d
movzwl %r12w, %esi
callq 0x64d2
testb $0x40, %bpl
je 0x77ae
orl $0x8000, %ebx           # imm = 0x8000
movq 0x8(%rsp), %rax
movq (%rax), %rdi
notl %r13d
addl 0x18(%rsp), %r13d
leal (%rbx,%r13,2), %ebx
movzwl %bx, %esi
callq 0x64d2
movl %eax, %r15d
movq 0x8(%rsp), %rax
movq (%rax), %rdi
orl $0x1, %ebx
movzwl %bx, %esi
callq 0x64d2
movq 0x8(%rsp), %rcx
movq 0x48(%rcx), %rdx
movzbl %r15b, %esi
movzbl %al, %eax
movq 0x40(%rsp), %rdi
leaq (,%r14,4), %r8
addq 0x88(%rcx), %r8
movq 0xa0(%rcx), %r9
addq %r14, %r9
pushq $0x7
popq %r10
xorl %r11d, %r11d
cmpq $0x8, %r11
je 0x7853
movzbl (%rdx), %ebx
cmpq $0x8f, %rbx
ja 0x784b
leaq (%r14,%r11), %rcx
cmpq $0x9f, %rcx
ja 0x784b
testb %bpl, %bpl
js 0x7844
testb $0x20, %bpl
movl %r11d, %ecx
cmovel %r10d, %ecx
movl %eax, %r15d
shrl %cl, %r15d
xorl %r12d, %r12d
btl %ecx, %esi
setb %r12b
addl %r15d, %r15d
andl $0x2, %r15d
orl %r12d, %r15d
je 0x784b
movl %r15d, %ecx
movl (%rdi,%rcx,4), %ecx
imulq $0x280, %rbx, %rbx      # imm = 0x280
addq %r8, %rbx
movl %ecx, (%rbx,%r11,4)
jmp 0x784b
cmpb $0x0, (%r9,%r11)
je 0x7807
incq %r11
decl %r10d
jmp 0x77e3
leaq 0x40(%rsp), %rdi
callq 0x5b6e
movq 0x60(%rsp), %rdx
movq 0x58(%rsp), %rsi
addq $0xc, %rdx
jmp 0x769d
leaq 0x20(%rsp), %rdi
callq 0x7f2c
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7897
jmp 0x7897
jmp 0x7897
jmp 0x7897
jmp 0x7897
jmp 0x789c
jmp 0x7897
movq %rax, %rbx
jmp 0x78a9
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x5b6e
leaq 0x20(%rsp), %rdi
callq 0x7f2c
movq %rbx, %rdi
callq 0x22b0