// File: 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
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x4260
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x43f0
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x4420
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x4450
testb $0x1, %al
jne 0x6b97
jmp 0x6bd1
leaq -0x40(%rbp), %rdi
callq 0x4490
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x6f90
jmp 0x6bb3
jmp 0x6bb5
leaq -0x40(%rbp), %rdi
callq 0x44b0
jmp 0x6b84
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x6f3e
leaq -0x30(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x42d0
movq -0x148(%rbp), %rdi
movq %rax, -0x68(%rbp)
callq 0x4300
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
leaq 0x34c(%rip), %rdx       # 0x6f50
callq 0x7000
jmp 0x6c0b
leaq -0x88(%rbp), %rdi
callq 0x5390
leaq -0x30(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
callq 0x42d0
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rdi
callq 0x4300
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x4330
testb $0x1, %al
jne 0x6c64
jmp 0x6da3
leaq -0x98(%rbp), %rdi
callq 0x4370
movq %rax, -0xa8(%rbp)
movb $0x0, -0xa9(%rbp)
leaq -0x88(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
callq 0x53b0
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
callq 0x53e0
movq %rax, -0xc8(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x5410
testb $0x1, %al
jne 0x6ccb
jmp 0x6d2f
leaq -0xc0(%rbp), %rdi
callq 0x5450
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
callq 0x3fc0
movb %al, -0x149(%rbp)
jmp 0x6cf9
movb -0x149(%rbp), %al
testb $0x1, %al
jne 0x6d05
jmp 0x6d1f
movb $0x1, -0xa9(%rbp)
jmp 0x6d2f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x6f32
jmp 0x6d21
leaq -0xc0(%rbp), %rdi
callq 0x5470
jmp 0x6cb2
testb $0x1, -0xa9(%rbp)
je 0x6d3a
jmp 0x6d75
movl -0x14(%rbp), %esi
movq -0xa8(%rbp), %rcx
leaq -0xf8(%rbp), %rdi
xorl %edx, %edx
callq 0x3f10
jmp 0x6d54
leaq -0x88(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x5490
jmp 0x6d69
leaq -0xf8(%rbp), %rdi
callq 0x2ca0
leaq -0x98(%rbp), %rdi
callq 0x43b0
jmp 0x6c48
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x2ca0
jmp 0x6f32
movl -0x18(%rbp), %eax
movl %eax, -0xfc(%rbp)
leaq -0x88(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rdi
callq 0x53b0
movq %rax, -0x110(%rbp)
movq -0x108(%rbp), %rdi
callq 0x53e0
movq %rax, -0x118(%rbp)
leaq -0x110(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x5410
testb $0x1, %al
jne 0x6dfc
jmp 0x6f14
leaq -0x110(%rbp), %rdi
callq 0x5450
movq %rax, -0x120(%rbp)
movl -0xfc(%rbp), %eax
movl %eax, -0x154(%rbp)
movq -0x120(%rbp), %rdi
callq 0x54c0
movl %eax, -0x150(%rbp)
jmp 0x6e2f
movl -0x150(%rbp), %ecx
movl -0x154(%rbp), %eax
addl %ecx, %eax
cmpl -0x18(%rbp), %eax
jbe 0x6eab
movq -0x10(%rbp), %rax
movq %rax, -0x160(%rbp)
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
leaq -0x140(%rbp), %rdi
callq 0x4e40
jmp 0x6e61
movq -0x160(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x54d0
jmp 0x6e76
leaq -0x140(%rbp), %rdi
callq 0x5500
movl $0x0, -0xfc(%rbp)
jmp 0x6eab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x5500
jmp 0x6f32
movq -0x120(%rbp), %rdi
movl -0xfc(%rbp), %esi
callq 0x40e0
jmp 0x6ebf
movq -0x120(%rbp), %rdi
callq 0x54c0
movl %eax, -0x164(%rbp)
jmp 0x6ed3
movl -0x164(%rbp), %ecx
movl -0xfc(%rbp), %eax
addl %ecx, %eax
movl %eax, -0xfc(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5520
movq %rax, %rdi
movq -0x120(%rbp), %rsi
callq 0x4ec0
jmp 0x6f01
jmp 0x6f03
leaq -0x110(%rbp), %rdi
callq 0x5470
jmp 0x6de0
leaq -0x88(%rbp), %rdi
callq 0x5560
leaq -0x30(%rbp), %rdi
callq 0x2cc0
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
leaq -0x88(%rbp), %rdi
callq 0x5560
leaq -0x30(%rbp), %rdi
callq 0x2cc0
movq -0x58(%rbp), %rdi
callq 0x2290