// File: /workspace/github_c_makefile_star2_O0/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
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
leaq 0x9b679(%rip), %rax     # 0x49d55a
movq %rax, -0x40(%rbp)
leaq 0x9b67e(%rip), %rax     # 0x49d56a
movq %rax, -0x48(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x10(%rbp), %rdi
callq 0x4010d8
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rdi
callq 0x4010d8
movq %rax, %rcx
movq -0x68(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rdi
callq 0x4010d8
movq -0x60(%rbp), %rdi
addq %rax, %rdi
addq $0x3, %rdi
shlq $0x0, %rdi
callq 0x421010
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x401020
movq -0x30(%rbp), %rdi
leaq 0xa29a7(%rip), %rsi     # 0x4a48fc
callq 0x401058
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x401058
movq -0x30(%rbp), %rdi
movl $0x1fd, %esi            # imm = 0x1FD
callq 0x455450
cmpl $0x0, %eax
jge 0x401fa5
callq 0x405460
cmpl $0x11, (%rax)
je 0x401fa5
leaq 0x9b21b(%rip), %rdi     # 0x49d1a6
callq 0x40ca90
movq -0x30(%rbp), %rdi
callq 0x421350
movl $0x1, -0x4(%rbp)
jmp 0x4021bf
movq -0x30(%rbp), %rdi
leaq 0xa294c(%rip), %rsi     # 0x4a48fc
callq 0x401058
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x401058
movq -0x30(%rbp), %rdi
movl $0x4, %esi
callq 0x455720
cmpl $-0x1, %eax
je 0x401ff6
leaq 0x9b597(%rip), %rdi     # 0x49d573
callq 0x40ca90
movq -0x30(%rbp), %rdi
callq 0x421350
movl $0x0, -0x4(%rbp)
jmp 0x4021bf
cmpl $0x1, -0x14(%rbp)
jne 0x402063
cmpq $0x0, -0x20(%rbp)
jne 0x402023
movl $0x9, %edi
callq 0x421010
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
leaq 0x9b57a(%rip), %rsi     # 0x49d596
callq 0x401020
jmp 0x402061
movq -0x20(%rbp), %rdi
callq 0x4010d8
movq %rax, %rdi
addq $0x9, %rdi
addq $0x1, %rdi
shlq $0x0, %rdi
callq 0x421010
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
leaq 0x9b550(%rip), %rsi     # 0x49d59f
callq 0x401020
movq -0x50(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x401058
jmp 0x40208d
movq -0x20(%rbp), %rdi
callq 0x4010d8
movq %rax, %rdi
addq $0x1, %rdi
shlq $0x0, %rdi
callq 0x421010
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x401020
cmpq $0x0, -0x28(%rbp)
je 0x4020eb
movq -0x50(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rdi
callq 0x4010d8
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4010d8
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdi
addq %rax, %rsi
addq $0x2, %rsi
shlq $0x0, %rsi
callq 0x4216a0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
leaq 0x9b6b9(%rip), %rsi     # 0x49d792
callq 0x401058
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x401058
leaq -0x58(%rbp), %rdi
callq 0x401690
cmpl $0x0, %eax
je 0x40212f
movq $0x4cd6e8, %rax         # imm = 0x4CD6E8
movq (%rax), %rdi
leaq 0x9b49f(%rip), %rsi     # 0x49d5a9
movb $0x0, %al
callq 0x40c7a0
movq -0x50(%rbp), %rdi
callq 0x421350
movq -0x30(%rbp), %rdi
callq 0x421350
movl $0x1, -0x4(%rbp)
jmp 0x4021bf
movq -0x30(%rbp), %rdi
leaq 0x9af42(%rip), %rsi     # 0x49d07c
callq 0x413660
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x40217a
leaq 0x9b472(%rip), %rdi     # 0x49d5c3
callq 0x40ca90
movq -0x30(%rbp), %rdi
callq 0x421350
movq -0x58(%rbp), %rdi
callq 0x421350
movq -0x50(%rbp), %rdi
callq 0x421350
movl $0x1, -0x4(%rbp)
jmp 0x4021bf
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %rcx
leaq 0x9b45b(%rip), %rsi     # 0x49d5e8
movb $0x0, %al
callq 0x40c7a0
movq -0x38(%rbp), %rdi
callq 0x412f30
movq -0x50(%rbp), %rdi
callq 0x421350
movq -0x58(%rbp), %rdi
callq 0x421350
movq -0x30(%rbp), %rdi
callq 0x421350
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)