// File: /workspace/github_cpp_makefile_star2_O1/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 0x4bf8
movb $0x0, 0xf(%rsp)
movb 0x10a75(%rip), %al      # 0x14668
testb %al, %al
jne 0x3c0e
testb $0x1, 0xf(%rsp)
je 0x3c29
testb %al, %al
jne 0x3c0e
leaq 0xca55(%rip), %rdi      # 0x1065e
callq 0x21c0
leaq 0x28(%rsp), %rdi
callq 0x4c04
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x3, %r12d
leaq 0x30(%rsp), %r14
leaq 0x28(%rsp), %r15
leaq 0x10610(%rip), %rbp     # 0x14250
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x242c
movl 0x38(%rsp), %esi
testl %esi, %esi
je 0x3d5c
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
addl $0x1f, %esi
andl $-0x20, %esi
leaq 0x10(%rsp), %rdi
callq 0x4aa0
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 0x109bb(%rip), %rsi     # 0x14670
movl $0x20, %edx
callq 0x603e
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
subq %rdx, %rcx
shrq $0x5, %rcx
movq %rbp, %rdi
movq %rdx, %rsi
movl $0x1, %r8d
callq 0xa682
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
movups -0x20(%rax), %xmm0
movups -0x10(%rax), %xmm1
movl %eax, %edx
subl %esi, %edx
movaps %xmm0, 0x10972(%rip)    # 0x14670
movaps %xmm1, 0x1097b(%rip)    # 0x14680
movl 0x10455(%rip), %edi     # 0x14160
callq 0xfbbb
movl %eax, %r13d
testl %eax, %eax
js 0x3d23
leaq 0xf(%rsp), %rdi
callq 0x3de3
jmp 0x3d40
callq 0x2030
movl (%rax), %edx
leaq 0xc768(%rip), %rdi      # 0x10499
movl %r13d, %esi
xorl %eax, %eax
callq 0x2040
movb $0x1, 0xf(%rsp)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3d57
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
testl %r13d, %r13d
js 0x3d86
cmpb $0x0, 0x10905(%rip)     # 0x14668
jne 0x3d7b
leaq 0xc917(%rip), %rdi      # 0x10683
callq 0x21c0
movl $0x1, %edi
callq 0x20d0
movq %r15, %rdi
movq %r14, %rsi
callq 0x5b56
movb 0x108dc(%rip), %al      # 0x14668
testb %al, %al
jne 0x3c0e
testl %r12d, %r12d
jle 0x3bfe
decl %r12d
testb $0x1, 0xf(%rsp)
je 0x3c40
jmp 0x3bfe
jmp 0x3db2
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3dd1
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
jmp 0x3dd1
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x4c04
movq %rbx, %rdi
callq 0x2240