// File: /workspace/github_c_makefile_star2_O2/dasJ[P]sd-zfs/src/zfs-generator.c
// Function: generateSysrootUnit
int generateSysrootUnit(char *directory, int bootfs, char *dataset, char *snapshot) {
	char *unitpath;
	FILE *fp;
	char *targetName = "sysroot.mount.d";
	char *unitName = "zfs.conf";
	char *what;
	char *options = NULL;

	// Build paths
	unitpath = malloc((strlen(directory) + strlen(targetName) + strlen(unitName) + 3) * sizeof(char));
	strcpy(unitpath, directory);
	strcat(unitpath, "/");
	strcat(unitpath, targetName);
	// Make dropin directory
	if (mkdir(unitpath, 0775) < 0 && errno != EEXIST) {
		perror("Can not create unit directory\n");
		free(unitpath);
		return(1);
	}
	strcat(unitpath, "/");
	strcat(unitpath, unitName);
	// Check if unit already exists
	if (access(unitpath, R_OK) != -1) {
		perror("Mounting unit file already exists\n");
		free(unitpath);
		return 0;
	}

	// Discover bootfs
	if (bootfs == 1) {
		if (dataset == NULL) {
			what = malloc((strlen("ZFS=AUTO") + 1) * sizeof(char));
			strcpy(what, "ZFS=AUTO");
		} else {
			what = malloc((strlen("ZFS=AUTO:") + strlen(dataset) + 1) * sizeof(char));
			strcpy(what, "ZFS=AUTO:");
			strcat(what, dataset);
		}
	} else {
		what = malloc((strlen(dataset) + 1) * sizeof(char));
		strcpy(what, dataset);
	}
	// Handle snapshots
	if (snapshot != NULL) {
		what = realloc(what, (strlen(what) + strlen(snapshot) + 2) * sizeof(char));
		strcat(what, "@");
		strcat(what, snapshot);
	}

	// Get options
	if (getRootOptions(&options) != 0) {
		fprintf(stderr, "Can not get root options\n");
		free(what);
		free(unitpath);
		return 1;
	}

	// Write
	fp = fopen(unitpath, "w");
	if (fp == NULL) {
		perror("Can not write to mounting unit file\n");
		free(unitpath);
		free(options);
		free(what);
		return 1;
	}
	fprintf(fp, "# This file was generated by sd-zfs\n\
[Mount]\n\
What=%s\n\
Type=initrd_zfs\n\
Options=%s\n", what, options);
	fclose(fp);
	free(what);
	free(options);
	free(unitpath);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %r14
movq %rdx, %r12
movl %esi, %r15d
movq %rdi, %r13
andq $0x0, -0x30(%rbp)
callq 0x4010d8
leaq 0x1a(%rax), %rdi
callq 0x420f40
movq %rax, %rbx
movq %rax, %rdi
movq %r13, %rsi
callq 0x401020
movq %rbx, %rdi
callq 0x4010d8
movw $0x2f, (%rbx,%rax)
movq %rbx, %rdi
callq 0x4010d8
movups 0x9b79a(%rip), %xmm0    # 0x49d530
movups %xmm0, (%rbx,%rax)
movq %rbx, %rdi
movl $0x1fd, %esi            # imm = 0x1FD
callq 0x455390
testl %eax, %eax
jns 0x401db9
callq 0x405060
cmpl $0x11, (%rax)
jne 0x401f96
movq %rbx, %rdi
callq 0x4010d8
movw $0x2f, (%rbx,%rax)
movq %rbx, %rdi
callq 0x4010d8
movabsq $0x666e6f632e73667a, %rcx # imm = 0x666E6F632E73667A
movq %rcx, (%rbx,%rax)
movb $0x0, 0x8(%rbx,%rax)
pushq $0x4
popq %rsi
movq %rbx, %rdi
callq 0x455660
cmpl $-0x1, %eax
jne 0x401f35
cmpl $0x1, %r15d
jne 0x401e36
testq %r12, %r12
je 0x401e57
movq %r12, %rdi
callq 0x4010d8
leaq 0xa(%rax), %rdi
callq 0x420f40
movq %rax, %r15
movabsq $0x4f5455413d53465a, %rax # imm = 0x4F5455413D53465A
movq %rax, (%r15)
movw $0x3a, 0x8(%r15)
movq %r15, %rdi
movq %r12, %rsi
callq 0x401058
jmp 0x401e74
movq %r12, %rdi
callq 0x4010d8
leaq 0x1(%rax), %rdi
callq 0x420f40
movq %rax, %r15
movq %rax, %rdi
movq %r12, %rsi
callq 0x401020
jmp 0x401e74
pushq $0x9
popq %rdi
callq 0x420f40
movq %rax, %r15
movabsq $0x4f5455413d53465a, %rax # imm = 0x4F5455413D53465A
movq %rax, (%r15)
movb $0x0, 0x8(%r15)
testq %r14, %r14
je 0x401eb9
movq %r15, %rdi
callq 0x4010d8
movq %rax, %r12
movq %r14, %rdi
callq 0x4010d8
leaq (%r12,%rax), %rsi
addq $0x2, %rsi
movq %r15, %rdi
callq 0x4215d0
movq %rax, %r15
movq %rax, %rdi
callq 0x4010d8
movw $0x40, (%r15,%rax)
movq %r15, %rdi
movq %r14, %rsi
callq 0x401058
leaq -0x30(%rbp), %rdi
callq 0x4016b8
testl %eax, %eax
jne 0x401f4e
leaq 0x9b1ab(%rip), %rsi     # 0x49d07c
movq %rbx, %rdi
callq 0x413260
testq %rax, %rax
je 0x401f74
movq %rax, %r12
movq -0x30(%rbp), %r13
leaq 0x9b6ce(%rip), %rsi     # 0x49d5be
xorl %r14d, %r14d
movq %rax, %rdi
movq %r15, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x40c3a0
movq %r12, %rdi
callq 0x412b30
movq %r15, %rdi
callq 0x421280
movq %r13, %rdi
callq 0x421280
movq %rbx, %rdi
callq 0x421280
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9b60d(%rip), %rdi     # 0x49d549
callq 0x40c690
movq %rbx, %rdi
callq 0x421280
xorl %r14d, %r14d
jmp 0x401f23
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rcx
leaq 0x9b620(%rip), %rdi     # 0x49d57f
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x413360
movq %r15, %rdi
callq 0x421280
jmp 0x401fa2
leaq 0x9b61e(%rip), %rdi     # 0x49d599
callq 0x40c690
movq %rbx, %rdi
callq 0x421280
movq -0x30(%rbp), %rdi
callq 0x421280
movq %r15, %rdi
jmp 0x401fa5
leaq 0x9b201(%rip), %rdi     # 0x49d19e
callq 0x40c690
movq %rbx, %rdi
callq 0x421280
pushq $0x1
popq %r14
jmp 0x401f23