// File: /workspace/github_cpp_makefile_star2_O3/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
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 0x4e40
movl 0x10(%r12), %eax
movl %eax, 0x10(%rsi)
movups (%r12), %xmm0
movups %xmm0, (%rsi)
addq $0x14, 0x18(%rsp)
jmp 0x4e4b
movq %r15, %rdi
movq %r12, %rdx
callq 0x4076
addq $0x14, %r12
cmpq %r13, %r12
jne 0x4e1c
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r12
cmpq %r12, %r15
je 0x4eb1
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 -0xe25(%rip), %rcx      # 0x406a
movq %r15, %rdi
movq %r12, %rsi
callq 0x4190
leaq -0xe37(%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
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4efb
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r13
cmpq %r13, %r15
je 0x4fef
movabsq $-0x3333333333333333, %rbp # imm = 0xCCCCCCCCCCCCCCCD
leaq 0x48(%rsp), %r12
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rdx
cmpq %rdx, %rax
je 0x4f77
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 0x4f3d
movslq %r8d, %rsi
jmp 0x4f79
xorl %esi, %esi
subq %rax, %rdx
sarq $0x3, %rdx
imulq %rbp, %rdx
cmpq %rsi, %rdx
jbe 0x50a4
leaq (%rsi,%rsi,4), %rcx
movl 0xc(%rax,%rcx,8), %edx
subl 0x8(%rax,%rcx,8), %edx
cmpl 0x8(%r15), %edx
jae 0x4fd6
movq %r12, %rdi
movl 0xc(%rsp), %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0x32e8
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x39c8
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4fe2
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
jmp 0x4fe2
leaq (%rax,%rcx,8), %rdi
movq %r15, %rsi
callq 0x3382
addq $0x14, %r15
cmpq %r13, %r15
jne 0x4f1d
movq 0x30(%rsp), %r15
movq 0x38(%rsp), %rbp
cmpq %rbp, %r15
je 0x5074
leaq 0x48(%rsp), %r12
movl %ebx, %r13d
movl (%r15), %eax
addl %r13d, %eax
cmpl %ebx, %eax
jbe 0x5045
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 0x5042
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 0x5006
leaq 0x30(%rsp), %rdi
callq 0x3974
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5095
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 0x1095(%rip), %rdi      # 0x6140
xorl %eax, %eax
callq 0x2170
jmp 0x50d2
jmp 0x50f5
jmp 0x50ee
jmp 0x50f5
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x50f8
movq 0x60(%rsp), %rsi
jmp 0x50e4
jmp 0x50f5
jmp 0x50f5
movq %rax, %rbx
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x50f8
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
jmp 0x50f8
movq %rax, %rbx
jmp 0x5102
jmp 0x50f5
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3974
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5119
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq %rbx, %rdi
callq 0x21c0