// File: /workspace/github_c_makefile_star2_O3/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, %r15
movl %esi, %r12d
movq %rdi, %r13
movq $0x0, -0x30(%rbp)
callq 0x4010d8
leaq 0x1a(%rax), %rdi
callq 0x4210d0
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 0x9b560(%rip), %xmm0    # 0x49d530
movups %xmm0, (%rbx,%rax)
movq %rbx, %rdi
movl $0x1fd, %esi            # imm = 0x1FD
callq 0x455510
testl %eax, %eax
jns 0x401ff3
callq 0x4051f0
cmpl $0x11, (%rax)
jne 0x4021aa
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)
movq %rbx, %rdi
movl $0x4, %esi
callq 0x4557e0
cmpl $-0x1, %eax
jne 0x402173
cmpl $0x1, %r12d
jne 0x402074
testq %r15, %r15
je 0x402095
movq %r15, %rdi
callq 0x4010d8
leaq 0xa(%rax), %rdi
callq 0x4210d0
movq %rax, %r12
movabsq $0x4f5455413d53465a, %rax # imm = 0x4F5455413D53465A
movq %rax, (%r12)
movw $0x3a, 0x8(%r12)
movq %r12, %rdi
movq %r15, %rsi
callq 0x401058
jmp 0x4020b6
movq %r15, %rdi
callq 0x4010d8
leaq 0x1(%rax), %rdi
callq 0x4210d0
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
callq 0x401020
jmp 0x4020b6
movl $0x9, %edi
callq 0x4210d0
movq %rax, %r12
movabsq $0x4f5455413d53465a, %rax # imm = 0x4F5455413D53465A
movq %rax, (%r12)
movb $0x0, 0x8(%r12)
testq %r14, %r14
je 0x4020fb
movq %r12, %rdi
callq 0x4010d8
movq %rax, %r15
movq %r14, %rdi
callq 0x4010d8
leaq (%r15,%rax), %rsi
addq $0x2, %rsi
movq %r12, %rdi
callq 0x421760
movq %rax, %r12
movq %rax, %rdi
callq 0x4010d8
movw $0x40, (%r12,%rax)
movq %r12, %rdi
movq %r14, %rsi
callq 0x401058
leaq -0x30(%rbp), %rdi
callq 0x401928
testl %eax, %eax
jne 0x40218c
leaq 0x9af69(%rip), %rsi     # 0x49d07c
movq %rbx, %rdi
callq 0x4133f0
testq %rax, %rax
je 0x402199
movq %rax, %r15
movq -0x30(%rbp), %r13
leaq 0x9b490(%rip), %rsi     # 0x49d5be
xorl %r14d, %r14d
movq %rax, %rdi
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x40c530
movq %r15, %rdi
callq 0x412cc0
movq %r12, %rdi
callq 0x421410
movq %r13, %rdi
callq 0x421410
movq %rbx, %rdi
callq 0x421410
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9b3cf(%rip), %rdi     # 0x49d549
callq 0x40c820
movq %rbx, %rdi
callq 0x421410
xorl %r14d, %r14d
jmp 0x402161
movq %r12, %rdi
movq %rbx, %rsi
callq 0x401188
jmp 0x4021be
leaq -0x30(%rbp), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x4011c5
jmp 0x4021be
leaq 0x9afed(%rip), %rdi     # 0x49d19e
callq 0x40c820
movq %rbx, %rdi
callq 0x421410
movl $0x1, %r14d
jmp 0x402161