// File: /workspace/github_c_makefile_star2_O3/dasJ[P]sd-zfs/src/zfs-generator.c
// Function: generateCacheUnit
int generateCacheUnit(char *directory, const char *targetName, const char *unitName, char *forceParam, char *poolName) {
	char *unitpath;
	char *targetpath;
	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) {
		free(unitpath);
		free(targetpath);
		printf("Caching unit file already exists\n");
		return 0;
	}
	// Write
	fp = fopen(unitpath, "w");
	if (fp == NULL) {
		perror("Cannot write to scanning unit file\n");
		free(unitpath);
		free(targetpath);
		return 1;
	}
	fprintf(fp, "# This file was generated by sd-zfs\n\
[Unit]\n\
Description=Import ZFS pools by cache file\n\
DefaultDependencies=no\n\
Requires=systemd-udev-settle.service\n\
After=systemd-udev-settle.service\n\
After=cryptsetup.target\n\
Before=sysroot.mount\n\
ConditionPathExists=/etc/zfs/zpool.cache\n\
\n\
[Service]\n\
Type=oneshot\n\
RemainAfterExit=yes\n\
ExecStart=/usr/bin/zpool import %s -N -c /etc/zfs/zpool.cache%s\n", poolName, forceParam);
	fclose(fp);
	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 %r8, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
movq %rdx, %r13
movq %rdx, -0x40(%rbp)
movq %rsi, %r12
movq %rdi, %r14
callq 0x4010d8
movq %rax, %r15
movq %r12, %rdi
callq 0x4010d8
movq %rax, %rbx
movq %r13, %rdi
callq 0x4010d8
movq %rax, %r13
addq %r15, %rax
leaq (%rbx,%rax), %rdi
addq $0x3, %rdi
callq 0x4210d0
movq %rax, %rbx
movq %rax, %rdi
movq %r14, %rsi
callq 0x401020
movq %rbx, %rdi
callq 0x4010d8
movw $0x2f, %cx
movw %cx, (%rbx,%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x401058
leaq (%r15,%r13), %rdi
addq $0x2, %rdi
callq 0x4210d0
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
movq -0x40(%rbp), %r14
callq 0x401020
movq %r12, %rdi
callq 0x4010d8
movw $0x2f, %cx
movw %cx, (%r12,%rax)
movq %r12, %rdi
movq %r14, %rsi
callq 0x401058
movq %rbx, %rdi
movl $0x1fd, %esi            # imm = 0x1FD
callq 0x455510
testl %eax, %eax
jns 0x401e84
callq 0x4051f0
cmpl $0x11, (%rax)
jne 0x401f38
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
je 0x401edb
movq %r12, %rdi
callq 0x421410
movq %rbx, %rdi
callq 0x421410
leaq 0x9b90a(%rip), %rdi     # 0x49d7db
callq 0x413a80
xorl %r14d, %r14d
jmp 0x401f26
leaq 0x9b19a(%rip), %rsi     # 0x49d07c
movq %r12, %rdi
callq 0x4133f0
testq %rax, %rax
je 0x401f51
movq %rax, %r15
leaq 0x9b4c0(%rip), %rsi     # 0x49d3b9
xorl %r14d, %r14d
movq %rax, %rdi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
xorl %eax, %eax
callq 0x40c530
movq %r15, %rdi
callq 0x412cc0
movq %r12, %rdi
callq 0x421410
movq %rbx, %rdi
callq 0x421410
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9b25f(%rip), %rdi     # 0x49d19e
callq 0x40c820
movq %rbx, %rdi
callq 0x421410
movq %r12, %rdi
jmp 0x401f68
leaq 0x9b43d(%rip), %rdi     # 0x49d395
callq 0x40c820
movq %r12, %rdi
callq 0x421410
movq %rbx, %rdi
callq 0x421410
movl $0x1, %r14d
jmp 0x401f26