// File: /workspace/github_cpp_makefile_star2_O3/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, %ebx
movl %edx, 0xc(%rsp)
movq %rsi, 0x70(%rsp)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r15
movaps %xmm0, (%r15)
movq $0x0, 0x10(%r15)
movq (%rdi), %r12
movq 0x8(%rdi), %r13
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x4b48
movl 0x10(%r12), %eax
movl %eax, 0x10(%rsi)
movups (%r12), %xmm0
movups %xmm0, (%rsi)
addq $0x14, 0x18(%rsp)
jmp 0x4b53
movq %r15, %rdi
movq %r12, %rdx
callq 0x4076
addq $0x14, %r12
cmpq %r13, %r12
jne 0x4b24
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r12
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
cmpq %r12, %r15
je 0x4bb9
movq %r12, %rax
subq %r15, %rax
sarq $0x2, %rax
imulq %rcx, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq -0xb2d(%rip), %rcx      # 0x406a
movq %r15, %rdi
movq %r12, %rsi
callq 0x4190
leaq -0xb3f(%rip), %rdx      # 0x406a
movq %r15, %rdi
movq %r12, %rsi
callq 0x4268
movq 0x10(%rsp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
leaq 0x48(%rsp), %rdi
movl 0xc(%rsp), %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0x32e8
leaq 0x30(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x39c8
movl %ebx, 0x2c(%rsp)
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4c07
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r13
cmpq %r13, %r15
je 0x4caf
xorl %r12d, %r12d
leaq 0x48(%rsp), %rbx
xorl %ebp, %ebp
subl $0x1, %r12d
jb 0x4ca2
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 0x4d68
leaq (%rsi,%rsi,4), %rcx
leaq (%rax,%rcx,8), %rdi
movq %r15, %rsi
callq 0x3382
testb %al, %al
jne 0x4ca2
movq %rbx, %r14
movq %rbx, %rdi
movl 0xc(%rsp), %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0x32e8
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x39c8
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4ca0
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
incl %ebp
addq $0x14, %r15
cmpq %r13, %r15
jne 0x4c24
movq 0x30(%rsp), %r15
movq 0x38(%rsp), %rbp
cmpq %rbp, %r15
movl 0x2c(%rsp), %ebx
je 0x4d38
leaq 0x48(%rsp), %r12
movl %ebx, %r13d
movl (%r15), %eax
addl %r13d, %eax
cmpl %ebx, %eax
jbe 0x4d09
movq %r12, %rdi
movl 0xc(%rsp), %esi
movl %ebx, %edx
callq 0x3720
movq 0x70(%rsp), %rdi
movq %r12, %rsi
callq 0x3bee
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x4d06
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
xorl %r13d, %r13d
movq %r15, %rdi
movl %r13d, %esi
callq 0x3446
movl (%r15), %r14d
movq 0x70(%rsp), %rax
movq 0x8(%rax), %rdi
addq $-0x20, %rdi
movq %r15, %rsi
callq 0x3780
addl %r14d, %r13d
addq $0x28, %r15
cmpq %rbp, %r15
jne 0x4cca
leaq 0x30(%rsp), %rdi
callq 0x3974
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4d59
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 0x13d1(%rip), %rdi      # 0x6140
xorl %eax, %eax
callq 0x2170
jmp 0x4d7c
jmp 0x4db9
jmp 0x4db4
movq %rax, %rbx
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4dbc
movq 0x68(%rsp), %rsi
jmp 0x4da6
jmp 0x4db9
jmp 0x4db9
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x4dbc
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
jmp 0x4dbc
jmp 0x4db9
jmp 0x4db9
movq %rax, %rbx
jmp 0x4dc6
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3974
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4ddd
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq %rbx, %rdi
callq 0x21c0
nopl (%rax)