// File: /workspace/github_c_makefile_star2_O1/enzo1982[P]smooth/classes/gui/window/backends/xlib/windowxlib.cpp
// Function: smooth::GUI::WindowXLib::SetIcon(smooth::GUI::Bitmap const&)
S::Int S::GUI::WindowXLib::SetIcon(const Bitmap &newIcon)
{
	if (sysIcon != NIL)
	{
		delete [] sysIcon;

		sysIcon	    = NIL;
		sysIconSize = 0;
	}

	Int	 index = 0;
	Size	 size  = newIcon.GetSize();
	Int	 depth = newIcon.GetDepth();

	sysIconSize = 2 + size.cx * size.cy;
	sysIcon	    = new UnsignedLong [sysIconSize];

	sysIcon[index++] = size.cx;
	sysIcon[index++] = size.cy;

	Color	 backgroundColor16 = ((Setup::BackgroundColor.GetBlue() >> 3) << 11) | ((Setup::BackgroundColor.GetGreen() >> 2) << 5) | (Setup::BackgroundColor.GetRed() >> 3);
	Color	 backgroundColor24 = Setup::BackgroundColor;

	XImage	*image = (XImage *) newIcon.GetSystemBitmap();

	for (Int y = 0; y < size.cy; y++)
	{
		for (Int x = 0; x < size.cx; x++)
		{
			Long	 value = XGetPixel(image, x, y);

			if	(depth == 16) sysIcon[index++] = (value == backgroundColor16 ? 0 : 255) << 24 | (((value >> 11) & 0x1F) << 19) | (((value >> 5) & 0x3F) << 10) | ((value & 0x1F) << 3);
			else if (depth == 24) sysIcon[index++] = (value == backgroundColor24 ? 0 : 255) << 24 | value;
			else if (depth == 32) sysIcon[index++] = 						value;
		}
	}

	return Success();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x158(%rdi), %rdi
testq %rdi, %rdi
je 0xc2468
callq 0x561e0
movq $0x0, 0x158(%rbx)
movl $0x0, 0x160(%rbx)
movq %r14, %rdi
callq 0x58410
movslq (%rax), %r13
movslq 0x4(%rax), %r15
movq %r14, %rdi
callq 0x56c20
movb %al, 0x7(%rsp)
movl %r15d, %eax
imull %r13d, %eax
leal 0x2(%rax), %ecx
movl %ecx, 0x160(%rbx)
cltq
cmpl $-0x2, %eax
leaq 0x10(,%rax,8), %rax
movq $-0x1, %rdi
cmovgeq %rax, %rdi
callq 0x56670
movq %rbx, 0x20(%rsp)
movq %rax, 0x158(%rbx)
movq %r13, (%rax)
movq %r15, 0x8(%rax)
movq 0xaa006(%rip), %rax     # 0x16c4d0
movq 0x8(%rax), %rax
movq %rax, 0x8(%rsp)
movq %r14, %rdi
callq 0x58da0
movq %r15, 0x10(%rsp)
testq %r15, %r15
jle 0xc25c7
movq %rax, %r14
movq 0x8(%rsp), %rdx
movl %edx, %eax
shrl $0x8, %eax
andl $0xf800, %eax           # imm = 0xF800
movl %edx, %ecx
shrl $0x5, %ecx
andl $0x7e0, %ecx            # imm = 0x7E0
orl %eax, %ecx
shrl $0x3, %edx
andl $0x1f, %edx
orl %ecx, %edx
movq %rdx, 0x18(%rsp)
xorl %r15d, %r15d
movl $0x2, %ebx
movzbl 0x7(%rsp), %ebp
testl %r13d, %r13d
jle 0xc25b9
xorl %r12d, %r12d
movq %r14, %rdi
movl %r12d, %esi
movl %r15d, %edx
callq *0x68(%r14)
cmpb $0x20, 0x7(%rsp)
je 0xc2598
cmpl $0x18, %ebp
je 0xc2581
cmpl $0x10, %ebp
jne 0xc25ad
xorl %ecx, %ecx
cmpq 0x18(%rsp), %rax
sete %cl
shll $0x18, %ecx
movl %eax, %edx
shll $0x8, %edx
andl $0xf80000, %edx         # imm = 0xF80000
orl %ecx, %edx
movl %eax, %ecx
shll $0x5, %ecx
andl $0xfc00, %ecx           # imm = 0xFC00
orl %edx, %ecx
andl $0x1f, %eax
leal (%rcx,%rax,8), %eax
addq $-0x1000000, %rax       # imm = 0xFF000000
jmp 0xc2598
xorl %ecx, %ecx
cmpq 0x8(%rsp), %rax
sete %cl
shll $0x18, %ecx
addq $-0x1000000, %rcx       # imm = 0xFF000000
orq %rcx, %rax
movq 0x20(%rsp), %rcx
movq 0x158(%rcx), %rcx
movslq %ebx, %rdx
incl %ebx
movq %rax, (%rcx,%rdx,8)
incl %r12d
cmpl %r12d, %r13d
jne 0xc252e
incl %r15d
cmpl 0x10(%rsp), %r15d
jne 0xc2522
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq