// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq 0x158(%rdi), %rdi
testq %rdi, %rdi
je 0xbb289
callq 0x572d0
andq $0x0, 0x158(%r15)
andl $0x0, 0x160(%r15)
movq %r14, %rdi
callq 0x596e0
movslq (%rax), %r12
movslq 0x4(%rax), %rbx
movq %r14, %rdi
callq 0x57da0
movl %eax, %ebp
movl %ebx, %eax
imull %r12d, %eax
leal 0x2(%rax), %ecx
movl %ecx, 0x160(%r15)
cltq
cmpl $-0x2, %eax
leaq 0x10(,%rax,8), %rax
pushq $-0x1
popq %rdi
cmovgeq %rax, %rdi
callq 0x577a0
movq %r15, 0x20(%rsp)
movq %rax, 0x158(%r15)
movq %r12, (%rax)
movq %rbx, 0x8(%rax)
movq 0xa61eb(%rip), %rax     # 0x1614d0
movq 0x8(%rax), %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
movq %rdx, 0x18(%rsp)
shrl $0x3, %edx
andl $0x1f, %edx
orl %ecx, %edx
movq %rdx, 0x10(%rsp)
movq %r14, %rdi
callq 0x5a0f0
movq %rax, %r14
xorl %r15d, %r15d
testl %r12d, %r12d
movl $0x0, %r13d
cmovgl %r12d, %r13d
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
movl %eax, 0xc(%rsp)
pushq $0x2
popq %rbx
movb %bpl, 0xb(%rsp)
movzbl %bpl, %ebp
cmpl 0xc(%rsp), %r15d
je 0xbb3ed
xorl %r12d, %r12d
cmpl %r12d, %r13d
je 0xbb3e5
movq %r14, %rdi
movl %r12d, %esi
movl %r15d, %edx
callq *0x68(%r14)
cmpb $0x20, 0xb(%rsp)
je 0xbb3c8
cmpl $0x18, %ebp
je 0xbb3b1
cmpl $0x10, %ebp
jne 0xbb3dd
xorl %ecx, %ecx
cmpq 0x10(%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 0xbb3c8
xorl %ecx, %ecx
cmpq 0x18(%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
jmp 0xbb355
incl %r15d
jmp 0xbb347
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq