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

	int i = 0;
	int currSize = 0;
	int minShelf = 0;
	int minShelfSize = std::numeric_limits<int>::max();

	// On cherche, pour chaque élément, l'étage qui laissera le moins d'espace horizontal lorsqu'on l'ajoutera
	for (const Rectangle &rect : sortedRects) {
		for (auto & shelf : shelves) {
			// On calcul l'espace horizontal restant de l'étage courant après insertion de l'élément
			currSize = shelf.getEmptySpace() - rect.getW();

			// Si l'espace horizontal est insuffisant, on avance dans le for
			if (currSize < 0) {
				i++;
				continue;
			}

			// Sinon, on regarde si l'espace de l'étage courant et inférieur à l'espace trouvé plus tôt dans la boucle
			if (minShelfSize > currSize) {
				minShelf = i;
				minShelfSize = currSize;
			}

			i++;
		}

		// Si pas de place, on insère un nouvel étage
		if (shelves.at(minShelf).getEmptySpace() < rect.getW()) {
			shelves.push_back(Shelf(bw, 0, rect));
		}
		// Sinon insertion de l'élément
		else {
			shelves.at(minShelf).putRect(rect);
		}

		// Réinitialisation de variable
		i = 0;
		minShelf = 0;
		minShelfSize = std::numeric_limits<int>::max();
	}

	// 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
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq (%rdi), %r15
movq 0x8(%rdi), %r13
cmpq %r13, %r15
je 0x4ddd
leaq 0x10(%rsp), %r12
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x4dc9
movl 0x10(%r15), %eax
movl %eax, 0x10(%rsi)
movups (%r15), %xmm0
movups %xmm0, (%rsi)
addq $0x14, 0x18(%rsp)
jmp 0x4dd4
movq %r12, %rdi
movq %r15, %rdx
callq 0x3fde
addq $0x14, %r15
cmpq %r13, %r15
jne 0x4da7
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r12
cmpq %r12, %r15
je 0x4e35
movq %r12, %rax
subq %r15, %rax
sarq $0x2, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %rcx
bsrq %rcx, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq -0xe46(%rip), %rcx      # 0x3fd2
movq %r15, %rdi
movq %r12, %rsi
callq 0x40f8
leaq -0xe58(%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 0xc(%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 0x4e81
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r13
cmpq %r13, %r15
je 0x4f75
movabsq $-0x3333333333333333, %rbp # imm = 0xCCCCCCCCCCCCCCCD
leaq 0x48(%rsp), %r12
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rdx
cmpq %rdx, %rax
je 0x4efd
movl 0x8(%r15), %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
xorl %edi, %edi
xorl %r8d, %r8d
movq %rax, %r9
movl 0xc(%r9), %r10d
movl 0x8(%r9), %r11d
addl %ecx, %r11d
subl %r11d, %r10d
cmpl %r10d, %esi
movl %r10d, %r11d
cmovll %esi, %r11d
movl %r8d, %r14d
cmovgl %edi, %r8d
testl %r10d, %r10d
cmovnsl %r11d, %esi
cmovsl %r14d, %r8d
incl %edi
addq $0x28, %r9
cmpq %rdx, %r9
jne 0x4ec3
movslq %r8d, %rsi
jmp 0x4eff
xorl %esi, %esi
subq %rax, %rdx
sarq $0x3, %rdx
imulq %rbp, %rdx
cmpq %rsi, %rdx
jbe 0x502a
leaq (%rsi,%rsi,4), %rcx
movl 0xc(%rax,%rcx,8), %edx
subl 0x8(%rax,%rcx,8), %edx
cmpl 0x8(%r15), %edx
jae 0x4f5c
movq %r12, %rdi
movl 0xc(%rsp), %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0x32d0
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x38d6
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4f68
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
jmp 0x4f68
leaq (%rax,%rcx,8), %rdi
movq %r15, %rsi
callq 0x336a
addq $0x14, %r15
cmpq %r13, %r15
jne 0x4ea3
movq 0x30(%rsp), %r15
movq 0x38(%rsp), %rbp
cmpq %rbp, %r15
je 0x4ffa
leaq 0x48(%rsp), %r12
movl %ebx, %r13d
movl (%r15), %eax
addl %r13d, %eax
cmpl %ebx, %eax
jbe 0x4fcb
movq %r12, %rdi
movl 0xc(%rsp), %esi
movl %ebx, %edx
callq 0x360c
movq 0x70(%rsp), %rdi
movq %r12, %rsi
callq 0x3b16
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x4fc8
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 0x4f8c
leaq 0x30(%rsp), %rdi
callq 0x3888
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x501b
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 0x10c8(%rip), %rdi      # 0x60f9
xorl %eax, %eax
callq 0x2170
jmp 0x505d
jmp 0x507b
jmp 0x503e
movq %rax, %rbx
jmp 0x5088
jmp 0x507b
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x507e
movq 0x60(%rsp), %rsi
jmp 0x506f
jmp 0x507b
jmp 0x507b
movq %rax, %rbx
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x507e
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
jmp 0x507e
jmp 0x507b
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3888
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x509f
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq %rbx, %rdi
callq 0x21c0