// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x148, %rsp            # imm = 0x148
movq %rdi, 0x140(%rsp)
movq 0x140(%rsp), %rdi
movl $0x1, %esi
leaq 0x20(%rsp), %rdx
callq 0x33a6b0
movq %rax, 0x18(%rsp)
movq 0x140(%rsp), %rdi
movl $0x2, %esi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x33a620
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rdi
addq $0x8, %rdi
shlq $0x0, %rdi
callq 0x19ef8
movq %rax, 0x138(%rsp)
cmpq $0x0, 0x138(%rsp)
jne 0x35c04f
movq 0x140(%rsp), %rdi
leaq 0x240606(%rip), %rsi    # 0x59c643
callq 0x335740
movq 0x140(%rsp), %rdi
callq 0x337ea0
cmpq $0x0, 0x10(%rsp)
je 0x35c0aa
movq 0x10(%rsp), %rdi
callq 0x192b0
cmpq $0x8, %rax
jb 0x35c0aa
movl $0x0, 0x4(%rsp)
cmpl $0x8, 0x4(%rsp)
jge 0x35c099
movq 0x10(%rsp), %rax
movslq 0x4(%rsp), %rcx
movb (%rax,%rcx), %cl
movslq 0x4(%rsp), %rax
movb %cl, 0x8(%rsp,%rax)
movl 0x4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x35c06f
leaq 0x8(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x4f5750
jmp 0x35c0cb
movq 0x140(%rsp), %rdi
leaq 0x2405b4(%rip), %rsi    # 0x59c66d
callq 0x335740
movq 0x140(%rsp), %rdi
callq 0x337ea0
movl $0x0, 0x34(%rsp)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movq 0x20(%rsp), %rcx
cmpl %ecx, %eax
jge 0x35c14c
movq 0x18(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0x138(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
cmpl $0x8, 0x34(%rsp)
jne 0x35c14a
movl $0x0, 0x34(%rsp)
movq 0x138(%rsp), %rdi
movl 0x2c(%rsp), %eax
subl $0x8, %eax
cltq
addq %rax, %rdi
xorl %esi, %esi
leaq 0x38(%rsp), %rdx
callq 0x4f6370
jmp 0x35c0db
movl $0x0, 0x30(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0x35c1b8
jmp 0x35c15d
cmpl $0x8, 0x34(%rsp)
jge 0x35c198
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
movq 0x138(%rsp), %rax
movl 0x2c(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x2c(%rsp)
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x35c15d
movq 0x138(%rsp), %rdi
movl 0x2c(%rsp), %eax
subl $0x8, %eax
cltq
addq %rax, %rdi
xorl %esi, %esi
leaq 0x38(%rsp), %rdx
callq 0x4f6370
movl 0x30(%rsp), %eax
movb %al, %dl
movq 0x138(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x140(%rsp), %rdi
movq 0x138(%rsp), %rsi
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdx
callq 0x335540
movq 0x138(%rsp), %rdi
callq 0x19ef0
movl $0x1, %eax
addq $0x148, %rsp            # imm = 0x148
retq
nopw (%rax,%rax)