// File: /workspace/github_c_makefile_star2_O0/j-hoppe[P]tu58fs/xxdp.c
// Function: xxdp_filesystem_layout
static int xxdp_filesystem_layout(xxdp_filesystem_t *_this) {
	int file_idx ;
	unsigned i, j, n;
	int overflow;
	xxdp_blocknr_t blknr;

	// mark preallocated blocks in bitmap
	// this covers boot, monitor, bitmap, mfd and default sized ufd
	memset(_this->bitmap->used, 0, sizeof(_this->bitmap->used));
	for (i = 0; i < _this->preallocated_blockcount; i++)
		_this->bitmap->used[i] = 1;

	// BOOT
	if (_this->bootblock->data_size) {
		_this->bootblock->blockcount = 1;
		blknr = _this->radi.boot_block; // set start
		_this->bootblock->blocknr = blknr;
	} else
		_this->bootblock->blockcount = 0;

	// MONITOR
	if (_this->monitor->data_size) {
		n = NEEDED_BLOCKS(XXDP_BLOCKSIZE, _this->monitor->data_size);
		blknr = _this->radi.monitor_block; // set start
		_this->monitor->blocknr = blknr;
		_this->monitor->blockcount = n;
	} else
		_this->monitor->blockcount = 0;

	// BITMAP
	n = _this->radi.bitmaps_num;
	blknr = _this->radi.bitmap_block_1; // set start
	for (i = 0; i < n; i++) { // enumerate sequential
		_this->bitmap->used[blknr] = 1;
		_this->bitmap->blocklist.blocknr[i] = blknr++;
	}
	_this->bitmap->blocklist.count = n;

	// MFD
	if (_this->mfd_variety == 1) {
		_this->mfd_blocklist->count = 2;
		_this->mfd_blocklist->blocknr[0] = _this->radi.mfd1;
		_this->mfd_blocklist->blocknr[1] = _this->radi.mfd2;
		_this->bitmap->used[_this->radi.mfd1] = 1;
		_this->bitmap->used[_this->radi.mfd2] = 1;
	} else if (_this->mfd_variety == 2) {
		_this->mfd_blocklist->count = 1;
		_this->mfd_blocklist->blocknr[0] = _this->radi.mfd1;
		_this->bitmap->used[_this->radi.mfd1] = 1;
	} else
	fatal("MFD variety must be 1 or 2");
	// UFD
	// starts in preallocated area, may extend into freespace
	n = NEEDED_BLOCKS(XXDP_UFD_ENTRIES_PER_BLOCK, _this->file_count);
	if (n < _this->radi.ufd_blocks_num)
		n = _this->radi.ufd_blocks_num; // RADI defines minimum
	_this->ufd_blocklist->count = n;
	// last UFD half filled
	blknr = _this->radi.ufd_block_1;	// start
	i = 0;
	// 1) fill UFD into preallocated space
	while (i < n && i < _this->radi.ufd_blocks_num) {
		_this->bitmap->used[blknr] = 1;
		_this->ufd_blocklist->blocknr[i++] = blknr++;
	}
	// 2) continue in free space, if larger than RADI defines
	blknr = _this->preallocated_blockcount; // 1st in free space
	while (i < n) {
		_this->bitmap->used[blknr] = 1;
		_this->ufd_blocklist->blocknr[i++] = blknr++;
	}
	// blknr now 1st block behind UFD.

	// FILES

	// files start in free space
	if (blknr < _this->preallocated_blockcount)
		blknr = _this->preallocated_blockcount;

	overflow = 0;
	for (file_idx = 0; !overflow && file_idx < _this->file_count; file_idx++) {
		xxdp_file_t *f = _this->file[file_idx];
		// amount of 510 byte blocks
		n = NEEDED_BLOCKS(XXDP_BLOCKSIZE-2, f->data_size);
// fprintf(stderr,"layout(): file %d %s.%s start from %d, needs %d blocks\n", i, f->filnam, f->ext, blknr, n);
		for (j = 0; !overflow && j < n; j++) {
			if (j >= sizeof(f->blocklist.blocknr)) {
 			error_set(ERROR_FILESYSTEM_OVERFLOW, "File %s.%s too large, uses more than %d blocks", f->filnam,
						f->ext, (int) sizeof(f->blocklist.blocknr));
				overflow = 1;
			} else if (blknr >= _this->blockcount) {
				error_set(ERROR_FILESYSTEM_OVERFLOW, "File system overflow, can hold max %d blocks.",
						_this->blockcount);
				overflow = 1;
			} else {
				_this->bitmap->used[blknr] = 1;
				f->blocklist.blocknr[j] = blknr++;
			}
		}
		f->block_count = n;
		f->blocklist.count = n;
		if (overflow)
			return ERROR_FILESYSTEM_OVERFLOW;
	}

	// expand file system size if needed.
	// later check wether physical image can be expanded.
	if (blknr >= _this->blockcount)
		return ERROR_FILESYSTEM_OVERFLOW;

	return ERROR_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rdi
addq $0x804, %rdi            # imm = 0x804
xorl %esi, %esi
movl $0x10000, %edx          # imm = 0x10000
callq 0x3220
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl 0x50(%rcx), %ecx
cmpl %ecx, %eax
jae 0x10cd1
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movl -0x18(%rbp), %ecx
movb $0x1, 0x804(%rax,%rcx)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x10ca1
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
cmpl $0x0, 0x10(%rax)
je 0x10d13
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movw $0x1, 0x2(%rax)
movq -0x10(%rbp), %rax
movw 0x48(%rax), %ax
movw %ax, -0x26(%rbp)
movw -0x26(%rbp), %cx
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movw %cx, (%rax)
jmp 0x10d24
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movw $0x0, 0x2(%rax)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
cmpl $0x0, 0x10(%rax)
je 0x10d86
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movl 0x10(%rax), %eax
addl $0x200, %eax            # imm = 0x200
subl $0x1, %eax
shrl $0x9, %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movw 0x4a(%rax), %ax
movw %ax, -0x26(%rbp)
movw -0x26(%rbp), %cx
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movw %cx, (%rax)
movl -0x20(%rbp), %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movw %cx, 0x2(%rax)
jmp 0x10d97
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movw $0x0, 0x2(%rax)
movq -0x10(%rbp), %rax
movzwl 0x32(%rax), %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movw 0x30(%rax), %ax
movw %ax, -0x26(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x20(%rbp), %eax
jae 0x10e01
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movzwl -0x26(%rbp), %ecx
movb $0x1, 0x804(%rax,%rcx)
movw -0x26(%rbp), %dx
movw %dx, %ax
addw $0x1, %ax
movw %ax, -0x26(%rbp)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movl -0x18(%rbp), %ecx
movw %dx, 0x4(%rax,%rcx,2)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x10db5
movl -0x20(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
cmpl $0x1, 0x58(%rax)
jne 0x10e96
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movl $0x2, (%rax)
movq -0x10(%rbp), %rax
movl 0x34(%rax), %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movw %cx, 0x4(%rax)
movq -0x10(%rbp), %rax
movl 0x38(%rax), %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movw %cx, 0x6(%rax)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x34(%rcx), %rcx
movb $0x1, 0x804(%rax,%rcx)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x38(%rcx), %rcx
movb $0x1, 0x804(%rax,%rcx)
jmp 0x10ef7
movq -0x10(%rbp), %rax
cmpl $0x2, 0x58(%rax)
jne 0x10ee7
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movl $0x1, (%rax)
movq -0x10(%rbp), %rax
movl 0x34(%rax), %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movq 0x98(%rax), %rax
movw %cx, 0x4(%rax)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x10(%rbp), %rcx
movslq 0x34(%rcx), %rcx
movb $0x1, 0x804(%rax,%rcx)
jmp 0x10ef5
leaq 0xc297(%rip), %rdi      # 0x1d185
movb $0x0, %al
callq 0xd140
jmp 0x10ef7
movq -0x10(%rbp), %rax
movl 0xa8(%rax), %eax
addl $0x1c, %eax
subl $0x1, %eax
movl $0x1c, %ecx
cltd
idivl %ecx
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl 0x2e(%rcx), %ecx
cmpl %ecx, %eax
jae 0x10f2c
movq -0x10(%rbp), %rax
movzwl 0x2e(%rax), %eax
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movw 0x2c(%rax), %ax
movw %ax, -0x26(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %ecx
xorl %eax, %eax
cmpl -0x20(%rbp), %ecx
movb %al, -0x31(%rbp)
jae 0x10f6f
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl 0x2e(%rcx), %ecx
cmpl %ecx, %eax
setb %al
movb %al, -0x31(%rbp)
movb -0x31(%rbp), %al
testb $0x1, %al
jne 0x10f78
jmp 0x10fbd
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movzwl -0x26(%rbp), %ecx
movb $0x1, 0x804(%rax,%rcx)
movw -0x26(%rbp), %dx
movw %dx, %ax
addw $0x1, %ax
movw %ax, -0x26(%rbp)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movl -0x18(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x18(%rbp)
movl %ecx, %ecx
movw %dx, 0x4(%rax,%rcx,2)
jmp 0x10f4f
movq -0x10(%rbp), %rax
movw 0x50(%rax), %ax
movw %ax, -0x26(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x20(%rbp), %eax
jae 0x11016
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movzwl -0x26(%rbp), %ecx
movb $0x1, 0x804(%rax,%rcx)
movw -0x26(%rbp), %dx
movw %dx, %ax
addw $0x1, %ax
movw %ax, -0x26(%rbp)
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rax
movl -0x18(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x18(%rbp)
movl %ecx, %ecx
movw %dx, 0x4(%rax,%rcx,2)
jmp 0x10fc9
movzwl -0x26(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl 0x50(%rcx), %ecx
cmpl %ecx, %eax
jge 0x11032
movq -0x10(%rbp), %rax
movw 0x50(%rax), %ax
movw %ax, -0x26(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x14(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x24(%rbp)
movb %al, -0x32(%rbp)
jne 0x1105e
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xa8(%rcx), %eax
setl %al
movb %al, -0x32(%rbp)
movb -0x32(%rbp), %al
testb $0x1, %al
jne 0x1106a
jmp 0x111b6
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq 0xb0(%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x884(%rax), %eax
addl $0x1fe, %eax            # imm = 0x1FE
subl $0x1, %eax
movl $0x1fe, %ecx            # imm = 0x1FE
xorl %edx, %edx
divl %ecx
movl %eax, -0x20(%rbp)
movl $0x0, -0x1c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x24(%rbp)
movb %al, -0x33(%rbp)
jne 0x110ba
movl -0x1c(%rbp), %eax
cmpl -0x20(%rbp), %eax
setb %al
movb %al, -0x33(%rbp)
movb -0x33(%rbp), %al
testb $0x1, %al
jne 0x110c6
jmp 0x1117d
movl -0x1c(%rbp), %eax
cmpq $0x800, %rax            # imm = 0x800
jb 0x11109
movq -0x30(%rbp), %rdx
addq $0x808, %rdx            # imm = 0x808
movq -0x30(%rbp), %rcx
addq $0x858, %rcx            # imm = 0x858
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
leaq 0xc0ad(%rip), %rsi      # 0x1d1a0
movl $0x800, %r8d            # imm = 0x800
movb $0x0, %al
callq 0xcd70
movl $0x1, -0x24(%rbp)
jmp 0x1116d
movzwl -0x26(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4c(%rcx), %eax
jb 0x11139
movq -0x10(%rbp), %rax
movl 0x4c(%rax), %edx
movl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
leaq 0xc0a6(%rip), %rsi      # 0x1d1cf
movb $0x0, %al
callq 0xcd70
movl $0x1, -0x24(%rbp)
jmp 0x1116b
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movzwl -0x26(%rbp), %ecx
movb $0x1, 0x804(%rax,%rcx)
movw -0x26(%rbp), %dx
movw %dx, %ax
addw $0x1, %ax
movw %ax, -0x26(%rbp)
movq -0x30(%rbp), %rax
movl -0x1c(%rbp), %ecx
movw %dx, 0x4(%rax,%rcx,2)
jmp 0x1116d
jmp 0x1116f
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x110a3
movl -0x20(%rbp), %eax
movw %ax, %cx
movq -0x30(%rbp), %rax
movw %cx, 0x880(%rax)
movl -0x20(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
cmpl $0x0, -0x24(%rbp)
je 0x111a6
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x111d3
jmp 0x111a8
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x11040
movzwl -0x26(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4c(%rcx), %eax
jb 0x111cc
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x111d3
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)