// File: /workspace/github_c_makefile_star2_O3/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, 0x15d91(%rip)     # 0x33558
jne 0x1d7ce
callq 0x1e0b7
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 0x1d7d0
movq %rsp, %rdi
callq 0x1db4d
movsbl (%r14), %eax
cmpl $0x5f, %eax
jne 0x1d850
leaq 0x1(%r14), %rax
xorl %edx, %edx
xorl %ecx, %ecx
xorl %ebx, %ebx
movsbl (%rax), %esi
movl $0x0, %edi
cmpl $0x7a, %esi
jg 0x1d823
cmpb $0x61, %sil
jl 0x1d81d
addl $-0x3b, %esi
movl %esi, %edi
jmp 0x1d823
cmpb $0x5a, %sil
jle 0x1d836
shll %cl, %edi
orl %edi, %ebx
addq $0x6, %rcx
incq %rax
cmpq $0x18, %rcx
jne 0x1d803
jmp 0x1d877
cmpb $0x41, %sil
jl 0x1d841
addl $-0x35, %esi
jmp 0x1d819
leal -0x2e(%rsi), %edi
addl $-0x2e, %esi
cmpb $0xc, %dil
cmovael %edx, %esi
jmp 0x1d819
movsbl 0x1(%r14), %ecx
xorl %r13d, %r13d
movl $0x0, %edx
cmpl $0x7a, %ecx
jg 0x1d9c3
cmpb $0x61, %cl
jl 0x1d9a5
leal -0x3b(%rcx), %edx
jmp 0x1d9c3
leaq 0x5(%r14), %rax
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r13d, %r13d
movsbl (%rax), %esi
movl $0x0, %edi
cmpl $0x7a, %esi
jg 0x1d8a2
cmpb $0x61, %sil
jl 0x1d89c
addl $-0x3b, %esi
movl %esi, %edi
jmp 0x1d8a2
cmpb $0x5a, %sil
jle 0x1d8b6
shll %cl, %edi
orl %edi, %r13d
addq $0x6, %rcx
incq %rax
cmpq $0x18, %rcx
jne 0x1d882
jmp 0x1d8d0
cmpb $0x41, %sil
jl 0x1d8c1
addl $-0x35, %esi
jmp 0x1d898
leal -0x2e(%rsi), %edi
addl $-0x2e, %esi
cmpb $0xc, %dil
cmovael %edx, %esi
jmp 0x1d898
movq %r14, 0x10(%rsp)
cmpb $0x0, (%r15)
je 0x1d975
leaq 0x8(%rsp), %rbp
xorl %r12d, %r12d
movq %rsp, %r14
cmpb $0x0, 0x15c67(%rip)     # 0x33558
jne 0x1d8f8
callq 0x1e0b7
cmpl $0x0, 0x15c69(%rip)     # 0x33568
je 0x1d90f
movl %r12d, 0x15c60(%rip)    # 0x33568
movl %r12d, 0x15c55(%rip)    # 0x33564
movl (%rsp), %edi
movl 0x4(%rsp), %esi
bswapl %edi
bswapl %esi
leaq 0xc(%rsp), %rdx
movq %rbp, %rcx
movl $0x1, %r8d
callq 0x1ddaf
movl 0xc(%rsp), %ecx
bswapl %ecx
movl 0x8(%rsp), %edx
bswapl %edx
movl %ecx, (%rsp)
movl %edx, 0x4(%rsp)
testl %eax, %eax
jne 0x1db3b
xorl %eax, %eax
movb (%r15,%rax), %cl
testb %cl, %cl
je 0x1d960
addb %cl, %cl
xorb %cl, (%rsp,%rax)
incq %rax
cmpq $0x8, %rax
jne 0x1d94a
addq %rax, %r15
movq %r14, %rdi
callq 0x1db4d
cmpb $0x0, (%r15)
jne 0x1d8ea
leaq 0x15bc4(%rip), %r15     # 0x33540
movl $0x9, %edx
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x60b0
movb $0x0, 0x15bb4(%rip)     # 0x33549
movq %r15, %rdi
callq 0x6170
movq %rax, %r14
addq %r15, %r14
jmp 0x1da1e
cmpb $0x5a, %cl
jg 0x1d9c3
cmpb $0x41, %cl
jl 0x1d9b4
leal -0x35(%rcx), %edx
jmp 0x1d9c3
leal -0x2e(%rcx), %esi
leal -0x2e(%rcx), %edi
xorl %edx, %edx
cmpb $0xc, %sil
cmovbl %edi, %edx
shll $0x6, %edx
cmpb $0x7a, %al
jg 0x1d9fb
cmpb $0x61, %al
jl 0x1d9d7
movl %eax, %r13d
addl $-0x3b, %r13d
jmp 0x1d9fb
cmpb $0x5a, %al
jg 0x1d9fb
cmpb $0x41, %al
jl 0x1d9e8
movl %eax, %r13d
addl $-0x35, %r13d
jmp 0x1d9fb
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, 0x15b34(%rip)      # 0x33540
movb %dl, 0x15b2f(%rip)      # 0x33541
leaq 0x15b29(%rip), %r14     # 0x33542
movl $0x19, %ebx
cmpl %r13d, 0x15b43(%rip)    # 0x33568
je 0x1da5e
movl %r13d, 0x15b3a(%rip)    # 0x33568
movl $0x0, 0x15b2c(%rip)     # 0x33564
xorl %eax, %eax
movl $0x800000, %ecx         # imm = 0x800000
movl $0x1, %edx
movl $0x18, %esi
testl %r13d, %edx
je 0x1da56
orl %ecx, %eax
movl %eax, 0x15b0e(%rip)     # 0x33564
addl %edx, %edx
shrl %ecx
decl %esi
jne 0x1da49
xorl %r12d, %r12d
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
xorl %edi, %edi
xorl %esi, %esi
movl %ebx, %r8d
callq 0x1ddaf
testl %eax, %eax
jne 0x1db3b
movl 0xc(%rsp), %ecx
movl %ecx, %edx
shrl $0x8, %edx
movl %ecx, %esi
shrl $0x1a, %esi
leaq 0x5fac(%rip), %rax      # 0x23a40
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 0x15a05(%rip), %r12     # 0x33540
movq %r12, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq