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

  padinfo = cypheredText[cypherlen-1];
  cypherlen--;

  /* Aloca array */
  decypheredText = 
    (char *) malloc( (cypherlen+1) * sizeof(char));
  if(decypheredText == NULL) {
    lua_pushstring(L, "Error decrypting file. Not enough memory.");
    lua_error(L);
  }

  /* Inicia decifragem */
  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 decrypting file. Invalid key.");
    lua_error(L);
  }

  rel_index = 0;
  abs_index = 0;

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

  lua_pushlstring(L, decypheredText, (abs_index-padinfo));
  free( decypheredText );
  return 1;
}

// Assembly:
subq $0x148, %rsp            # imm = 0x148
movq %rdi, 0x140(%rsp)
movq 0x140(%rsp), %rdi
movl $0x1, %esi
leaq 0x28(%rsp), %rdx
callq 0x33a6b0
movq %rax, 0x20(%rsp)
movq 0x140(%rsp), %rdi
movl $0x2, %esi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x33a620
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
movl %eax, 0x14(%rsp)
movq 0x28(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rdi
addq $0x1, %rdi
shlq $0x0, %rdi
callq 0x19ef8
movq %rax, 0x138(%rsp)
cmpq $0x0, 0x138(%rsp)
jne 0x35c2c3
movq 0x140(%rsp), %rdi
leaq 0x2403e0(%rip), %rsi    # 0x59c691
callq 0x335740
movq 0x140(%rsp), %rdi
callq 0x337ea0
cmpq $0x0, 0x18(%rsp)
je 0x35c31e
movq 0x18(%rsp), %rdi
callq 0x192b0
cmpq $0x8, %rax
jb 0x35c31e
movl $0x0, 0x8(%rsp)
cmpl $0x8, 0x8(%rsp)
jge 0x35c30d
movq 0x18(%rsp), %rax
movslq 0x8(%rsp), %rcx
movb (%rax,%rcx), %cl
movslq 0x8(%rsp), %rax
movb %cl, 0xc(%rsp,%rax)
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0x35c2e3
leaq 0xc(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x4f5750
jmp 0x35c33f
movq 0x140(%rsp), %rdi
leaq 0x24038e(%rip), %rsi    # 0x59c6bb
callq 0x335740
movq 0x140(%rsp), %rdi
callq 0x337ea0
movl $0x0, 0x34(%rsp)
movl $0x0, 0x30(%rsp)
movl 0x30(%rsp), %eax
movq 0x28(%rsp), %rcx
cmpl %ecx, %eax
jge 0x35c3c3
movq 0x20(%rsp), %rax
movslq 0x30(%rsp), %rcx
movb (%rax,%rcx), %dl
movq 0x138(%rsp), %rax
movslq 0x30(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
cmpl $0x8, 0x34(%rsp)
jne 0x35c3c1
movl $0x0, 0x34(%rsp)
movq 0x138(%rsp), %rdi
movl 0x30(%rsp), %eax
subl $0x8, %eax
cltq
addq %rax, %rdi
movl $0x1, %esi
leaq 0x38(%rsp), %rdx
callq 0x4f6370
jmp 0x35c34f
movq 0x138(%rsp), %rax
movslq 0x30(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movq 0x140(%rsp), %rdi
movq 0x138(%rsp), %rsi
movl 0x30(%rsp), %eax
subl 0x14(%rsp), %eax
movslq %eax, %rdx
callq 0x335540
movq 0x138(%rsp), %rdi
callq 0x19ef0
movl $0x1, %eax
addq $0x148, %rsp            # imm = 0x148
retq
nop