// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
andq $0x0, -0x60(%rbp)
andq $0x0, -0x68(%rbp)
andq $0x0, -0x58(%rbp)
cmpl $0x3, %edi
jl 0x401d5c
movl %edi, %eax
movq %rax, -0x40(%rbp)
pushq $0x1
popq %r14
movq $0x0, -0x50(%rbp)
xorl %r15d, %r15d
xorl %r12d, %r12d
movq $0x0, -0x48(%rbp)
cmpq %r14, -0x40(%rbp)
je 0x401837
movq -0x38(%rbp), %rax
movq (%rax,%r14,8), %rbx
cmpb $0x1, %r15b
jne 0x4017c4
movq %rbx, %rdi
callq 0x4010e0
movq %rax, %r13
addq $0x2, %r13
testq %r12, %r12
je 0x401781
movq %r12, %rdi
callq 0x4010e0
addq %rax, %r13
movq %r13, %rdi
callq 0x420cc0
movq %rax, %r13
testq %r12, %r12
je 0x40179c
movq %r13, %rdi
movq %r12, %rsi
callq 0x401020
movq %r13, %rdi
movq %rbx, %rsi
callq 0x401058
movq %r13, %rdi
callq 0x4010e0
movw $0x2c, (%r13,%rax)
movq %r12, %rdi
callq 0x421000
xorl %r15d, %r15d
jmp 0x401827
cmpb $0x2d, (%rbx)
jne 0x4017d5
cmpb $0x6f, 0x1(%rbx)
jne 0x4017d5
cmpb $0x0, 0x2(%rbx)
je 0x401832
cmpq $0x0, -0x50(%rbp)
je 0x401800
movq -0x48(%rbp), %r13
testq %r13, %r13
jne 0x401d4b
movq %rbx, %rdi
callq 0x4010e0
leaq 0x1(%rax), %rdi
callq 0x420cc0
movq %rax, -0x48(%rbp)
jmp 0x401819
movq %rbx, %rdi
callq 0x4010e0
leaq 0x1(%rax), %rdi
callq 0x420cc0
movq %rax, -0x30(%rbp)
movq %rax, -0x50(%rbp)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x401020
movq %r12, %r13
incq %r14
movq %r13, %r12
jmp 0x40174a
movb $0x1, %r15b
jmp 0x401824
cmpq $0x0, -0x50(%rbp)
movq -0x48(%rbp), %r14
je 0x401d5c
testq %r14, %r14
je 0x401d5c
testq %r12, %r12
je 0x401884
pushq $0x7
popq %rdi
callq 0x420cc0
movq %rax, %rbx
movq %r12, %rdi
callq 0x4010e0
leaq -0x1(%rax), %rdx
movq %rbx, -0x40(%rbp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x401088
movq %r12, %rdi
callq 0x421000
jmp 0x40188c
movq $0x0, -0x40(%rbp)
leaq 0xa17b4(%rip), %rbx     # 0x4a3047
movq -0x50(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x423ee0
xorl %edi, %edi
movq %rbx, %rsi
callq 0x423ee0
movq %rax, %rbx
leaq 0xa1793(%rip), %rsi     # 0x4a3049
pushq $0x8
popq %rdx
movq %r15, %rdi
callq 0x401080
testl %eax, %eax
jne 0x4018d6
leaq -0x30(%rbp), %rdi
callq 0x401688
testl %eax, %eax
jne 0x401d92
testq %rbx, %rbx
je 0x4018f4
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
callq 0x402128
testl %eax, %eax
js 0x401ac6
cmpl $0x1, %eax
jne 0x4018fc
xorl %r15d, %r15d
jmp 0x401b5d
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 0x420cc0
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 0x40219c
testl %eax, %eax
js 0x401ae7
jne 0x401977
movq %r15, %rdi
callq 0x401fe6
testl %eax, %eax
jne 0x401d2a
movq -0x30(%rbp), %rdi
leaq -0x80(%rbp), %rdx
movq %rbx, %rsi
callq 0x4023b3
testl %eax, %eax
jne 0x401aff
movq %r15, -0x38(%rbp)
movq -0x80(%rbp), %rdi
pushq $0x40
popq %r14
movq -0x38(%rbp), %r12
leaq 0xa1f04(%rip), %rsi     # 0x4a38aa
callq 0x423ee0
testq %rax, %rax
je 0x401b59
movq %rax, %r13
movq %r12, %rdi
callq 0x4010e0
movq %rax, %rbx
movl %r14d, %r15d
movq -0x30(%rbp), %r14
movq %r14, %rdi
callq 0x4010e0
leaq (%rax,%r13), %rdi
callq 0x4010e0
leaq (%rbx,%rax), %rdi
incq %rdi
callq 0x420cc0
movq %rax, %rbx
movq %rax, %rdi
movq %r12, %rsi
callq 0x401020
movq %r14, %rdi
movl %r15d, %r14d
movq %rbx, %r15
callq 0x4010e0
leaq (%rax,%r13), %rsi
movq %rbx, %rdi
callq 0x401058
movq %rbx, %rdi
movl %r14d, %esi
callq 0x401050
testq %rax, %rax
je 0x401a21
movb $0x0, (%rax)
movq %r13, %rdi
callq 0x4010e0
leaq 0x1(%rax), %rdi
callq 0x420cc0
movq %rax, %rbx
movq %rax, %rdi
movq %r13, %rsi
callq 0x401020
movq %rbx, %rdi
movl %r14d, %esi
callq 0x401050
testq %rax, %rax
je 0x401a53
movb $0x0, (%rax)
andq $0x0, -0x60(%rbp)
movq %rbx, %rdi
leaq -0x60(%rbp), %rsi
callq 0x4025e8
testl %eax, %eax
jne 0x401b17
movq -0x60(%rbp), %rdx
cmpb $0x2f, (%rdx)
jne 0x401aa8
cmpb $0x0, 0x1(%rdx)
jne 0x401aa8
movq -0x30(%rbp), %rdi
callq 0x421000
movq %r15, %rdi
callq 0x4010e0
leaq 0x1(%rax), %rdi
callq 0x420cc0
movq %rax, -0x30(%rbp)
movq %rax, %rdi
movq %r15, %rsi
callq 0x401020
movq -0x60(%rbp), %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x402686
testl %eax, %eax
jne 0x401b35
movq %r15, %rdi
callq 0x421000
xorl %edi, %edi
jmp 0x40199f
movq $0x4d3708, %rax         # imm = 0x4D3708
movq (%rax), %rcx
leaq 0xa1595(%rip), %rdi     # 0x4a306c
pushq $0x2f
popq %rsi
pushq $0x1
popq %rdx
callq 0x413540
jmp 0x4018f4
movq $0x4d3708, %rax         # imm = 0x4D3708
movq (%rax), %rcx
leaq 0xa15ad(%rip), %rdi     # 0x4a30a5
pushq $0x38
jmp 0x401d3d
movq $0x4d3708, %rax         # imm = 0x4D3708
movq (%rax), %rcx
leaq 0xa1603(%rip), %rdi     # 0x4a3113
pushq $0x25
jmp 0x401d3d
movq $0x4d3708, %rax         # imm = 0x4D3708
movq (%rax), %rcx
leaq 0xa1611(%rip), %rdi     # 0x4a3139
pushq $0x23
popq %rsi
pushq $0x1
popq %rdx
callq 0x413540
jmp 0x401b59
movq $0x4d3708, %rax         # imm = 0x4D3708
movq (%rax), %rcx
leaq 0xa1617(%rip), %rdi     # 0x4a315d
pushq $0x22
popq %rsi
pushq $0x1
popq %rdx
callq 0x413540
movq %r15, %rdi
callq 0x421000
movq -0x38(%rbp), %r15
movq %r15, %rdi
callq 0x421000
movq -0x30(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x40231a
testl %eax, %eax
jne 0x401d83
movq -0x68(%rbp), %rdi
leaq 0xa1d25(%rip), %rbx     # 0x4a38aa
leaq -0x78(%rbp), %r12
movq %rbx, %rsi
movq %r12, %rdx
callq 0x423ef0
testq %rax, %rax
je 0x401cf5
movq %rax, %rdi
leaq 0xa15d9(%rip), %rsi     # 0x4a3180
leaq -0x70(%rbp), %rdx
callq 0x423ef0
movq %rax, %r13
movq -0x70(%rbp), %r14
cmpb $0x2d, (%r14)
jne 0x401bc4
cmpb $0x0, 0x1(%r14)
je 0x401bea
movq %r14, %rdi
leaq 0xa15b4(%rip), %rsi     # 0x4a3182
callq 0x4010b8
testl %eax, %eax
je 0x401bea
movq %r14, %rdi
leaq 0xa1739(%rip), %rsi     # 0x4a331a
callq 0x4010b8
testl %eax, %eax
jne 0x401c57
movq %r13, %rdi
leaq -0x58(%rbp), %rsi
callq 0x402767
movl $0x0, %edi
testl %eax, %eax
jne 0x401b89
movq -0x58(%rbp), %r15
cmpb $0x2d, (%r15)
jne 0x401c19
cmpb $0x0, 0x1(%r15)
movl $0x0, %edi
je 0x401b89
movq %r15, %rdi
leaq 0xa155f(%rip), %rsi     # 0x4a3182
callq 0x4010b8
movl $0x0, %edi
testl %eax, %eax
je 0x401b89
movq %r15, %rdi
leaq 0xa16db(%rip), %rsi     # 0x4a331a
callq 0x4010b8
movl $0x0, %edi
testl %eax, %eax
je 0x401b89
movq -0x70(%rbp), %r14
jmp 0x401c5b
movq -0x58(%rbp), %r15
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
callq 0x4010e0
movq %rax, %r12
testq %r15, %r15
cmovneq %r15, %r14
movq %r14, %rdi
callq 0x4010e0
leaq (%r12,%rax), %rdi
incq %rdi
callq 0x420cc0
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x401020
movq %r15, %rdi
movq %r14, %rsi
callq 0x401058
movq %r13, %rdi
movq %r15, %rsi
movq -0x40(%rbp), %rdx
callq 0x402554
movl %eax, %r14d
movq -0x58(%rbp), %rdi
callq 0x421000
andq $0x0, -0x58(%rbp)
testl %r14d, %r14d
jne 0x401cd2
movq %r15, %rdi
callq 0x421000
xorl %edi, %edi
jmp 0x401b7e
movq $0x4d3708, %rax         # imm = 0x4D3708
movq (%rax), %rdi
leaq 0xa14a6(%rip), %rsi     # 0x4a3189
movl %r14d, %edx
xorl %eax, %eax
callq 0x40c670
movq %r15, %rdi
callq 0x421000
movq -0x68(%rbp), %rdi
callq 0x421000
movq -0x30(%rbp), %rdi
callq 0x421000
movq -0x48(%rbp), %rdi
callq 0x421000
movq -0x40(%rbp), %rdi
callq 0x421000
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x4d3708, %rax         # imm = 0x4D3708
movq (%rax), %rcx
leaq 0xa13a3(%rip), %rdi     # 0x4a30de
pushq $0x34
popq %rsi
pushq $0x1
popq %rdx
callq 0x413540
jmp 0x401b5d
movq -0x50(%rbp), %rdi
callq 0x421000
movq %r13, %rdi
callq 0x421000
movq $0x4d3708, %rax         # imm = 0x4D3708
movq (%rax), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rdx
leaq 0xa129a(%rip), %rsi     # 0x4a300e
xorl %eax, %eax
callq 0x40c670
pushq $0x1
popq %rdi
callq 0x40bb40
movq -0x30(%rbp), %rdi
callq 0x421000
movq -0x48(%rbp), %rdi
jmp 0x401dba
movq $0x4d3708, %rax         # imm = 0x4D3708
movq (%rax), %rcx
leaq 0xa12af(%rip), %rdi     # 0x4a3052
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x413540
movq -0x30(%rbp), %rdi
callq 0x421000
movq %r14, %rdi
callq 0x421000
movq -0x40(%rbp), %rdi
callq 0x421000
jmp 0x401d7b
nop