// File: /workspace/github_cpp_makefile_star2_O1/Slexis-Works[P]texture-packing/algoFFDH.cpp
// Function: algoFFDH(std::vector<Rectangle, std::allocator<Rectangle>> const&, std::vector<Bin, std::allocator<Bin>>&, unsigned int, unsigned int)
void algoFFDH(const std::vector<Rectangle> &rects, std::vector<Bin> &bins, coord bw, coord by) {
	// Tri des données
	std::vector<Rectangle> sortedRects;
	for (const Rectangle &rect : rects) {
		sortedRects.push_back(rect);
	}
	std::sort(sortedRects.begin(), sortedRects.end(), goesBeforeByHeight);

	// Création de tous les étages
	std::vector<Shelf> shelves;
	for (const Rectangle &rect : sortedRects) {
		bool inserted = false;
		for (auto & shelf : shelves) {
			if (shelf.putRect(rect)) {
				inserted = true;
				break;
			}
		}
		if (inserted) {
			continue;
		}
		shelves.push_back(Shelf(bw, 0, rect));
	}

	// Insertion de tous les étages de manière naïve
	coord curY = by;
	for (auto & shelf : shelves) {
		if (curY + shelf.getHeight() > by) {
			bins.push_back(Bin(bw, by));
			curY = 0;
		}
		shelf.setY(curY);
		curY += shelf.getHeight();
		bins.back().importShelf(shelf);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %ecx, %ebx
movl %edx, 0xc(%rsp)
movq %rsi, 0x50(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq (%rdi), %r15
movq 0x8(%rdi), %r14
cmpq %r14, %r15
je 0x3d9c
leaq 0x10(%rsp), %r12
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x3d88
movl 0x10(%r15), %eax
movl %eax, 0x10(%rsi)
movups (%r15), %xmm0
movups %xmm0, (%rsi)
addq $0x14, 0x18(%rsp)
jmp 0x3d93
movq %r12, %rdi
movq %r15, %rdx
callq 0x3fde
addq $0x14, %r15
cmpq %r14, %r15
jne 0x3d66
movl %ebx, 0x2c(%rsp)
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r12
cmpq %r12, %r15
je 0x3df8
movq %r12, %rax
subq %r15, %rax
sarq $0x2, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %rcx
bsrq %rcx, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x1f7(%rip), %rcx       # 0x3fd2
movq %r15, %rdi
movq %r12, %rsi
callq 0x40f8
leaq 0x1e5(%rip), %rdx       # 0x3fd2
movq %r15, %rdi
movq %r12, %rsi
callq 0x41d0
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rax
movq %rax, 0x58(%rsp)
cmpq %rax, %r15
je 0x3eaa
leaq 0x60(%rsp), %rbp
movl $0x1, %r14d
movq 0x30(%rsp), %r13
movq 0x38(%rsp), %rbx
cmpq %rbx, %r13
je 0x3e67
xorl %r12d, %r12d
movq %r13, %rdi
movq %r15, %rsi
callq 0x336a
movzbl %r12b, %r12d
testb %al, %al
cmovnel %r14d, %r12d
addq $0x28, %r13
cmpq %rbx, %r13
sete %cl
orb %al, %cl
je 0x3e3e
testb $0x1, %r12b
jne 0x3e9f
movq %rbp, %rdi
movl 0xc(%rsp), %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0x32d0
leaq 0x30(%rsp), %rdi
movq %rbp, %rsi
callq 0x38d6
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x3e9f
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
addq $0x14, %r15
cmpq 0x58(%rsp), %r15
jne 0x3e2c
movq 0x30(%rsp), %r15
movq 0x38(%rsp), %r14
cmpq %r14, %r15
movl 0x2c(%rsp), %ebp
je 0x3f33
leaq 0x60(%rsp), %r12
movl %ebp, %r13d
movl (%r15), %eax
addl %r13d, %eax
cmpl %ebp, %eax
jbe 0x3f04
movq %r12, %rdi
movl 0xc(%rsp), %esi
movl %ebp, %edx
callq 0x360c
movq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x3b16
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x3f01
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
xorl %r13d, %r13d
movq %r15, %rdi
movl %r13d, %esi
callq 0x342e
movl (%r15), %ebx
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rdi
addq $-0x20, %rdi
movq %r15, %rsi
callq 0x366c
addl %ebx, %r13d
addq $0x28, %r15
cmpq %r14, %r15
jne 0x3ec5
leaq 0x30(%rsp), %rdi
callq 0x3888
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3f54
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3f68
movq %rax, %rbx
jmp 0x3fb3
jmp 0x3fa6
movq %rax, %rbx
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x3fa9
movq 0x78(%rsp), %rsi
jmp 0x3f9a
jmp 0x3fa6
movq %rax, %rbx
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x3fa9
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
jmp 0x3fa9
jmp 0x3fa6
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3888
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3fca
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq %rbx, %rdi
callq 0x21c0