// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %ecx, %ebp
movl %edx, 0x8(%rsp)
movq %rsi, %r14
leaq 0x30(%rsp), %rbx
andq $0x0, 0x10(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
movq (%rdi), %r12
movq 0x8(%rdi), %r13
cmpq %r13, %r12
je 0x452b
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3998
addq $0x14, %r12
jmp 0x4515
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rsi
leaq -0xbb0(%rip), %rdx      # 0x398c
callq 0x3a92
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq 0x30(%rsp), %rcx
leaq 0x60(%rsp), %rdi
movl 0x8(%rsp), %esi
xorl %edx, %edx
callq 0x2e3c
movq %r14, 0x50(%rsp)
movl %ebp, 0xc(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x33ac
leaq 0x70(%rsp), %rdi
callq 0x2774
movq 0x30(%rsp), %r12
movq 0x38(%rsp), %rbx
xorl %ebp, %ebp
leaq 0x60(%rsp), %r15
xorl %r13d, %r13d
cmpq %rbx, %r12
je 0x45f4
subl $0x1, %ebp
jb 0x45ee
movl %r13d, %esi
leaq 0x10(%rsp), %rdi
callq 0x46e4
movq %rax, %rdi
movq %r12, %rsi
callq 0x2ea0
testb %al, %al
jne 0x45ee
movq %r15, %r14
movq %r15, %rdi
movl 0x8(%rsp), %esi
xorl %edx, %edx
movq %r12, %rcx
callq 0x2e3c
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x33ac
leaq 0x70(%rsp), %rdi
callq 0x2774
incl %r13d
addq $0x14, %r12
jmp 0x459a
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x60(%rsp), %rbp
movl 0xc(%rsp), %r14d
movl %r14d, %ebx
movq 0x50(%rsp), %r15
cmpq 0x58(%rsp), %r12
je 0x4674
movl (%r12), %eax
addl %ebx, %eax
cmpl %r14d, %eax
jbe 0x464d
movq %rbp, %rdi
movl 0x8(%rsp), %esi
movl %r14d, %edx
callq 0x3140
movq %r15, %rdi
movq %rbp, %rsi
callq 0x35e0
leaq 0x68(%rsp), %rdi
callq 0x2774
xorl %ebx, %ebx
movq %r12, %rdi
movl %ebx, %esi
callq 0x2f4e
movl (%r12), %r13d
movq 0x8(%r15), %rdi
addq $-0x20, %rdi
movq %r12, %rsi
callq 0x3180
addl %r13d, %ebx
addq $0x28, %r12
jmp 0x4615
leaq 0x10(%rsp), %rdi
callq 0x3328
leaq 0x30(%rsp), %rdi
callq 0x2774
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x46a0
jmp 0x46c4
jmp 0x46bf
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x46b6
jmp 0x46c4
jmp 0x46c4
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x2774
jmp 0x46c7
jmp 0x46c4
movq %rax, %rbx
jmp 0x46d1
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3328
leaq 0x30(%rsp), %rdi
callq 0x2774
movq %rbx, %rdi
callq 0x21e0
nop