// File: /workspace/github_cpp_makefile_star2_O3/jBlinden[P]private-codis/ProtocolServer.cpp
// Function: RunServerProtocol(CSocket*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void RunServerProtocol(CSocket *socket, string pArgsFile, string serverDataDir)
{
  ServerLog("starting server protocol");
#if !OPTIMAL
  ServerLog("Reading in protocol args");
#endif

  ifstream in;
  in.open(pArgsFile, ios::in | ios::binary);
  struct ProtocolArgs pArgs(in);
  in.close();

#if !OPTIMAL
  ServerLog("Reading in sender OTs");
#endif

  ifstream sOTFile1;
  sOTFile1.open(serverDataDir + "sOT.txt", ios::in | ios::binary);

  SenderOTCorrelation **FSM1_SC = ReadInSenderOTsFSM1(sOTFile1, pArgs);
  SenderOTCorrelation **FSM2_SC = ReadInSenderOTsFSM2(sOTFile1, pArgs);

  sOTFile1.close();

#if !OPTIMAL
  ServerLog("Finished Reading in OTs");
  ServerLog("Creating fake DB");
#endif

  uint32_t N = pArgs.N;
  uint32_t C = pArgs.C;
  uint8_t* bits_per_component = pArgs.bits_per_component;

  ifstream dbFile;
  dbFile.open(serverDataDir + "db.txt");
  uint32_t **db = ReadInDB(dbFile, N, C);
  dbFile.close();

#if VERBOSE
  for (int i = 0; i < N; i++)
  {
    for (int j = 0; j < C; j++)
    {
      cout << db[i][j] << ", ";
    }
    cout << endl;
  }
#endif

#if !OPTIMAL
  ServerLog("Creating random permutations");
#endif

  AESContext PRF;

  uint16_t **flipped_bits = new uint16_t *[N];
  for (int i = 0; i < N; i++)
  {
    flipped_bits[i] = new uint16_t[C];
    PRF.FillBuffer((uint8_t *)flipped_bits[i], 2 * C, i);
  }

#if !OPTIMAL
  ServerLog("Running first part FSM");
#endif

  int l = 0;
  int smallest_number_of_bits = 0;
  for (int round = 0; round < pArgs.FSM1_rounds; round++)
  {
    uint8_t concurrent_iterations = pArgs.FSM1_steps[round];
    uint8_t all_ones_string = (uint8_t)((1 << concurrent_iterations) - 1);
    uint8_t pow_iterations = (uint8_t)((1 << concurrent_iterations));
    uint8_t pos_states = 2 * pow_iterations;

    if (l >= bits_per_component[smallest_number_of_bits])
    {
      while(l >= bits_per_component[smallest_number_of_bits])
        smallest_number_of_bits ++;
    }
    uint8_t number_of_bit_entries = C - smallest_number_of_bits;

    uint8_t *msgs = new uint8_t[N * number_of_bit_entries * 2 * pow_iterations];

    for (int i = 0; i < N; i++)
    {
      for (int j = 0; j < number_of_bit_entries; j++)
      {
        uint8_t postImageJ = j + C - number_of_bit_entries;

        bool previous_reject = round == 0 ? 1 : !(flipped_bits[i][postImageJ] >> (round - 1) & 0x1);
        bool next_flipped_bit = (flipped_bits[i][postImageJ] >> round) & 0x1;

        uint8_t entry_bits = (db[i][postImageJ] >> l) & all_ones_string;

        for (int state = 0; state < pos_states; state++)
        {
          if (state < (pos_states / 2))
          {
            msgs[number_of_bit_entries * pos_states * i + j * pos_states + state] = (previous_reject != 0 && entry_bits == (state % pow_iterations)) ? next_flipped_bit : !next_flipped_bit;
          }
          else
          {
            msgs[number_of_bit_entries * pos_states * i + j * pos_states + state] = (previous_reject != 1 && entry_bits == (state % pow_iterations)) ? next_flipped_bit : !next_flipped_bit;
          }
        }
      }
    }

#if !OPTIMAL
    ServerLog("Finished creating messages");
#endif

    uint8_t *rMsg = new uint8_t[N * number_of_bit_entries];

    ReceiveLargePacked(socket, 1 + concurrent_iterations, N * number_of_bit_entries, rMsg);

    uint8_t *sMsg = new uint8_t[N * number_of_bit_entries * pos_states];
    ConstructOTSenderMsg(sMsg, *(FSM1_SC[round]), msgs, rMsg, N * number_of_bit_entries, 1);

    SendLargePacked(socket, 1, N * number_of_bit_entries * pos_states, sMsg);

    delete[] msgs;
    delete[] rMsg;
    delete[] sMsg;

    l += concurrent_iterations;
  }

#if !OPTIMAL
  ServerLog("Finished part 1 of the FSM");
#endif
  uint8_t highest_number_of_bits = bits_per_component[C - 1];
  uint8_t **FinalRandomness = new uint8_t *[N];
  for (int i = 0; i < N; i++)
  {
    FinalRandomness[i] = new uint8_t[C];
    for (int j = 0; j < C; j++)
    {
      FinalRandomness[i][j] = (flipped_bits[i][j] >> (pArgs.FSM1_rounds - 1 - ((highest_number_of_bits - bits_per_component[j]) / 2))) & 0x1;
    }
  }

#if !OPTIMAL
  ServerLog("Starting Second FSM");
#endif

  AESContext PRF2;

  uint8_t **random_shift = new uint8_t *[N];
  uint32_t tag_counter = 0;
  for (int i = 0; i < N; i++)
  {
    random_shift[i] = new uint8_t[pArgs.FSM2_rounds];
    PRF2.FillBuffer(random_shift[i], pArgs.FSM2_rounds, tag_counter);
    for (int j = 0; j < pArgs.FSM2_rounds; j++)
    {
      while (random_shift[i][j] == 255)
      {
        tag_counter += 1;
        PRF2.FillBuffer(&random_shift[i][j], 1, tag_counter);
      }
      random_shift[i][j] %= 3;
    }
    tag_counter += 1;
  }

  for (int i = 0; i < N; i++)
    random_shift[i][pArgs.FSM2_rounds - 1] = 0; // Make sure that the final permutation is always 0 to reveal the true state to the client

  uint32_t c = 0;
  for (int round = 0; round < pArgs.FSM2_rounds; round++)
  {
    uint8_t round_step = pArgs.FSM2_steps[round];
    uint8_t round_factor = 1 << round_step;

    uint8_t *msgs = new uint8_t[N * 3 * round_factor];
    for (int i = 0; i < N; i++)
    {
      int previous_shift = (c == 0) ? 0 : random_shift[i][round - 1];
      for (int possible_state = 0; possible_state < 3; possible_state++)
      {
        uint8_t old_state = (possible_state + 3 - previous_shift) % 3;

        uint8_t equality_bits = 0;
        for (int j = 0; j < round_step; j++)
        {
          equality_bits += (FinalRandomness[i][c + j] * (1 << j));
        }

        // equality bits = the best correct state -> having it correct is plus FSM_RC
        // ~equality bits = worst case -> having it correct is plus 0
        //
        for (uint8_t j = 0; j < round_factor; j++)
        {
          uint8_t no_match_count = round_step - count_ones(((~(j ^ equality_bits)) & (round_factor - 1)));
          if (old_state - no_match_count < 0)
            msgs[((3 * round_factor) * i + possible_state * round_factor + j)] = (0 + random_shift[i][round]);
          else
            msgs[((3 * round_factor) * i + possible_state * round_factor + j)] = (old_state - no_match_count + random_shift[i][round]) % 3;
  
        }
      }

    }

#if !OPTIMAL
    ServerLog("Finished one round");
#endif

    

    uint8_t *rMsg = new uint8_t[N];

    ReceiveLargePacked(socket, 2 + round_step, N, rMsg);
    
    uint8_t *sMsg = new uint8_t[N * 3 * round_factor];
    ConstructOTSenderMsg(sMsg, *(FSM2_SC[round]), msgs, rMsg, N, 2);

    SendLargePacked(socket, 2, N * 3 * round_factor, sMsg);

    c += round_step;

    delete[] msgs;
    delete[] rMsg;
    delete[] sMsg;
  }

#if !OPTIMAL
  ServerLog("finished everything");
  cout << "Memory clean up" << endl;
#endif

  for (int i = 0; i < N; i++)
  {
    delete[] db[i];
    delete[] flipped_bits[i];
    delete[] random_shift[i];
    delete[] FinalRandomness[i];
  }
  delete[] db;
  delete[] flipped_bits;
  delete[] random_shift;
  delete[] FinalRandomness;

  for (int i = 0; i < pArgs.FSM1_rounds; i++)
  {
    delete FSM1_SC[i];
  }
  delete[] FSM1_SC;

  for (int i = 0; i < pArgs.FSM2_rounds; i++)
  {
    delete FSM2_SC[i];
  }
  delete[] FSM2_SC;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x978, %rsp            # imm = 0x978
leaq 0x468(%rsp), %r15
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, 0x70(%rsp)
movq %r15, -0x10(%r15)
leaq 0x3e19(%rip), %rsi      # 0x63d4
leaq 0x3e2a(%rip), %rdx      # 0x63ec
leaq 0x458(%rsp), %rdi
callq 0x3aa4
leaq 0x458(%rsp), %rdi
callq 0x3425
movq 0x458(%rsp), %rdi
cmpq %r15, %rdi
je 0x25ee
callq 0x21f0
leaq 0x458(%rsp), %r15
movq %r15, %rdi
callq 0x2270
movl $0xc, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x21b0
leaq 0x90(%rsp), %rdi
leaq 0x458(%rsp), %rsi
callq 0x4b88
leaq 0x458(%rsp), %rdi
callq 0x2070
leaq 0x770(%rsp), %rdi
callq 0x2270
leaq 0x260(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x250(%rsp), %rdi
callq 0x3a0e
leaq 0x3d86(%rip), %rsi      # 0x63ed
leaq 0x250(%rsp), %rdi
callq 0x23f0
leaq 0x770(%rsp), %rdi
leaq 0x250(%rsp), %rsi
movl $0xc, %edx
callq 0x21b0
movq 0x250(%rsp), %rdi
cmpq %r14, %rdi
je 0x26a0
callq 0x21f0
vmovdqu 0x90(%rsp), %ymm0
vmovdqu 0xa0(%rsp), %ymm1
leaq 0x110(%rsp), %rsi
vmovdqu %ymm0, (%rsi)
vmovdqu %ymm1, 0x10(%rsi)
leaq 0x770(%rsp), %rdi
vzeroupper
callq 0x4f09
movq %rax, 0x68(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x4c88
vmovdqu 0x90(%rsp), %ymm0
vmovdqu 0xa0(%rsp), %ymm1
leaq 0xe0(%rsp), %rsi
vmovdqu %ymm1, 0x10(%rsi)
vmovdqu %ymm0, (%rsi)
leaq 0x770(%rsp), %rdi
vzeroupper
callq 0x4f80
movq %rax, 0x60(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x4c88
leaq 0x770(%rsp), %rdi
callq 0x2070
movl 0x90(%rsp), %eax
movl 0x94(%rsp), %r13d
movq 0xb0(%rsp), %rcx
movq %rax, 0x10(%rsp)
movq %rcx, 0x48(%rsp)
leaq 0x250(%rsp), %rdi
callq 0x2270
leaq 0x150(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x140(%rsp), %rdi
callq 0x3a0e
leaq 0x3c66(%rip), %rsi      # 0x63f5
leaq 0x140(%rsp), %rdi
callq 0x23f0
leaq 0x250(%rsp), %rdi
leaq 0x140(%rsp), %rsi
movl $0x8, %edx
callq 0x21b0
movq 0x140(%rsp), %rdi
cmpq %r14, %rdi
je 0x27c8
callq 0x21f0
movq 0x10(%rsp), %r14
leaq 0x250(%rsp), %rdi
movl %r13d, %edx
movl %r14d, %esi
callq 0x5251
movq %rax, 0x78(%rsp)
leaq 0x250(%rsp), %rdi
callq 0x2070
leaq 0x140(%rsp), %rdi
callq 0x5980
leaq (,%r14,8), %rdi
movq %rdi, 0xc0(%rsp)
callq 0x2030
movq %rax, 0x40(%rsp)
testq %r14, %r14
je 0x285c
leaq (%r13,%r13), %rbx
leal (%r13,%r13), %ebp
leaq 0x140(%rsp), %r12
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0x2030
movq 0x40(%rsp), %rcx
movq %rax, (%rcx,%r15,8)
movq %r12, %rdi
movq %rax, %rsi
movl %ebp, %edx
movq %r15, %rcx
callq 0x5ea8
incq %r15
cmpq %r15, %r14
jne 0x2833
cmpl $0x0, 0x98(%rsp)
movq %r13, 0x28(%rsp)
je 0x2d45
movq $0x0, (%rsp)
xorl %esi, %esi
xorl %r13d, %r13d
movq 0x48(%rsp), %rcx
movslq %r13d, %rax
movzbl (%rcx,%rax), %ecx
cmpl %ecx, %esi
jb 0x28a2
leal -0x1(%rax), %r13d
addq 0x48(%rsp), %rax
movzbl (%rax), %ecx
incl %r13d
incq %rax
cmpl %ecx, %esi
jae 0x2895
movq 0xa0(%rsp), %rax
movq (%rsp), %rcx
movl %esi, 0x18(%rsp)
movzbl (%rax,%rcx), %ecx
movl $0x1, %eax
shlxl %ecx, %eax, %r15d
movl $0xff, %eax
movq %rcx, 0x30(%rsp)
movl %r15d, %ebx
andl %eax, %ebx
movq 0x28(%rsp), %rax
subl %r13d, %eax
movzbl %al, %eax
movl %ebx, %edi
movl %eax, 0x80(%rsp)
imull %r14d, %eax
imull %eax, %edi
movl %eax, 0x50(%rsp)
addl %edi, %edi
callq 0x2030
movq %rax, 0x20(%rsp)
vpmovsxbd 0x3a90(%rip), %zmm22 # 0x6394
vpmovsxbd 0x3a96(%rip), %zmm23 # 0x63a4
vpmovsxbq 0x3a38(%rip), %zmm24 # 0x6350
vpmovsxbq 0x3a36(%rip), %zmm25 # 0x6358
vpmovsxbq 0x3a34(%rip), %zmm26 # 0x6360
vpmovsxbq 0x3a32(%rip), %zmm27 # 0x6368
vpmovsxbq 0x3a30(%rip), %zmm28 # 0x6370
vpmovsxbq 0x3a2e(%rip), %zmm29 # 0x6378
vpmovsxbq 0x3a2c(%rip), %zmm30 # 0x6380
vpmovsxbq 0x3a2a(%rip), %zmm31 # 0x6388
leal (%r15,%r15), %eax
movzbl %al, %ebp
testl %r14d, %r14d
je 0x2c86
movl $0xff, %ecx
movl $0x1, %esi
movl %ebp, %r9d
leal 0x3f(%rbp), %r8d
shrl %r9d
decl %ebx
leaq -0x1(%rbp), %r10
movq $0x0, 0x38(%rsp)
addl %r15d, %ecx
andl $0x7f, %r15d
vpbroadcastq %r10, %zmm0
andl $-0x40, %r8d
vpbroadcastq %r9, %zmm1
vpbroadcastd %ebx, %zmm2
xorl %r10d, %r10d
movl %ecx, 0x8(%rsp)
movq (%rsp), %rcx
leal -0x1(%rcx), %edx
shlxl %ecx, %esi, %ecx
shlxl %edx, %esi, %edx
movl %ecx, 0x58(%rsp)
movl %edx, 0x88(%rsp)
movl 0x80(%rsp), %edx
imull %edx, %r15d
movl %edx, %edi
addl %r15d, %r15d
movq %r15, 0xc8(%rsp)
cmpl $0x0, 0x80(%rsp)
je 0x2c60
movq 0x40(%rsp), %rcx
xorl %r9d, %r9d
movq (%rcx,%r10,8), %r11
movq 0x38(%rsp), %rcx
movl %ecx, %ebx
cmpq $0x0, (%rsp)
leal (%r13,%r9), %r14d
movzbl %r14b, %r15d
je 0x2a2d
movzwl (%r11,%r15,2), %r14d
testl %r14d, 0x88(%rsp)
sete %r14b
jmp 0x2a30
movb $0x1, %r14b
vpbroadcastq 0x390e(%rip), %zmm20 # 0x6348
vpbroadcastd 0x394c(%rip), %zmm21 # 0x6390
testb %al, %al
je 0x2c52
movq 0x78(%rsp), %rdx
movl 0x18(%rsp), %esi
movzwl (%r11,%r15,2), %ecx
vpmovsxbd 0x3950(%rip), %zmm14 # 0x63b4
vpmovsxbd 0x3956(%rip), %zmm15 # 0x63c4
movl %ebx, %r12d
addq 0x20(%rsp), %r12
vmovdqa64 %zmm31, %zmm4
vmovdqa64 %zmm30, %zmm5
vmovdqa64 %zmm29, %zmm6
vmovdqa64 %zmm28, %zmm7
vmovdqa64 %zmm27, %zmm8
vmovdqa64 %zmm26, %zmm9
vmovdqa64 %zmm25, %zmm10
vmovdqa64 %zmm24, %zmm11
vmovdqa64 %zmm23, %zmm12
vmovdqa64 %zmm22, %zmm13
andl 0x58(%rsp), %ecx
movq (%rdx,%r10,8), %rdx
shrxl %esi, (%rdx,%r15,4), %edx
andl 0x8(%rsp), %edx
testb %r14b, %r14b
movq $-0x1, %r14
movzbl %dl, %edx
vpbroadcastd %edx, %zmm3
movl $0x0, %edx
cmovneq %r14, %rdx
negl %ecx
sbbq %rcx, %rcx
kmovq %rdx, %k0
xorl %r15d, %r15d
kmovq %rcx, %k1
vpcmpleuq %zmm0, %zmm4, %k2
vpcmpleuq %zmm0, %zmm5, %k3
vpcmpleuq %zmm0, %zmm7, %k4
vpcmpleuq %zmm0, %zmm11, %k5
vpcmpltuq %zmm1, %zmm11, %k6
vpandd %zmm13, %zmm2, %zmm18
vpandd %zmm12, %zmm2, %zmm19
vpandd %zmm15, %zmm2, %zmm16
vpandd %zmm14, %zmm2, %zmm17
vpaddq %zmm20, %zmm11, %zmm11
vpaddd %zmm21, %zmm12, %zmm12
vpaddd %zmm21, %zmm13, %zmm13
vpaddd %zmm21, %zmm14, %zmm14
vpaddd %zmm21, %zmm15, %zmm15
kunpckbw %k2, %k3, %k2
vpcmpleuq %zmm0, %zmm6, %k3
kunpckbw %k3, %k4, %k3
vpcmpleuq %zmm0, %zmm9, %k4
kunpckwd %k2, %k3, %k2
vpcmpleuq %zmm0, %zmm8, %k3
kunpckbw %k3, %k4, %k3
vpcmpleuq %zmm0, %zmm10, %k4
kunpckbw %k4, %k5, %k4
vpcmpltuq %zmm1, %zmm7, %k5
vpaddq %zmm20, %zmm7, %zmm7
kunpckwd %k3, %k4, %k3
vpcmpltuq %zmm1, %zmm5, %k4
vpaddq %zmm20, %zmm5, %zmm5
kunpckdq %k2, %k3, %k2
vpcmpltuq %zmm1, %zmm4, %k3
vpaddq %zmm20, %zmm4, %zmm4
kunpckbw %k3, %k4, %k3
vpcmpltuq %zmm1, %zmm6, %k4
vpaddq %zmm20, %zmm6, %zmm6
kunpckbw %k4, %k5, %k4
vpcmpltuq %zmm1, %zmm9, %k5
vpaddq %zmm20, %zmm9, %zmm9
kunpckwd %k3, %k4, %k3
vpcmpltuq %zmm1, %zmm8, %k4
vpaddq %zmm20, %zmm8, %zmm8
kunpckbw %k4, %k5, %k4
vpcmpltuq %zmm1, %zmm10, %k5
vpaddq %zmm20, %zmm10, %zmm10
kunpckbw %k5, %k6, %k5
vpcmpneqd %zmm16, %zmm3, %k6
kunpckwd %k4, %k5, %k4
vpcmpneqd %zmm18, %zmm3, %k5
kunpckdq %k3, %k4, %k3
vpcmpneqd %zmm19, %zmm3, %k4
kunpckwd %k4, %k5, %k4
vpcmpneqd %zmm17, %zmm3, %k5
kxorq %k3, %k0, %k3
kunpckwd %k5, %k6, %k5
kunpckdq %k4, %k5, %k4
korq %k4, %k3, %k3
kxorq %k3, %k1, %k3
vpbroadcastb 0x3702(%rip), %zmm16 {%k3} {z} # 0x6340
vmovdqu8 %zmm16, (%r12,%r15) {%k2}
addq $0x40, %r15
cmpq %r15, %r8
jne 0x2af2
incq %r9
addl %ebp, %ebx
cmpq %rdi, %r9
jne 0x2a0a
movq 0xc8(%rsp), %r15
movq 0x38(%rsp), %rcx
movq 0x10(%rsp), %r14
incq %r10
addl %r15d, %ecx
movq %rcx, 0x38(%rsp)
cmpq %r14, %r10
jne 0x29e9
movl 0x50(%rsp), %edi
movq %rdi, %r12
vzeroupper
callq 0x2030
movq 0x30(%rsp), %rbx
movq %rax, %r15
leal 0x1(%rbx), %eax
movq 0x70(%rsp), %rdi
movzbl %al, %esi
movq %r12, %rdx
movq %r15, %rcx
callq 0x537f
movl 0x50(%rsp), %r12d
imull %r12d, %ebp
movq %rbp, %rdi
callq 0x2030
movq %rax, 0x8(%rsp)
movq 0x68(%rsp), %rax
movq (%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rdx
movl $0x1, %r9d
movq %r15, %rcx
movl %r12d, %r8d
callq 0x4216
movq 0x70(%rsp), %rdi
movq 0x8(%rsp), %rcx
movl $0x1, %esi
movq %rbp, %rdx
callq 0x52a5
movq 0x20(%rsp), %rdi
callq 0x22c0
movq %r15, %rdi
callq 0x22c0
movq 0x8(%rsp), %rdi
callq 0x22c0
movl 0x18(%rsp), %esi
movq (%rsp), %rdx
movl 0x98(%rsp), %eax
incq %rdx
addl %ebx, %esi
movq %rdx, (%rsp)
cmpq %rax, %rdx
jb 0x287c
movq 0x28(%rsp), %r15
movq 0x48(%rsp), %r12
leal -0x1(%r15), %eax
movzbl (%r12,%rax), %ebp
movq 0xc0(%rsp), %r13
movq %r13, %rdi
callq 0x2030
movq %rax, 0x30(%rsp)
testl %r14d, %r14d
je 0x2ddd
movl 0x98(%rsp), %ebx
movzwl %bp, %ebp
xorl %r14d, %r14d
decl %ebx
movq %r15, %rdi
callq 0x2030
movq 0x30(%rsp), %rcx
movq %rax, (%rcx,%r14,8)
testl %r15d, %r15d
je 0x2dd3
movq 0x40(%rsp), %rcx
xorl %edx, %edx
movq (%rcx,%r14,8), %rcx
movzbl (%r12,%rdx), %edi
movl %ebp, %r8d
movzwl (%rcx,%rdx,2), %esi
subl %edi, %r8d
movzwl %r8w, %edi
shrl $0xf, %edi
addl %r8d, %edi
movl %ebx, %r8d
shrl %edi
subl %edi, %r8d
btl %r8d, %esi
setb (%rax,%rdx)
incq %rdx
cmpq %rdx, %r15
jne 0x2da2
incq %r14
cmpq 0x10(%rsp), %r14
jne 0x2d81
leaq 0x660(%rsp), %rdi
callq 0x5980
movq %r13, %rdi
callq 0x2030
movq 0x10(%rsp), %r14
movq %rax, %r15
movq %rax, 0x18(%rsp)
testl %r14d, %r14d
je 0x2ed0
movl 0x9c(%rsp), %r13d
leaq 0x660(%rsp), %rbx
xorl %r14d, %r14d
xorl %ebp, %ebp
movl %r13d, %edi
callq 0x2030
movq %rax, %r12
movq %rax, (%r15,%r14,8)
movl %ebp, %ecx
movq %rbx, %rdi
movq %rax, %rsi
movl %r13d, %edx
callq 0x5ea8
cmpl $0x0, 0x9c(%rsp)
je 0x2e98
xorl %r15d, %r15d
leaq (%r12,%r15), %r13
movb (%r13), %al
cmpb $-0x1, %al
jne 0x2e6c
incl %ebp
movl $0x1, %edx
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rcx
callq 0x5ea8
jmp 0x2e4d
movzbl %al, %ecx
incq %r15
imull $0xab, %ecx, %ecx
shrl $0x9, %ecx
leal (%rcx,%rcx,2), %ecx
subb %cl, %al
movb %al, (%r13)
movl 0x9c(%rsp), %r13d
cmpq %r13, %r15
jb 0x2e49
movq 0x18(%rsp), %r15
jmp 0x2e9b
xorl %r13d, %r13d
incl %ebp
incq %r14
cmpq 0x10(%rsp), %r14
jne 0x2e1d
movq 0x10(%rsp), %r14
testl %r14d, %r14d
je 0x2ed0
xorl %eax, %eax
movl 0x9c(%rsp), %edx
movq (%r15,%rax,8), %rcx
incq %rax
decl %edx
movb $0x0, (%rcx,%rdx)
cmpq %rax, %r14
jne 0x2eb7
cmpl $0x0, 0x9c(%rsp)
je 0x3188
leal (%r14,%r14,2), %eax
xorl %ebx, %ebx
movl $0x0, 0x28(%rsp)
movl %eax, 0xd4(%rsp)
movq 0xa8(%rsp), %rax
movl 0xd4(%rsp), %edx
movq %rbx, 0x20(%rsp)
movzbl (%rax,%rbx), %ecx
movl $0x1, %eax
movl %edx, %edi
shlxl %ecx, %eax, %eax
movq %rcx, (%rsp)
movzbl %al, %ecx
movq %rax, 0x88(%rsp)
movq %rcx, 0x38(%rsp)
imull %ecx, %edi
movq %rdi, 0xd8(%rsp)
callq 0x2030
movq %rax, 0x8(%rsp)
testl %r14d, %r14d
je 0x30d8
movq 0x88(%rsp), %rax
movl (%rsp), %esi
movl 0x28(%rsp), %edi
xorl %r12d, %r12d
xorl %ebx, %ebx
leal 0xff(%rax), %r15d
movq 0x38(%rsp), %rax
movq %rsi, 0x80(%rsp)
movq %rdi, 0xc8(%rsp)
leal (%rax,%rax,2), %eax
movl %eax, 0xc0(%rsp)
cmpl $0x0, 0x28(%rsp)
je 0x2fab
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rbx,8), %rax
movzbl -0x1(%rax,%rcx), %eax
movl $0x3, %ecx
subl %eax, %ecx
movq %rcx, 0x50(%rsp)
jmp 0x2fb5
movl $0x3, %eax
movq %rax, 0x50(%rsp)
movq %r12, 0x48(%rsp)
movq $0x0, 0x58(%rsp)
xorl %r13d, %r13d
cmpl $0x0, (%rsp)
je 0x2ffc
movq 0x30(%rsp), %rax
xorl %ecx, %ecx
xorl %r14d, %r14d
movq (%rax,%rbx,8), %rax
leal (%rdi,%rcx), %edx
movzbl (%rax,%rdx), %edx
shlxl %ecx, %edx, %edx
incq %rcx
addb %dl, %r14b
cmpq %rcx, %rsi
jne 0x2fda
cmpb $0x7, (%rsp)
jbe 0x2fff
jmp 0x3090
xorl %r14d, %r14d
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rcx
addl %ecx, %eax
cltq
imulq $0x55555556, %rax, %rcx # imm = 0x55555556
movq %rcx, %rdx
shrq $0x3f, %rdx
shrq $0x20, %rcx
addl %edx, %ecx
leal (%rcx,%rcx,2), %ecx
subl %ecx, %eax
movzbl %al, %ebp
movl %r13d, %eax
xorb %r14b, %al
movzbl %al, %eax
andnl %r15d, %eax, %eax
movzbl %al, %edi
callq 0x50ed
movq 0x18(%rsp), %rdx
movq (%rsp), %rcx
subb %al, %cl
movl %ebp, %eax
movzbl %cl, %ecx
subl %ecx, %eax
movq (%rdx,%rbx,8), %rcx
movq 0x20(%rsp), %rdx
movb (%rcx,%rdx), %cl
js 0x3077
movzbl %cl, %ecx
addl %ecx, %eax
movzwl %ax, %ecx
imull $0xaaab, %ecx, %ecx     # imm = 0xAAAB
shrl $0x11, %ecx
leal (%rcx,%rcx,2), %ecx
subl %ecx, %eax
movl %eax, %ecx
movq 0x8(%rsp), %rdx
leal (%r12,%r13), %eax
incl %r13d
movb %cl, (%rdx,%rax)
cmpb 0x88(%rsp), %r13b
jb 0x3029
movq 0x58(%rsp), %rcx
addl 0x38(%rsp), %r12d
movq 0x80(%rsp), %rsi
movq 0xc8(%rsp), %rdi
incl %ecx
movq %rcx, 0x58(%rsp)
cmpl $0x3, %ecx
jne 0x2fc3
movq 0x48(%rsp), %r12
movq 0x10(%rsp), %r14
incq %rbx
addl 0xc0(%rsp), %r12d
cmpq %r14, %rbx
jne 0x2f83
movq %r14, %rdi
callq 0x2030
movq %rax, %r12
movq (%rsp), %rax
addl $0x2, %eax
movq 0x70(%rsp), %rdi
movzbl %al, %esi
movq %r14, %rdx
movq %r12, %rcx
callq 0x537f
movq 0xd8(%rsp), %rdi
callq 0x2030
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %rbx
movq 0x60(%rsp), %rsi
movq %rax, %r13
movq (%rsi,%rbx,8), %rsi
movq 0x8(%rsp), %rdx
movl $0x2, %r9d
movq %r13, %rdi
movq %r12, %rcx
movl %r14d, %r8d
callq 0x4216
movq 0x70(%rsp), %rdi
movq 0xd8(%rsp), %rdx
movl $0x2, %esi
movq %r13, %rcx
callq 0x52a5
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
addl %eax, 0x28(%rsp)
callq 0x22c0
movq %r12, %rdi
callq 0x22c0
movq %r13, %rdi
callq 0x22c0
movl 0x9c(%rsp), %eax
incq %rbx
cmpq %rax, %rbx
jb 0x2ef3
testl %r14d, %r14d
je 0x31e0
xorl %ebx, %ebx
movq 0x78(%rsp), %rax
movq (%rax,%rbx,8), %rdi
testq %rdi, %rdi
je 0x31a2
callq 0x22c0
movq 0x40(%rsp), %rax
movq (%rax,%rbx,8), %rdi
testq %rdi, %rdi
je 0x31b5
callq 0x22c0
movq (%r15,%rbx,8), %rdi
testq %rdi, %rdi
je 0x31c3
callq 0x22c0
movq 0x30(%rsp), %rax
movq (%rax,%rbx,8), %rdi
testq %rdi, %rdi
je 0x31d6
callq 0x22c0
incq %rbx
cmpq %rbx, %r14
jne 0x318f
jmp 0x31e8
cmpq $0x0, 0x78(%rsp)
je 0x31f2
movq 0x78(%rsp), %rdi
callq 0x22c0
movq 0x40(%rsp), %rdi
callq 0x22c0
movq %r15, %rdi
callq 0x22c0
movq 0x30(%rsp), %rdi
callq 0x22c0
cmpl $0x0, 0x98(%rsp)
je 0x324a
xorl %r14d, %r14d
movq 0x68(%rsp), %rax
movq (%rax,%r14,8), %rbx
testq %rbx, %rbx
je 0x3231
movq %rbx, %rdi
callq 0x3e46
movq %rbx, %rdi
callq 0x21f0
movl 0x98(%rsp), %eax
incq %r14
cmpq %rax, %r14
jb 0x321b
jmp 0x3252
cmpq $0x0, 0x68(%rsp)
je 0x325c
movq 0x68(%rsp), %rdi
callq 0x22c0
cmpl $0x0, 0x9c(%rsp)
je 0x3298
xorl %r14d, %r14d
movq 0x60(%rsp), %rax
movq (%rax,%r14,8), %rbx
testq %rbx, %rbx
je 0x327f
movq %rbx, %rdi
callq 0x3e46
movq %rbx, %rdi
callq 0x21f0
movl 0x9c(%rsp), %eax
incq %r14
cmpq %rax, %r14
jb 0x3269
jmp 0x32a0
cmpq $0x0, 0x60(%rsp)
je 0x32aa
movq 0x60(%rsp), %rdi
callq 0x22c0
leaq 0x660(%rsp), %rdi
callq 0x5d4e
leaq 0x140(%rsp), %rdi
callq 0x5d4e
leaq 0x250(%rsp), %rdi
callq 0x2090
leaq 0x770(%rsp), %rdi
callq 0x2090
leaq 0x90(%rsp), %rdi
callq 0x4c88
leaq 0x458(%rsp), %rdi
callq 0x2090
addq $0x978, %rsp            # imm = 0x978
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x33cc
jmp 0x33c5
jmp 0x33c5
jmp 0x33c5
jmp 0x33b0
jmp 0x3325
movq 0x140(%rsp), %rdi
movq %rax, %rbx
cmpq %r14, %rdi
je 0x33e9
callq 0x21f0
jmp 0x33e9
jmp 0x33b0
jmp 0x3381
jmp 0x3381
leaq 0xe0(%rsp), %rdi
jmp 0x335b
leaq 0x110(%rsp), %rdi
movq %rax, %rbx
callq 0x4c88
jmp 0x33f6
jmp 0x336a
movq 0x250(%rsp), %rdi
movq %rax, %rbx
cmpq %r14, %rdi
je 0x33f6
callq 0x21f0
jmp 0x33f6
movq %rax, %rbx
jmp 0x33f6
jmp 0x3388
movq %rax, %rbx
jmp 0x3403
jmp 0x338f
movq %rax, %rbx
jmp 0x3410
movq 0x458(%rsp), %rdi
movq %rax, %rbx
cmpq %r15, %rdi
je 0x341d
callq 0x21f0
jmp 0x341d
movq %rax, %rbx
jmp 0x341d
movq %rax, %rbx
jmp 0x33e9
jmp 0x33cc
jmp 0x33c5
jmp 0x33c5
jmp 0x33cc
jmp 0x33cc
jmp 0x33c5
jmp 0x33c5
jmp 0x33cc
movq %rax, %rbx
jmp 0x33dc
jmp 0x33cc
leaq 0x660(%rsp), %rdi
movq %rax, %rbx
callq 0x5d4e
leaq 0x140(%rsp), %rdi
callq 0x5d4e
leaq 0x250(%rsp), %rdi
callq 0x2090
leaq 0x770(%rsp), %rdi
callq 0x2090
leaq 0x90(%rsp), %rdi
callq 0x4c88
leaq 0x458(%rsp), %rdi
callq 0x2090
movq %rbx, %rdi
callq 0x23a0