// File: Alcaro[P]bridges/arlib/image.cpp
// Function: image::insert_tile(int, int, unsigned int, unsigned int, image const&, int, int)
void image::insert_tile(int32_t x, int32_t y, uint32_t width, uint32_t height,
                        const image& other, int32_t offx, int32_t offy)
{
	if (offx < 0) { offx = -offx; offx %= other.width;  offx = -offx; offx += other.width;  }
	if (offy < 0) { offy = -offy; offy %= other.height; offy = -offy; offx += other.height; }
	if ((uint32_t)offx > other.width)  { offx %= other.width;  }
	if ((uint32_t)offy > other.height) { offy %= other.height; }
	
	if (offy + height <= other.height)
	{
		image_insert_tile_row(*this, x, y, width, height, other, offx, offy);
		return;
	}
	
	if (offy != 0)
	{
		image_insert_tile_row(*this, x, y, width, other.height-offy, other, offx, offy);
		y += other.height-offy;
		height -= other.height-offy;
	}
	
	uint32_t yy = 0;
	if (height >= other.height)
	{
		for (yy = 0; yy < height-other.height; yy += other.height)
		{
			image_insert_tile_row(*this, x, y+yy, width, other.height, other, offx, 0);
		}
	}
	
	if (yy < height)
	{
		image_insert_tile_row(*this, x, y+yy, width, height-yy, other, offx, 0);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movl %r8d, -0x18(%rbp)
movq %r9, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpl $0x0, 0x10(%rbp)
jge 0x1889d
xorl %eax, %eax
subl 0x10(%rbp), %eax
movl %eax, 0x10(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %ecx
movl 0x10(%rbp), %eax
xorl %edx, %edx
divl %ecx
movl %edx, 0x10(%rbp)
xorl %eax, %eax
subl 0x10(%rbp), %eax
movl %eax, 0x10(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %eax
addl 0x10(%rbp), %eax
movl %eax, 0x10(%rbp)
cmpl $0x0, 0x18(%rbp)
jge 0x188d1
xorl %eax, %eax
subl 0x18(%rbp), %eax
movl %eax, 0x18(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %ecx
movl 0x18(%rbp), %eax
xorl %edx, %edx
divl %ecx
movl %edx, 0x18(%rbp)
xorl %eax, %eax
subl 0x18(%rbp), %eax
movl %eax, 0x18(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
addl 0x10(%rbp), %eax
movl %eax, 0x10(%rbp)
movl 0x10(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl (%rcx), %eax
jbe 0x188ec
movq -0x20(%rbp), %rax
movl (%rax), %ecx
movl 0x10(%rbp), %eax
xorl %edx, %edx
divl %ecx
movl %edx, 0x10(%rbp)
movl 0x18(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jbe 0x18909
movq -0x20(%rbp), %rax
movl 0x4(%rax), %ecx
movl 0x18(%rbp), %eax
xorl %edx, %edx
divl %ecx
movl %edx, 0x18(%rbp)
movl 0x18(%rbp), %eax
addl -0x18(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x4(%rcx), %eax
ja 0x18946
movq -0x30(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
movq -0x20(%rbp), %r9
movl 0x10(%rbp), %r10d
movl 0x18(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x18a50
jmp 0x18a47
cmpl $0x0, 0x18(%rbp)
je 0x1899f
movq -0x30(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movl -0x14(%rbp), %ecx
movq -0x20(%rbp), %rax
movl 0x4(%rax), %r8d
subl 0x18(%rbp), %r8d
movq -0x20(%rbp), %r9
movl 0x10(%rbp), %r10d
movl 0x18(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x18a50
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
subl 0x18(%rbp), %eax
addl -0x10(%rbp), %eax
movl %eax, -0x10(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %ecx
subl 0x18(%rbp), %ecx
movl -0x18(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x18(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jb 0x18a0d
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl -0x18(%rbp), %ecx
movq -0x20(%rbp), %rdx
subl 0x4(%rdx), %ecx
cmpl %ecx, %eax
jae 0x18a0b
movq -0x30(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
addl -0x24(%rbp), %edx
movl -0x14(%rbp), %ecx
movq -0x20(%rbp), %rax
movl 0x4(%rax), %r8d
movq -0x20(%rbp), %r9
movl 0x10(%rbp), %eax
xorl %r10d, %r10d
movl %eax, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x18a50
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x189b9
jmp 0x18a0d
movl -0x24(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0x18a47
movq -0x30(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
addl -0x24(%rbp), %edx
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
subl -0x24(%rbp), %r8d
movq -0x20(%rbp), %r9
movl 0x10(%rbp), %eax
xorl %r10d, %r10d
movl %eax, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x18a50
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)