// File: /workspace/github_c_makefile_star2_O3/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 0x402448
movq %rsi, %rbx
xorl %eax, %eax
leaq -0x30(%rbp), %rsi
movq %rax, (%rsi)
movq %rax, -0x38(%rbp)
movb $0x0, -0x21(%rbp)
movq %rax, -0x40(%rbp)
leaq 0x9b48a(%rip), %rdi     # 0x49d687
callq 0x40256b
testl %eax, %eax
js 0x40244d
jne 0x4023f3
movq -0x30(%rbp), %rdi
leaq 0x9b508(%rip), %rsi     # 0x49d723
movl $0x4, %edx
callq 0x401080
testl %eax, %eax
jne 0x402459
leaq -0x40(%rbp), %rdi
callq 0x401a7d
testl %eax, %eax
jne 0x402467
leaq -0x21(%rbp), %rdi
callq 0x401b1f
movq -0x30(%rbp), %r15
testl %eax, %eax
jne 0x40246d
leaq 0x9b312(%rip), %rsi     # 0x49d56c
movq %r15, %rdi
callq 0x4010b0
testl %eax, %eax
je 0x4022cd
leaq 0x9b4bb(%rip), %rsi     # 0x49d728
movl $0x9, %edx
movq %r15, %rdi
callq 0x401080
testl %eax, %eax
je 0x4022cd
movq %r15, %rdi
callq 0x4010d8
leaq -0x3(%rax), %rdi
callq 0x4210d0
movq %rax, %r14
addq $0x4, %r15
movq %rax, %rdi
movq %r15, %rsi
callq 0x401020
movq %r14, %rdi
movl $0x2f, %esi
callq 0x4010e0
testq %rax, %rax
je 0x4022b6
movb $0x0, (%rax)
movq %r14, %rdi
movl $0x40, %esi
callq 0x4010e0
testq %rax, %rax
je 0x4022e3
movb $0x0, (%rax)
jmp 0x4022e3
movl $0x3, %edi
callq 0x4210d0
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 0x9b33c(%rip), %rsi     # 0x49d636
leaq 0x9b355(%rip), %rdx     # 0x49d656
movq %r15, %r8
movq %r14, %r9
callq 0x401ba2
testl %eax, %eax
jne 0x402432
testb %r12b, %r12b
jne 0x40233e
movq 0x8(%rbx), %rdi
leaq 0x9b312(%rip), %rsi     # 0x49d636
leaq 0x9b343(%rip), %rdx     # 0x49d66e
movq %r15, %rcx
movq %r14, %r8
callq 0x401dad
testl %eax, %eax
jne 0x402432
movq %r15, %rdi
callq 0x421410
movq %r14, %rdi
callq 0x421410
movq -0x30(%rbp), %rdi
leaq 0x9b3d7(%rip), %r14     # 0x49d730
movq %r14, %rsi
callq 0x424200
xorl %edi, %edi
movq %r14, %rsi
callq 0x424200
movq %rax, %r14
movq -0x30(%rbp), %r15
leaq 0x9b1f3(%rip), %rsi     # 0x49d56c
movq %r15, %rdi
callq 0x4010b0
testl %eax, %eax
jne 0x402488
leaq 0x9b3c4(%rip), %rdi     # 0x49d754
leaq -0x38(%rbp), %rsi
callq 0x40256b
testl %eax, %eax
js 0x4024d6
jne 0x4023fd
movq -0x38(%rbp), %rsi
leaq 0x9b411(%rip), %rdi     # 0x49d7bf
xorl %eax, %eax
callq 0x40c5f0
movq 0x8(%rbx), %rdi
movq -0x38(%rbp), %rdx
movl $0x1, %esi
movq %r14, %rcx
callq 0x401f75
testl %eax, %eax
jne 0x4024e2
movq -0x30(%rbp), %rdi
callq 0x421410
movq -0x38(%rbp), %rdi
callq 0x421410
xorl %eax, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x1, %eax
jne 0x402454
callq 0x4012f4
cmpl $0x1, %eax
jne 0x4024dd
leaq 0x9b3ef(%rip), %rdi     # 0x49d7fc
callq 0x413a80
movq 0x8(%rbx), %rdi
movl $0x1, %esi
xorl %edx, %edx
movq %r14, %rcx
callq 0x401f75
testl %eax, %eax
je 0x4024cf
callq 0x40127e
movq -0x30(%rbp), %rdi
callq 0x421410
movq %r15, %rdi
callq 0x421410
movq %r14, %rdi
jmp 0x402479
callq 0x401206
movl %eax, %edi
callq 0x401343
callq 0x40131b
leaq 0x9b3be(%rip), %rdi     # 0x49d81e
callq 0x413a80
jmp 0x4024cf
movq -0x30(%rbp), %rdi
jmp 0x402479
movq %r15, %rdi
callq 0x421410
movq -0x40(%rbp), %rdi
callq 0x421410
movl $0x1, %edi
callq 0x40ba00
movq %r15, %rdi
callq 0x4010d8
leaq -0x3(%rax), %rdi
callq 0x4210d0
movq %rax, %r12
leaq 0x4(%r15), %rsi
movq %rax, %rdi
callq 0x401020
movq %r15, %rdi
callq 0x421410
movq 0x8(%rbx), %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
callq 0x401f75
movl %eax, %ebx
movq %r12, %rdi
callq 0x421410
testl %ebx, %ebx
jne 0x4024e7
xorl %edi, %edi
callq 0x40ba00
movl %eax, %edi
callq 0x4012ce
callq 0x4012a6
callq 0x401256
callq 0x40122e