// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, -0x38(%rbp)
movq %r8, -0x40(%rbp)
movl %ecx, -0x2c(%rbp)
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
callq 0x4010d8
movq %rax, %rbx
movq %r12, %rdi
callq 0x4010d8
movq %rax, %r13
movq %r14, %rdi
callq 0x4010d8
addq %rbx, %r13
leaq (%rax,%r13), %rdi
addq $0x3, %rdi
callq 0x4210c0
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x401020
movq %rbx, %rdi
callq 0x4010d8
movw $0x2f, %r13w
movw %r13w, (%rbx,%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x401058
movq %r15, %rdi
callq 0x4010d8
movq %rax, %r12
movq %r14, %rdi
callq 0x4010d8
leaq (%r12,%rax), %rdi
addq $0x2, %rdi
callq 0x4210c0
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
callq 0x401020
movq %r12, %rdi
callq 0x4010d8
movw %r13w, (%r12,%rax)
movq %r12, %rdi
movq %r14, %rsi
callq 0x401058
movq %rbx, %rdi
movl $0x1fd, %esi            # imm = 0x1FD
callq 0x455510
testl %eax, %eax
jns 0x401c73
callq 0x4051e0
cmpl $0x11, (%rax)
jne 0x401d8d
movq %rbx, %rdi
callq 0x4010d8
movw $0x2f, (%rbx,%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x401058
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4560f0
movq %r12, %rdi
movl $0x4, %esi
callq 0x4557e0
cmpl $-0x1, %eax
jne 0x401d60
cmpl $0x0, -0x2c(%rbp)
je 0x401cc5
movl $0x1, %edi
callq 0x4210c0
movq %rax, %r14
movb $0x0, (%rax)
jmp 0x401cf2
movl $0x2b, %edi
callq 0x4210c0
movq %rax, %r14
movups 0x9b51d(%rip), %xmm0    # 0x49d1f6
movups %xmm0, (%rax)
movups 0x9b523(%rip), %xmm0    # 0x49d206
movups %xmm0, 0x10(%rax)
movups 0x9b523(%rip), %xmm0    # 0x49d211
movups %xmm0, 0x1b(%rax)
leaq 0x9b383(%rip), %rsi     # 0x49d07c
movq %r12, %rdi
callq 0x4133e0
testq %rax, %rax
je 0x401d71
movq %rax, %r13
leaq 0x9b536(%rip), %rsi     # 0x49d246
xorl %r15d, %r15d
movq %rax, %rdi
movq %r14, %rdx
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
xorl %eax, %eax
callq 0x40c520
movq %r13, %rdi
callq 0x412cb0
movq %r14, %rdi
callq 0x421400
movq %r12, %r14
movq %rbx, %r12
movq %r14, %rdi
callq 0x421400
movq %r12, %rdi
callq 0x421400
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9b456(%rip), %rdi     # 0x49d1bd
callq 0x40c810
xorl %r15d, %r15d
jmp 0x401d38
leaq 0x9b4a9(%rip), %rdi     # 0x49d221
callq 0x40c810
movq %r12, %rdi
callq 0x421400
movl $0x1, %r15d
jmp 0x401d3b
leaq 0x9b40a(%rip), %rdi     # 0x49d19e
callq 0x40c810
movl $0x1, %r15d
movq %rbx, %r14
jmp 0x401d3e