// File: /workspace/github_c_makefile_star2_O1/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 0x4210c0
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 0x9b561(%rip), %xmm0    # 0x49d530
movups %xmm0, (%rbx,%rax)
movq %rbx, %rdi
movl $0x1fd, %esi            # imm = 0x1FD
callq 0x455510
testl %eax, %eax
jns 0x401ff2
callq 0x4051e0
cmpl $0x11, (%rax)
jne 0x40219e
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 0x40216c
cmpl $0x1, %r12d
jne 0x402073
testq %r15, %r15
je 0x402094
movq %r15, %rdi
callq 0x4010d8
leaq 0xa(%rax), %rdi
callq 0x4210c0
movq %rax, %r12
movabsq $0x4f5455413d53465a, %rax # imm = 0x4F5455413D53465A
movq %rax, (%r12)
movw $0x3a, 0x8(%r12)
movq %r12, %rdi
movq %r15, %rsi
callq 0x401058
jmp 0x4020b5
movq %r15, %rdi
callq 0x4010d8
leaq 0x1(%rax), %rdi
callq 0x4210c0
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
callq 0x401020
jmp 0x4020b5
movl $0x9, %edi
callq 0x4210c0
movq %rax, %r12
movabsq $0x4f5455413d53465a, %rax # imm = 0x4F5455413D53465A
movq %rax, (%r12)
movb $0x0, 0x8(%r12)
testq %r14, %r14
je 0x4020fa
movq %r12, %rdi
callq 0x4010d8
movq %rax, %r15
movq %r14, %rdi
callq 0x4010d8
leaq (%r15,%rax), %rsi
addq $0x2, %rsi
movq %r12, %rdi
callq 0x421750
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 0x401908
testl %eax, %eax
jne 0x40217d
leaq 0x9af6e(%rip), %rsi     # 0x49d07c
movq %rbx, %rdi
callq 0x4133e0
testq %rax, %rax
je 0x402187
movq %rax, %r15
movq -0x30(%rbp), %rcx
leaq 0x9b495(%rip), %rsi     # 0x49d5be
xorl %r14d, %r14d
movq %rax, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x40c520
movq %r15, %rdi
callq 0x412cb0
movq %r12, %rdi
callq 0x421400
movq -0x30(%rbp), %rdi
callq 0x421400
movq %rbx, %rdi
callq 0x421400
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9b3d6(%rip), %rdi     # 0x49d549
callq 0x40c810
xorl %r14d, %r14d
jmp 0x402152
movq %r12, %rdi
callq 0x401188
jmp 0x4021aa
leaq -0x30(%rbp), %rsi
movq %rbx, %rdi
callq 0x4011bb
movl $0x1, %r14d
movq %r12, %rbx
jmp 0x402152
leaq 0x9aff9(%rip), %rdi     # 0x49d19e
callq 0x40c810
movl $0x1, %r14d
jmp 0x402152