// File: 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
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
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 0x8eb7
jmp 0x8ef1
leaq -0x40(%rbp), %rdi
callq 0x4490
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x6f90
jmp 0x8ed3
jmp 0x8ed5
leaq -0x40(%rbp), %rdi
callq 0x44b0
jmp 0x8ea4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x93f3
leaq -0x30(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x42d0
movq -0x178(%rbp), %rdi
movq %rax, -0x68(%rbp)
callq 0x4300
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
leaq -0x1fd4(%rip), %rdx     # 0x6f50
callq 0x7000
jmp 0x8f2b
leaq -0x88(%rbp), %rdi
callq 0x5390
movl -0x14(%rbp), %eax
movl %eax, -0x180(%rbp)
xorl %eax, %eax
movl %eax, -0x17c(%rbp)
movl %eax, %esi
leaq -0x30(%rbp), %rdi
callq 0x8d80
movl -0x180(%rbp), %esi
movl -0x17c(%rbp), %edx
movq %rax, %rcx
leaq -0xb0(%rbp), %rdi
callq 0x3f10
jmp 0x8f70
leaq -0x88(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x5490
jmp 0x8f85
leaq -0xb0(%rbp), %rdi
callq 0x2ca0
movl $0x0, -0xb4(%rbp)
movl $0x0, -0xb8(%rbp)
movl $0x0, -0xbc(%rbp)
callq 0x9410
movl %eax, -0xc0(%rbp)
leaq -0x30(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rdi
callq 0x42d0
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rdi
callq 0x4300
movq %rax, -0xd8(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x4330
testb $0x1, %al
jne 0x9007
jmp 0x9258
leaq -0xd0(%rbp), %rdi
callq 0x4370
movq %rax, -0xe0(%rbp)
leaq -0x88(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
callq 0x53b0
movq %rax, -0xf0(%rbp)
movq -0xe8(%rbp), %rdi
callq 0x53e0
movq %rax, -0xf8(%rbp)
leaq -0xf0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x5410
testb $0x1, %al
jne 0x906a
jmp 0x9147
leaq -0xf0(%rbp), %rdi
callq 0x5450
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rdi
callq 0x9420
movl %eax, -0x184(%rbp)
jmp 0x9091
movq -0xe0(%rbp), %rdi
callq 0x4240
movl %eax, -0x188(%rbp)
jmp 0x90a5
movl -0x188(%rbp), %ecx
movl -0x184(%rbp), %eax
subl %ecx, %eax
movl %eax, -0xb8(%rbp)
cmpl $0x0, -0xb8(%rbp)
jge 0x9101
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x9136
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x93e7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x2ca0
jmp 0x93e7
movl -0xc0(%rbp), %eax
cmpl -0xb8(%rbp), %eax
jle 0x9127
movl -0xb4(%rbp), %eax
movl %eax, -0xbc(%rbp)
movl -0xb8(%rbp), %eax
movl %eax, -0xc0(%rbp)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x5470
jmp 0x904e
movslq -0xbc(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x8da0
movq %rax, -0x190(%rbp)
jmp 0x9163
movq -0x190(%rbp), %rdi
callq 0x9420
movl %eax, -0x194(%rbp)
jmp 0x9177
movq -0xe0(%rbp), %rdi
callq 0x4240
movl %eax, -0x198(%rbp)
jmp 0x918b
movl -0x194(%rbp), %eax
movl -0x198(%rbp), %ecx
cmpl %ecx, %eax
jae 0x91f5
movl -0x14(%rbp), %esi
movq -0xe0(%rbp), %rcx
leaq -0x128(%rbp), %rdi
xorl %edx, %edx
callq 0x3f10
jmp 0x91b5
leaq -0x88(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x5490
jmp 0x91ca
leaq -0x128(%rbp), %rdi
callq 0x2ca0
jmp 0x9228
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x2ca0
jmp 0x93e7
movslq -0xbc(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x8da0
movq %rax, -0x1a0(%rbp)
jmp 0x9211
movq -0x1a0(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x3fc0
jmp 0x9226
jmp 0x9228
movl $0x0, -0xb4(%rbp)
movl $0x0, -0xbc(%rbp)
callq 0x9410
movl %eax, -0xc0(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x43b0
jmp 0x8feb
movl -0x18(%rbp), %eax
movl %eax, -0x12c(%rbp)
leaq -0x88(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rdi
callq 0x53b0
movq %rax, -0x140(%rbp)
movq -0x138(%rbp), %rdi
callq 0x53e0
movq %rax, -0x148(%rbp)
leaq -0x140(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x5410
testb $0x1, %al
jne 0x92b1
jmp 0x93c9
leaq -0x140(%rbp), %rdi
callq 0x5450
movq %rax, -0x150(%rbp)
movl -0x12c(%rbp), %eax
movl %eax, -0x1a8(%rbp)
movq -0x150(%rbp), %rdi
callq 0x54c0
movl %eax, -0x1a4(%rbp)
jmp 0x92e4
movl -0x1a4(%rbp), %ecx
movl -0x1a8(%rbp), %eax
addl %ecx, %eax
cmpl -0x18(%rbp), %eax
jbe 0x9360
movq -0x10(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
leaq -0x170(%rbp), %rdi
callq 0x4e40
jmp 0x9316
movq -0x1b0(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x54d0
jmp 0x932b
leaq -0x170(%rbp), %rdi
callq 0x5500
movl $0x0, -0x12c(%rbp)
jmp 0x9360
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x5500
jmp 0x93e7
movq -0x150(%rbp), %rdi
movl -0x12c(%rbp), %esi
callq 0x40e0
jmp 0x9374
movq -0x150(%rbp), %rdi
callq 0x54c0
movl %eax, -0x1b4(%rbp)
jmp 0x9388
movl -0x1b4(%rbp), %ecx
movl -0x12c(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x12c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5520
movq %rax, %rdi
movq -0x150(%rbp), %rsi
callq 0x4ec0
jmp 0x93b6
jmp 0x93b8
leaq -0x140(%rbp), %rdi
callq 0x5470
jmp 0x9295
leaq -0x88(%rbp), %rdi
callq 0x5560
leaq -0x30(%rbp), %rdi
callq 0x2cc0
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
leaq -0x88(%rbp), %rdi
callq 0x5560
leaq -0x30(%rbp), %rdi
callq 0x2cc0
movq -0x58(%rbp), %rdi
callq 0x2290
nopw %cs:(%rax,%rax)
nop