// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/md5-1.2/src/ldes56.c
// Function: des56_crypt
static int des56_crypt( lua_State *L )
{
  char *cypheredText;
  keysched KS;
  int rel_index, pad, abs_index;
  size_t plainlen;
  const char *plainText = luaL_checklstring( L, 1, &plainlen );
  const char *key = luaL_optstring( L, 2, NULL );

  cypheredText = (char *) malloc( (plainlen+8) * sizeof(char));
  if(cypheredText == NULL) {
    lua_pushstring(L, "Error encrypting file. Not enough memory."); 
    lua_error(L);
  }

  if (key && strlen(key) >= 8)
  {
    char k[8];
    int i;

    for (i=0; i<8; i++)
      k[i] = (unsigned char)key[i];
    fsetkey(k, &KS);
  } else {
    lua_pushstring(L, "Error encrypting file. Invalid key.");
    lua_error(L);
  }

  rel_index = 0;
  abs_index = 0;
  while (abs_index < (int) plainlen) {
    cypheredText[abs_index] = plainText[abs_index];
    abs_index++;
    rel_index++;
    if( rel_index == 8 ) {
      rel_index = 0;
      fencrypt(&(cypheredText[abs_index - 8]), 0, &KS);
    }
  }

  pad = 0;
  if(rel_index != 0) { /* Pads remaining bytes with zeroes */
    while(rel_index < 8)
    {
      pad++;
      cypheredText[abs_index++] = 0;
      rel_index++;
    }
    fencrypt(&(cypheredText[abs_index - 8]), 0, &KS);
  }
  cypheredText[abs_index] = pad;

  lua_pushlstring( L, cypheredText, abs_index+1 );
  free( cypheredText );
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdi, %rbx
pushq $0x1
popq %rsi
leaq 0x8(%rsp), %r14
movq %r14, %rdx
callq 0x1de9c6
movq %rax, %r15
pushq $0x2
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1de96d
movq %rax, %r12
movq (%r14), %rdi
addq $0x8, %rdi
callq 0x17f50
movq %rax, %r14
testq %rax, %rax
jne 0x1ef9bf
leaq 0x179b24(%rip), %rsi    # 0x3694d3
movq %rbx, %rdi
callq 0x1dc009
movq %rbx, %rdi
callq 0x1dd306
testq %r12, %r12
movq %rbx, (%rsp)
je 0x1ef9eb
movq %r12, %rdi
callq 0x172d0
cmpq $0x8, %rax
jb 0x1ef9eb
xorl %eax, %eax
cmpq $0x8, %rax
je 0x1efa04
movb (%r12,%rax), %cl
movb %cl, 0x10(%rsp,%rax)
incq %rax
jmp 0x1ef9d8
leaq 0x179b0b(%rip), %rsi    # 0x3694fd
movq %rbx, %rdi
callq 0x1dc009
movq %rbx, %rdi
callq 0x1dd306
jmp 0x1efa13
leaq 0x10(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x2d5584
leaq -0x7(%r14), %rbp
xorl %r13d, %r13d
leaq 0x18(%rsp), %r12
xorl %ebx, %ebx
movslq 0x8(%rsp), %rax
cmpq %rax, %r13
jge 0x1efa51
movb (%r15,%r13), %al
movb %al, (%r14,%r13)
incl %ebx
cmpl $0x8, %ebx
jne 0x1efa4c
movq %rbp, %rdi
addq %r13, %rdi
xorl %ebx, %ebx
xorl %esi, %esi
movq %r12, %rdx
callq 0x2d5b2f
incq %r13
jmp 0x1efa21
testl %ebx, %ebx
je 0x1efa8d
movl %ebx, %eax
xorl %r15d, %r15d
movq (%rsp), %rbx
leal (%rax,%r15), %ecx
cmpl $0x7, %ecx
jg 0x1efa74
movb $0x0, (%r14,%r13)
incq %r13
incq %r15
jmp 0x1efa5e
movl %r13d, %eax
leaq (%r14,%rax), %rdi
addq $-0x8, %rdi
leaq 0x18(%rsp), %rdx
xorl %esi, %esi
callq 0x2d5b2f
jmp 0x1efa94
xorl %r15d, %r15d
movq (%rsp), %rbx
movslq %r13d, %rax
movb %r15b, (%r14,%rax)
incl %eax
movslq %eax, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1dbe62
movq %r14, %rdi
callq 0x17f48
pushq $0x1
popq %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq