// File: /workspace/github_c_makefile_star2_O1/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, %r13
movq %rsp, %r14
movl $0x1, %esi
movq %r14, %rdx
callq 0x230d0f
movq %rax, %r15
movq %r13, %rdi
movl $0x2, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x230cab
movq %rax, %r12
movq (%r14), %rdi
addq $0x8, %rdi
callq 0x17ff0
movq %rax, %r14
testq %rax, %rax
jne 0x244f4a
leaq 0x18d8b9(%rip), %rsi    # 0x3d27f3
movq %r13, %rdi
callq 0x22e0c4
movq %r13, %rdi
callq 0x22f4f8
testq %r12, %r12
je 0x244f81
movq %r12, %rdi
callq 0x172f0
cmpq $0x8, %rax
jb 0x244f81
xorl %eax, %eax
movb (%r12,%rax), %cl
movb %cl, 0x10(%rsp,%rax)
incq %rax
cmpq $0x8, %rax
jne 0x244f5f
leaq 0x10(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x33dcc8
jmp 0x244f98
leaq 0x18d895(%rip), %rsi    # 0x3d281d
movq %r13, %rdi
callq 0x22e0c4
movq %r13, %rdi
callq 0x22f4f8
cmpl $0x0, (%rsp)
jle 0x244fe5
movq %r13, 0x8(%rsp)
leaq -0x7(%r14), %r12
xorl %ebp, %ebp
leaq 0x18(%rsp), %r13
xorl %ebx, %ebx
movb (%r15,%rbp), %al
movb %al, 0x7(%r12)
incl %ebx
cmpl $0x8, %ebx
jne 0x244fcf
xorl %ebx, %ebx
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x33e32c
incq %rbp
movslq (%rsp), %rax
incq %r12
cmpq %rax, %rbp
jl 0x244fb0
movq 0x8(%rsp), %r13
jmp 0x244fe9
xorl %ebp, %ebp
xorl %ebx, %ebx
testl %ebx, %ebx
je 0x245018
cmpl $0x7, %ebx
jg 0x24501d
movl %ebp, %edi
addq %r14, %rdi
movl $0x7, %r15d
subl %ebx, %r15d
leaq 0x1(%r15), %rdx
xorl %esi, %esi
callq 0x173a0
addl %r15d, %ebp
incl %ebp
movb $0x8, %r15b
subb %bl, %r15b
jmp 0x245020
xorl %r15d, %r15d
jmp 0x245036
xorl %r15d, %r15d
movl %ebp, %eax
leaq (%r14,%rax), %rdi
addq $-0x8, %rdi
leaq 0x18(%rsp), %rdx
xorl %esi, %esi
callq 0x33e32c
movslq %ebp, %rax
movb %r15b, (%r14,%rax)
incl %eax
movslq %eax, %rdx
movq %r13, %rdi
movq %r14, %rsi
callq 0x22deed
movq %r14, %rdi
callq 0x17fe8
movl $0x1, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq