// File: Slexis-Works[P]texture-packing/algoNFDH.cpp
// Function: algoNFDH(std::vector<Rectangle, std::allocator<Rectangle>> const&, std::vector<Bin, std::allocator<Bin>>&, unsigned int, unsigned int)
void algoNFDH(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;

	// Insertion du premier rectangle afin d'initialiser un premier étage
	shelves.push_back(Shelf(bw, 0, sortedRects[0]));

	unsigned int i = 0; // Compteur des étages
	unsigned int j = 0; // Compteur des rectangles

	for (const Rectangle &rect : sortedRects) {
		bool inserted = true;
		if (j != 0) {
			inserted = shelves.at(i).putRect(rect);
		}
		if (!inserted) {
			shelves.push_back(Shelf(bw, 0, rect));
			i++;
		}
		j++;
	}

	// 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 $0x190, %rsp            # imm = 0x190
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 0x8967
jmp 0x89a1
leaq -0x40(%rbp), %rdi
callq 0x4490
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x6f90
jmp 0x8983
jmp 0x8985
leaq -0x40(%rbp), %rdi
callq 0x44b0
jmp 0x8954
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x8d68
leaq -0x30(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x42d0
movq -0x158(%rbp), %rdi
movq %rax, -0x68(%rbp)
callq 0x4300
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
leaq -0x1a84(%rip), %rdx     # 0x6f50
callq 0x7000
jmp 0x89db
leaq -0x88(%rbp), %rdi
callq 0x5390
movl -0x14(%rbp), %eax
movl %eax, -0x160(%rbp)
xorl %eax, %eax
movl %eax, -0x15c(%rbp)
movl %eax, %esi
leaq -0x30(%rbp), %rdi
callq 0x8d80
movl -0x160(%rbp), %esi
movl -0x15c(%rbp), %edx
movq %rax, %rcx
leaq -0xb0(%rbp), %rdi
callq 0x3f10
jmp 0x8a20
leaq -0x88(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x5490
jmp 0x8a35
leaq -0xb0(%rbp), %rdi
callq 0x2ca0
movl $0x0, -0xb4(%rbp)
movl $0x0, -0xb8(%rbp)
leaq -0x30(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x42d0
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rdi
callq 0x4300
movq %rax, -0xd0(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x4330
testb $0x1, %al
jne 0x8aa2
jmp 0x8bcd
leaq -0xc8(%rbp), %rdi
callq 0x4370
movq %rax, -0xd8(%rbp)
movb $0x1, -0xd9(%rbp)
cmpl $0x0, -0xb8(%rbp)
je 0x8b3b
movl -0xb4(%rbp), %eax
movl %eax, %esi
leaq -0x88(%rbp), %rdi
callq 0x8da0
movq %rax, -0x168(%rbp)
jmp 0x8ae2
movq -0x168(%rbp), %rdi
movq -0xd8(%rbp), %rsi
callq 0x3fc0
movb %al, -0x169(%rbp)
jmp 0x8afd
movb -0x169(%rbp), %al
andb $0x1, %al
movb %al, -0xd9(%rbp)
jmp 0x8b3b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x8d5c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x2ca0
jmp 0x8d5c
testb $0x1, -0xd9(%rbp)
jne 0x8bad
movl -0x14(%rbp), %esi
movq -0xd8(%rbp), %rcx
leaq -0x108(%rbp), %rdi
xorl %edx, %edx
callq 0x3f10
jmp 0x8b5e
leaq -0x88(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x5490
jmp 0x8b73
leaq -0x108(%rbp), %rdi
callq 0x2ca0
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x8bad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x2ca0
jmp 0x8d5c
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x43b0
jmp 0x8a86
movl -0x18(%rbp), %eax
movl %eax, -0x10c(%rbp)
leaq -0x88(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rdi
callq 0x53b0
movq %rax, -0x120(%rbp)
movq -0x118(%rbp), %rdi
callq 0x53e0
movq %rax, -0x128(%rbp)
leaq -0x120(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x5410
testb $0x1, %al
jne 0x8c26
jmp 0x8d3e
leaq -0x120(%rbp), %rdi
callq 0x5450
movq %rax, -0x130(%rbp)
movl -0x10c(%rbp), %eax
movl %eax, -0x174(%rbp)
movq -0x130(%rbp), %rdi
callq 0x54c0
movl %eax, -0x170(%rbp)
jmp 0x8c59
movl -0x170(%rbp), %ecx
movl -0x174(%rbp), %eax
addl %ecx, %eax
cmpl -0x18(%rbp), %eax
jbe 0x8cd5
movq -0x10(%rbp), %rax
movq %rax, -0x180(%rbp)
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
leaq -0x150(%rbp), %rdi
callq 0x4e40
jmp 0x8c8b
movq -0x180(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x54d0
jmp 0x8ca0
leaq -0x150(%rbp), %rdi
callq 0x5500
movl $0x0, -0x10c(%rbp)
jmp 0x8cd5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x5500
jmp 0x8d5c
movq -0x130(%rbp), %rdi
movl -0x10c(%rbp), %esi
callq 0x40e0
jmp 0x8ce9
movq -0x130(%rbp), %rdi
callq 0x54c0
movl %eax, -0x184(%rbp)
jmp 0x8cfd
movl -0x184(%rbp), %ecx
movl -0x10c(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x10c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5520
movq %rax, %rdi
movq -0x130(%rbp), %rsi
callq 0x4ec0
jmp 0x8d2b
jmp 0x8d2d
leaq -0x120(%rbp), %rdi
callq 0x5470
jmp 0x8c0a
leaq -0x88(%rbp), %rdi
callq 0x5560
leaq -0x30(%rbp), %rdi
callq 0x2cc0
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
leaq -0x88(%rbp), %rdi
callq 0x5560
leaq -0x30(%rbp), %rdi
callq 0x2cc0
movq -0x58(%rbp), %rdi
callq 0x2290
nopw (%rax,%rax)