// File: /workspace/github_c_makefile_star2_O1/ajpaulson[P]srogue11/xcrypt.c
// Function: xcrypt
char *xcrypt(const char *key,const char *setting)
{
	int		i;
	unsigned int	count, salt, l, r0, r1, keybuf[2];
	unsigned char		*p, *q;
	static unsigned char	output[21];

	if (!des_initialised)
		des_init();

	/*
	 * Copy the key, shifting each character up by one bit
	 * and padding with zeros.
	 */
	q = (unsigned char *) keybuf;
	while ((q - (unsigned char *) keybuf) < sizeof(keybuf)) {
		if ((*q++ = *key << 1))
			key++;
	}
	if (des_setkey((const char *) keybuf))
		return(NULL);

	if (*setting == _PASSWORD_EFMT1) {
		/*
		 * "new"-style:
		 *	setting - underscore, 4 bytes of count, 4 bytes of salt
		 *	key - unlimited characters
		 */
		for (i = 1, count = 0; i < 5; i++)
			count |= ascii_to_bin(setting[i]) << (i - 1) * 6;

		for (i = 5, salt = 0; i < 9; i++)
			salt |= ascii_to_bin(setting[i]) << (i - 5) * 6;

		while (*key) {
			/*
			 * Encrypt the key with itself.
			 */
			if (des_cipher((const char*)keybuf, (char*)keybuf, 0, 1))
				return(NULL);
			/*
			 * And XOR with the next 8 characters of the key.
			 */
			q = (unsigned char *) keybuf;
			while (((q - (unsigned char *) keybuf) < sizeof(keybuf)) &&
					*key)
				*q++ ^= *key++ << 1;

			if (des_setkey((const char *) keybuf))
				return(NULL);
		}
		strncpy((char *)output, setting, 9);

		/*
		 * Double check that we weren't given a short setting.
		 * If we were, the above code will probably have created
		 * wierd values for count and salt, but we don't really care.
		 * Just make sure the output string doesn't have an extra
		 * NUL in it.
		 */
		output[9] = '\0';
		p = output + strlen((const char *)output);
	} else {
		/*
		 * "old"-style:
		 *	setting - 2 bytes of salt
		 *	key - up to 8 characters
		 */
		count = 25;

		salt = (ascii_to_bin(setting[1]) << 6)
		     |  ascii_to_bin(setting[0]);

		output[0] = setting[0];
		/*
		 * If the encrypted password that the salt was extracted from
		 * is only 1 character long, the salt will be corrupted.  We
		 * need to ensure that the output string doesn't have an extra
		 * NUL in it!
		 */
		output[1] = setting[1] ? setting[1] : output[0];

		p = output + 2;
	}
	setup_salt(salt);
	/*
	 * Do it.
	 */
	if (do_des(0, 0, &r0, &r1, count))
		return(NULL);
	/*
	 * Now encode the result...
	 */
	l = (r0 >> 8);
	*p++ = ascii64[(l >> 18) & 0x3f];
	*p++ = ascii64[(l >> 12) & 0x3f];
	*p++ = ascii64[(l >> 6) & 0x3f];
	*p++ = ascii64[l & 0x3f];

	l = (r0 << 16) | ((r1 >> 16) & 0xffff);
	*p++ = ascii64[(l >> 18) & 0x3f];
	*p++ = ascii64[(l >> 12) & 0x3f];
	*p++ = ascii64[(l >> 6) & 0x3f];
	*p++ = ascii64[l & 0x3f];

	l = r1 << 2;
	*p++ = ascii64[(l >> 12) & 0x3f];
	*p++ = ascii64[(l >> 6) & 0x3f];
	*p++ = ascii64[l & 0x3f];
	*p = 0;

	return((char *)output);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %r15
cmpb $0x0, 0x15ca5(%rip)     # 0x33558
jne 0x1d8ba
callq 0x1e1b1
xorl %eax, %eax
movb (%r15), %cl
addb %cl, %cl
movb %cl, (%rsp,%rax)
incq %rax
cmpb $0x1, %cl
sbbq $-0x1, %r15
cmpq $0x8, %rax
jne 0x1d8bc
movq %rsp, %rdi
callq 0x1dc39
movsbl (%r14), %eax
cmpl $0x5f, %eax
jne 0x1d93c
leaq 0x1(%r14), %rax
xorl %edx, %edx
xorl %ecx, %ecx
xorl %ebx, %ebx
movsbl (%rax), %esi
movl $0x0, %edi
cmpl $0x7a, %esi
jg 0x1d90f
cmpb $0x61, %sil
jl 0x1d909
addl $-0x3b, %esi
movl %esi, %edi
jmp 0x1d90f
cmpb $0x5a, %sil
jle 0x1d922
shll %cl, %edi
orl %edi, %ebx
addq $0x6, %rcx
incq %rax
cmpq $0x18, %rcx
jne 0x1d8ef
jmp 0x1d963
cmpb $0x41, %sil
jl 0x1d92d
addl $-0x35, %esi
jmp 0x1d905
leal -0x2e(%rsi), %edi
addl $-0x2e, %esi
cmpb $0xc, %dil
cmovael %edx, %esi
jmp 0x1d905
movsbl 0x1(%r14), %ecx
xorl %r13d, %r13d
movl $0x0, %edx
cmpl $0x7a, %ecx
jg 0x1daaf
cmpb $0x61, %cl
jl 0x1da91
leal -0x3b(%rcx), %edx
jmp 0x1daaf
leaq 0x5(%r14), %rax
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r13d, %r13d
movsbl (%rax), %esi
movl $0x0, %edi
cmpl $0x7a, %esi
jg 0x1d98e
cmpb $0x61, %sil
jl 0x1d988
addl $-0x3b, %esi
movl %esi, %edi
jmp 0x1d98e
cmpb $0x5a, %sil
jle 0x1d9a2
shll %cl, %edi
orl %edi, %r13d
addq $0x6, %rcx
incq %rax
cmpq $0x18, %rcx
jne 0x1d96e
jmp 0x1d9bc
cmpb $0x41, %sil
jl 0x1d9ad
addl $-0x35, %esi
jmp 0x1d984
leal -0x2e(%rsi), %edi
addl $-0x2e, %esi
cmpb $0xc, %dil
cmovael %edx, %esi
jmp 0x1d984
movq %r14, 0x10(%rsp)
cmpb $0x0, (%r15)
je 0x1da61
leaq 0x8(%rsp), %rbp
xorl %r12d, %r12d
movq %rsp, %r14
cmpb $0x0, 0x15b7b(%rip)     # 0x33558
jne 0x1d9e4
callq 0x1e1b1
cmpl $0x0, 0x15b7d(%rip)     # 0x33568
je 0x1d9fb
movl %r12d, 0x15b74(%rip)    # 0x33568
movl %r12d, 0x15b69(%rip)    # 0x33564
movl (%rsp), %edi
movl 0x4(%rsp), %esi
bswapl %edi
bswapl %esi
leaq 0xc(%rsp), %rdx
movq %rbp, %rcx
movl $0x1, %r8d
callq 0x1de9b
movl 0xc(%rsp), %ecx
bswapl %ecx
movl 0x8(%rsp), %edx
bswapl %edx
movl %ecx, (%rsp)
movl %edx, 0x4(%rsp)
testl %eax, %eax
jne 0x1dc27
xorl %eax, %eax
movb (%r15,%rax), %cl
testb %cl, %cl
je 0x1da4c
addb %cl, %cl
xorb %cl, (%rsp,%rax)
incq %rax
cmpq $0x8, %rax
jne 0x1da36
addq %rax, %r15
movq %r14, %rdi
callq 0x1dc39
cmpb $0x0, (%r15)
jne 0x1d9d6
leaq 0x15ad8(%rip), %r15     # 0x33540
movl $0x9, %edx
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x60b0
movb $0x0, 0x15ac8(%rip)     # 0x33549
movq %r15, %rdi
callq 0x6170
movq %rax, %r14
addq %r15, %r14
jmp 0x1db0a
cmpb $0x5a, %cl
jg 0x1daaf
cmpb $0x41, %cl
jl 0x1daa0
leal -0x35(%rcx), %edx
jmp 0x1daaf
leal -0x2e(%rcx), %esi
leal -0x2e(%rcx), %edi
xorl %edx, %edx
cmpb $0xc, %sil
cmovbl %edi, %edx
shll $0x6, %edx
cmpb $0x7a, %al
jg 0x1dae7
cmpb $0x61, %al
jl 0x1dac3
movl %eax, %r13d
addl $-0x3b, %r13d
jmp 0x1dae7
cmpb $0x5a, %al
jg 0x1dae7
cmpb $0x41, %al
jl 0x1dad4
movl %eax, %r13d
addl $-0x35, %r13d
jmp 0x1dae7
leal -0x2e(%rax), %esi
movl %eax, %edi
addl $-0x2e, %edi
xorl %r13d, %r13d
cmpb $0xc, %sil
cmovbl %edi, %r13d
addl %edx, %r13d
testb %cl, %cl
movzbl %al, %edx
cmovnel %ecx, %edx
movb %al, 0x15a48(%rip)      # 0x33540
movb %dl, 0x15a43(%rip)      # 0x33541
leaq 0x15a3d(%rip), %r14     # 0x33542
movl $0x19, %ebx
cmpl %r13d, 0x15a57(%rip)    # 0x33568
je 0x1db4a
movl %r13d, 0x15a4e(%rip)    # 0x33568
movl $0x0, 0x15a40(%rip)     # 0x33564
xorl %eax, %eax
movl $0x800000, %ecx         # imm = 0x800000
movl $0x1, %edx
movl $0x18, %esi
testl %r13d, %edx
je 0x1db42
orl %ecx, %eax
movl %eax, 0x15a22(%rip)     # 0x33564
addl %edx, %edx
shrl %ecx
decl %esi
jne 0x1db35
xorl %r12d, %r12d
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
xorl %edi, %edi
xorl %esi, %esi
movl %ebx, %r8d
callq 0x1de9b
testl %eax, %eax
jne 0x1dc27
movl 0xc(%rsp), %ecx
movl %ecx, %edx
shrl $0x8, %edx
movl %ecx, %esi
shrl $0x1a, %esi
leaq 0x5e20(%rip), %rax      # 0x239a0
movb (%rsi,%rax), %sil
movb %sil, (%r14)
movl %ecx, %esi
shrl $0x14, %esi
andl $0x3f, %esi
movb (%rsi,%rax), %sil
movb %sil, 0x1(%r14)
movl %ecx, %esi
shrl $0xe, %esi
andl $0x3f, %esi
movb (%rsi,%rax), %sil
movb %sil, 0x2(%r14)
andl $0x3f, %edx
movb (%rdx,%rax), %dl
movb %dl, 0x3(%r14)
movl 0x8(%rsp), %edx
shldl $0x10, %edx, %ecx
movl %ecx, %esi
shrl $0x12, %esi
andl $0x3f, %esi
movb (%rsi,%rax), %sil
movb %sil, 0x4(%r14)
movl %ecx, %esi
shrl $0xc, %esi
andl $0x3f, %esi
movb (%rsi,%rax), %sil
movb %sil, 0x5(%r14)
movl %ecx, %esi
shrl $0x6, %esi
andl $0x3f, %esi
movb (%rsi,%rax), %sil
movb %sil, 0x6(%r14)
andl $0x3f, %ecx
movb (%rcx,%rax), %cl
movb %cl, 0x7(%r14)
movl %edx, %ecx
shrl $0xa, %ecx
andl $0x3f, %ecx
movb (%rcx,%rax), %cl
movb %cl, 0x8(%r14)
movl %edx, %ecx
shrl $0x4, %ecx
andl $0x3f, %ecx
movb (%rcx,%rax), %cl
movb %cl, 0x9(%r14)
andl $0xf, %edx
movb (%rax,%rdx,4), %al
movb %al, 0xa(%r14)
movb $0x0, 0xb(%r14)
leaq 0x15919(%rip), %r12     # 0x33540
movq %r12, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq