// File: /workspace/github_cpp_makefile_star2_O2/jBlinden[P]private-codis/ProtocolClient.cpp
// Function: RunClientProtocol(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 RunClientProtocol(CSocket *socket, string pArgsFile, string clientDataDir)
{
  ClientLog("starting client protocol");

#if !OPTIMAL
  ClientLog("Reading in protocol arguments");
#endif

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

  in.close();

#if !OPTIMAL
  ClientLog("Reading in receiver OTs");
#endif

  ifstream rOTFile1;
  rOTFile1.open(clientDataDir + "rOT.txt", ios::in | ios::binary);

  ReceiverOTCorrelation **FSM1_RC = ReadInReceiverOTsFSM1(rOTFile1, pArgs);
  ReceiverOTCorrelation **FSM2_RC = ReadInReceiverOTsFSM2(rOTFile1, pArgs);

  rOTFile1.close();

#if !OPTIMAL
  ClientLog("Finished Reading in OTs");
#endif

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

#if !OPTIMAL
  cout << "N: " << N << " C: " << C << endl;
#endif

  ifstream queryFile;
  queryFile.open(clientDataDir + "query.txt");
  uint32_t *query = ReadInEntry(queryFile, C);
  queryFile.close();

#if VERBOSE
  cout << "Query: ";
  for (int i = 0; i < C; i++)
  {
    cout << query[i] << " ";
  }
  cout << endl;
#endif

#if !OPTIMAL
  ClientLog("Creating clients states");
#endif

  uint8_t **client_states = new uint8_t *[N];
  for (int i = 0; i < N; i++)
  {
    client_states[i] = new uint8_t[C];
    for (int j = 0; j < C; j++)
    {
      client_states[i][j] = 0; //Default state is 0
    }
  }

#if !OPTIMAL
  ClientLog("Starting first FSM");

  cout << pArgs.FSM1_rounds << endl;
  for (int i = 0; i < pArgs.FSM1_rounds; i++)
  {
    cout << i << " : " << (uint32_t)pArgs.FSM1_steps[i] << endl;
  }
#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_higher_bit_entries = C - smallest_number_of_bits;
    uint8_t *choices = new uint8_t[N * number_of_higher_bit_entries * concurrent_iterations];
    
    for (int i = 0; i < N; i++)
    {
      for (int j = 0; j < number_of_higher_bit_entries; j++)
      {
          
        uint8_t postImageJ = j + C - number_of_higher_bit_entries;
        uint8_t query_bits = (query[postImageJ] >> l) & all_ones_string;
        choices[number_of_higher_bit_entries * i + j] = pow_iterations * client_states[i][postImageJ] + query_bits;
      }
    }

#if !OPTIMAL
      ClientLog("Created Choice bits");
#endif
  
      
    uint8_t *rMsg = new uint8_t[N * number_of_higher_bit_entries];
    ConstructOTReceiverMsg(rMsg, *(FSM1_RC[round]), choices, N * number_of_higher_bit_entries);

      
    SendLargePacked(socket, 1 + concurrent_iterations, N * number_of_higher_bit_entries, rMsg);

   
    uint8_t *sMsg = new uint8_t[N * number_of_higher_bit_entries * pos_states];
    ReceiveLargePacked(socket, 1, N * number_of_higher_bit_entries * pos_states, sMsg);

      
    uint8_t *msgs2 = new uint8_t[N * number_of_higher_bit_entries];
    RecoverReceiverMsg(msgs2, *(FSM1_RC[round]), choices, sMsg, N * number_of_higher_bit_entries, 1, bits_FSM1);

#if !OPTIMAL
      ClientLog("Finished One Round");
#endif
    for (int i = 0; i < N; i++)
    {
      for (int j = 0; j < number_of_higher_bit_entries; j++)
      {
        client_states[i][j + C - number_of_higher_bit_entries] = msgs2[number_of_higher_bit_entries * i + j];
      }
    }
    delete[] choices;
    delete[] msgs2;
    delete[] rMsg;
    delete[] sMsg;

    l += concurrent_iterations;
  }

#if !OPTIMAL
  ClientLog("Finished first FSM");
#endif

