// File: /workspace/github_cpp_makefile_star2_O2/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 0x8(%rsp), %rdi
callq 0x4380
leaq 0x7(%rsp), %rax
movb $0x0, (%rax)
pushq $0x3
popq %rbp
leaq 0x30(%rsp), %r12
leaq 0x10(%rsp), %r13
leaq 0x1000b(%rip), %r15     # 0x13270
cmpb $0x0, 0x1041c(%rip)     # 0x13688
jne 0x33c6
testl %ebp, %ebp
js 0x33ba
testb $0x1, 0x7(%rsp)
jne 0x33ba
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x23ec
movl 0x38(%rsp), %esi
testl %esi, %esi
je 0x3389
andq $0x0, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
addl $0x1f, %esi
andl $-0x20, %esi
movq %r13, %rdi
callq 0x3f7e
movl 0x38(%rsp), %edi
addq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
subq %rdi, %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x38(%rsp), %edx
callq 0x2120
movq %r15, %rdi
leaq 0x1039b(%rip), %rsi     # 0x13690
pushq $0x20
popq %rdx
callq 0x54ba
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
subq %rdx, %rcx
shrq $0x5, %rcx
movq %r15, %rdi
movq %rdx, %rsi
pushq $0x1
popq %r8
callq 0x9b3c
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
movups -0x20(%rax), %xmm0
movups -0x10(%rax), %xmm1
movl %eax, %edx
subl %esi, %edx
movaps %xmm0, 0x10356(%rip)    # 0x13690
movaps %xmm1, 0x1035f(%rip)    # 0x136a0
movl 0xfe39(%rip), %edi      # 0x13180
callq 0xeb64
movl %eax, %r14d
testl %eax, %eax
js 0x335f
leaq 0x7(%rsp), %rdi
callq 0x3407
jmp 0x337c
callq 0x2030
movl (%rax), %edx
leaq 0xc12c(%rip), %rdi      # 0xf499
movl %r14d, %esi
xorl %eax, %eax
callq 0x2040
movb $0x1, 0x7(%rsp)
movq %r13, %rdi
callq 0x4214
testl %r14d, %r14d
js 0x33b3
cmpb $0x0, 0x102f8(%rip)     # 0x13688
jne 0x33a6
leaq 0xc31b(%rip), %rdi      # 0xf6b4
callq 0x2210
pushq $0x1
popq %rdi
callq 0x2100
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x502e
decl %ebp
jmp 0x3265
leaq 0xc2ce(%rip), %rdi      # 0xf68f
callq 0x2210
leaq 0x8(%rsp), %rdi
callq 0x438c
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x33e3
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x4214
jmp 0x33f5
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x438c
movq %rbx, %rdi
callq 0x2280