// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movl %ecx, %ebp
movl %edx, 0xc(%rsp)
movq %rsi, 0x48(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq (%rdi), %r15
movq 0x8(%rdi), %rbx
cmpq %rbx, %r15
je 0x3eb8
leaq 0x10(%rsp), %r12
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x3e45
movl 0x10(%r15), %eax
movl %eax, 0x10(%rsi)
movups (%r15), %xmm0
movups %xmm0, (%rsi)
addq $0x14, 0x18(%rsp)
jmp 0x3e50
movq %r12, %rdi
movq %r15, %rdx
callq 0x4076
addq $0x14, %r15
cmpq %rbx, %r15
jne 0x3e23
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r12
cmpq %r12, %r15
je 0x3ebb
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 0x1d6(%rip), %rcx       # 0x406a
movq %r15, %rdi
movq %r12, %rsi
callq 0x4190
leaq 0x1c4(%rip), %rdx       # 0x406a
movq %r15, %rdi
movq %r12, %rsi
callq 0x4268
movq 0x10(%rsp), %r15
jmp 0x3ebb
xorl %r15d, %r15d
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x18(%rsp), %rbx
cmpq %rbx, %r15
je 0x3fd1
movl %ebp, 0x2c(%rsp)
leaq 0x50(%rsp), %r12
leaq 0x30(%rsp), %r13
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %rbp
cmpq %rbp, %r14
je 0x3f0c
movq %r14, %rdi
movq %r15, %rsi
callq 0x3382
testb %al, %al
jne 0x3f3f
addq $0x28, %r14
jmp 0x3ef2
movq %r12, %rdi
movl 0xc(%rsp), %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0x32e8
movq %r13, %rdi
movq %r12, %rsi
callq 0x39c8
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x3f3f
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
addq $0x14, %r15
cmpq %rbx, %r15
jne 0x3ee8
movq 0x30(%rsp), %r15
movq 0x38(%rsp), %r14
cmpq %r14, %r15
movl 0x2c(%rsp), %ebp
je 0x3fd1
leaq 0x50(%rsp), %r12
movl %ebp, %r13d
movl (%r15), %eax
addl %r13d, %eax
cmpl %ebp, %eax
jbe 0x3fa2
movq %r12, %rdi
movl 0xc(%rsp), %esi
movl %ebp, %edx
callq 0x3720
movq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x3bee
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x3f9f
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
xorl %r13d, %r13d
movq %r15, %rdi
movl %r13d, %esi
callq 0x3446
movl (%r15), %ebx
movq 0x48(%rsp), %rax
movq 0x8(%rax), %rdi
addq $-0x20, %rdi
movq %r15, %rsi
callq 0x3780
addl %ebx, %r13d
addq $0x28, %r15
cmpq %r14, %r15
jne 0x3f63
leaq 0x30(%rsp), %rdi
callq 0x3974
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3ff2
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4019
jmp 0x403e
movq %rax, %rbx
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4041
movq 0x68(%rsp), %rsi
jmp 0x4032
movq %rax, %rbx
jmp 0x404b
jmp 0x403e
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x4041
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
jmp 0x4041
jmp 0x403e
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3974
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4062
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq %rbx, %rdi
callq 0x21c0