// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movl %ecx, 0xc(%rsp)
movl %edx, 0x8(%rsp)
movq %rsi, 0x70(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq (%rdi), %r15
movq 0x8(%rdi), %r13
cmpq %r13, %r15
je 0x4adf
leaq 0x10(%rsp), %r12
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x4acb
movl 0x10(%r15), %eax
movl %eax, 0x10(%rsi)
movups (%r15), %xmm0
movups %xmm0, (%rsi)
addq $0x14, 0x18(%rsp)
jmp 0x4ad6
movq %r12, %rdi
movq %r15, %rdx
callq 0x3fde
addq $0x14, %r15
cmpq %r13, %r15
jne 0x4aa9
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r12
cmpq %r12, %r15
je 0x4b37
movq %r12, %rax
subq %r15, %rax
sarq $0x2, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq -0xb48(%rip), %rcx      # 0x3fd2
movq %r15, %rdi
movq %r12, %rsi
callq 0x40f8
leaq -0xb5a(%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), %rcx
leaq 0x48(%rsp), %rdi
movl 0x8(%rsp), %esi
xorl %edx, %edx
callq 0x32d0
leaq 0x30(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x38d6
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4b83
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r13
cmpq %r13, %r15
je 0x4c31
xorl %r12d, %r12d
leaq 0x48(%rsp), %r14
xorl %ebp, %ebp
subl $0x1, %r12d
jb 0x4c24
movl %ebp, %esi
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rdx
cmpq %rsi, %rdx
jbe 0x4cec
movq %r14, %rbx
leaq (%rsi,%rsi,4), %rcx
leaq (%rax,%rcx,8), %rdi
movq %r15, %rsi
callq 0x336a
testb %al, %al
movq %rbx, %r14
jne 0x4c24
movq %r14, %rdi
movl 0x8(%rsp), %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0x32d0
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x38d6
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4c1f
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
incl %ebp
movq %rbx, %r14
addq $0x14, %r15
cmpq %r13, %r15
jne 0x4ba0
movq 0x30(%rsp), %r15
movq 0x38(%rsp), %rbp
cmpq %rbp, %r15
je 0x4cbc
leaq 0x48(%rsp), %r12
movl 0xc(%rsp), %r13d
movl (%r15), %eax
addl %r13d, %eax
cmpl 0xc(%rsp), %eax
jbe 0x4c8d
movq %r12, %rdi
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %edx
callq 0x360c
movq 0x70(%rsp), %rdi
movq %r12, %rsi
callq 0x3b16
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x4c8a
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
xorl %r13d, %r13d
movq %r15, %rdi
movl %r13d, %esi
callq 0x342e
movl (%r15), %r14d
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rdi
addq $-0x20, %rdi
movq %r15, %rsi
callq 0x366c
addl %r14d, %r13d
addq $0x28, %r15
cmpq %rbp, %r15
jne 0x4c4a
leaq 0x30(%rsp), %rdi
callq 0x3888
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4cdd
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
leaq 0x1406(%rip), %rdi      # 0x60f9
xorl %eax, %eax
callq 0x2170
jmp 0x4d00
jmp 0x4d3d
jmp 0x4d16
movq %rax, %rbx
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4d40
movq 0x68(%rsp), %rsi
jmp 0x4d2f
jmp 0x4d3d
movq %rax, %rbx
jmp 0x4d4a
jmp 0x4d3d
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x4d40
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
jmp 0x4d40
jmp 0x4d3d
jmp 0x4d3d
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3888
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4d61
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq %rbx, %rdi
callq 0x21c0
nopl (%rax)