// File: /workspace/github_c_makefile_star2_O1/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, -0x38(%rbp)
xorl %eax, %eax
movq %rax, -0x30(%rbp)
movq %rax, -0x50(%rbp)
movq %rax, -0x58(%rbp)
movq %rax, -0x48(%rbp)
cmpl $0x3, %edi
jl 0x401ef8
movl %edi, %eax
movq %rax, -0x68(%rbp)
movl $0x1, %r14d
movq $0x0, -0x40(%rbp)
xorl %r15d, %r15d
xorl %ebx, %ebx
movq -0x38(%rbp), %rax
movq (%rax,%r14,8), %r12
movq %r12, %rdi
cmpb $0x1, %bl
jne 0x4019c4
callq 0x4010e0
movq %rax, %r13
addq $0x2, %r13
testq %r15, %r15
je 0x40197f
movq %r15, %rdi
callq 0x4010e0
addq %rax, %r13
movq %r13, %rdi
callq 0x420f00
movq %rax, %r13
testq %r15, %r15
je 0x40199a
movq %r13, %rdi
movq %r15, %rsi
callq 0x401020
movq %r13, %rdi
movq %r12, %rsi
callq 0x401058
movq %r13, %rdi
callq 0x4010e0
movw $0x2c, (%r13,%rax)
movq %r15, %rdi
callq 0x421240
xorl %ebx, %ebx
movq %r13, %r15
jmp 0x401a25
leaq 0xa167c(%rip), %rsi     # 0x4a3047
callq 0x4010b8
testl %eax, %eax
je 0x401a01
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x401a05
movq -0x40(%rbp), %rsi
testq %rsi, %rsi
jne 0x401eef
movq %r12, %rdi
callq 0x4010e0
leaq 0x1(%rax), %rdi
callq 0x420f00
movq %rax, -0x40(%rbp)
jmp 0x401a1a
movb $0x1, %bl
jmp 0x401a25
movq %r12, %rdi
callq 0x4010e0
leaq 0x1(%rax), %rdi
callq 0x420f00
movq %rax, -0x30(%rbp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x401020
incq %r14
cmpq %r14, -0x68(%rbp)
jne 0x401953
cmpq $0x0, -0x30(%rbp)
movq -0x40(%rbp), %r12
je 0x401f01
testq %r12, %r12
je 0x401f01
testq %r15, %r15
je 0x401a81
movl $0x7, %edi
callq 0x420f00
movq %rax, %rbx
movq %r15, %rdi
callq 0x4010e0
leaq -0x1(%rax), %rdx
movq %rbx, -0x38(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x401088
movq %r15, %rdi
callq 0x421240
jmp 0x401a89
movq $0x0, -0x38(%rbp)
movq -0x30(%rbp), %rdi
leaq 0xa15b6(%rip), %rbx     # 0x4a304a
movq %rbx, %rsi
callq 0x424120
xorl %edi, %edi
movq %rbx, %rsi
callq 0x424120
movq %rax, %rbx
movq -0x30(%rbp), %rdi
leaq 0xa1598(%rip), %rsi     # 0x4a304c
movl $0x8, %edx
callq 0x401080
testl %eax, %eax
jne 0x401ad3
leaq -0x30(%rbp), %rdi
callq 0x401898
testl %eax, %eax
jne 0x401f2e
testq %rbx, %rbx
je 0x401af5
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
callq 0x40228e
testl %eax, %eax
js 0x401e8d
cmpl $0x1, %eax
jne 0x401cad
xorl %r15d, %r15d
movq %r15, %rdi
callq 0x421240
movq -0x30(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x402512
testl %eax, %eax
jne 0x401f0a
movq -0x58(%rbp), %rdi
leaq 0xa1d8a(%rip), %rsi     # 0x4a38aa
leaq -0x70(%rbp), %rdx
callq 0x424130
testq %rax, %rax
je 0x401c79
leaq 0xa7191(%rip), %r13     # 0x4a8cca
leaq -0x70(%rbp), %rbx
movq %rax, %rdi
leaq 0xa163c(%rip), %rsi     # 0x4a3183
leaq -0x60(%rbp), %rdx
callq 0x424130
movq %rax, %r15
movq -0x60(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x4010b8
testl %eax, %eax
je 0x401b8c
movq %r14, %rdi
leaq 0xa1615(%rip), %rsi     # 0x4a3185
callq 0x4010b8
testl %eax, %eax
je 0x401b8c
movq %r14, %rdi
leaq 0xa1797(%rip), %rsi     # 0x4a331a
callq 0x4010b8
testl %eax, %eax
jne 0x401be1
movq %r15, %rdi
leaq -0x48(%rbp), %rsi
callq 0x4029a2
testl %eax, %eax
jne 0x401c5f
movq -0x48(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x4010b8
testl %eax, %eax
je 0x401c5f
movq %r14, %rdi
leaq 0xa15c4(%rip), %rsi     # 0x4a3185
callq 0x4010b8
testl %eax, %eax
je 0x401c5f
movq %r14, %rdi
leaq 0xa1742(%rip), %rsi     # 0x4a331a
callq 0x4010b8
testl %eax, %eax
je 0x401c5f
movq %r12, %rdi
callq 0x4010e0
movq %rax, %r12
movq -0x48(%rbp), %r14
testq %r14, %r14
jne 0x401bf9
movq -0x60(%rbp), %r14
movq %r14, %rdi
callq 0x4010e0
leaq (%r12,%rax), %rdi
incq %rdi
callq 0x420f00
movq %rax, %r12
movq %rax, %rdi
movq -0x40(%rbp), %rsi
callq 0x401020
movq %r12, %rdi
movq %r14, %rsi
callq 0x401058
movq %r15, %rdi
movq %r12, %rsi
movq -0x38(%rbp), %rdx
callq 0x402776
movl %eax, %r14d
movq -0x48(%rbp), %rdi
callq 0x421240
movq $0x0, -0x48(%rbp)
testl %r14d, %r14d
jne 0x401eab
movq %r12, %rdi
callq 0x421240
movq -0x40(%rbp), %r12
xorl %edi, %edi
leaq 0xa1c42(%rip), %rsi     # 0x4a38aa
movq %rbx, %rdx
callq 0x424130
testq %rax, %rax
jne 0x401b3d
movq -0x58(%rbp), %rdi
callq 0x421240
movq -0x30(%rbp), %rdi
callq 0x421240
movq %r12, %rdi
callq 0x421240
movq -0x38(%rbp), %rdi
callq 0x421240
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x30(%rbp), %r14
movq %r14, %rdi
callq 0x4010e0
movq %rax, %r15
movq %rbx, %rdi
callq 0x4010e0
leaq (%r15,%rax), %rdi
addq $0x9, %rdi
callq 0x420f00
movq %rax, %r15
movq %rax, %rdi
movq %r14, %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 %rbx, %rsi
callq 0x401058
movq %r15, %rdi
callq 0x40234f
testl %eax, %eax
js 0x401e97
jne 0x401d28
movq %r15, %rdi
callq 0x402156
testl %eax, %eax
jne 0x401ee5
movq -0x30(%rbp), %rdi
leaq -0x78(%rbp), %rdx
movq %rbx, %rsi
callq 0x4025b7
testl %eax, %eax
jne 0x401ea1
movq -0x78(%rbp), %rdi
leaq 0xa1b5f(%rip), %rsi     # 0x4a38aa
callq 0x424120
testq %rax, %rax
je 0x401af8
movq %rax, %rbx
leaq 0xa1b47(%rip), %r12     # 0x4a38aa
movq %r15, %rdi
callq 0x4010e0
movq %rax, %r14
movq -0x30(%rbp), %r13
movq %r13, %rdi
callq 0x4010e0
leaq (%rax,%rbx), %rdi
callq 0x4010e0
leaq (%r14,%rax), %rdi
incq %rdi
callq 0x420f00
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
callq 0x401020
movq %r13, %rdi
callq 0x4010e0
leaq (%rax,%rbx), %rsi
movq %r14, %rdi
callq 0x401058
movq %r14, %rdi
movl $0x40, %esi
callq 0x401050
testq %rax, %rax
je 0x401dc6
movb $0x0, (%rax)
movq %rbx, %rdi
callq 0x4010e0
leaq 0x1(%rax), %rdi
callq 0x420f00
movq %rax, %r13
movq %rax, %rdi
movq %rbx, %rsi
callq 0x401020
movq %r13, %rdi
movl $0x40, %esi
callq 0x401050
testq %rax, %rax
je 0x401dfa
movb $0x0, (%rax)
movq $0x0, -0x50(%rbp)
movq %r13, %rdi
leaq -0x50(%rbp), %rsi
callq 0x40280a
testl %eax, %eax
jne 0x401ed7
movq -0x50(%rbp), %rdi
leaq 0xa873b(%rip), %rsi     # 0x4aa55c
callq 0x4010b8
testl %eax, %eax
jne 0x401e53
movq -0x30(%rbp), %rdi
callq 0x421240
movq %r14, %rdi
callq 0x4010e0
leaq 0x1(%rax), %rdi
callq 0x420f00
movq %rax, -0x30(%rbp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x401020
movq -0x50(%rbp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4028b7
movq %r14, %rdi
testl %eax, %eax
jne 0x401ede
callq 0x421240
xorl %edi, %edi
movq %r12, %rsi
callq 0x424120
movq %rax, %rbx
testq %rax, %rax
jne 0x401d63
movq -0x40(%rbp), %r12
jmp 0x401af8
callq 0x401248
jmp 0x401af5
callq 0x401226
jmp 0x401af8
callq 0x4011ae
jmp 0x401af8
movq $0x4d3708, %rax         # imm = 0x4D3708
movq (%rax), %rdi
leaq 0xa12d0(%rip), %rsi     # 0x4a318c
movl %r14d, %edx
xorl %eax, %eax
callq 0x40c8b0
movq %r12, %rdi
callq 0x421240
movq -0x40(%rbp), %r12
jmp 0x401c79
callq 0x4011d0
jmp 0x401e84
callq 0x4011f2
jmp 0x401e84
callq 0x40118c
jmp 0x401af8
movq -0x38(%rbp), %rdx
callq 0x401100
movq -0x38(%rbp), %rdi
callq 0x401294
movq -0x38(%rbp), %rdi
callq 0x40126a
movq -0x30(%rbp), %rdi
callq 0x421240
movq %r12, %rdi
callq 0x421240
movq -0x38(%rbp), %rdi
callq 0x421240
movl $0x1, %edi
callq 0x40bd80
leaq -0x30(%rbp), %rdi
movq %r12, %rsi
movq -0x38(%rbp), %rdx
callq 0x40113d
nop