// File: /workspace/github_c_makefile_star2_O0/dasJ[P]sd-zfs/src/zfs-generator.c
// Function: generateScanUnit
int generateScanUnit(char *directory, const char *targetName, const char *unitName, int ignoreCache/* 1 = yes, 0 = no */, char *forceParam, char *poolName) {
	char *unitpath;
	char *targetpath;
	char *cacheLine;
	FILE *fp;

	// Build paths
	targetpath = malloc((strlen(directory) + strlen(targetName) + strlen(unitName) + 3) * sizeof(char));
	strcpy(targetpath, directory);
	strcat(targetpath, "/");
	strcat(targetpath, targetName);
	unitpath = malloc((strlen(directory) + strlen(unitName) + 2) * sizeof(char));
	strcpy(unitpath, directory);
	strcat(unitpath, "/");
	strcat(unitpath, unitName);
	// Make wants directory
	if (mkdir(targetpath, 0775) < 0 && errno != EEXIST) {
		perror("Can not create unit directory\n");
		free(targetpath);
		free(unitpath);
		return(1);
	}
	// Make symlink
	strcat(targetpath, "/");
	strcat(targetpath, unitName);
	symlink(unitName, targetpath);
	// Check if unit already exists
	if (access(unitpath, R_OK) != -1) {
		perror("Scanning unit file already exists or cannot be accessed\n");
		free(unitpath);
		free(targetpath);
		return 0;
	}
	// Check if we need to ignore the cache file
	if (ignoreCache == 0) {
		cacheLine = malloc((strlen("ConditionPathExists=!/etc/zfs/zpool.cache\n") + 1) * sizeof(char));
		strcpy(cacheLine, "ConditionPathExists=!/etc/zfs/zpool.cache\n");
	} else {
		cacheLine = malloc(sizeof(char));
		strcpy(cacheLine, "");
	}
	// Write
	fp = fopen(unitpath, "w");
	if (fp == NULL) {
		perror("Can not write to scanning unit file\n");
		free(unitpath);
		free(cacheLine);
		free(targetpath);
		return 1;
	}
	fprintf(fp, "# This file was generated by sd-zfs\n\
[Unit]\n\
Description=Import ZFS pools by device scanning\n\
DefaultDependencies=no\n\
Requires=systemd-udev-settle.service\n\
After=systemd-udev-settle.service\n\
After=cryptsetup.target\n\
Before=sysroot.mount\n\
%s\
\n\
[Service]\n\
Type=oneshot\n\
RemainAfterExit=yes\n\
ExecStart=/usr/bin/zpool import %s -N -o cachefile=none%s\n", cacheLine, poolName, forceParam);
	fclose(fp);

	free(cacheLine);
	free(unitpath);
	free(targetpath);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0x4010d8
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rdi
callq 0x4010d8
movq %rax, %rcx
movq -0x70(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rdi
callq 0x4010d8
movq -0x68(%rbp), %rdi
addq %rax, %rdi
addq $0x3, %rdi
shlq $0x0, %rdi
callq 0x421010
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x401020
movq -0x48(%rbp), %rdi
leaq 0xa2e60(%rip), %rsi     # 0x4a48fc
callq 0x401058
movq -0x48(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x401058
movq -0x10(%rbp), %rdi
callq 0x4010d8
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rdi
callq 0x4010d8
movq -0x60(%rbp), %rdi
addq %rax, %rdi
addq $0x2, %rdi
shlq $0x0, %rdi
callq 0x421010
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x401020
movq -0x40(%rbp), %rdi
leaq 0xa2e08(%rip), %rsi     # 0x4a48fc
callq 0x401058
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x401058
movq -0x48(%rbp), %rdi
movl $0x1fd, %esi            # imm = 0x1FD
callq 0x455450
cmpl $0x0, %eax
jge 0x401b4d
callq 0x405460
cmpl $0x11, (%rax)
je 0x401b4d
leaq 0x9b67c(%rip), %rdi     # 0x49d1a6
callq 0x40ca90
movq -0x48(%rbp), %rdi
callq 0x421350
movq -0x40(%rbp), %rdi
callq 0x421350
movl $0x1, -0x4(%rbp)
jmp 0x401c8c
movq -0x48(%rbp), %rdi
leaq 0xa2da4(%rip), %rsi     # 0x4a48fc
callq 0x401058
movq -0x48(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x401058
movq -0x20(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x456030
movq -0x40(%rbp), %rdi
movl $0x4, %esi
callq 0x455720
cmpl $-0x1, %eax
je 0x401bb4
leaq 0x9b634(%rip), %rdi     # 0x49d1c5
callq 0x40ca90
movq -0x40(%rbp), %rdi
callq 0x421350
movq -0x48(%rbp), %rdi
callq 0x421350
movl $0x0, -0x4(%rbp)
jmp 0x401c8c
cmpl $0x0, -0x24(%rbp)
jne 0x401bda
movl $0x2b, %edi
callq 0x421010
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
leaq 0x9b62b(%rip), %rsi     # 0x49d1fe
callq 0x401020
jmp 0x401bf8
movl $0x1, %edi
callq 0x421010
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
leaq 0xb322a(%rip), %rsi     # 0x4b4e1d
callq 0x401020
movq -0x40(%rbp), %rdi
leaq 0x9b479(%rip), %rsi     # 0x49d07c
callq 0x413660
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x401c43
leaq 0x9b60f(%rip), %rdi     # 0x49d229
callq 0x40ca90
movq -0x40(%rbp), %rdi
callq 0x421350
movq -0x50(%rbp), %rdi
callq 0x421350
movq -0x48(%rbp), %rdi
callq 0x421350
movl $0x1, -0x4(%rbp)
jmp 0x401c8c
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x30(%rbp), %r8
leaq 0x9b5f4(%rip), %rsi     # 0x49d24e
movb $0x0, %al
callq 0x40c7a0
movq -0x58(%rbp), %rdi
callq 0x412f30
movq -0x50(%rbp), %rdi
callq 0x421350
movq -0x40(%rbp), %rdi
callq 0x421350
movq -0x48(%rbp), %rdi
callq 0x421350
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)