// File: /workspace/github_c_makefile_star2_O1/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:
movl %esi, %eax
testl %esi, %esi
je 0x3570
movslq %eax, %rcx
addq %rdi, %rcx
cmpl $0x1, %edx
je 0x3456
cmpl $0x2, %edx
jne 0x34ee
movl $0x7ff, %edx            # imm = 0x7FF
movq %rdi, %r8
movl (%r8), %r9d
leaq 0x4(%r8), %rsi
movl %r9d, %r10d
andl $0xf800f800, %r10d      # imm = 0xF800F800
cmpl $0xf800f000, %r10d      # imm = 0xF800F000
jne 0x33f3
movq %rsi, %r10
subq %rdi, %r10
movl %r9d, %r11d
shrl $0x10, %r11d
andl %edx, %r11d
shll $0xb, %r9d
andl $0x3ff800, %r9d         # imm = 0x3FF800
orl %r11d, %r9d
shrq %r10
subl %r10d, %r9d
movl %r9d, %r10d
shll $0x10, %r10d
shrl $0xb, %r9d
andl %edx, %r9d
orl %r10d, %r9d
orl $0xf800f000, %r9d       # imm = 0xF800F000
jmp 0x3429
movl %r9d, %r10d
andl $0x8000fbf0, %r10d      # imm = 0x8000FBF0
cmpl $0xf2c0, %r10d          # imm = 0xF2C0
jne 0x343d
movl %r9d, %r10d
andl $0x70fff6c0, %r10d      # imm = 0x70FFF6C0
movl %r9d, %r11d
andl $0xf, %r11d
shll $0x18, %r11d
orl %r10d, %r11d
shrl $0x18, %r9d
andl $0xf, %r9d
orl %r11d, %r9d
movl %r9d, (%r8)
movq %rsi, %r8
cmpq %rsi, %rcx
ja 0x339b
jmp 0x3570
andl $0xf8000000, %r9d       # imm = 0xF8000000
cmpl $0xf0000000, %r9d       # imm = 0xF0000000
jne 0x342c
addq $0x2, %r8
movq %r8, %rsi
jmp 0x342c
movl (%rdi), %esi
movl %esi, %r8d
andl $0xf800f800, %r8d       # imm = 0xF800F800
movq %rdi, %rdx
cmpl $0xf800f000, %r8d       # imm = 0xF800F000
jne 0x34b9
leaq 0x4(%rdx), %r8
movl %esi, %r9d
shrl $0x10, %r9d
movl $0x7ff, %r10d           # imm = 0x7FF
andl %r10d, %r9d
shll $0xb, %esi
andl $0x3ff800, %esi         # imm = 0x3FF800
orl %r9d, %esi
movq %r8, %r9
subq %rdi, %r9
shrq %r9
addl %esi, %r9d
movl %r9d, %esi
shll $0x10, %esi
shrl $0xb, %r9d
andl %r10d, %r9d
orl %esi, %r9d
orl $0xf800f000, %r9d       # imm = 0xF800F000
movl %r9d, (%rdx)
movq %r8, %rdx
jmp 0x34d4
andl $0xf8000000, %esi       # imm = 0xF8000000
xorl %r8d, %r8d
cmpl $0xf0000000, %esi       # imm = 0xF0000000
setne %r8b
leaq (%rdx,%r8,2), %rdx
addq $0x2, %rdx
cmpq %rcx, %rdx
jae 0x3570
movl (%rdx), %esi
movl %esi, %r8d
andl $0xf800f800, %r8d       # imm = 0xF800F800
jmp 0x3465
movl $0x7ff, %edx            # imm = 0x7FF
movq %rdi, %r8
movl (%r8), %r9d
leaq 0x4(%r8), %rsi
movl %r9d, %r10d
andl $0xf800f800, %r10d      # imm = 0xF800F800
cmpl $0xf800f000, %r10d      # imm = 0xF800F000
jne 0x3551
movl %r9d, %r10d
shrl $0x10, %r10d
andl %edx, %r10d
shll $0xb, %r9d
andl $0x3ff800, %r9d         # imm = 0x3FF800
orl %r10d, %r9d
movq %rsi, %r10
subq %rdi, %r10
shrq %r10
subl %r10d, %r9d
movl %r9d, %r10d
shll $0x10, %r10d
shrl $0xb, %r9d
andl %edx, %r9d
orl %r10d, %r9d
orl $0xf800f000, %r9d       # imm = 0xF800F000
movl %r9d, (%r8)
jmp 0x3568
andl $0xf8000000, %r9d       # imm = 0xF8000000
cmpl $0xf0000000, %r9d       # imm = 0xF0000000
jne 0x3568
addq $0x2, %r8
movq %r8, %rsi
movq %rsi, %r8
cmpq %rsi, %rcx
ja 0x34f6
retq
nopl (%rax)