// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
cmpl $0x4, %edi
jne 0x402248
movq %rsi, %rbx
leaq -0x30(%rbp), %rsi
andq $0x0, (%rsi)
andq $0x0, -0x38(%rbp)
movb $0x0, -0x21(%rbp)
andq $0x0, -0x40(%rbp)
leaq 0x9b69c(%rip), %rdi     # 0x49d687
callq 0x4023d6
testl %eax, %eax
js 0x402260
jne 0x4021d5
movq -0x30(%rbp), %rdi
leaq 0x9b71a(%rip), %rsi     # 0x49d723
pushq $0x4
popq %rdx
callq 0x401080
testl %eax, %eax
jne 0x40228e
leaq -0x40(%rbp), %rdi
callq 0x401834
testl %eax, %eax
jne 0x40229c
leaq -0x21(%rbp), %rdi
callq 0x4018e4
movq -0x30(%rbp), %r15
testl %eax, %eax
jne 0x4022a2
leaq 0x9b526(%rip), %rsi     # 0x49d56c
movq %r15, %rdi
callq 0x4010b0
testl %eax, %eax
je 0x4020b3
leaq 0x9b6cf(%rip), %rsi     # 0x49d728
pushq $0x9
popq %rdx
movq %r15, %rdi
callq 0x401080
testl %eax, %eax
je 0x4020b3
movq %r15, %rdi
callq 0x4010d8
leaq -0x3(%rax), %rdi
callq 0x420f40
movq %rax, %r14
addq $0x4, %r15
movq %rax, %rdi
movq %r15, %rsi
callq 0x401020
pushq $0x2f
popq %rsi
movq %r14, %rdi
callq 0x4010e0
testq %rax, %rax
je 0x40209e
movb $0x0, (%rax)
pushq $0x40
popq %rsi
movq %r14, %rdi
callq 0x4010e0
testq %rax, %rax
je 0x4020c7
movb $0x0, (%rax)
jmp 0x4020c7
pushq $0x3
popq %rdi
callq 0x420f40
movq %rax, %r14
movw $0x612d, (%rax)         # imm = 0x612D
movb $0x0, 0x2(%rax)
movq 0x8(%rbx), %rdi
movb -0x21(%rbp), %r12b
movsbl %r12b, %ecx
movq -0x40(%rbp), %r15
leaq 0x9b558(%rip), %rsi     # 0x49d636
leaq 0x9b571(%rip), %rdx     # 0x49d656
movq %r15, %r8
movq %r14, %r9
callq 0x401979
testl %eax, %eax
jne 0x402232
testb %r12b, %r12b
jne 0x402122
movq 0x8(%rbx), %rdi
leaq 0x9b52e(%rip), %rsi     # 0x49d636
leaq 0x9b55f(%rip), %rdx     # 0x49d66e
movq %r15, %rcx
movq %r14, %r8
callq 0x401b7a
testl %eax, %eax
jne 0x402232
movq %r15, %rdi
callq 0x421280
movq %r14, %rdi
callq 0x421280
movq -0x30(%rbp), %rdi
leaq 0x9b5f3(%rip), %r14     # 0x49d730
movq %r14, %rsi
callq 0x424070
xorl %edi, %edi
movq %r14, %rsi
callq 0x424070
movq %rax, %r14
movq -0x30(%rbp), %r15
leaq 0x9b40f(%rip), %rsi     # 0x49d56c
movq %r15, %rdi
callq 0x4010b0
testl %eax, %eax
jne 0x4022b8
leaq 0x9b5e0(%rip), %rdi     # 0x49d754
leaq -0x38(%rbp), %rsi
callq 0x4023d6
testl %eax, %eax
js 0x40231b
jne 0x4021ff
movq -0x38(%rbp), %rsi
leaq 0x9b62d(%rip), %rdi     # 0x49d7bf
xorl %eax, %eax
callq 0x40c460
movq 0x8(%rbx), %rdi
movq -0x38(%rbp), %rdx
pushq $0x1
popq %rsi
movq %r14, %rcx
callq 0x401d3e
testl %eax, %eax
jne 0x402306
movq -0x30(%rbp), %rdi
callq 0x421280
movq -0x38(%rbp), %rdi
callq 0x421280
xorl %eax, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x1, %eax
jne 0x402276
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rcx
leaq 0x9b4c7(%rip), %rdi     # 0x49d6b6
pushq $0x33
popq %rsi
pushq $0x1
popq %rdx
callq 0x413360
jmp 0x4022ff
cmpl $0x1, %eax
jne 0x402337
leaq 0x9b5ed(%rip), %rdi     # 0x49d7fc
callq 0x4138f0
movq 0x8(%rbx), %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
movq %r14, %rcx
callq 0x401d3e
testl %eax, %eax
je 0x4022ff
jmp 0x402306
movq -0x30(%rbp), %rdi
callq 0x421280
movq %r15, %rdi
callq 0x421280
movq %r14, %rdi
jmp 0x4022ae
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rcx
leaq 0x9b3b5(%rip), %rdi     # 0x49d60e
pushq $0x27
jmp 0x40234a
movq $0x4cd6e8, %rcx         # imm = 0x4CD6E8
movq (%rcx), %rdi
leaq 0x9b41c(%rip), %rsi     # 0x49d68d
jmp 0x40232c
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rcx
leaq 0x9b463(%rip), %rdi     # 0x49d6ea
pushq $0x38
jmp 0x40234a
leaq 0x9b589(%rip), %rdi     # 0x49d81e
callq 0x4138f0
jmp 0x4022ff
movq -0x30(%rbp), %rdi
jmp 0x4022ae
movq %r15, %rdi
callq 0x421280
movq -0x40(%rbp), %rdi
callq 0x421280
jmp 0x402353
movq %r15, %rdi
callq 0x4010d8
leaq -0x3(%rax), %rdi
callq 0x420f40
movq %rax, %r12
leaq 0x4(%r15), %rsi
movq %rax, %rdi
callq 0x401020
movq %r15, %rdi
callq 0x421280
movq 0x8(%rbx), %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
callq 0x401d3e
movl %eax, %ebx
movq %r12, %rdi
callq 0x421280
testl %ebx, %ebx
jne 0x402306
xorl %edi, %edi
callq 0x40b870
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rcx
leaq 0x9b41e(%rip), %rdi     # 0x49d735
pushq $0x1e
jmp 0x40234a
movq $0x4cd6e8, %rcx         # imm = 0x4CD6E8
movq (%rcx), %rdi
leaq 0x9b42f(%rip), %rsi     # 0x49d75b
movl %eax, %edx
xorl %eax, %eax
callq 0x40c3a0
jmp 0x402353
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rcx
leaq 0x9b43d(%rip), %rdi     # 0x49d785
pushq $0x39
popq %rsi
pushq $0x1
popq %rdx
callq 0x413360
pushq $0x1
popq %rdi
callq 0x40b870
nop