// File: Alcaro[P]bridges/arlib/image.cpp
// Function: image::insert_rectangle(int, int, unsigned int, unsigned int, unsigned int)
void image::insert_rectangle(int32_t x, int32_t y, uint32_t width, uint32_t height, uint32_t argb)
{
	if (x<0) { if (width  < (uint32_t)-x) return; width  -= -x; x=0; }
	if (y<0) { if (height < (uint32_t)-y) return; height -= -y; y=0; }
	if (x+width  >= this->width)  { if ((uint32_t)x >= this->width)  return; width  = this->width  - x; }
	if (y+height >= this->height) { if ((uint32_t)y >= this->height) return; height = this->height - y; }
	
	if (argb >= 0xFF000000)
	{
		if (!height) return;
		
		if (fmt == ifmt_0rgb8888) argb &= 0x00FFFFFF;
		uint32_t* targetpx = this->pixels32 + y*this->stride/sizeof(uint32_t) + x;
		for (uint32_t xx=0;xx<width;xx++)
		{
			targetpx[xx] = argb;
		}
		for (uint32_t yy=1;yy<height;yy++)
		{
			memcpy(targetpx + yy*this->stride/sizeof(uint32_t), targetpx, width*sizeof(uint32_t));
		}
	}
	else
	{
		for (uint32_t yy=0;yy<height;yy++)
		{
			uint32_t* targetpx = this->pixels32 + (y+yy)*this->stride/sizeof(uint32_t) + x;
			for (uint32_t xx=0;xx<width;xx++)
			{
				targetpx[xx] = blend_8888_on_8888(targetpx[xx], argb);
				if (fmt == ifmt_0rgb8888) targetpx[xx] &= 0x00FFFFFF;
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movl %r8d, -0x18(%rbp)
movl %r9d, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpl $0x0, -0xc(%rbp)
jge 0x18e90
movl -0x14(%rbp), %eax
xorl %ecx, %ecx
subl -0xc(%rbp), %ecx
cmpl %ecx, %eax
jae 0x18e7c
jmp 0x19088
xorl %ecx, %ecx
subl -0xc(%rbp), %ecx
movl -0x14(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x14(%rbp)
movl $0x0, -0xc(%rbp)
cmpl $0x0, -0x10(%rbp)
jge 0x18ebb
movl -0x18(%rbp), %eax
xorl %ecx, %ecx
subl -0x10(%rbp), %ecx
cmpl %ecx, %eax
jae 0x18ea7
jmp 0x19088
xorl %ecx, %ecx
subl -0x10(%rbp), %ecx
movl -0x18(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x10(%rbp)
movq -0x50(%rbp), %rcx
movl -0xc(%rbp), %eax
addl -0x14(%rbp), %eax
cmpl (%rcx), %eax
jb 0x18ee5
movq -0x50(%rbp), %rcx
movl -0xc(%rbp), %eax
cmpl (%rcx), %eax
jb 0x18ed9
jmp 0x19088
movq -0x50(%rbp), %rax
movl (%rax), %eax
subl -0xc(%rbp), %eax
movl %eax, -0x14(%rbp)
movq -0x50(%rbp), %rcx
movl -0x10(%rbp), %eax
addl -0x18(%rbp), %eax
cmpl 0x4(%rcx), %eax
jb 0x18f12
movq -0x50(%rbp), %rcx
movl -0x10(%rbp), %eax
cmpl 0x4(%rcx), %eax
jb 0x18f05
jmp 0x19088
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
subl -0x10(%rbp), %eax
movl %eax, -0x18(%rbp)
cmpl $0xff000000, -0x1c(%rbp) # imm = 0xFF000000
jb 0x18fde
cmpl $0x0, -0x18(%rbp)
jne 0x18f2a
jmp 0x19088
movq -0x50(%rbp), %rax
cmpl $0x7, 0x8(%rax)
jne 0x18f3f
movl -0x1c(%rbp), %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
movl %eax, -0x1c(%rbp)
movq -0x50(%rbp), %rdx
movq 0x10(%rdx), %rax
movl -0x10(%rbp), %ecx
imull 0xc(%rdx), %ecx
movl %ecx, %ecx
shrq $0x2, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0xc(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x18f91
movl -0x1c(%rbp), %edx
movq -0x28(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x18f71
movl $0x1, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0x18fd9
movq -0x50(%rbp), %rcx
movq -0x28(%rbp), %rdi
movl -0x30(%rbp), %eax
imull 0xc(%rcx), %eax
movl %eax, %eax
shrq $0x2, %rax
shlq $0x2, %rax
addq %rax, %rdi
movq -0x28(%rbp), %rsi
movl -0x14(%rbp), %eax
movl %eax, %edx
shlq $0x2, %rdx
callq 0x4280
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x18f98
jmp 0x19088
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0x19086
movq -0x50(%rbp), %rdx
movq 0x10(%rdx), %rax
movl -0x10(%rbp), %ecx
addl -0x34(%rbp), %ecx
imull 0xc(%rdx), %ecx
movl %ecx, %ecx
shrq $0x2, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movslq -0xc(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x19076
movq -0x40(%rbp), %rax
movl -0x44(%rbp), %ecx
movl (%rax,%rcx,4), %edi
movl -0x1c(%rbp), %esi
callq 0x19090
movl %eax, %esi
movq -0x50(%rbp), %rax
movq -0x40(%rbp), %rcx
movl -0x44(%rbp), %edx
movl %esi, (%rcx,%rdx,4)
cmpl $0x7, 0x8(%rax)
jne 0x19069
movq -0x40(%rbp), %rax
movl -0x44(%rbp), %ecx
movl (%rax,%rcx,4), %edx
andl $0xffffff, %edx         # imm = 0xFFFFFF
movl %edx, (%rax,%rcx,4)
jmp 0x1906b
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x19026
jmp 0x19078
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x18fe5
jmp 0x19088
addq $0x50, %rsp
popq %rbp
retq
nop