// File: /workspace/github_c_makefile_star2_O1/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 0x402434
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 0x9b49e(%rip), %rdi     # 0x49d687
callq 0x402557
testl %eax, %eax
js 0x402439
jne 0x4023df
movq -0x30(%rbp), %rdi
leaq 0x9b51c(%rip), %rsi     # 0x49d723
movl $0x4, %edx
callq 0x401080
testl %eax, %eax
jne 0x402445
leaq -0x40(%rbp), %rdi
callq 0x401a57
testl %eax, %eax
jne 0x402453
leaq -0x21(%rbp), %rdi
callq 0x401b01
movq -0x30(%rbp), %r15
testl %eax, %eax
jne 0x402459
leaq 0x9b326(%rip), %rsi     # 0x49d56c
movq %r15, %rdi
callq 0x4010b0
testl %eax, %eax
je 0x4022b9
leaq 0x9b4cf(%rip), %rsi     # 0x49d728
movl $0x9, %edx
movq %r15, %rdi
callq 0x401080
testl %eax, %eax
je 0x4022b9
movq %r15, %rdi
callq 0x4010d8
leaq -0x3(%rax), %rdi
callq 0x4210c0
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 0x4022a2
movb $0x0, (%rax)
movq %r14, %rdi
movl $0x40, %esi
callq 0x4010e0
testq %rax, %rax
je 0x4022cf
movb $0x0, (%rax)
jmp 0x4022cf
movl $0x3, %edi
callq 0x4210c0
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 0x9b350(%rip), %rsi     # 0x49d636
leaq 0x9b369(%rip), %rdx     # 0x49d656
movq %r15, %r8
movq %r14, %r9
callq 0x401b93
testl %eax, %eax
jne 0x40241e
testb %r12b, %r12b
jne 0x40232a
movq 0x8(%rbx), %rdi
leaq 0x9b326(%rip), %rsi     # 0x49d636
leaq 0x9b357(%rip), %rdx     # 0x49d66e
movq %r15, %rcx
movq %r14, %r8
callq 0x401da4
testl %eax, %eax
jne 0x40241e
movq %r15, %rdi
callq 0x421400
movq %r14, %rdi
callq 0x421400
movq -0x30(%rbp), %rdi
leaq 0x9b3eb(%rip), %r14     # 0x49d730
movq %r14, %rsi
callq 0x4241f0
xorl %edi, %edi
movq %r14, %rsi
callq 0x4241f0
movq %rax, %r14
movq -0x30(%rbp), %r15
leaq 0x9b207(%rip), %rsi     # 0x49d56c
movq %r15, %rdi
callq 0x4010b0
testl %eax, %eax
jne 0x402474
leaq 0x9b3d8(%rip), %rdi     # 0x49d754
leaq -0x38(%rbp), %rsi
callq 0x402557
testl %eax, %eax
js 0x4024c2
jne 0x4023e9
movq -0x38(%rbp), %rsi
leaq 0x9b425(%rip), %rdi     # 0x49d7bf
xorl %eax, %eax
callq 0x40c5e0
movq 0x8(%rbx), %rdi
movq -0x38(%rbp), %rdx
movl $0x1, %esi
movq %r14, %rcx
callq 0x401f74
testl %eax, %eax
jne 0x4024ce
movq -0x30(%rbp), %rdi
callq 0x421400
movq -0x38(%rbp), %rdi
callq 0x421400
xorl %eax, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x1, %eax
jne 0x402440
callq 0x4012d6
cmpl $0x1, %eax
jne 0x4024c9
leaq 0x9b403(%rip), %rdi     # 0x49d7fc
callq 0x413a70
movq 0x8(%rbx), %rdi
movl $0x1, %esi
xorl %edx, %edx
movq %r14, %rcx
callq 0x401f74
testl %eax, %eax
je 0x4024bb
callq 0x401260
movq -0x30(%rbp), %rdi
callq 0x421400
movq %r15, %rdi
callq 0x421400
movq %r14, %rdi
jmp 0x402465
callq 0x4011e8
movl %eax, %edi
callq 0x401325
callq 0x4012fd
leaq 0x9b3d2(%rip), %rdi     # 0x49d81e
callq 0x413a70
jmp 0x4024bb
movq -0x30(%rbp), %rdi
jmp 0x402465
movq %r15, %rdi
callq 0x421400
movq -0x40(%rbp), %rdi
callq 0x421400
movl $0x1, %edi
callq 0x40b9f0
movq %r15, %rdi
callq 0x4010d8
leaq -0x3(%rax), %rdi
callq 0x4210c0
movq %rax, %r12
leaq 0x4(%r15), %rsi
movq %rax, %rdi
callq 0x401020
movq %r15, %rdi
callq 0x421400
movq 0x8(%rbx), %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
callq 0x401f74
movl %eax, %ebx
movq %r12, %rdi
callq 0x421400
testl %ebx, %ebx
jne 0x4024d3
xorl %edi, %edi
callq 0x40b9f0
movl %eax, %edi
callq 0x4012b0
callq 0x401288
callq 0x401238
callq 0x401210