// File: /workspace/github_cpp_makefile_star2_O3/rellla[P]E3DC-Rscp/RscpMain.cpp
// Function: authLoop(e3dc_config_t*)
int authLoop(e3dc_config_t *config)
{
    int auth_retry = MAX_AUTH_RETRY;

    RscpProtocol protocol;
    bool bStopExecution = false;
    while ((iAuthenticated != 1) && ((auth_retry + 1) > 0)
	   && !bStopExecution) {
	auth_retry--;
	//--------------------------------------------------------------------------------------------------------------
	// RSCP Transmit Frame Block Data
	//--------------------------------------------------------------------------------------------------------------
	SRscpFrameBuffer frameBuffer;
	memset(&frameBuffer, 0, sizeof(frameBuffer));

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

	// 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;
		continue;
	    } else {
		// go into receive loop and wait for response
		receiveLoop(bStopExecution);
	    }
	}

	// auth loop sleep / cycle time before next request
	if (!iAuthenticated) {
	    printf("Authentication failed, retry...\n");
	    sleep(1);
	}
	// free frame buffer memory
	protocol.destroyFrameData(&frameBuffer);
    }

    if (!iAuthenticated)
	printf("Authentication failed due to timeout\n");

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
leaq 0x28(%rsp), %rdi
callq 0x49e0
movb $0x0, 0xf(%rsp)
cmpb $0x0, 0xfbe0(%rip)      # 0x13668
jne 0x3c1d
movl $0x3, %r12d
leaq 0x30(%rsp), %r14
leaq 0x28(%rsp), %r15
leaq 0xf7ab(%rip), %rbp      # 0x13250
testl %r12d, %r12d
js 0x3c11
testb $0x1, 0xf(%rsp)
jne 0x3c11
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x249c
movl 0x38(%rsp), %esi
testl %esi, %esi
je 0x3bd5
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
addl $0x1f, %esi
andl $-0x20, %esi
leaq 0x10(%rsp), %rdi
callq 0x4896
movl 0x38(%rsp), %edi
addq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
subq %rdi, %rdx
xorl %esi, %esi
callq 0x20b0
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x38(%rsp), %edx
callq 0x20f0
movq %rbp, %rdi
leaq 0xfb42(%rip), %rsi      # 0x13670
movl $0x20, %edx
callq 0x5dd0
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
subq %rdx, %rcx
shrq $0x5, %rcx
movq %rbp, %rdi
movq %rdx, %rsi
movl $0x1, %r8d
callq 0xa414
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
movups -0x20(%rax), %xmm0
movups -0x10(%rax), %xmm1
movl %eax, %edx
subl %esi, %edx
movaps %xmm0, 0xfaf9(%rip)     # 0x13670
movaps %xmm1, 0xfb02(%rip)     # 0x13680
movl 0xf5dc(%rip), %edi      # 0x13160
callq 0xf94c
movl %eax, %r13d
testl %eax, %eax
js 0x3b9c
leaq 0xf(%rsp), %rdi
callq 0x3c6b
jmp 0x3bb9
callq 0x2030
movl (%rax), %edx
leaq 0xc8ef(%rip), %rdi      # 0x10499
movl %r13d, %esi
xorl %eax, %eax
callq 0x2040
movb $0x1, 0xf(%rsp)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3bd0
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
testl %r13d, %r13d
js 0x3bff
cmpb $0x0, 0xfa8c(%rip)      # 0x13668
jne 0x3bf4
leaq 0xca9e(%rip), %rdi      # 0x10683
callq 0x21c0
movl $0x1, %edi
callq 0x20d0
movq %r15, %rdi
movq %r14, %rsi
callq 0x58e0
decl %r12d
cmpb $0x0, 0xfa5f(%rip)      # 0x13668
je 0x3aa5
jmp 0x3c1d
leaq 0xca46(%rip), %rdi      # 0x1065e
callq 0x21c0
leaq 0x28(%rsp), %rdi
callq 0x49ec
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3c3a
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3c59
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
jmp 0x3c59
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x49ec
movq %rbx, %rdi
callq 0x2240