// File: /workspace/github_c_makefile_star2_O3/dasJ[P]sd-zfs/src/mount.initrd_zfs.c
// Function: main
int main(int argc, char *argv[]) {
	char *dataset = NULL;
	char *mountpoint = NULL;
	char *options = NULL;
	// For parameter parsing
	char isOption = 0;
	char *newoptions;
	size_t size;
	// For snapshotting
	int ret;
	char *snapDS = NULL;
	char *snapMP = NULL;
	char *snapBase;
	char *snapPath;
	char *snaps;
	char *snapshot;
	char *at;
	// For mounting
	char *snap = NULL;
	char *lines = NULL;
	char *endLine;
	char *lineToken;
	char *mountpointToken;
	char *what;
	char *where;
	char *where_tmp = NULL;

	// Parse parameters
	if (argc < 3) {
		fprintf(stderr, "Call like: %s <dataset> <mount_point> [-o option[,...]]\n", argv[0]);
		exit(1);
	}
	for (int i = 1; i < argc; i++) {
		// This is an option
		if (isOption == 1) {
			size = strlen(argv[i]) + 2;
			if (options != NULL) {
				size += strlen(options);
			}
			newoptions = malloc(size * sizeof(char));
			if (options != NULL) {
				strcpy(newoptions, options);
			}
			strcat(newoptions, argv[i]);
			strcat(newoptions, ",");
			free(options);

			options = newoptions;
			isOption = 0;
			continue;
		}
		// Next will be an option
		if (strcmp(argv[i], "-o") == 0) {
			isOption = 1;
			continue;
		}
		// Next is the dataset
		if (dataset == NULL) {
			dataset = malloc((strlen(argv[i]) + 1) * sizeof(char));
			strcpy(dataset, argv[i]);
			continue;
		}
		// Next is the mountpoint
		if (mountpoint == NULL) {
			mountpoint = malloc((strlen(argv[i]) + 1) * sizeof(char));
			strcpy(mountpoint, argv[i]);
			continue;
		}
		// Call is broken
		free(dataset);
		free(mountpoint);
		fprintf(stderr, "Call like: %s <dataset> <mount_point> [-o option[,...]]\n", argv[0]);
		exit(1);
	}
	// Check if all parameters are supplied
	if (dataset == NULL || mountpoint == NULL) {
		fprintf(stderr, "Call like: %s <dataset> <mount_point> [-o option[,...]]\n", argv[0]);
		exit(1);
	}
	// Get rid of the trailing comma
	if (options != NULL) {
		newoptions = malloc(sizeof(options) - sizeof(char));
		strncpy(newoptions, options, strlen(options) - 1);
		free(options);
		options = newoptions;
	}

	// Split away snapshot name
	strtok(dataset, "@");
	snap = strtok(NULL, "@");

	// Check if we need to handle bootfs
	if (strncmp(dataset, "ZFS=AUTO", strlen("ZFS=AUTO")) == 0) {
		if (handleBootfs(&dataset) != 0) {
			fprintf(stderr, "Can not get bootfs value\n");
			free(dataset);
			free(mountpoint);
			free(options);
			exit(1);
		}
	}

	// Handle snapshot
	if (snap != NULL) {
		ret = zfs_snapshot_exists(dataset, snap);
		if (ret < 0) {
			fprintf(stderr, "Error while trying to check if snapshot exists\n");
			goto aftersnap;
		} else if (ret == 1) {
			goto aftersnap;
		}
		// Build name of temporary dataset
		snapDS = malloc((strlen(dataset) + strlen(snap) + strlen("_initrd_") + 1) * sizeof(char));
		strcpy(snapDS, dataset);
		strcat(snapDS, "_initrd_");
		strcat(snapDS, snap);
		// Remove temporary datasets if they exist
		ret = zfs_ds_exists(snapDS);
		if (ret < 0) {
			fprintf(stderr, "Error while trying to check if temporary dataset exists\n");
			goto aftersnap;
		} else if (ret == 0) {
			if (zfs_destroy_recursively(snapDS) != 0) {
				fprintf(stderr, "Failed to remove existing datasets for snapshotting\n");
				goto aftersnap;
			}
		}
		// Create new datasets
		ret = zfs_list_snapshots(dataset, snap, &snaps);
		if (ret != 0) {
			fprintf(stderr, "Error while trying to list snapshots\n");
			goto aftersnap;
		}
		snapshot = strtok(snaps, "\n");
		while (snapshot != NULL) {
			snapPath = malloc((strlen(snapDS) + strlen(&snapshot[strlen(dataset)]) + 1) * sizeof(char));
			strcpy(snapPath, snapDS);
			strcat(snapPath, &(snapshot[strlen(dataset)]));
			at = strrchr(snapPath, '@');
			if (at != 0) {
				*at = '\0';
			}
			snapBase = malloc((strlen(snapshot) + 1) * sizeof(char));
			strcpy(snapBase, snapshot);
			at = strrchr(snapBase, '@');
			if (at != 0) {
				*at = '\0';
			}
			snapMP = NULL;
			if (zfs_get_mountpoint(snapBase, &snapMP) != 0) {
				fprintf(stderr, "Can not get mountpoint of snapshot\n");
				goto aftersnap;
			}

			if (strcmp(snapMP, "/") == 0) {
				free(dataset);
				dataset = malloc((strlen(snapPath) + 1) * sizeof(char));
				strcpy(dataset, snapPath);
			}

			if (zfs_clone_snap(snapshot, snapPath, snapMP) != 0) {
				fprintf(stderr, "Can not clone snapshot to dataset\n");
				free(snapPath);
				goto aftersnap;
			}
			free(snapPath);

			snapshot = strtok(NULL, "\n");
		}
	}
aftersnap:
	free(snapDS);

	// Mount the dataset(s)
	ret = zfs_list_datasets_with_mp(dataset, &lines);
	if (ret != 0) {
		free(dataset);
		free(mountpoint);
		free(options);
		exit(1);
	}

	lineToken = strtok_r(lines, "\n", &endLine);
	while (lineToken != NULL) {
		what = strtok_r(lineToken, "\t", &mountpointToken);
		// Do not mount what we don't need to mount
		if (strcmp(mountpointToken, "-") == 0 || strcmp(mountpointToken, "legacy") == 0 || strcmp(mountpointToken, "none") == 0) {
			if (zfs_get_alt_mp(what, &where_tmp) != 0) {
				goto loopend;
			}
			if (strcmp(where_tmp, "-") == 0 || strcmp(where_tmp, "legacy") == 0 || strcmp(where_tmp, "none") == 0) {
				goto loopend;
			}
		}
		// Build where to mount
		where = malloc((strlen(mountpoint) + strlen((where_tmp == NULL) ? mountpointToken : where_tmp) + 1) * sizeof(char));
		strcpy(where, mountpoint);
		strcat(where, (where_tmp == NULL) ? mountpointToken : where_tmp);
		// Mount
		ret = zfs_mount(what, where, options);

		free(where_tmp);
		where_tmp = NULL;

		if (ret != 0) {
			fprintf(stderr, "ZFS command failed with exit code %d, bailing out\n", ret);
			free(where);
			break;
		}
		free(where);

loopend:
		lineToken = strtok_r(NULL, "\n", &endLine);
	}

	free(lines);
	free(dataset);
	free(mountpoint);
	free(options);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
xorl %eax, %eax
movq %rax, -0x30(%rbp)
movq %rax, -0x58(%rbp)
movq %rax, -0x70(%rbp)
movq %rax, -0x50(%rbp)
cmpl $0x3, %edi
jl 0x401f5f
movl %edi, %r12d
movl $0x1, %r14d
movq $0x0, -0x40(%rbp)
movq $0x0, -0x60(%rbp)
xorl %r15d, %r15d
movq $0x0, -0x48(%rbp)
movq %r12, -0x38(%rbp)
movq (%rbx,%r14,8), %r13
cmpb $0x1, %r15b
jne 0x4019d9
movq %rbx, %r12
movq %r13, %rdi
callq 0x4010e0
movq %rax, %rbx
addq $0x2, %rbx
movq -0x60(%rbp), %r15
testq %r15, %r15
je 0x401a1d
movq %r15, %rdi
callq 0x4010e0
addq %rax, %rbx
movq %rbx, %rdi
callq 0x420f50
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x401020
jmp 0x401a28
cmpb $0x2d, (%r13)
jne 0x4019f2
cmpb $0x6f, 0x1(%r13)
jne 0x4019f2
cmpb $0x0, 0x2(%r13)
je 0x401a8b
cmpq $0x0, -0x48(%rbp)
je 0x401a59
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
jne 0x401f53
movq %r13, %rdi
callq 0x4010e0
leaq 0x1(%rax), %rdi
callq 0x420f50
movq %rax, -0x40(%rbp)
jmp 0x401a72
movq %rbx, %rdi
callq 0x420f50
movq %rax, %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x401058
movq %rbx, %rdi
callq 0x4010e0
movw $0x2c, (%rbx,%rax)
movq %r15, %rdi
callq 0x421290
xorl %r15d, %r15d
movq %rbx, -0x60(%rbp)
movq %r12, %rbx
movq -0x38(%rbp), %r12
jmp 0x401a7d
movq %r13, %rdi
callq 0x4010e0
leaq 0x1(%rax), %rdi
callq 0x420f50
movq %rax, -0x30(%rbp)
movq %rax, -0x48(%rbp)
movq %rax, %rdi
movq %r13, %rsi
callq 0x401020
incq %r14
cmpq %r14, %r12
jne 0x401991
jmp 0x401a90
movb $0x1, %r15b
jmp 0x401a7d
cmpq $0x0, -0x48(%rbp)
movq -0x40(%rbp), %r15
je 0x401f67
testq %r15, %r15
je 0x401f67
movq -0x60(%rbp), %r14
testq %r14, %r14
je 0x401ae3
movl $0x7, %edi
callq 0x420f50
movq %rax, %rbx
movq %r14, %rdi
callq 0x4010e0
leaq -0x1(%rax), %rdx
movq %rbx, -0x38(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x401088
movq %r14, %rdi
callq 0x421290
jmp 0x401aeb
movq $0x0, -0x38(%rbp)
leaq 0xa2555(%rip), %rbx     # 0x4a4047
movq -0x48(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x424170
xorl %edi, %edi
movq %rbx, %rsi
callq 0x424170
movq %rax, %r14
leaq 0xa2534(%rip), %rsi     # 0x4a4049
movl $0x8, %edx
movq %r12, %rdi
callq 0x401080
testl %eax, %eax
jne 0x401b37
leaq -0x30(%rbp), %rdi
callq 0x4018c8
testl %eax, %eax
jne 0x401f94
testq %r14, %r14
je 0x401b59
movq -0x30(%rbp), %rdi
movq %r14, %rsi
callq 0x4022ee
testl %eax, %eax
js 0x401eef
cmpl $0x1, %eax
jne 0x401d1b
xorl %r15d, %r15d
movq %r15, %rdi
callq 0x421290
movq -0x30(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x402564
testl %eax, %eax
jne 0x401f6f
movq -0x70(%rbp), %rdi
leaq 0xa2d26(%rip), %rsi     # 0x4a48aa
leaq -0x78(%rbp), %rdx
callq 0x424180
testq %rax, %rax
je 0x401ce6
leaq 0xa25e3(%rip), %r14     # 0x4a4180
leaq -0x68(%rbp), %r15
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x424180
movq %rax, %r12
movq -0x68(%rbp), %r13
cmpb $0x2d, (%r13)
jne 0x401bc4
cmpb $0x0, 0x1(%r13)
je 0x401bf0
movq %r13, %rdi
leaq 0xa25b4(%rip), %rsi     # 0x4a4182
callq 0x4010b8
testl %eax, %eax
je 0x401bf0
movq %r13, %rdi
leaq 0xa2739(%rip), %rsi     # 0x4a431a
callq 0x4010b8
testl %eax, %eax
je 0x401bf0
movq -0x50(%rbp), %rbx
jmp 0x401c49
movq %r12, %rdi
leaq -0x50(%rbp), %rsi
callq 0x4029f4
testl %eax, %eax
jne 0x401ccb
movq -0x50(%rbp), %rbx
cmpb $0x2d, (%rbx)
jne 0x401c17
cmpb $0x0, 0x1(%rbx)
je 0x401ccb
movq %rbx, %rdi
leaq 0xa2561(%rip), %rsi     # 0x4a4182
callq 0x4010b8
testl %eax, %eax
je 0x401ccb
movq %rbx, %rdi
leaq 0xa26e2(%rip), %rsi     # 0x4a431a
callq 0x4010b8
testl %eax, %eax
je 0x401ccb
movq -0x68(%rbp), %r13
movq -0x40(%rbp), %r15
movq %r15, %rdi
callq 0x4010e0
movq %rax, %r14
testq %rbx, %rbx
cmovneq %rbx, %r13
movq %r13, %rdi
callq 0x4010e0
leaq (%r14,%rax), %rdi
incq %rdi
callq 0x420f50
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x401020
movq %rbx, %rdi
movq %r13, %rsi
callq 0x401058
movq %r12, %rdi
movq %rbx, %rsi
movq -0x38(%rbp), %rdx
callq 0x4027c8
movl %eax, %r12d
movq -0x50(%rbp), %rdi
callq 0x421290
movq $0x0, -0x50(%rbp)
testl %r12d, %r12d
jne 0x401f0d
movq %rbx, %rdi
callq 0x421290
leaq 0xa24b9(%rip), %r14     # 0x4a4180
leaq -0x68(%rbp), %r15
xorl %edi, %edi
leaq 0xa2bd6(%rip), %rsi     # 0x4a48aa
leaq -0x78(%rbp), %rdx
callq 0x424180
testq %rax, %rax
jne 0x401ba1
movq -0x70(%rbp), %rdi
callq 0x421290
movq -0x30(%rbp), %rdi
callq 0x421290
movq -0x40(%rbp), %rdi
callq 0x421290
movq -0x38(%rbp), %rdi
callq 0x421290
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
callq 0x4010e0
movq %rax, %r15
movq %r14, %rdi
callq 0x4010e0
leaq (%r15,%rax), %rdi
addq $0x9, %rdi
callq 0x420f50
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x401020
movq %r15, %rdi
callq 0x4010e0
movabsq $0x5f647274696e695f, %rcx # imm = 0x5F647274696E695F
movq %rcx, (%r15,%rax)
movb $0x0, 0x8(%r15,%rax)
movq %r15, %rdi
movq %r14, %rsi
callq 0x401058
movq %r15, %rdi
callq 0x4023af
testl %eax, %eax
js 0x401ef9
jne 0x401d96
movq %r15, %rdi
callq 0x4021b6
testl %eax, %eax
jne 0x401f49
movq -0x30(%rbp), %rdi
leaq -0x80(%rbp), %rdx
movq %r14, %rsi
callq 0x402609
testl %eax, %eax
jne 0x401f03
movq -0x80(%rbp), %rdi
leaq 0xa2af1(%rip), %rsi     # 0x4a48aa
callq 0x424170
testq %rax, %rax
je 0x401b5c
movq %rax, %r14
movq %r15, %rdi
callq 0x4010e0
movq %rax, %rbx
movq -0x30(%rbp), %r13
movq %r13, %rdi
callq 0x4010e0
leaq (%rax,%r14), %rdi
callq 0x4010e0
leaq (%rbx,%rax), %rdi
incq %rdi
callq 0x420f50
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
callq 0x401020
movq %r13, %rdi
callq 0x4010e0
leaq (%rax,%r14), %rsi
movq %r12, %rdi
callq 0x401058
movq %r12, %rdi
movl $0x40, %esi
callq 0x401050
testq %rax, %rax
je 0x401e2d
movb $0x0, (%rax)
movq %r14, %rdi
callq 0x4010e0
leaq 0x1(%rax), %rdi
callq 0x420f50
movq %rax, %rbx
movq %rax, %rdi
movq %r14, %rsi
callq 0x401020
movq %rbx, %rdi
movl $0x40, %esi
callq 0x401050
testq %rax, %rax
je 0x401e61
movb $0x0, (%rax)
movq $0x0, -0x58(%rbp)
movq %rbx, %rdi
leaq -0x58(%rbp), %rsi
callq 0x40285c
testl %eax, %eax
jne 0x401f35
movq -0x58(%rbp), %rdx
cmpb $0x2f, (%rdx)
jne 0x401eb9
cmpb $0x0, 0x1(%rdx)
jne 0x401eb9
movq -0x30(%rbp), %rdi
callq 0x421290
movq %r12, %rdi
callq 0x4010e0
leaq 0x1(%rax), %rdi
callq 0x420f50
movq %rax, -0x30(%rbp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x401020
movq -0x58(%rbp), %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x402909
movq %r12, %rdi
testl %eax, %eax
jne 0x401f3f
callq 0x421290
xorl %edi, %edi
leaq 0xa29d1(%rip), %rsi     # 0x4a48aa
callq 0x424170
movq %rax, %r14
testq %rax, %rax
jne 0x401dca
jmp 0x401b5c
callq 0x401248
jmp 0x401b59
callq 0x401226
jmp 0x401b5c
callq 0x4011ae
jmp 0x401b5c
movq $0x4d4708, %rax         # imm = 0x4D4708
movq (%rax), %rdi
leaq 0xa226b(%rip), %rsi     # 0x4a4189
movl %r12d, %edx
xorl %eax, %eax
callq 0x40c900
movq %rbx, %rdi
callq 0x421290
jmp 0x401ce6
callq 0x4011d0
jmp 0x401b5c
callq 0x4011f2
jmp 0x401b5c
callq 0x40118c
jmp 0x401b5c
movq -0x48(%rbp), %rdi
movq %rbx, %rdx
callq 0x401100
movq %rbx, %rdi
callq 0x401294
movq %rbx, %rdi
callq 0x40126a
movq -0x30(%rbp), %rdi
callq 0x421290
movq -0x40(%rbp), %rdi
callq 0x421290
movq -0x38(%rbp), %rdi
callq 0x421290
movl $0x1, %edi
callq 0x40bdd0
leaq -0x30(%rbp), %rdi
movq %r15, %rsi
movq -0x38(%rbp), %rdx
callq 0x40113d