// File: /workspace/github_c_makefile_star2_O0/jogolden[P]ps4flashtool/crypto.c
// Function: arzl_deobfuscate
int arzl_deobfuscate(unsigned char *buffer, int len, int version) {
  unsigned char *buf; // r3@1
  unsigned char *bufend; // r12@1
  unsigned int data; // t1@3 MAPDST
  unsigned int change_stride; // r5@3
  unsigned char *buf_1; // r3@8
  unsigned int v10; // r4@8
  unsigned int v11; // r5@8
  unsigned int v12; // t1@12
  unsigned int v13; // r4@13
  unsigned int v14; // r4@14
  intptr_t offset;

  buf = buffer;
  bufend = &buffer[len];
  if ( len )
  {
    if ( version == 2 )
    {
      do
      {
        data = *(uint32_t *)buf;
        buf += 4;
        change_stride = (data & 0xF800F800) >> 27;
        offset = (buf - buffer);
        if ( (data & 0xF800F800) == 0xF800F000 )
        {
          v14 = (((data >> 16) & 0xFFC007FF) | ((data & 0x7FF) << 11)) - (offset >> 1);
          v14 = ((((v14 & 0x7FF) << 16) | 0xF800F000) & 0xFFFFF800) | ((v14 >> 11) & 0x7FF);
          *((uint32_t *)buf - 1) = v14;
        }
        else if ( (data & 0x8000FBF0) == 0x0000F2C0 )
        {
          v14 = (data & 0xF0FFFFF0) | ((data & 0xF) << 24) | ((data >> 24) & 0xF);
          *((uint32_t *)buf - 1) = v14;
        }
        else if ( change_stride == 30 )
        {
          buf -= 2;
        }
      }
      while ( bufend > buf );
    }
    else if ( version == 1 )
    {
      buf_1 = buffer + 4;
      v10 = *(uint32_t *)buffer;
      v11 = (*(uint32_t *)buffer & 0xF800F800) >> 27;
      if ( (*(uint32_t *)buffer & 0xF800F800) == 0xF800F000 )
        goto LABEL_13;
LABEL_9:
      if ( v11 == 30 )
        buf_1 -= 2;
      while ( buf_1 < bufend )
      {
        v12 = *(uint32_t *)buf_1;
        buf_1 += 4;
        v10 = v12;
        v11 = (v12 & 0xF800F800) >> 27;
        if ( (v12 & 0xF800F800) != 0xF800F000 )
          goto LABEL_9;
LABEL_13:
        v13 = (((v10 >> 16) & 0xFFC007FF) | ((v10 & 0x7FF) << 11)) + ((buf_1 - buffer) >> 1);
        *((uint32_t *)buf_1 - 1) = ((((v13 & 0x7FF) << 16) | 0xF800F000) & 0xFFFFF800) | ((v13 >> 11) & 0x7FF);
      }
    }
    else
    {
      do
      {
        data = *(uint32_t *)buf;
        buf += 4;
        change_stride = (data & 0xF800F800) >> 27;
        if ( (data & 0xF800F800) == 0xF800F000 )
        {
          v14 = (((data >> 16) & 0xFFC007FF) | ((data & 0x7FF) << 11)) - ((buf - buffer) >> 1);
          *((uint32_t *)buf - 1) = ((((v14 & 0x7FF) << 16) | 0xF800F000) & 0xFFFFF800) | ((v14 >> 11) & 0x7FF);
        }
        else if ( change_stride == 30 )
        {
          buf -= 2;
        }
      }
      while ( bufend > buf );
    }
  }
  return len;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0x5491
cmpl $0x2, -0x10(%rbp)
jne 0x52cd
jmp 0x51cb
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x18(%rbp)
movl -0x24(%rbp), %eax
andl $0xf800f800, %eax       # imm = 0xF800F800
shrl $0x1b, %eax
movl %eax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq -0x8(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x50(%rbp)
movl -0x24(%rbp), %eax
andl $0xf800f800, %eax       # imm = 0xF800F800
cmpl $0xf800f000, %eax       # imm = 0xF800F000
jne 0x5266
movl -0x24(%rbp), %eax
shrl $0x10, %eax
andl $0xffc007ff, %eax       # imm = 0xFFC007FF
movl -0x24(%rbp), %ecx
andl $0x7ff, %ecx            # imm = 0x7FF
shll $0xb, %ecx
orl %ecx, %eax
movl %eax, %eax
movq -0x50(%rbp), %rcx
sarq %rcx
subq %rcx, %rax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
andl $0x7ff, %eax            # imm = 0x7FF
shll $0x10, %eax
orl $0xf800f000, %eax       # imm = 0xF800F000
andl $0xfffff800, %eax       # imm = 0xFFFFF800
movl -0x44(%rbp), %ecx
shrl $0xb, %ecx
andl $0x7ff, %ecx            # imm = 0x7FF
orl %ecx, %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, -0x4(%rax)
jmp 0x52b8
movl -0x24(%rbp), %eax
andl $0x8000fbf0, %eax       # imm = 0x8000FBF0
cmpl $0xf2c0, %eax           # imm = 0xF2C0
jne 0x52a2
movl -0x24(%rbp), %eax
andl $0xf0fffff0, %eax       # imm = 0xF0FFFFF0
movl -0x24(%rbp), %ecx
andl $0xf, %ecx
shll $0x18, %ecx
orl %ecx, %eax
movl -0x24(%rbp), %ecx
shrl $0x18, %ecx
andl $0xf, %ecx
orl %ecx, %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, -0x4(%rax)
jmp 0x52b6
cmpl $0x1e, -0x28(%rbp)
jne 0x52b4
movq -0x18(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x18(%rbp)
jmp 0x52b6
jmp 0x52b8
jmp 0x52ba
movq -0x20(%rbp), %rax
cmpq -0x18(%rbp), %rax
ja 0x51cb
jmp 0x548f
cmpl $0x1, -0x10(%rbp)
jne 0x53d6
movq -0x8(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
andl $0xf800f800, %eax       # imm = 0xF800F800
shrl $0x1b, %eax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
andl $0xf800f800, %eax       # imm = 0xF800F800
cmpl $0xf800f000, %eax       # imm = 0xF800F000
jne 0x5311
jmp 0x5371
jmp 0x5313
cmpl $0x1e, -0x38(%rbp)
jne 0x5325
movq -0x30(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x30(%rbp)
jmp 0x5327
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x53d1
movq -0x30(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x3c(%rbp), %eax
andl $0xf800f800, %eax       # imm = 0xF800F800
shrl $0x1b, %eax
movl %eax, -0x38(%rbp)
movl -0x3c(%rbp), %eax
andl $0xf800f800, %eax       # imm = 0xF800F800
cmpl $0xf800f000, %eax       # imm = 0xF800F000
je 0x536f
jmp 0x5313
jmp 0x5371
movl -0x34(%rbp), %eax
shrl $0x10, %eax
andl $0xffc007ff, %eax       # imm = 0xFFC007FF
movl -0x34(%rbp), %ecx
andl $0x7ff, %ecx            # imm = 0x7FF
shll $0xb, %ecx
orl %ecx, %eax
movl %eax, %eax
movq -0x30(%rbp), %rcx
movq -0x8(%rbp), %rdx
subq %rdx, %rcx
sarq %rcx
addq %rcx, %rax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %ecx
andl $0x7ff, %ecx            # imm = 0x7FF
shll $0x10, %ecx
orl $0xf800f000, %ecx       # imm = 0xF800F000
andl $0xfffff800, %ecx       # imm = 0xFFFFF800
movl -0x40(%rbp), %eax
shrl $0xb, %eax
andl $0x7ff, %eax            # imm = 0x7FF
orl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, -0x4(%rax)
jmp 0x5327
jmp 0x548d
jmp 0x53d8
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x18(%rbp)
movl -0x24(%rbp), %eax
andl $0xf800f800, %eax       # imm = 0xF800F800
shrl $0x1b, %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
andl $0xf800f800, %eax       # imm = 0xF800F800
cmpl $0xf800f000, %eax       # imm = 0xF800F000
jne 0x5467
movl -0x24(%rbp), %eax
shrl $0x10, %eax
andl $0xffc007ff, %eax       # imm = 0xFFC007FF
movl -0x24(%rbp), %ecx
andl $0x7ff, %ecx            # imm = 0x7FF
shll $0xb, %ecx
orl %ecx, %eax
movl %eax, %eax
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rdx
subq %rdx, %rcx
sarq %rcx
subq %rcx, %rax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %ecx
andl $0x7ff, %ecx            # imm = 0x7FF
shll $0x10, %ecx
orl $0xf800f000, %ecx       # imm = 0xF800F000
andl $0xfffff800, %ecx       # imm = 0xFFFFF800
movl -0x44(%rbp), %eax
shrl $0xb, %eax
andl $0x7ff, %eax            # imm = 0x7FF
orl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, -0x4(%rax)
jmp 0x547b
cmpl $0x1e, -0x28(%rbp)
jne 0x5479
movq -0x18(%rbp), %rax
addq $-0x2, %rax
movq %rax, -0x18(%rbp)
jmp 0x547b
jmp 0x547d
movq -0x20(%rbp), %rax
cmpq -0x18(%rbp), %rax
ja 0x53d8
jmp 0x548d
jmp 0x548f
jmp 0x5491
movl -0xc(%rbp), %eax
popq %rbp
retq
nopw %cs:(%rax,%rax)