#if VERBOSE
  for (int i = 0; i < N; i++)
  {
    cout << "Database entry: " << i << endl;
    cout << "DB   : ";
    for (int j = 0; j < C; j++)
    {
      cout << 0 << ", ";
    }
    cout << endl;
    cout << "Query: ";
    for (int j = 0; j < C; j++)
    {
      cout << query[j] << ", ";
    }
    cout << endl;
    cout << "Equal: ";
    for (int j = 0; j < C; j++)
    {
      cout << (uint32_t)client_states[i][j] << ", ";
    }
    cout << endl;
    cout << "Actual: ";
    for (int j = 0; j < C; j++)
    {
      uint32_t value = (uint32_t)client_states[i][j];
      uint8_t accept_state = 0;

      if (accept_state == 1)
      {
        cout << value << ", ";
      }
      if (accept_state == 0)
      {
        cout << !value << ", ";
      }
    }
    cout << endl;
  }
#endif
#if !OPTIMAL
  ClientLog("Starting Second FSM");

  cout << "Finished with 1st part of OT correlation calculations" << endl;
  cout << "Setting up second FSM to compute k matches" << endl;

#endif

  uint8_t *client_k_state = new uint8_t[N];
  for (int i = 0; i < N; i++)
  {
    client_k_state[i] = 2; // default state
  }
  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 *choices = new uint8_t[N];
    for (int i = 0; i < N; i++)
    {
      // (client state, components equal)
      uint8_t client_state_value = 0;
      for (int j = 0; j < round_step; j++)
      {
        client_state_value += client_states[i][c + j] * (1 << j);
      }
      choices[i] = round_factor * client_k_state[i] + client_state_value;

    }

#if !OPTIMAL
    ClientLog("One one rounds");
#endif


    uint8_t *rMsg = new uint8_t[N];
    ConstructOTReceiverMsg(rMsg, *(FSM2_RC[round]), choices, N);

    SendLargePacked(socket, 2 + round_step, N, rMsg);
    
#if !OPTIMAL
    cout << "Sending: " << N << endl;
#endif

    uint8_t *sMsg = new uint8_t[N * 3 * round_factor];

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

    uint8_t *msgs2 = new uint8_t[N];
    RecoverReceiverMsg(msgs2, *(FSM2_RC[round]), choices, sMsg, N, 2, bits_FSM2);

    for (int i = 0; i < N; i++)
    {
      client_k_state[i] = msgs2[i];
    }

    c += round_step;

    delete[] choices;
    delete[] msgs2;
    delete[] rMsg;
    delete[] sMsg;
  }
#if !OPTIMAL
  ClientLog("Finished second FSM");
#endif
  for (int i = 0; i < N; i++)
  {
    if (client_k_state[i] > 0)
      ClientLog("found a match at index " + to_string(i));
  }

#if !OPTIMAL
  cout << "Memory clean up" << endl;
#endif

  delete[] query;
  for (int i = 0; i < N; i++)
  {
    delete[] client_states[i];
  }
  delete[] client_states;
  delete[] client_k_state;

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

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x758, %rsp            # imm = 0x758
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, 0x40(%rsp)
leaq 0x3a19(%rip), %rsi      # 0x6008
leaq 0x348(%rsp), %rdi
leaq 0x550(%rsp), %rdx
callq 0x2100
leaq 0x348(%rsp), %rdi
callq 0x2e2c
leaq 0x348(%rsp), %rdi
callq 0x2110
leaq 0x348(%rsp), %r15
movq %r15, %rdi
callq 0x22b0
pushq $0xc
popq %rdx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x21e0
leaq 0x68(%rsp), %rdi
leaq 0x348(%rsp), %rsi
callq 0x4274
leaq 0x348(%rsp), %rdi
callq 0x2080
leaq 0x550(%rsp), %rdi
callq 0x22b0
leaq 0x39b2(%rip), %rdx      # 0x6021
leaq 0x140(%rsp), %rdi
movq %r14, %rsi
callq 0x3267
leaq 0x550(%rsp), %rdi
leaq 0x140(%rsp), %rsi
pushq $0xc
popq %rdx
callq 0x21e0
leaq 0x140(%rsp), %rdi
callq 0x2110
vmovups 0x68(%rsp), %ymm0
vmovups 0x78(%rsp), %ymm1
leaq 0xf0(%rsp), %rsi
vmovups %ymm0, (%rsi)
vmovups %ymm1, 0x10(%rsi)
leaq 0x550(%rsp), %rdi
vzeroupper
callq 0x4640
leaq 0xf0(%rsp), %rdi
movq %rax, %rbx
callq 0x437e
vmovups 0x68(%rsp), %ymm0
vmovups 0x78(%rsp), %ymm1
leaq 0xc0(%rsp), %rsi
vmovups %ymm1, 0x10(%rsi)
vmovups %ymm0, (%rsi)
leaq 0x550(%rsp), %rdi
vzeroupper
callq 0x46c2
leaq 0xc0(%rsp), %rdi
movq %rax, %r15
callq 0x437e
leaq 0x550(%rsp), %rdi
callq 0x2080
movl 0x68(%rsp), %eax
movq 0x88(%rsp), %rcx
movq %rax, 0x8(%rsp)
movl 0x6c(%rsp), %eax
movq %rcx, 0x60(%rsp)
movq %rax, 0x18(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x22b0
leaq 0x38cb(%rip), %rdx      # 0x6029
leaq 0xa0(%rsp), %rdi
movq %r14, %rsi
callq 0x3267
leaq 0x140(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x21e0
leaq 0xa0(%rsp), %rdi
movq %rbx, 0x50(%rsp)
callq 0x2110
leaq 0x140(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x475a
leaq 0x140(%rsp), %rdi
movq %rax, %rbp
callq 0x2080
movq 0x8(%rsp), %rax
leaq (,%rax,8), %rdi
callq 0x2030
movq %rax, %r13
xorl %ebx, %ebx
cmpq 0x8(%rsp), %rbx
je 0x27fe
movq 0x18(%rsp), %rdi
callq 0x2030
xorl %ecx, %ecx
movq %rax, (%r13,%rbx,8)
cmpq %rcx, 0x18(%rsp)
je 0x27f9
movb $0x0, (%rax,%rcx)
incq %rcx
jmp 0x27e9
incq %rbx
jmp 0x27d1
movq %r15, 0x48(%rsp)
xorl %edx, %edx
xorl %edi, %edi
xorl %r15d, %r15d
movl 0x70(%rsp), %eax
cmpq %rax, %rdx
jae 0x2a26
movq 0x60(%rsp), %rcx
movslq %edi, %rax
movq 0x8(%rsp), %rsi
movzbl (%rcx,%rax), %ecx
cmpl %ecx, %r15d
jb 0x2842
leal -0x1(%rax), %edi
addq 0x60(%rsp), %rax
movzbl (%rax), %ecx
incl %edi
incq %rax
cmpl %ecx, %r15d
jae 0x2835
movq 0x78(%rsp), %rax
movq %rdx, 0x28(%rsp)
movl %edi, %ebx
movzbl (%rax,%rdx), %r14d
movq 0x18(%rsp), %rax
subl %edi, %eax
movzbl %al, %r12d
movl %r12d, %eax
imull %esi, %eax
movl %eax, %edi
movl %eax, 0x10(%rsp)
imull %r14d, %edi
callq 0x2030
movq %rax, %rdx
pushq $0x1
popq %rax
shlxl %r14d, %eax, %eax
addl $0xff, %eax
pushq $0x2
popq %rcx
shlxl %r14d, %ecx, %r11d
movl %r12d, 0x5c(%rsp)
movl %r12d, %r12d
movl %ebx, 0x58(%rsp)
movl %ebx, %ecx
xorl %esi, %esi
movq %rdx, 0x30(%rsp)
cmpq 0x8(%rsp), %rsi
je 0x28e3
xorl %edi, %edi
cmpq %rdi, %r12
je 0x28db
movq (%r13,%rsi,8), %r10
leal (%rcx,%rdi), %r8d
movzbl %r8b, %r8d
shrxl %r15d, (%rbp,%r8,4), %r9d
movzbl (%r10,%r8), %r8d
andl %eax, %r9d
shlxl %r14d, %r8d, %r8d
addl %r9d, %r8d
movb %r8b, (%rdx,%rdi)
incq %rdi
jmp 0x28a9
incq %rsi
addq %r12, %rdx
jmp 0x28a0
movl 0x10(%rsp), %edi
movq %r11, 0x38(%rsp)
movq %rdi, 0x20(%rsp)
callq 0x2030
movq %rax, %rbx
movq 0x50(%rsp), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x30(%rsp), %rdx
movl 0x10(%rsp), %ecx
movq %rbx, %rdi
callq 0x39af
leal 0x1(%r14), %eax
movq %rbx, %rcx
movq 0x40(%rsp), %rdi
movq 0x20(%rsp), %rdx
movzbl %al, %esi
callq 0x489f
movq %rbx, 0x98(%rsp)
movq 0x38(%rsp), %rbx
andl $0xfe, %ebx
imull 0x10(%rsp), %ebx
movq %rbx, %rdi
callq 0x2030
movq %rax, 0x38(%rsp)
movq 0x40(%rsp), %rdi
pushq $0x1
popq %rsi
movq 0x38(%rsp), %rcx
movq %rbx, %rdx
callq 0x4972
movq 0x20(%rsp), %rdi
callq 0x2030
movq 0x38(%rsp), %rbx
movq %rax, %rdi
movq 0x50(%rsp), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x30(%rsp), %rdx
movl 0x10(%rsp), %r8d
movq %rbx, %rcx
movl $0x4, (%rsp)
movq %rdi, 0x20(%rsp)
pushq $0x1
popq %r9
callq 0x3ad7
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %rax
subl 0x5c(%rsp), %eax
xorl %edx, %edx
cmpq 0x8(%rsp), %rdx
je 0x29e9
xorl %esi, %esi
cmpq %rsi, %r12
je 0x29e1
movb (%rcx,%rsi), %dil
movq (%r13,%rdx,8), %r8
leal (%rax,%rsi), %r9d
incq %rsi
movb %dil, (%r8,%r9)
jmp 0x29c6
incq %rdx
addq %r12, %rcx
jmp 0x29bd
movq 0x30(%rsp), %rdi
callq 0x2300
movq 0x20(%rsp), %rdi
callq 0x2300
movq 0x98(%rsp), %rdi
callq 0x2300
movq %rbx, %rdi
callq 0x2300
movq 0x28(%rsp), %rdx
movl 0x58(%rsp), %edi
addl %r14d, %r15d
incq %rdx
jmp 0x280a
movq 0x8(%rsp), %rdi
callq 0x2030
movq %rax, %rbx
xorl %eax, %eax
cmpq %rax, 0x8(%rsp)
je 0x2a45
movb $0x2, (%rbx,%rax)
incq %rax
jmp 0x2a35
movq 0x8(%rsp), %rax
xorl %ecx, %ecx
movl $0x0, 0x28(%rsp)
leal (%rax,%rax,2), %eax
movl %eax, 0x20(%rsp)
movl 0x74(%rsp), %eax
cmpq %rax, %rcx
jae 0x2bde
movq 0x80(%rsp), %rax
movq %rcx, 0x18(%rsp)
movzbl (%rax,%rcx), %r12d
movq 0x8(%rsp), %rdi
callq 0x2030
movq %rax, %r14
pushq $0x1
popq %rax
movl 0x28(%rsp), %ecx
shlxl %r12d, %eax, %eax
xorl %edx, %edx
movl %eax, 0x10(%rsp)
movl %r12d, %eax
cmpq 0x8(%rsp), %rdx
je 0x2adc
xorl %edi, %edi
xorl %esi, %esi
cmpq %rdi, %rax
je 0x2ac7
movq (%r13,%rdx,8), %r8
leal (%rcx,%rdi), %r9d
movzbl (%r8,%r9), %r8d
shlxl %edi, %r8d, %r8d
incq %rdi
addb %r8b, %sil
jmp 0x2aa7
movzbl (%rbx,%rdx), %edi
shlxl %r12d, %edi, %edi
addb %dil, %sil
movb %sil, (%r14,%rdx)
incq %rdx
jmp 0x2a9c
movq 0x8(%rsp), %rdi
callq 0x2030
movq %rax, %r15
movq 0x48(%rsp), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x8(%rsp), %rcx
movq %r15, %rdi
movq %r14, %rdx
callq 0x39af
leal 0x2(%r12), %eax
movq %r15, %rcx
movq 0x40(%rsp), %rdi
movq 0x8(%rsp), %rdx
movzbl %al, %esi
callq 0x489f
movq %r15, 0x30(%rsp)
movzbl 0x10(%rsp), %r15d
imull 0x20(%rsp), %r15d
movq %r15, %rdi
callq 0x2030
movq 0x40(%rsp), %rdi
pushq $0x2
popq %rsi
movq %r15, %rdx
movq %rax, %rcx
movq %rax, %r15
callq 0x4972
movq %r15, %rax
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%rsp)
callq 0x2030
movq %rax, %r15
movq 0x48(%rsp), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %r8
movq %r14, %rdx
movl $0x6, (%rsp)
movq %r15, %rdi
pushq $0x2
popq %r9
callq 0x3ad7
xorl %eax, %eax
cmpq %rax, 0x8(%rsp)
je 0x2ba8
movb (%r15,%rax), %cl
movb %cl, (%rbx,%rax)
incq %rax
jmp 0x2b95
addl %r12d, 0x28(%rsp)
movq %r14, %rdi
callq 0x2300
movq %r15, %rdi
callq 0x2300
movq 0x30(%rsp), %rdi
callq 0x2300
movq 0x10(%rsp), %rdi
callq 0x2300
movq 0x18(%rsp), %rcx
incq %rcx
jmp 0x2a5b
leaq 0x120(%rsp), %r15
xorl %r14d, %r14d
cmpq %r14, 0x8(%rsp)
je 0x2c44
cmpb $0x0, (%rbx,%r14)
je 0x2c3f
movq %r15, %rdi
movl %r14d, %esi
callq 0x32c3
leaq 0xa0(%rsp), %rdi
leaq 0x3422(%rip), %rsi      # 0x6033
movq %r15, %rdx
movq %r15, %r12
callq 0x329f
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
callq 0x2e2c
movq %r15, %rdi
callq 0x2110
movq %r12, %rdi
callq 0x2110
movq %r12, %r15
incq %r14
jmp 0x2be9
testq %rbp, %rbp
je 0x2c51
movq %rbp, %rdi
callq 0x2300
movq 0x48(%rsp), %r15
xorl %r14d, %r14d
cmpq %r14, 0x8(%rsp)
je 0x2c74
movq (%r13,%r14,8), %rdi
testq %rdi, %rdi
je 0x2c6f
callq 0x2300
incq %r14
jmp 0x2c59
movq %r13, %rdi
callq 0x2300
movq %rbx, %rdi
callq 0x2300
movq 0x50(%rsp), %r12
xorl %r14d, %r14d
movl 0x70(%rsp), %eax
cmpq %rax, %r14
jae 0x2cb3
movq (%r12,%r14,8), %rbx
testq %rbx, %rbx
je 0x2ca6
movq %rbx, %rdi
callq 0x37d6
movq %rbx, %rdi
callq 0x2230
incq %r14
jmp 0x2c8c
testq %r12, %r12
je 0x2cc0
movq %r12, %rdi
callq 0x2300
xorl %r14d, %r14d
movl 0x74(%rsp), %eax
cmpq %rax, %r14
jae 0x2cea
movq (%r15,%r14,8), %rbx
testq %rbx, %rbx
je 0x2cdd
movq %rbx, %rdi
callq 0x37d6
movq %rbx, %rdi
callq 0x2230
incq %r14
jmp 0x2cc3
testq %r15, %r15
je 0x2cf7
movq %r15, %rdi
callq 0x2300
leaq 0x140(%rsp), %rdi
callq 0x20b0
leaq 0x550(%rsp), %rdi
callq 0x20b0
leaq 0x68(%rsp), %rdi
callq 0x437e
leaq 0x348(%rsp), %rdi
callq 0x20b0
addq $0x758, %rsp            # imm = 0x758
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2df0
jmp 0x2df0
leaq 0xa0(%rsp), %rdi
movq %rax, %rbx
jmp 0x2dd9
jmp 0x2df0
jmp 0x2d8e
jmp 0x2d8e
leaq 0xc0(%rsp), %rdi
jmp 0x2d6f
leaq 0xf0(%rsp), %rdi
movq %rax, %rbx
callq 0x437e
jmp 0x2e00
leaq 0x140(%rsp), %rdi
movq %rax, %rbx
callq 0x2110
jmp 0x2e00
movq %rax, %rbx
jmp 0x2e00
jmp 0x2d95
movq %rax, %rbx
jmp 0x2e0d
jmp 0x2d9c
movq %rax, %rbx
jmp 0x2e17
leaq 0x348(%rsp), %rdi
movq %rax, %rbx
callq 0x2110
jmp 0x2e24
movq %rax, %rbx
jmp 0x2e24
jmp 0x2df0
leaq 0xa0(%rsp), %rdi
movq %rax, %rbx
callq 0x2110
jmp 0x2dd1
jmp 0x2df0
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0x2110
jmp 0x2df3
jmp 0x2df0
jmp 0x2df0
jmp 0x2df0
jmp 0x2df0
jmp 0x2df0
jmp 0x2df0
jmp 0x2df0
jmp 0x2df0
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x20b0
leaq 0x550(%rsp), %rdi
callq 0x20b0
leaq 0x68(%rsp), %rdi
callq 0x437e
leaq 0x348(%rsp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x23e0