// File: rellla[P]E3DC-Rscp/RscpMain.cpp
// Function: mainLoop(int)
static void mainLoop(int requests)
{
    RscpProtocol protocol;
    bool bStopExecution = false;

    while (!bStopExecution) {
	//--------------------------------------------------------------------------------------------------------------
	// RSCP Transmit Frame Block Data
	//--------------------------------------------------------------------------------------------------------------
	SRscpFrameBuffer frameBuffer;
	memset(&frameBuffer, 0, sizeof(frameBuffer));

	// create an RSCP frame with requests to some example data
	createRequest(&frameBuffer, requests);

	// check that frame data was created
	if (frameBuffer.dataLength > 0) {
	    // resize temporary encryption buffer to a multiple of AES_BLOCK_SIZE
	    std::vector < uint8_t > encryptionBuffer;
	    encryptionBuffer.resize(ROUNDUP
				    (frameBuffer.dataLength,
				     AES_BLOCK_SIZE));
	    // zero padding for data above the desired length
	    memset(&encryptionBuffer[0] +
		   frameBuffer.dataLength, 0,
		   encryptionBuffer.size() - frameBuffer.dataLength);
	    // copy desired data length
	    memcpy(&encryptionBuffer[0], frameBuffer.data,
		   frameBuffer.dataLength);
	    // set continues encryption IV
	    aesEncrypter.SetIV(ucEncryptionIV, AES_BLOCK_SIZE);
	    // start encryption from encryptionBuffer to encryptionBuffer, blocks = encryptionBuffer.size() / AES_BLOCK_SIZE
	    aesEncrypter.Encrypt(&encryptionBuffer[0],
				 &encryptionBuffer[0],
				 encryptionBuffer.size() / AES_BLOCK_SIZE);
	    // save new IV for next encryption block
	    memcpy(ucEncryptionIV,
		   &encryptionBuffer[0] +
		   encryptionBuffer.size() - AES_BLOCK_SIZE,
		   AES_BLOCK_SIZE);

	    // send data on socket
	    int iResult = SocketSendData(iSocket,
					 &encryptionBuffer[0],
					 encryptionBuffer.size());
	    if (iResult < 0) {
		printf("Socket send error %i. errno %i\n", iResult, errno);
		bStopExecution = true;
	    } else {
		// go into receive loop and wait for response
		receiveLoop(bStopExecution);
	    }
	}
	// free frame buffer memory
	protocol.destroyFrameData(&frameBuffer);

	// main loop sleep / cycle time before next request

	if (!bStopExecution)
	    sleep(1);
    }
}

// Assembly:
subq $0xd8, %rsp
movl %edi, 0xd4(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x7840
movb $0x0, 0xc7(%rsp)
movb 0xc7(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x56a5
jmp 0x5954
xorps %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
movl 0xd4(%rsp), %esi
leaq 0xb0(%rsp), %rdi
callq 0x2570
jmp 0x56c6
cmpl $0x0, 0xb8(%rsp)
jbe 0x5920
leaq 0x88(%rsp), %rdi
movq %rdi, 0x78(%rsp)
callq 0x5fe0
movq 0x78(%rsp), %rdi
movl 0xb8(%rsp), %eax
addl $0x1f, %eax
andl $-0x20, %eax
movl %eax, %esi
callq 0x5ff0
jmp 0x5701
xorl %eax, %eax
movl %eax, 0x64(%rsp)
movl %eax, %esi
movq %rsi, 0x70(%rsp)
leaq 0x88(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x6090
movq 0x68(%rsp), %rdi
movl 0xb8(%rsp), %ecx
addq %rcx, %rax
movq %rax, 0x58(%rsp)
callq 0x60b0
movq 0x58(%rsp), %rdi
movl 0x64(%rsp), %esi
movq %rax, %rdx
movl 0xb8(%rsp), %eax
subq %rax, %rdx
callq 0x20e0
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x6090
movq %rax, %rdi
movq 0xb0(%rsp), %rsi
movl 0xb8(%rsp), %eax
movl %eax, %edx
callq 0x2130
leaq 0x1eafd(%rip), %rdi     # 0x24280
leaq 0x1ef16(%rip), %rsi     # 0x246a0
movl $0x20, %edx
callq 0xac30
jmp 0x5796
xorl %eax, %eax
movl %eax, %esi
movq %rsi, 0x38(%rsp)
leaq 0x88(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x6090
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdi
movq %rax, 0x48(%rsp)
callq 0x6090
movq 0x40(%rsp), %rdi
movq %rax, 0x50(%rsp)
callq 0x60b0
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq %rax, %rcx
shrq $0x5, %rcx
leaq 0x1ea94(%rip), %rdi     # 0x24280
movl $0x1, %r8d
callq 0x14500
jmp 0x57f9
xorl %eax, %eax
movl %eax, %esi
movq %rsi, 0x8(%rsp)
leaq 0x88(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x6090
movq 0x18(%rsp), %rdi
movq %rax, 0x10(%rsp)
callq 0x60b0
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movq 0x10(%rsp), %rax
movups -0x20(%rax,%rcx), %xmm0
movups -0x10(%rax,%rcx), %xmm1
movups %xmm1, 0x1ee6a(%rip)    # 0x246b0
movups %xmm0, 0x1ee53(%rip)    # 0x246a0
movl 0x1e93d(%rip), %eax     # 0x24190
movl %eax, 0x24(%rsp)
callq 0x6090
movq 0x18(%rsp), %rdi
movq %rax, 0x28(%rsp)
callq 0x60b0
movl 0x24(%rsp), %edi
movq 0x28(%rsp), %rsi
movl %eax, %edx
callq 0x1f6b0
movl %eax, 0x34(%rsp)
jmp 0x5881
movl 0x34(%rsp), %eax
movl %eax, 0x84(%rsp)
cmpl $0x0, 0x84(%rsp)
jge 0x5902
movl 0x84(%rsp), %eax
movl %eax, 0x4(%rsp)
callq 0x2030
movl 0x4(%rsp), %esi
movl (%rax), %edx
leaq 0x1abf3(%rip), %rdi     # 0x204a6
xorl %eax, %eax
callq 0x2040
jmp 0x58bc
movb $0x1, 0xc7(%rsp)
jmp 0x5913
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
jmp 0x5969
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x60d0
jmp 0x5969
leaq 0xc7(%rsp), %rdi
callq 0x4b10
jmp 0x5911
jmp 0x5913
leaq 0x88(%rsp), %rdi
callq 0x60d0
leaq 0xc8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x9580
jmp 0x5937
testb $0x1, 0xc7(%rsp)
jne 0x594f
movl $0x1, %edi
callq 0x2110
jmp 0x594d
jmp 0x594f
jmp 0x5693
leaq 0xc8(%rsp), %rdi
callq 0x7860
addq $0xd8, %rsp
retq
leaq 0xc8(%rsp), %rdi
callq 0x7860
movq 0xa8(%rsp), %rdi
callq 0x22a0
nopw %cs:(%rax,%rax)