// File: /workspace/github_c_makefile_star2_O0/dasJ[P]sd-zfs/src/zfs-generator.c
// Function: main
int main(int argc, char *argv[]) {
	if (argc != 4) {
		fprintf(stderr, "Only to be used as a systemd generator\n");
		exit(1);
	}

	char *root = NULL;
	char *rpool = NULL;
	char ignoreCache = 0;
	char *forceParam = NULL;
	char *poolName;
	char *dataset;
	char *slash;
	char *snap;
	int ret;
	const int systemd_param = 1;
	const char *importTarget = "initrd-root-device.target.wants";
	const char *scanUnitName = "zfs-import-scan.service";
	const char *cacheUnitName = "zfs-import-cache.service";

	ret = cmdline_getParam("root=", &root);
	if (ret < 0) {
		fprintf(stderr, "Could not get root= parameter. Error %d\n", ret);
		exit(1);
	} else if (ret == 1) {
		fprintf(stderr, "No root parameter specified, don't know what to do\n");
		exit(0);
	} else if (ret != 0) {
		fprintf(stderr, "Unknown thing happened while reading the root= paremter\n");
		exit(1);
	}
	// Handle non-ZFS values
	if (strncmp(root, "ZFS=", strlen("ZFS=")) != 0) {
		printf("root= does not point to anything ZFS-related. Quitting\n");
		exit(0);
	}
	// Check if we need forcing
	// Not doing this earlier because we are not sure if we even need ZFS
	if (getForce(&forceParam) != 0) {
		free(root);
		exit(1);
	}
	if (getIgnoreCache(&ignoreCache) != 0) {
		free(root);
		free(forceParam);
		exit(1);
	}
	// Generate units
	if (strcmp(root, "ZFS=AUTO") != 0 && strncmp(root, "ZFS=AUTO@", strlen("ZFS=AUTO@")) != 0) {
		poolName = malloc((strlen(root) - 4 + 1) * sizeof(char));
		strcpy(poolName, &(root[4]));

		slash = strchr(poolName, '/');
		if (slash != NULL) {
			*slash = '\0';
		}
		slash = strchr(poolName, '@');
		if (slash != NULL) {
			*slash = '\0';
		}
	} else {
		poolName = malloc(3 * sizeof(char));
		strcpy(poolName, "-a");
	}
	if (generateScanUnit(argv[systemd_param], importTarget, scanUnitName, ignoreCache, forceParam, poolName) != 0) {
		free(root);
		free(forceParam);
		free(poolName);
		exit(1);
	}
	if ((ignoreCache == 0) && generateCacheUnit(argv[systemd_param], importTarget, cacheUnitName, forceParam, poolName) != 0) {
		free(root);
		free(forceParam);
		free(poolName);
		exit(1);
	}
	free(forceParam);
	free(poolName);
	// Snapshot
	strtok(root, "@");
	snap = strtok(NULL, "@");
	// Direct dataset
	if (strcmp(root, "ZFS=AUTO") != 0) {
		dataset = malloc((strlen(root) - strlen("ZFS=") + 1) * sizeof(char));
		strcpy(dataset, &(root[strlen("ZFS=")]));
		free(root);

		if (generateSysrootUnit(argv[systemd_param], 0, dataset, snap) != 0) {
			free(dataset);
			fprintf(stderr, "Can not generate sysroot unit\n");
			exit(1);
		}
		free(dataset);
		exit(0);
	}
	// Bootfs
	ret = cmdline_getParam("rpool=", &rpool);
	if (ret < 0) {
		fprintf(stderr, "Could not get rpool= parameter. Error %d\n", ret);
		exit(1);
	} else if (ret == 1) {
		printf("Will use bootfs value of any pool\n");
		if (generateSysrootUnit(argv[systemd_param], 1, NULL, snap) != 0) {
			fprintf(stderr, "Can not generate sysroot unit\n");
			exit(1);
		}
		exit(0);
	} else if (ret != 0) {
		fprintf(stderr, "Unknown thing happened while reading the rpool= paremter\n");
		exit(1);
	}
	printf("Will use bootfs of pool %s\n", rpool);
	if (generateSysrootUnit(argv[systemd_param], 1, rpool, snap) != 0) {
		fprintf(stderr, "Can not generate sysroot unit\n");
		exit(1);
	}

	free(root);
	free(rpool);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x4, -0x8(%rbp)
je 0x40220e
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rdi
leaq 0x9b43b(%rip), %rsi     # 0x49d638
movb $0x0, %al
callq 0x40c7a0
movl $0x1, %edi
callq 0x40bc70
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movb $0x0, -0x21(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x1, -0x58(%rbp)
leaq 0x9b428(%rip), %rax     # 0x49d660
movq %rax, -0x60(%rbp)
leaq 0x9b43d(%rip), %rax     # 0x49d680
movq %rax, -0x68(%rbp)
leaq 0x9b44a(%rip), %rax     # 0x49d698
movq %rax, -0x70(%rbp)
leaq 0x9b458(%rip), %rdi     # 0x49d6b1
leaq -0x18(%rbp), %rsi
callq 0x402760
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jge 0x402290
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rdi
movl -0x54(%rbp), %edx
leaq 0x9b438(%rip), %rsi     # 0x49d6b7
movb $0x0, %al
callq 0x40c7a0
movl $0x1, %edi
callq 0x40bc70
cmpl $0x1, -0x54(%rbp)
jne 0x4022b5
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rdi
leaq 0x9b439(%rip), %rsi     # 0x49d6e0
movb $0x0, %al
callq 0x40c7a0
xorl %edi, %edi
callq 0x40bc70
cmpl $0x0, -0x54(%rbp)
je 0x4022dd
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rdi
leaq 0x9b448(%rip), %rsi     # 0x49d714
movb $0x0, %al
callq 0x40c7a0
movl $0x1, %edi
callq 0x40bc70
jmp 0x4022df
jmp 0x4022e1
movq -0x18(%rbp), %rdi
leaq 0x9b461(%rip), %rsi     # 0x49d74d
movl $0x4, %edx
callq 0x401080
cmpl $0x0, %eax
je 0x402310
leaq 0x9b450(%rip), %rdi     # 0x49d752
movb $0x0, %al
callq 0x40c860
xorl %edi, %edi
callq 0x40bc70
leaq -0x30(%rbp), %rdi
callq 0x401840
cmpl $0x0, %eax
je 0x402331
movq -0x18(%rbp), %rdi
callq 0x421350
movl $0x1, %edi
callq 0x40bc70
leaq -0x21(%rbp), %rdi
callq 0x401960
cmpl $0x0, %eax
je 0x40235b
movq -0x18(%rbp), %rdi
callq 0x421350
movq -0x30(%rbp), %rdi
callq 0x421350
movl $0x1, %edi
callq 0x40bc70
movq -0x18(%rbp), %rdi
leaq 0x9b230(%rip), %rsi     # 0x49d596
callq 0x4010b0
cmpl $0x0, %eax
je 0x402402
movq -0x18(%rbp), %rdi
leaq 0x9b40b(%rip), %rsi     # 0x49d78a
movl $0x9, %edx
callq 0x401080
cmpl $0x0, %eax
je 0x402402
movq -0x18(%rbp), %rdi
callq 0x4010d8
movq %rax, %rdi
subq $0x4, %rdi
addq $0x1, %rdi
shlq $0x0, %rdi
callq 0x421010
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x4, %rsi
callq 0x401020
movq -0x38(%rbp), %rdi
movl $0x2f, %esi
callq 0x4010e0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x4023e0
movq -0x48(%rbp), %rax
movb $0x0, (%rax)
movq -0x38(%rbp), %rdi
movl $0x40, %esi
callq 0x4010e0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x402400
movq -0x48(%rbp), %rax
movb $0x0, (%rax)
jmp 0x402420
movl $0x3, %edi
callq 0x421010
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
leaq 0x9b379(%rip), %rsi     # 0x49d794
callq 0x401020
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x60(%rbp), %rsi
movq -0x68(%rbp), %rdx
movsbl -0x21(%rbp), %ecx
movq -0x30(%rbp), %r8
movq -0x38(%rbp), %r9
callq 0x401a20
cmpl $0x0, %eax
je 0x40246b
movq -0x18(%rbp), %rdi
callq 0x421350
movq -0x30(%rbp), %rdi
callq 0x421350
movq -0x38(%rbp), %rdi
callq 0x421350
movl $0x1, %edi
callq 0x40bc70
movsbl -0x21(%rbp), %eax
cmpl $0x0, %eax
jne 0x4024bb
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x60(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %r8
callq 0x401ca0
cmpl $0x0, %eax
je 0x4024bb
movq -0x18(%rbp), %rdi
callq 0x421350
movq -0x30(%rbp), %rdi
callq 0x421350
movq -0x38(%rbp), %rdi
callq 0x421350
movl $0x1, %edi
callq 0x40bc70
movq -0x30(%rbp), %rdi
callq 0x421350
movq -0x38(%rbp), %rdi
callq 0x421350
movq -0x18(%rbp), %rdi
leaq 0x9b2ba(%rip), %rsi     # 0x49d792
callq 0x424140
xorl %eax, %eax
movl %eax, %edi
leaq 0x9b2aa(%rip), %rsi     # 0x49d792
callq 0x424140
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x9b09a(%rip), %rsi     # 0x49d596
callq 0x4010b0
cmpl $0x0, %eax
je 0x40259c
movq -0x18(%rbp), %rdi
callq 0x4010d8
movq %rax, %rdi
subq $0x4, %rdi
addq $0x1, %rdi
shlq $0x0, %rdi
callq 0x421010
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x4, %rsi
callq 0x401020
movq -0x18(%rbp), %rdi
callq 0x421350
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x40(%rbp), %rdx
movq -0x50(%rbp), %rcx
xorl %esi, %esi
callq 0x401ec0
cmpl $0x0, %eax
je 0x40258c
movq -0x40(%rbp), %rdi
callq 0x421350
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rdi
leaq 0x9b21c(%rip), %rsi     # 0x49d797
movb $0x0, %al
callq 0x40c7a0
movl $0x1, %edi
callq 0x40bc70
movq -0x40(%rbp), %rdi
callq 0x421350
xorl %edi, %edi
callq 0x40bc70
leaq 0x9b213(%rip), %rdi     # 0x49d7b6
leaq -0x20(%rbp), %rsi
callq 0x402760
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jge 0x4025da
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rdi
movl -0x54(%rbp), %edx
leaq 0x9b1f4(%rip), %rsi     # 0x49d7bd
movb $0x0, %al
callq 0x40c7a0
movl $0x1, %edi
callq 0x40bc70
cmpl $0x1, -0x54(%rbp)
jne 0x402636
leaq 0x9b200(%rip), %rdi     # 0x49d7e7
movb $0x0, %al
callq 0x40c860
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x50(%rbp), %rcx
movl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x401ec0
cmpl $0x0, %eax
je 0x40262f
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rdi
leaq 0x9b179(%rip), %rsi     # 0x49d797
movb $0x0, %al
callq 0x40c7a0
movl $0x1, %edi
callq 0x40bc70
xorl %edi, %edi
callq 0x40bc70
cmpl $0x0, -0x54(%rbp)
je 0x40265e
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rdi
leaq 0x9b1bd(%rip), %rsi     # 0x49d80a
movb $0x0, %al
callq 0x40c7a0
movl $0x1, %edi
callq 0x40bc70
jmp 0x402660
jmp 0x402662
movq -0x20(%rbp), %rsi
leaq 0x9b1d7(%rip), %rdi     # 0x49d844
movb $0x0, %al
callq 0x40c860
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rdx
movq -0x50(%rbp), %rcx
movl $0x1, %esi
callq 0x401ec0
cmpl $0x0, %eax
je 0x4026b5
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rdi
leaq 0x9b0f3(%rip), %rsi     # 0x49d797
movb $0x0, %al
callq 0x40c7a0
movl $0x1, %edi
callq 0x40bc70
movq -0x18(%rbp), %rdi
callq 0x421350
movq -0x20(%rbp), %rdi
callq 0x421350
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq